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 <[email protected]> 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.platform.web.common.vh.VirtualHostHelper; 038import org.nuxeo.runtime.api.Framework; 039 040/** 041 * Simple download servlet used for big files that can not be downloaded from within the JSF context (because of 042 * buffered ResponseWrapper). 043 */ 044public class DownloadServlet extends HttpServlet { 045 046 private static final long serialVersionUID = 1L; 047 048 /** @deprecated since 7.4, use nxfile instead */ 049 @Deprecated 050 public static final String NXBIGFILE = DownloadService.NXBIGFILE; 051 052 @Override 053 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { 054 try { 055 handleDownload(req, resp); 056 } catch (IOException ioe) { 057 DownloadHelper.handleClientDisconnect(ioe); 058 } 059 } 060 061 protected void handleDownload(HttpServletRequest req, HttpServletResponse resp) throws IOException { 062 String requestURI; 063 try { 064 requestURI = new URI(req.getRequestURI()).getPath(); 065 } catch (URISyntaxException e) { 066 requestURI = req.getRequestURI(); 067 } 068 // remove context 069 String context = VirtualHostHelper.getContextPath(req) + "/"; 070 if (!requestURI.startsWith(context)) { 071 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid URL syntax"); 072 return; 073 } 074 String baseUrl = VirtualHostHelper.getBaseURL(req); 075 String path = requestURI.substring(context.length()); 076 DownloadService downloadService = Framework.getService(DownloadService.class); 077 downloadService.handleDownload(req, resp, baseUrl, path); 078 } 079 080 /** 081 * @deprecated since 9.1. It was defined for ClipboardActionsBean but it seems not to be used anymore. 082 */ 083 @Deprecated 084 protected void handleDownloadTemporaryZip(HttpServletRequest req, HttpServletResponse resp, String filePath) 085 throws IOException { 086 String[] pathParts = filePath.split("/"); 087 String tmpFileName = pathParts[0]; 088 File tmpZip = new File(Environment.getDefault().getTemp(), tmpFileName); 089 try { 090 Blob zipBlob = Blobs.createBlob(tmpZip); 091 DownloadService downloadService = Framework.getService(DownloadService.class); 092 downloadService.downloadBlob(req, resp, null, null, zipBlob, "clipboard.zip", "clipboardZip"); 093 } finally { 094 tmpZip.delete(); 095 } 096 } 097 098}