001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/enterprise/servlet/ServletResponseWrapper.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.ByteArrayOutputStream;
039    import java.io.IOException;
040    import java.io.PrintWriter;
041    import java.io.UnsupportedEncodingException;
042    
043    import javax.servlet.ServletOutputStream;
044    import javax.servlet.http.HttpServletResponse;
045    import javax.servlet.http.HttpServletResponseWrapper;
046    
047    /**
048     * The <code>ServletResponse</code> class is a wrapper for an HttpServletResponse object. It allows to repeatedly
049     * access the stream, without emptying it.
050     *
051     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
052     *
053     * @author last edited by: $Author: mschneider $
054     *
055     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
056     *
057     */
058    public class ServletResponseWrapper extends HttpServletResponseWrapper {
059    
060        /**
061         * The output as a stream
062         */
063        protected ServletOutputStream stream = null;
064    
065        /**
066         * The output as a writer
067         */
068        protected PrintWriter writer = null;
069    
070        /**
071         * the original response
072         */
073        protected HttpServletResponse origResponse = null;
074    
075        private String contentType = null;
076    
077        /**
078         *
079         * @param response
080         */
081        public ServletResponseWrapper( HttpServletResponse response ) {
082            super( response );
083            origResponse = response;
084        }
085    
086        /**
087         * It is possible to re-send the response of an already handled request (by a servlet) as a new request object. The
088         * new response will then be added (at the end) of the first response, if -for some reason- the (new) response alone
089         * should be send to the client. In this case the response stream must be reset before it is sent to the client
090         * anew. This is what this method is for.
091         */
092        @Override
093        public void reset() {
094            createOutputStream();
095        }
096    
097        /**
098         * set the local variable to new stream.
099         */
100        private void createOutputStream() {
101            stream = new ProxyServletOutputStream( 10000 );
102        }
103    
104        @Override
105        public ServletOutputStream getOutputStream()
106                                throws IOException {
107            if ( stream == null ) {
108                createOutputStream();
109            }
110            return stream;
111        }
112    
113        @Override
114        public PrintWriter getWriter()
115                                throws IOException {
116            if ( writer != null ) {
117                return writer;
118            }
119            createOutputStream();
120            writer = new PrintWriter( stream );
121            return writer;
122        }
123    
124        @Override
125        public void setContentType( String contentType ) {
126            this.contentType = contentType;
127            if ( contentType != null ) {
128                super.setContentType( contentType );
129            }
130        }
131    
132        @Override
133        public String getContentType() {
134            return this.contentType;
135        }
136    
137        // ///////////////////////////////////////////////////////////////////////
138        // inner classes //
139        // ///////////////////////////////////////////////////////////////////////
140    
141        /**
142         * The <code>ProxyServletOutputStream</code> class is a wrapper for OutputStream object thus allowing repeated
143         * access to the stream.
144         *
145         * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
146         *
147         * @author last edited by: $Author: mschneider $
148         *
149         * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
150         *
151         */
152        public class ProxyServletOutputStream extends ServletOutputStream {
153    
154            private ByteArrayOutputStream bos = null;
155    
156            /**
157             * @param length
158             *            of the buffer
159             */
160            public ProxyServletOutputStream( int length ) {
161                if ( length > 0 )
162                    bos = new ByteArrayOutputStream( length );
163                else
164                    bos = new ByteArrayOutputStream( 10000 );
165            }
166    
167            @Override
168            public void close()
169                                    throws IOException {
170                bos.close();
171            }
172    
173            @Override
174            public void flush()
175                                    throws IOException {
176                bos.flush();
177            }
178    
179            @Override
180            public void write( byte[] b, int off, int len )
181                                    throws IOException {
182                bos.write( b, off, len );
183            }
184    
185            @Override
186            public void write( byte[] b )
187                                    throws IOException {
188                bos.write( b );
189            }
190    
191            @Override
192            public void write( int v )
193                                    throws IOException {
194                bos.write( v );
195            }
196    
197            /**
198             * @return the actual bytes of the stream.
199             */
200            public byte[] toByteArray() {
201                return bos.toByteArray();
202            }
203    
204            /**
205             * @param enc
206             *            encoding to which the bytes must encoded.
207             * @return a string representation of the byte array with the given encoding.
208             * @throws UnsupportedEncodingException
209             *             if the encoding is not supported
210             */
211            public String toString( String enc )
212                                    throws UnsupportedEncodingException {
213                return bos.toString( enc );
214            }
215        }
216    }