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