001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/enterprise/servlet/DirectoryAccessServlet.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 by:
006     Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53177 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041     
042     ---------------------------------------------------------------------------*/
043    
044    package org.deegree.enterprise.servlet;
045    
046    import java.io.File;
047    import java.io.IOException;
048    import java.io.OutputStream;
049    import java.io.RandomAccessFile;
050    import java.util.Map;
051    
052    import javax.servlet.ServletException;
053    import javax.servlet.http.HttpServlet;
054    import javax.servlet.http.HttpServletRequest;
055    import javax.servlet.http.HttpServletResponse;
056    
057    import org.deegree.framework.util.KVP2Map;
058    
059    /**
060     * This is one possible web application to be used for the printing functionality in iGeoPortal Std.
061     * 
062     * @author <a href="mailto:mays@lat-lon.de">Judit Mays</a>
063     * @author last edited by: $Author: bezema $
064     * 
065     * @version $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
066     */
067    public class DirectoryAccessServlet extends HttpServlet {
068    
069        /**
070         * 
071         */
072        private static final long serialVersionUID = 2816833149662971995L;
073        private String rootDir;
074    
075        /* (non-Javadoc)
076         * @see javax.servlet.GenericServlet#init()
077         */
078        @Override
079        public void init()
080                                throws ServletException {
081            super.init();
082            rootDir = getInitParameter( "ROOTDIR" );
083            if ( rootDir == null ) {
084                rootDir = getServletContext().getRealPath( "." );
085            } else {
086                File file = new File( rootDir );
087                if ( !file.isAbsolute() ) {
088                    rootDir = getServletContext().getRealPath( rootDir );
089                }
090            }
091            if ( !rootDir.endsWith( "/" ) ) {
092                rootDir += '/';
093            }
094        }
095    
096        /* (non-Javadoc)
097         * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
098         */
099        @Override
100        protected void doGet( HttpServletRequest request, HttpServletResponse response )
101                                throws ServletException, IOException {
102            doPost( request, response );
103        }
104    
105        /* (non-Javadoc)
106         * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
107         */
108        @Override
109        protected void doPost( HttpServletRequest request, HttpServletResponse response )
110                                throws ServletException, IOException {
111            Map<String, String> reqParams = KVP2Map.toMap( request );
112            String req = reqParams.get( "REQUEST" );
113            if ( "download".equals( req ) ) {
114                download( reqParams, response );
115            } else {
116                throw new ServletException( "Unknown operation: " + req );
117            }
118        }
119    
120        /**
121         * @param reqParams
122         * @param response
123         * @throws IOException
124         */
125        private void download( Map<String, String> reqParams, HttpServletResponse response )
126                                throws IOException {
127    
128            String file = reqParams.get( "FILENAME" );
129            String path = rootDir + file;
130            RandomAccessFile raf = new RandomAccessFile( path, "r" );
131            long l = raf.length();
132            byte[] b = new byte[(int) l];
133            raf.read( b );
134            raf.close();
135    
136            OutputStream os = response.getOutputStream();
137            os.write( b );
138            os.close();
139        }
140    
141    }
142    
143    /* ********************************************************************
144     Changes to this class. What the people have been up to:
145    
146     $Log$
147     Revision 1.1  2006/12/05 17:55:22  mays
148     add new servlet used for printing in iGeoPortal
149    
150     ********************************************************************** */