001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/enterprise/servlet/DirectoryAccessServlet.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.util.Map;
044    
045    import javax.servlet.ServletException;
046    import javax.servlet.http.HttpServlet;
047    import javax.servlet.http.HttpServletRequest;
048    import javax.servlet.http.HttpServletResponse;
049    
050    import org.deegree.framework.util.KVP2Map;
051    
052    /**
053     * This is one possible web application to be used for the printing functionality in iGeoPortal Std.
054     *
055     * @author <a href="mailto:mays@lat-lon.de">Judit Mays</a>
056     * @author last edited by: $Author: mschneider $
057     *
058     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
059     */
060    public class DirectoryAccessServlet extends HttpServlet {
061    
062        /**
063         *
064         */
065        private static final long serialVersionUID = 2816833149662971995L;
066    
067        private String rootDir;
068    
069        /*
070         * (non-Javadoc)
071         *
072         * @see javax.servlet.GenericServlet#init()
073         */
074        @Override
075        public void init()
076                                throws ServletException {
077            super.init();
078            rootDir = getInitParameter( "ROOTDIR" );
079            if ( rootDir == null ) {
080                rootDir = getServletContext().getRealPath( "." );
081            } else {
082                File file = new File( rootDir );
083                if ( !file.isAbsolute() ) {
084                    rootDir = getServletContext().getRealPath( rootDir );
085                }
086            }
087            if ( !rootDir.endsWith( "/" ) ) {
088                rootDir += '/';
089            }
090        }
091    
092        /*
093         * (non-Javadoc)
094         *
095         * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
096         *      javax.servlet.http.HttpServletResponse)
097         */
098        @Override
099        protected void doGet( HttpServletRequest request, HttpServletResponse response )
100                                throws ServletException, IOException {
101            doPost( request, response );
102        }
103    
104        /*
105         * (non-Javadoc)
106         *
107         * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
108         *      javax.servlet.http.HttpServletResponse)
109         */
110        @Override
111        protected void doPost( HttpServletRequest request, HttpServletResponse response )
112                                throws ServletException, IOException {
113            Map<String, String> reqParams = KVP2Map.toMap( request );
114            String req = reqParams.get( "REQUEST" );
115            if ( "download".equals( req ) ) {
116                download( reqParams, response );
117            } else {
118                throw new ServletException( "Unknown operation: " + req );
119            }
120        }
121    
122        /**
123         * @param reqParams
124         * @param response
125         * @throws IOException
126         */
127        private void download( Map<String, String> reqParams, HttpServletResponse response )
128                                throws IOException {
129    
130            String file = reqParams.get( "FILENAME" );
131            String path = rootDir + file;
132            RandomAccessFile raf = new RandomAccessFile( path, "r" );
133            long l = raf.length();
134            byte[] b = new byte[(int) l];
135            raf.read( b );
136            raf.close();
137    
138            OutputStream os = response.getOutputStream();
139            os.write( b );
140            os.close();
141        }
142    
143    }