001/* 002 * (C) Copyright 2015 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 */ 017 018package org.nuxeo.ecm.platform.rendition.lazy; 019 020import java.security.MessageDigest; 021import java.security.NoSuchAlgorithmException; 022import java.util.ArrayList; 023import java.util.Calendar; 024import java.util.List; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.core.api.Blob; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.NuxeoException; 031import org.nuxeo.ecm.core.api.impl.blob.StringBlob; 032import org.nuxeo.ecm.core.transientstore.api.TransientStore; 033import org.nuxeo.ecm.core.transientstore.api.TransientStoreService; 034import org.nuxeo.ecm.core.work.api.Work; 035import org.nuxeo.ecm.core.work.api.WorkManager; 036import org.nuxeo.ecm.platform.rendition.Rendition; 037import org.nuxeo.ecm.platform.rendition.extension.RenditionProvider; 038import org.nuxeo.ecm.platform.rendition.impl.LazyRendition; 039import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition; 040import org.nuxeo.runtime.api.Framework; 041 042/** 043 * Default implementation of an asynchronous {@link RenditionProvider} 044 * 045 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 046 * @since 7.2 047 */ 048public abstract class AbstractLazyCachableRenditionProvider implements RenditionProvider { 049 050 public static final String WORKERID_KEY = "workerid"; 051 052 public static final String CACHE_NAME = "LazyRenditionCache"; 053 054 protected static Log log = LogFactory.getLog(AbstractLazyCachableRenditionProvider.class); 055 056 /** 057 * Define if rendition caching key should include the user login 058 * 059 * @return 060 */ 061 protected abstract boolean perUserRendition(); 062 063 @Override 064 public List<Blob> render(DocumentModel doc, RenditionDefinition def) { 065 066 // build the key 067 String key = buildRenditionKey(doc, def); 068 069 // see if rendition is already in process 070 071 TransientStoreService tss = Framework.getService(TransientStoreService.class); 072 073 TransientStore ts = tss.getStore(CACHE_NAME); 074 075 if (ts == null) { 076 throw new NuxeoException("Unable to find Transient Store " + CACHE_NAME); 077 } 078 079 if (!ts.exists(key)) { 080 Work work = getRenditionWork(key, doc, def); 081 WorkManager wm = Framework.getService(WorkManager.class); 082 ts.putParameter(key, WORKERID_KEY, work.getId()); 083 wm.schedule(work); 084 } else { 085 if (ts.isCompleted(key)) { 086 List<Blob> blobs = ts.getBlobs(key); 087 ts.release(key); 088 return blobs; 089 } 090 } 091 // return an empty Blob 092 List<Blob> blobs = new ArrayList<Blob>(); 093 StringBlob emptyBlob = new StringBlob(""); 094 emptyBlob.setFilename("inprogress"); 095 emptyBlob.setMimeType("text/plain;" + LazyRendition.EMPTY_MARKER); 096 blobs.add(emptyBlob); 097 return blobs; 098 } 099 100 protected String buildRenditionKey(DocumentModel doc, RenditionDefinition def) { 101 102 StringBuffer sb = new StringBuffer(doc.getId()); 103 sb.append("::"); 104 String modificationDatePropertyName = def.getSourceDocumentModificationDatePropertyName(); 105 Calendar modif = (Calendar) doc.getPropertyValue(modificationDatePropertyName); 106 if (modif != null) { 107 sb.append(modif.getTimeInMillis()); 108 sb.append("::"); 109 } 110 if (perUserRendition()) { 111 sb.append(doc.getCoreSession().getPrincipal().getName()); 112 sb.append("::"); 113 } 114 sb.append(def.getName()); 115 116 return getDigest(sb.toString()); 117 } 118 119 protected String getDigest(String key) { 120 MessageDigest digest; 121 try { 122 digest = MessageDigest.getInstance("MD5"); 123 } catch (NoSuchAlgorithmException e) { 124 return key; 125 } 126 byte[] buf = digest.digest(key.getBytes()); 127 return toHexString(buf); 128 } 129 130 private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray(); 131 132 protected String toHexString(byte[] data) { 133 StringBuilder buf = new StringBuilder(2 * data.length); 134 for (byte b : data) { 135 buf.append(HEX_DIGITS[(0xF0 & b) >> 4]); 136 buf.append(HEX_DIGITS[0x0F & b]); 137 } 138 return buf.toString(); 139 } 140 141 /** 142 * Return the {@link Work} that will compute the {@link Rendition}. {@link AbstractRenditionBuilderWork} can be used 143 * as a base class 144 * 145 * @param key the key used to rendition 146 * @param doc the target {@link DocumentModel} 147 * @param def the {@link RenditionDefinition} 148 * @return 149 */ 150 protected abstract Work getRenditionWork(final String key, final DocumentModel doc, final RenditionDefinition def); 151 152}