    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/enterprise/servlet/LoggingFilter.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    package org.deegree.enterprise.servlet;
    037
    038    import java.io.IOException;
    039    import java.io.InputStream;
    040    import java.io.OutputStream;
    041    import java.util.List;
    042
    043    import javax.servlet.Filter;
    044    import javax.servlet.FilterChain;
    045    import javax.servlet.FilterConfig;
    046    import javax.servlet.ServletException;
    047    import javax.servlet.ServletRequest;
    048    import javax.servlet.ServletResponse;
    049    import javax.servlet.http.HttpServletRequest;
    050    import javax.servlet.http.HttpServletResponse;
    051
    052    import org.deegree.framework.util.StringTools;
    053
    054    /**
    055     * servlet filter for logging incoming requests and outgoing responses. Through the init-params a
    056     * user may limit logging to:
    057     * <ul>
    058     * <li>requests received from a list of defined source/remote addresses</li>
    059     * <li>returned mime types</li>
    060     * <li>meta informations about the incoming request</li>
    061     * </ul>
    062     *
    063     *
    064     * @version $Revision: 18195 $
    065     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    066     * @author last edited by: $Author: mschneider $
    067     *
    068     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    069     *
    070     * @since 2.0
    071     */
    072    public class LoggingFilter implements Filter {
    073
    074        private List<String> remoteAddresses;
    075
    076        private List<String> mimeTypes;
    077
    078        private boolean metaInfo = false;
    079
    080        /**
    081         * @param config
    082         * @throws ServletException
    083         */
    084        public void init( FilterConfig config )
    085                                throws ServletException {
    086
    087            try {
    088                if ( config.getInitParameter( "sourceAddresses" ) != null ) {
    089                    remoteAddresses = StringTools.toList( config.getInitParameter( "sourceAddresses" ), ";", true );
    090                }
    091
    092                if ( config.getInitParameter( "mimeTypes" ) != null ) {
    093                    mimeTypes = StringTools.toList( config.getInitParameter( "mimeTypes" ), ";", true );
    094                }
    095                metaInfo = "true".equalsIgnoreCase( config.getInitParameter( "metaInfo" ) );
    096            } catch ( Exception e ) {
    097                e.printStackTrace();
    098            }
    099        }
    100
    101        /**
    102         * @param request
    103         * @param response
    104         * @param chain
    105         * @throws IOException
    106         * @throws ServletException
    107         */
    108        public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
    109                                throws IOException, ServletException {
    110
    111            synchronized ( this ) {
    112
    113                ServletResponseWrapper resWrap = new ServletResponseWrapper( (HttpServletResponse) response );
    114
    115                String url = ( (HttpServletRequest) request ).getRemoteAddr();
    116                if ( remoteAddresses == null || remoteAddresses.contains( url ) ) {
    117                    ServletRequestWrapper reqWrap = new ServletRequestWrapper( (HttpServletRequest) request );
    118                    // log request content
    119                    System.out.println( reqWrap.getRequestURI() );
    120                    InputStream is = reqWrap.getInputStream();
    121                    int c = 0;
    122                    while ( ( c = is.read() ) > -1 ) {
    123                        System.out.print( (char) c );
    124                    }
    125                    is.close();
    126
    127                    if ( metaInfo ) {
    128                        // log request meta information
    129                        System.out.println( "getRemoteAddr " + reqWrap.getRemoteAddr() );
    130                        System.out.println( "getPort " + reqWrap.getServerPort() );
    131                        System.out.println( "getMethod " + reqWrap.getMethod() );
    132                        System.out.println( "getPathInfo " + reqWrap.getPathInfo() );
    133                        System.out.println( "getRequestURI " + reqWrap.getRequestURI() );
    134                        System.out.println( "getServerName " + reqWrap.getServerName() );
    135                        System.out.println( "getServerPort " + reqWrap.getServerPort() );
    136                        System.out.println( "getServletPath " + reqWrap.getServletPath() );
    137                    }
    138                    chain.doFilter( reqWrap, resWrap );
    139                } else {
    140                    chain.doFilter( request, resWrap );
    141                }
    142
    143                OutputStream os = resWrap.getOutputStream();
    144                byte[] b = ( (ServletResponseWrapper.ProxyServletOutputStream) os ).toByteArray();
    145                os.close();
    146
    147                String mime = resWrap.getContentType();
    148                System.out.println( "mime type: " + mime );
    149                if ( mimeTypes == null || ( mime != null && mimeTypes.contains( mime ) ) ) {
    150                    System.out.write( b );
    151                }
    152
    153                response.setContentType( mime );
    154                response.setContentLength( b.length );
    155
    156                os = response.getOutputStream();
    157                os.write( b );
    158                os.close();
    159            }
    160
    161        }
    162
    163        /**
    164         *
    165         */
    166        public void destroy() {
    167            // TODO Auto-generated method stub
    168        }
    169
    170    }