001/*
002 * (C) Copyright 2012 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 * Contributors:
014 * Nuxeo - initial API and implementation
015 */
016
017package org.nuxeo.ecm.platform.rendition.url;
018
019import java.io.IOException;
020import java.io.Serializable;
021import java.util.Collections;
022import java.util.Map;
023
024import javax.faces.context.FacesContext;
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.http.HttpServletResponse;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.jboss.seam.annotations.Begin;
031import org.jboss.seam.annotations.In;
032import org.jboss.seam.faces.FacesMessages;
033import org.jboss.seam.international.StatusMessage;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentLocation;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.NuxeoException;
039import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
040import org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions;
041import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils;
042import org.nuxeo.ecm.platform.url.api.DocumentView;
043import org.nuxeo.ecm.platform.util.RepositoryLocation;
044
045/**
046 * Base class for Rendition url codec bindings.
047 * <p>
048 * This class is shared with Template rendering system.
049 *
050 * @since 5.6
051 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
052 */
053public abstract class AbstractRenditionRestHelper implements Serializable {
054
055    private static final long serialVersionUID = 1L;
056
057    protected static final Log log = LogFactory.getLog(AbstractRenditionRestHelper.class);
058
059    @In(create = true, required = false)
060    protected transient CoreSession documentManager;
061
062    @In(create = true)
063    protected transient NavigationContext navigationContext;
064
065    @In(create = true, required = false)
066    protected FacesMessages facesMessages;
067
068    @In(create = true)
069    protected Map<String, String> messages;
070
071    protected abstract Blob renderAsBlob(DocumentModel doc, String renditionName) throws Exception;
072
073    @Begin(id = "#{conversationIdGenerator.nextMainConversationId}", join = true)
074    public void render(DocumentView docView) throws Exception {
075
076        DocumentLocation docLoc = docView.getDocumentLocation();
077        if (documentManager == null) {
078            RepositoryLocation loc = new RepositoryLocation(docLoc.getServerName());
079            navigationContext.setCurrentServerLocation(loc);
080            documentManager = navigationContext.getOrCreateDocumentManager();
081        }
082        DocumentModel doc = documentManager.getDocument(docLoc.getDocRef());
083        if (doc != null) {
084            String renditionName = docView.getParameter(RenditionBasedCodec.RENDITION_PARAM_NAME);
085            FacesContext context = FacesContext.getCurrentInstance();
086            Blob rendered = null;
087            try {
088                rendered = renderAsBlob(doc, renditionName);
089            } catch (NuxeoException e) {
090                log.error("Unable to generate rendition " + renditionName, e);
091                facesMessages.add(StatusMessage.Severity.WARN, messages.get("rendition.not.available"), renditionName);
092                // now we need to redirect
093                // otherwise the page will be rendered via Seam PDF
094                HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getRequest();
095                String url = DocumentModelFunctions.documentUrl(doc, req);
096
097                HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
098                try {
099                    response.sendRedirect(url);
100                    // be sure we block the codec chain
101                    response.flushBuffer();
102                    FacesContext.getCurrentInstance().responseComplete();
103                } catch (IOException ioe) {
104                    log.error("Error while redirecting to standard view", ioe);
105                }
106                return;
107            }
108            if (rendered != null) {
109                if (rendered.getMimeType() != null && rendered.getMimeType().startsWith("text/")) {
110                    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
111                    // add inline download flag
112                    request.setAttribute("inline", "true");
113                }
114                ComponentUtils.download(doc, null, rendered, rendered.getFilename(), "rendition",
115                        Collections.singletonMap("rendition", renditionName));
116            } else {
117                HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
118                response.sendError(404, "Unable to find rendition " + renditionName);
119            }
120        }
121    }
122
123}