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