001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/enterprise/servlet/GZIPResponseWrapper.java $
002    /*----------------------------------------------------------------------------
003     This file originated as work from Jayson Falkner.
004    
005     Copyright 2003 Jayson Falkner (jayson@jspinsider.com)
006     This code is from "Servlets and JavaServer pages; the J2EE Web Tier",
007     http://www.jspbook.com. You may freely use the code both commercially
008     and non-commercially. If you like the code, please pick up a copy of
009     the book and help support the authors, development of more free code,
010     and the JSP/Servlet/J2EE community.
011     ----------------------------------------------------------------------------*/
012    package org.deegree.enterprise.servlet;
013    
014    import java.io.IOException;
015    import java.io.OutputStreamWriter;
016    import java.io.PrintWriter;
017    
018    import javax.servlet.ServletOutputStream;
019    import javax.servlet.http.HttpServletResponse;
020    import javax.servlet.http.HttpServletResponseWrapper;
021    
022    /**
023     *
024     *
025     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
026     * @author last edited by: $Author: mschneider $
027     *
028     * @version $Revision: 20326 $, $Date: 2009-10-22 18:41:56 +0200 (Do, 22 Okt 2009) $
029     */
030    public class GZIPResponseWrapper extends HttpServletResponseWrapper {
031        /**
032         * the original response
033         */
034        protected HttpServletResponse origResponse = null;
035    
036        /**
037         * the output as a stream
038         */
039        protected ServletOutputStream stream = null;
040    
041        /**
042         * the output as a writer
043         */
044        protected PrintWriter writer = null;
045    
046        /**
047         *
048         * @param response
049         */
050        public GZIPResponseWrapper( HttpServletResponse response ) {
051            super( response );
052            origResponse = response;
053        }
054    
055        /**
056         *
057         * @return response stream
058         * @throws IOException
059         */
060        public ServletOutputStream createOutputStream()
061                                throws IOException {
062            return ( new GZIPResponseStream( origResponse ) );
063        }
064    
065        /**
066         *
067         *
068         */
069        public void finishResponse() {
070            try {
071                if ( writer != null ) {
072                    writer.close();
073                } else {
074                    if ( stream != null ) {
075                        stream.close();
076                    }
077                }
078            } catch ( IOException e ) {
079                //notting todo?
080            }
081        }
082    
083        /**
084         * @throws IOException
085         */
086        @Override
087        public void flushBuffer()
088                                throws IOException {
089            stream.flush();
090        }
091    
092        /**
093         * @return ServletOutputStream
094         * @throws IOException
095         */
096        @Override
097        public ServletOutputStream getOutputStream()
098                                throws IOException {
099            if ( writer != null ) {
100                throw new IllegalStateException( "getWriter() has already been called!" );
101            }
102    
103            if ( stream == null )
104                stream = createOutputStream();
105            return ( stream );
106        }
107    
108        /**
109         * @return PrintWriter
110         * @throws IOException
111         */
112        @Override
113        public PrintWriter getWriter()
114                                throws IOException {
115            if ( writer != null ) {
116                return ( writer );
117            }
118    
119            if ( stream != null ) {
120                throw new IllegalStateException( "getOutputStream() has already been called!" );
121            }
122    
123            stream = createOutputStream();
124            writer = new PrintWriter( new OutputStreamWriter( stream, "UTF-8" ) );
125            return ( writer );
126        }
127    
128        @Override
129        public void setContentLength( int length ) {
130            super.setContentLength( length );
131        }
132    }