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 * Thierry Delprat 018 * Florent Guillaume 019 * Estelle Giuly <egiuly@nuxeo.com> 020 */ 021package org.nuxeo.ecm.platform.ui.web.download; 022 023import java.io.File; 024import java.io.IOException; 025import java.net.URI; 026import java.net.URISyntaxException; 027 028import javax.servlet.http.HttpServlet; 029import javax.servlet.http.HttpServletRequest; 030import javax.servlet.http.HttpServletResponse; 031 032import org.nuxeo.common.Environment; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.ecm.core.api.Blobs; 035import org.nuxeo.ecm.core.io.download.DownloadHelper; 036import org.nuxeo.ecm.core.io.download.DownloadService; 037import org.nuxeo.ecm.core.io.download.DownloadService.DownloadContext; 038import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 039import org.nuxeo.runtime.api.Framework; 040 041/** 042 * Simple download servlet used for big files that can not be downloaded from within the JSF context (because of 043 * buffered ResponseWrapper). 044 */ 045public class DownloadServlet extends HttpServlet { 046 047 private static final long serialVersionUID = 1L; 048 049 /** @deprecated since 7.4, use nxfile instead */ 050 @Deprecated 051 public static final String NXBIGFILE = DownloadService.NXBIGFILE; 052 053 @Override 054 public void doHead(HttpServletRequest req, HttpServletResponse resp) throws IOException { 055 doGet(req, resp); // downstream methods will deal with HEAD and avoid sending a body 056 } 057 058 @Override 059 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { 060 try { 061 handleDownload(req, resp); 062 } catch (IOException ioe) { 063 DownloadHelper.handleClientDisconnect(ioe); 064 } 065 } 066 067 protected void handleDownload(HttpServletRequest req, HttpServletResponse resp) throws IOException { 068 String requestURI; 069 try { 070 requestURI = new URI(req.getRequestURI()).getPath(); 071 } catch (URISyntaxException e) { 072 requestURI = req.getRequestURI(); 073 } 074 // remove context 075 String context = VirtualHostHelper.getContextPath(req) + "/"; 076 if (!requestURI.startsWith(context)) { 077 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid URL syntax"); 078 return; 079 } 080 String baseUrl = VirtualHostHelper.getBaseURL(req); 081 String path = requestURI.substring(context.length()); 082 DownloadService downloadService = Framework.getService(DownloadService.class); 083 downloadService.handleDownload(req, resp, baseUrl, path); 084 } 085 086 /** 087 * @deprecated since 9.1. It was defined for ClipboardActionsBean but it seems not to be used anymore. 088 */ 089 @Deprecated 090 protected void handleDownloadTemporaryZip(HttpServletRequest req, HttpServletResponse resp, String filePath) 091 throws IOException { 092 String[] pathParts = filePath.split("/"); 093 String tmpFileName = pathParts[0]; 094 File tmpZip = new File(Environment.getDefault().getTemp(), tmpFileName); 095 try { 096 Blob zipBlob = Blobs.createBlob(tmpZip); 097 DownloadContext context = DownloadContext.builder(req, resp) 098 .blob(zipBlob) 099 .filename("clipboard.zip") 100 .reason("clipboardZip") 101 .build(); 102 Framework.getService(DownloadService.class).downloadBlob(context); 103 } finally { 104 tmpZip.delete(); 105 } 106 } 107 108}