001/*
002 * (C) Copyright 2006-2016 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 */
019package org.nuxeo.ecm.platform.ui.web.restAPI;
020
021import static org.jboss.seam.ScopeType.EVENT;
022
023import java.io.IOException;
024import java.io.Serializable;
025import java.util.Map;
026
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029
030import org.apache.commons.lang3.StringUtils;
031import org.jboss.seam.annotations.In;
032import org.jboss.seam.annotations.Name;
033import org.jboss.seam.annotations.Scope;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentModel;
037import org.nuxeo.ecm.core.api.IdRef;
038import org.nuxeo.ecm.core.api.NuxeoException;
039import org.nuxeo.ecm.core.io.download.DownloadService;
040import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
041import org.nuxeo.ecm.platform.ui.web.tag.fn.LiveEditConstants;
042import org.nuxeo.ecm.platform.util.RepositoryLocation;
043import org.nuxeo.runtime.api.Framework;
044import org.restlet.data.Request;
045import org.restlet.data.Response;
046
047/**
048 * Restlet to help LiveEdit clients download the blob content of a document
049 *
050 * @author Sun Tan <stan@nuxeo.com>
051 * @author Olivier Grisel <ogrisel@nuxeo.com>
052 */
053@Name("downloadFileRestlet")
054@Scope(EVENT)
055public class DownloadFileRestlet extends BaseNuxeoRestlet implements LiveEditConstants, Serializable {
056
057    private static final long serialVersionUID = -2163290273836947871L;
058
059    @In(create = true)
060    protected transient NavigationContext navigationContext;
061
062    protected CoreSession documentManager;
063
064    @Override
065    public void handle(Request req, Response res) {
066        HttpServletRequest request = getHttpRequest(req);
067        HttpServletResponse response = getHttpResponse(res);
068
069        String repo = (String) req.getAttributes().get("repo");
070        if (repo == null || repo.equals("*")) {
071            handleError(res, "you must specify a repository");
072            return;
073        }
074
075        DocumentModel dm;
076        try {
077            navigationContext.setCurrentServerLocation(new RepositoryLocation(repo));
078            documentManager = navigationContext.getOrCreateDocumentManager();
079            String docid = (String) req.getAttributes().get("docid");
080            if (docid != null) {
081                dm = documentManager.getDocument(new IdRef(docid));
082            } else {
083                handleError(res, "you must specify a valid document IdRef");
084                return;
085            }
086        } catch (NuxeoException e) {
087            handleError(res, e);
088            return;
089        }
090
091        try {
092            String blobPropertyName = getQueryParamValue(req, BLOB_PROPERTY_NAME, null);
093            String filenamePropertyName = getQueryParamValue(req, FILENAME_PROPERTY_NAME, null);
094            Blob blob;
095            String xpath;
096            String filename;
097            if (blobPropertyName != null && filenamePropertyName != null) {
098                filename = (String) dm.getPropertyValue(filenamePropertyName);
099                blob = (Blob) dm.getPropertyValue(blobPropertyName);
100                xpath = blobPropertyName;
101            } else {
102                String schemaName = getQueryParamValue(req, SCHEMA, DEFAULT_SCHEMA);
103                String blobFieldName = getQueryParamValue(req, BLOB_FIELD, DEFAULT_BLOB_FIELD);
104                blob = (Blob) dm.getProperty(schemaName, blobFieldName);
105                filename = StringUtils.defaultIfBlank(blob.getFilename(), "file");
106                xpath = schemaName + ':' + blobFieldName;
107            }
108
109            // trigger download
110            String reason = "download";
111            Map<String, Serializable> extendedInfos = null;
112            DownloadService downloadService = Framework.getService(DownloadService.class);
113            downloadService.downloadBlob(request, response, dm, xpath, blob, filename, reason, extendedInfos, null,
114                    byteRange -> setEntityToBlobOutput(blob, byteRange, res));
115        } catch (IOException | NuxeoException e) {
116            handleError(res, e);
117        }
118    }
119
120}