001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 */ 019 020package org.nuxeo.ecm.platform.rendition.lazy; 021 022import java.security.MessageDigest; 023import java.security.NoSuchAlgorithmException; 024import java.util.ArrayList; 025import java.util.Calendar; 026import java.util.List; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.ecm.core.api.Blob; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.NuxeoException; 033import org.nuxeo.ecm.core.api.impl.blob.StringBlob; 034import org.nuxeo.ecm.core.transientstore.api.TransientStore; 035import org.nuxeo.ecm.core.transientstore.api.TransientStoreService; 036import org.nuxeo.ecm.core.work.api.Work; 037import org.nuxeo.ecm.core.work.api.WorkManager; 038import org.nuxeo.ecm.platform.rendition.Rendition; 039import org.nuxeo.ecm.platform.rendition.extension.AutomationRenderer; 040import org.nuxeo.ecm.platform.rendition.extension.RenditionProvider; 041import org.nuxeo.ecm.platform.rendition.impl.LazyRendition; 042import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition; 043import org.nuxeo.runtime.api.Framework; 044 045/** 046 * Default implementation of an asynchronous {@link RenditionProvider} 047 * 048 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 049 * @since 7.2 050 */ 051public abstract class AbstractLazyCachableRenditionProvider implements RenditionProvider { 052 053 public static final String WORKERID_KEY = "workerid"; 054 055 public static final String CACHE_NAME = "LazyRenditionCache"; 056 057 protected static Log log = LogFactory.getLog(AbstractLazyCachableRenditionProvider.class); 058 059 @Override 060 public List<Blob> render(DocumentModel doc, RenditionDefinition def) { 061 062 // build the key 063 String key = buildRenditionKey(doc, def); 064 065 // see if rendition is already in process 066 067 TransientStoreService tss = Framework.getService(TransientStoreService.class); 068 069 TransientStore ts = tss.getStore(CACHE_NAME); 070 071 if (ts == null) { 072 throw new NuxeoException("Unable to find Transient Store " + CACHE_NAME); 073 } 074 075 if (!ts.exists(key)) { 076 Work work = getRenditionWork(key, doc, def); 077 ts.putParameter(key, WORKERID_KEY, work.getId()); 078 List<Blob> blobs = new ArrayList<Blob>(); 079 StringBlob emptyBlob = new StringBlob(""); 080 emptyBlob.setFilename("inprogress"); 081 emptyBlob.setMimeType("text/plain;" + LazyRendition.EMPTY_MARKER); 082 blobs.add(emptyBlob); 083 ts.putBlobs(key, blobs); 084 Framework.getService(WorkManager.class).schedule(work); 085 } else { 086 ts.release(key); 087 } 088 089 return ts.getBlobs(key); 090 } 091 092 @Override 093 public String getVariant(DocumentModel doc, RenditionDefinition definition) { 094 return AutomationRenderer.getVariant(doc, definition); 095 } 096 097 protected String buildRenditionKey(DocumentModel doc, RenditionDefinition def) { 098 099 StringBuffer sb = new StringBuffer(doc.getId()); 100 sb.append("::"); 101 String modificationDatePropertyName = def.getSourceDocumentModificationDatePropertyName(); 102 Calendar modif = (Calendar) doc.getPropertyValue(modificationDatePropertyName); 103 if (modif != null) { 104 sb.append(modif.getTimeInMillis()); 105 sb.append("::"); 106 } 107 String variant = getVariant(doc, def); 108 if (variant != null) { 109 sb.append(variant); 110 sb.append("::"); 111 } 112 sb.append(def.getName()); 113 114 return getDigest(sb.toString()); 115 } 116 117 protected String getDigest(String key) { 118 MessageDigest digest; 119 try { 120 digest = MessageDigest.getInstance("MD5"); 121 } catch (NoSuchAlgorithmException e) { 122 return key; 123 } 124 byte[] buf = digest.digest(key.getBytes()); 125 return toHexString(buf); 126 } 127 128 private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray(); 129 130 protected String toHexString(byte[] data) { 131 StringBuilder buf = new StringBuilder(2 * data.length); 132 for (byte b : data) { 133 buf.append(HEX_DIGITS[(0xF0 & b) >> 4]); 134 buf.append(HEX_DIGITS[0x0F & b]); 135 } 136 return buf.toString(); 137 } 138 139 /** 140 * Return the {@link Work} that will compute the {@link Rendition}. {@link AbstractRenditionBuilderWork} can be used 141 * as a base class 142 * 143 * @param key the key used to rendition 144 * @param doc the target {@link DocumentModel} 145 * @param def the {@link RenditionDefinition} 146 * @return 147 */ 148 protected abstract Work getRenditionWork(final String key, final DocumentModel doc, final RenditionDefinition def); 149 150}