001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/enterprise/servlet/DownloadServlet.java $$
002 /*----------------------------------------------------------------------------
003 This file is part of deegree, http://deegree.org/
004 Copyright (C) 2001-2009 by:
005 - Department of Geography, University of Bonn
006 and
007 - lat/lon GmbH
008
009 This library is free software; you can redistribute it and/or modify it under
010 the terms of the GNU Lesser General Public License as published by the Free
011 Software Foundation; either version 2.1 of the License, or (at your option)
012 any later version.
013 This library is distributed in the hope that it will be useful, but WITHOUT
014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016 details.
017 You should have received a copy of the GNU Lesser General Public License
018 along with this library; if not, write to the Free Software Foundation, Inc.,
019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020
021 Contact information:
022
023 lat/lon GmbH
024 Aennchenstr. 19, 53177 Bonn
025 Germany
026 http://lat-lon.de/
027
028 Department of Geography, University of Bonn
029 Prof. Dr. Klaus Greve
030 Postfach 1147, 53001 Bonn
031 Germany
032 http://www.geographie.uni-bonn.de/deegree/
033
034 e-mail: info@deegree.org
035 ----------------------------------------------------------------------------*/
036
037 package org.deegree.enterprise.servlet;
038
039 import java.io.File;
040 import java.io.IOException;
041 import java.io.OutputStream;
042 import java.io.RandomAccessFile;
043 import java.net.MalformedURLException;
044 import java.net.URL;
045
046 import javax.servlet.ServletConfig;
047 import javax.servlet.ServletContext;
048 import javax.servlet.ServletException;
049 import javax.servlet.http.HttpServlet;
050 import javax.servlet.http.HttpServletRequest;
051 import javax.servlet.http.HttpServletResponse;
052
053 import org.deegree.framework.log.ILogger;
054 import org.deegree.framework.log.LoggerFactory;
055 import org.deegree.framework.util.StringTools;
056 import org.deegree.portal.Constants;
057
058 /**
059 * TODO add documentation here
060 *
061 * @author <a href="mailto:elmasry@lat-lon.de">Moataz Elmasry</a>
062 * @author last edited by: $Author: jmays $
063 *
064 * @version $Revision: 20729 $, $Date: 2009-11-11 16:36:59 +0100 (Mi, 11. Nov 2009) $
065 */
066 public class DownloadServlet extends HttpServlet {
067
068 private static final long serialVersionUID = -1194881119569788359L;
069
070 private static final ILogger LOG = LoggerFactory.getLogger( DownloadServlet.class );
071
072 private String ipAddress = null;
073
074 private String contentType = "application/zip";
075
076 private String downloadDir = null;
077
078 /*
079 * (non-Javadoc)
080 *
081 * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
082 */
083 @Override
084 public void init( ServletConfig config )
085 throws ServletException {
086 super.init( config );
087 ipAddress = this.getInitParameter( "ALLOWED_IP_ADDRESS" );
088 downloadDir = this.getServletContext().getRealPath( this.getInitParameter( "DOWNLOAD_DIR" ) );
089 LOG.logDebug( "************ init download servlet ************" );
090 LOG.logDebug( "Download directory: ", downloadDir );
091 LOG.logDebug( "ipAddress: ", ipAddress );
092 LOG.logDebug( "contentType: ", contentType );
093 }
094
095 /**
096 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
097 *
098 * @param request
099 * servlet request
100 * @param response
101 * servlet response
102 */
103 protected void processRequest( HttpServletRequest request, HttpServletResponse response )
104 throws ServletException, IOException {
105 LOG.logDebug( "Starting to process request" );
106
107 String addr = request.getRemoteAddr();
108 LOG.logDebug( "request.remoteAddr: ", addr );
109
110 OutputStream os = response.getOutputStream();
111 String docPath = downloadDir;
112
113 ServletContext sc = getServletContext();
114 if ( sc != null ) {
115 if ( sc.getAttribute( Constants.DOWNLOADDIR ) != null ) {
116 docPath = (String) sc.getAttribute( Constants.DOWNLOADDIR );
117 }
118 }
119
120 File fileDir;
121 try {
122 fileDir = new File( new URL( docPath ).getFile() );
123 } catch ( MalformedURLException e ) {
124 LOG.logDebug( "Download dir did not start with 'file', trying just as file now.", e );
125 fileDir = new File( docPath );
126 }
127 LOG.logDebug( "download directory (fileDir): ", fileDir.toString() );
128
129 response.setContentType( contentType );
130
131 RandomAccessFile raf = null;
132
133 // TODO make sure the path exists, otherwise return an error.
134 try {
135 if ( ipAddress == null || ipAddress.equals( addr ) ) {
136 String fileName = request.getParameter( "file" ).trim();
137 LOG.logDebug( "zip file name= ", fileName );
138 String filename = StringTools.concat( 100, "attachment;filename=\"", fileName, "\"" );
139 response.addHeader( "Content-Disposition", filename );
140 // response.setHeader( "Content-Disposition", filename );
141 // read each file from the local file system
142 File f = new File( fileDir, fileName );
143 raf = new RandomAccessFile( f, "r" );
144 // LOG.logDebug( "raf.name: " + f.toString() );
145 long size = raf.length();
146 byte[] b = new byte[(int) size];
147 // reads the file to the byte array
148 raf.read( b );
149
150 // write current byte array (file) to the output stream
151 os.write( b );
152 LOG.logDebug( "File is written successfully" );
153 } else {
154 os.write( Messages.getString( "DownloadServlet.ERR_ACC_DENY" ).getBytes( "UTF-8" ) );
155 }
156 } catch ( Exception e ) {
157 LOG.logDebug( "catching error: ", e );
158 String message = org.deegree.i18n.Messages.get( "IGEO_STD_CNTXT_WRONG_PATH", fileDir.toString() );
159 os.write( message.getBytes( "UTF-8" ) );
160 os.write( e.toString().getBytes( "UTF-8" ) );
161 } finally {
162 try {
163 raf.close();
164 os.close();
165 } catch ( Exception e ) {
166 LOG.logError( e.getMessage(), e );
167 }
168 }
169 }
170
171 /*
172 * (non-Javadoc)
173 *
174 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
175 * javax.servlet.http.HttpServletResponse)
176 */
177 @Override
178 protected void doGet( HttpServletRequest request, HttpServletResponse response )
179 throws ServletException, IOException {
180 processRequest( request, response );
181 }
182
183 /*
184 * (non-Javadoc)
185 *
186 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
187 * javax.servlet.http.HttpServletResponse)
188 */
189 @Override
190 protected void doPost( HttpServletRequest request, HttpServletResponse response )
191 throws ServletException, IOException {
192 processRequest( request, response );
193 }
194
195 /*
196 * (non-Javadoc)
197 *
198 * @see javax.servlet.GenericServlet#getServletInfo()
199 */
200 @Override
201 public String getServletInfo() {
202 return "Servlet for accessing documents assigned to a metadata entry";
203 }
204
205 }