001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/enterprise/servlet/GZIPResponseWrapper.java $
002    /*
003     This file is part of deegree.
004     Copyright (C) 2001-2008 by:
005     EXSE, Department of Geography, University of Bonn
006     http://www.giub.uni-bonn.de/deegree/
007     lat/lon GmbH
008     http://www.lat-lon.de
009    
010     This library is free software; you can redistribute it and/or
011     modify it under the terms of the GNU Lesser General Public
012     License as published by the Free Software Foundation; either
013     version 2.1 of the License, or (at your option) any later version.
014    
015     This library is distributed in the hope that it will be useful,
016     but WITHOUT ANY WARRANTY; without even the implied warranty of
017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
018     Lesser General Public License for more details.
019    
020     You should have received a copy of the GNU Lesser General Public
021     License along with this library; if not, write to the Free Software
022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
023    
024     Contact:
025    
026     Andreas Poth
027     lat/lon GmbH
028     Aennchenstr. 19
029     53115 Bonn
030     Germany
031     E-Mail: poth@lat-lon.de
032    
033     Prof. Dr. Klaus Greve
034     Department of Geography
035     University of Bonn
036     Meckenheimer Allee 166
037     53115 Bonn
038     Germany
039     E-Mail: greve@giub.uni-bonn.de
040     
041     Copyright 2003 Jayson Falkner (jayson@jspinsider.com)
042     This code is from "Servlets and JavaServer pages; the J2EE Web Tier",
043     http://www.jspbook.com.
044     */
045    package org.deegree.enterprise.servlet;
046    
047    import java.io.IOException;
048    import java.io.OutputStreamWriter;
049    import java.io.PrintWriter;
050    
051    import javax.servlet.ServletOutputStream;
052    import javax.servlet.http.HttpServletResponse;
053    import javax.servlet.http.HttpServletResponseWrapper;
054    
055    /**
056     * 
057     * 
058     *
059     * @version $Revision: 9338 $
060     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
061     * @author last edited by: $Author: apoth $
062     *
063     * @version 1.0. $Revision: 9338 $, $Date: 2007-12-27 13:31:31 +0100 (Do, 27 Dez 2007) $
064     *
065     * @since 2.0
066     */
067    public class GZIPResponseWrapper extends HttpServletResponseWrapper {
068        protected HttpServletResponse origResponse = null;
069    
070        protected ServletOutputStream stream = null;
071    
072        protected PrintWriter writer = null;
073    
074        /**
075         * 
076         * @param response
077         */
078        public GZIPResponseWrapper( HttpServletResponse response ) {
079            super( response );
080            origResponse = response;
081        }
082    
083        /**
084         * 
085         * @return response stream
086         * @throws IOException
087         */
088        public ServletOutputStream createOutputStream() throws IOException {
089            return ( new GZIPResponseStream( origResponse ) );
090        }
091    
092        /**
093         * 
094         *
095         */
096        public void finishResponse() {
097            try {
098                if ( writer != null ) {
099                    writer.close();
100                } else {
101                    if ( stream != null ) {
102                        stream.close();
103                    }
104                }
105            } catch ( IOException e ) {
106            }
107        }
108    
109        /**
110         * @throws IOException
111         */
112        public void flushBuffer() throws IOException {
113            stream.flush();
114        }
115    
116        /**
117         * @return ServletOutputStream
118         * @throws IOException
119         */
120        public ServletOutputStream getOutputStream() throws IOException {
121            if ( writer != null ) {
122                throw new IllegalStateException( "getWriter() has already been called!" );
123            }
124    
125            if ( stream == null )
126                stream = createOutputStream();
127            return ( stream );
128        }
129    
130        /**
131         * @return PrintWriter
132         * @throws IOException
133         */
134        public PrintWriter getWriter() throws IOException {
135            if ( writer != null ) {
136                return ( writer );
137            }
138    
139            if ( stream != null ) {
140                throw new IllegalStateException( "getOutputStream() has already been called!" );
141            }
142    
143            stream = createOutputStream();
144            writer = new PrintWriter( new OutputStreamWriter( stream, "UTF-8" ) );
145            return ( writer );
146        }
147    
148        public void setContentLength( int length ) {
149            super.setContentLength( length );
150        }
151    }