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