001/* 002 * (C) Copyright 2010-2018 Nuxeo (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 * Contributors: 016 * Nuxeo - initial API and implementation 017 */ 018 019package org.nuxeo.ecm.platform.rendition.publisher; 020 021import static org.nuxeo.ecm.platform.rendition.Constants.RENDITION_FACET; 022 023import java.util.Map; 024 025import org.apache.commons.lang3.StringUtils; 026import org.nuxeo.ecm.core.api.CoreSession; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.DocumentRef; 029import org.nuxeo.ecm.core.api.NuxeoException; 030import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner; 031import org.nuxeo.ecm.core.api.security.ACP; 032import org.nuxeo.ecm.core.api.security.impl.ACPImpl; 033import org.nuxeo.ecm.platform.publisher.api.PublicationNode; 034import org.nuxeo.ecm.platform.publisher.api.PublishedDocument; 035import org.nuxeo.ecm.platform.publisher.api.PublishedDocumentFactory; 036import org.nuxeo.ecm.platform.publisher.impl.core.SimpleCorePublishedDocument; 037import org.nuxeo.ecm.platform.publisher.task.CoreProxyWithWorkflowFactory; 038import org.nuxeo.ecm.platform.rendition.Rendition; 039import org.nuxeo.ecm.platform.rendition.service.RenditionService; 040import org.nuxeo.ecm.platform.rendition.service.RenditionsRemover; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * Implementation of {@link PublishedDocumentFactory} that uses the {@link RenditionService} to publish a Rendition of 045 * the given document. 046 * 047 * @author <a href="mailto:[email protected]">Thomas Roger</a> 048 * @since 5.4.1 049 */ 050public class RenditionPublicationFactory extends CoreProxyWithWorkflowFactory implements PublishedDocumentFactory { 051 052 public static final String RENDITION_NAME_PARAMETER_KEY = "renditionName"; 053 054 @Override 055 public PublishedDocument publishDocument(DocumentModel doc, PublicationNode targetNode, 056 Map<String, String> params) { 057 if (params != null && params.containsKey(RENDITION_NAME_PARAMETER_KEY)) { 058 String renditionName = params.get(RENDITION_NAME_PARAMETER_KEY); 059 if (!StringUtils.isEmpty(renditionName)) { 060 DocumentModel renditionDocument; 061 Rendition rendition; 062 rendition = getRenditionService().getRendition(doc, renditionName, true); 063 if (rendition != null) { 064 renditionDocument = rendition.getHostDocument(); 065 } else { 066 throw new NuxeoException("Unable to render the document"); 067 } 068 PublishedDocument publishedDocument = super.publishDocument(renditionDocument, targetNode, params); 069 DocumentModel proxy = ((SimpleCorePublishedDocument) publishedDocument).getProxy(); 070 proxy.attach(doc.getSessionId()); 071 if (!hasValidationTask(publishedDocument)) { 072 removeExistingProxiesOnPreviousVersions(proxy); 073 } 074 return publishedDocument; 075 } 076 077 } 078 return super.publishDocument(doc, targetNode, params); 079 } 080 081 @Override 082 protected DocumentModel getLiveDocument(CoreSession session, DocumentModel proxy) { 083 if (!proxy.hasFacet(RENDITION_FACET)) { 084 return super.getLiveDocument(session, proxy); 085 } 086 RenditionLiveDocFetcher fetcher = new RenditionLiveDocFetcher(session, proxy); 087 fetcher.runUnrestricted(); 088 return fetcher.getLiveDocument(); 089 } 090 091 @Override 092 protected void removeExistingProxiesOnPreviousVersions(DocumentModel newProxy) { 093 if (!newProxy.hasFacet(RENDITION_FACET)) { 094 super.removeExistingProxiesOnPreviousVersions(newProxy); 095 return; 096 } 097 RenditionsRemover remover = new RenditionsRemover(newProxy); 098 remover.runUnrestricted(); 099 } 100 101 protected RenditionService getRenditionService() { 102 return Framework.getService(RenditionService.class); 103 } 104 105 public static class RemoveACP extends UnrestrictedSessionRunner { 106 107 protected DocumentRef docRef; 108 109 public RemoveACP(CoreSession session, DocumentRef docRef) { 110 super(session); 111 this.docRef = docRef; 112 } 113 114 @Override 115 public void run() { 116 ACP acp = new ACPImpl(); 117 session.setACP(docRef, acp, true); 118 } 119 120 } 121 122}