001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/enterprise/servlet/ServletResponseWrapper.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 This file is part of deegree. 004 Copyright (C) 2001-2008 by: 005 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 This library is distributed in the hope that it will be useful, 015 but WITHOUT ANY WARRANTY; without even the implied warranty of 016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 017 Lesser General Public License for more details. 018 You should have received a copy of the GNU Lesser General Public 019 License along with this library; if not, write to the Free Software 020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 021 Contact: 022 023 Andreas Poth 024 lat/lon GmbH 025 Aennchenstr. 19 026 53177 Bonn 027 Germany 028 E-Mail: poth@lat-lon.de 029 030 Prof. Dr. Klaus Greve 031 Department of Geography 032 University of Bonn 033 Meckenheimer Allee 166 034 53115 Bonn 035 Germany 036 E-Mail: greve@giub.uni-bonn.de 037 ---------------------------------------------------------------------------*/ 038 package org.deegree.enterprise.servlet; 039 040 import java.io.ByteArrayOutputStream; 041 import java.io.IOException; 042 import java.io.PrintWriter; 043 import java.io.UnsupportedEncodingException; 044 045 import javax.servlet.ServletOutputStream; 046 import javax.servlet.http.HttpServletResponse; 047 import javax.servlet.http.HttpServletResponseWrapper; 048 049 /** 050 * The <code>ServletResponse</code> class is a wrapper for an HttpServletResponse object. It allows to repeadetly 051 * access the stream, without emptying it. 052 * 053 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 054 * 055 * @author last edited by: $Author: apoth $ 056 * 057 * @version $Revision: 9338 $, $Date: 2007-12-27 13:31:31 +0100 (Do, 27 Dez 2007) $ 058 * 059 */ 060 public class ServletResponseWrapper extends HttpServletResponseWrapper { 061 062 protected ServletOutputStream stream = null; 063 064 protected PrintWriter writer = null; 065 066 protected HttpServletResponse origResponse = null; 067 068 private String contentType = null; 069 070 /** 071 * 072 * @param response 073 */ 074 public ServletResponseWrapper( HttpServletResponse response ) { 075 super( response ); 076 origResponse = response; 077 } 078 079 /** 080 * It is possible to re-send the response of an allready handled request (by a servlet) as a new request object. The 081 * new reponse will then be adde (at the end) of the first response, if -for some reason- the (new) reponse alone 082 * should be send to the client. In this case the response stream must be resetted before it is sent anew to the 083 * servlet. This is what this method is for. 084 */ 085 @Override 086 public void reset() { 087 createOutputStream(); 088 } 089 090 /** 091 * 092 * @return a new ServletOutputStream. 093 * @throws IOException 094 */ 095 private ServletOutputStream createOutputStream() { 096 stream = new ProxyServletOutputStream( 10000 ); 097 return stream; 098 } 099 100 @Override 101 public ServletOutputStream getOutputStream() throws IOException { 102 103 if ( stream == null ) { 104 stream = createOutputStream(); 105 } 106 return stream; 107 } 108 109 @Override 110 public PrintWriter getWriter() throws IOException { 111 if ( writer != null ) { 112 return writer; 113 } 114 stream = createOutputStream(); 115 writer = new PrintWriter( stream ); 116 return writer; 117 } 118 119 120 @Override 121 public void setContentType( String contentType ) { 122 this.contentType = contentType; 123 if ( contentType != null ) { 124 super.setContentType( contentType ); 125 } 126 } 127 128 129 @Override 130 public String getContentType() { 131 return this.contentType; 132 } 133 134 // /////////////////////////////////////////////////////////////////////// 135 // inner classes // 136 // /////////////////////////////////////////////////////////////////////// 137 138 /** 139 * The <code>ProxyServletOutputStream</code> class is a wrapper for OutputStream object thus allowing repeaded 140 * access to the stream. 141 * 142 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 143 * 144 * @author last edited by: $Author: apoth $ 145 * 146 * @version $Revision: 9338 $, $Date: 2007-12-27 13:31:31 +0100 (Do, 27 Dez 2007) $ 147 * 148 */ 149 public class ProxyServletOutputStream extends ServletOutputStream { 150 151 private ByteArrayOutputStream bos = null; 152 153 /** 154 * @param length of the buffer 155 */ 156 public ProxyServletOutputStream( int length ) { 157 if ( length > 0 ) 158 bos = new ByteArrayOutputStream( length ); 159 else 160 bos = new ByteArrayOutputStream( 10000 ); 161 } 162 163 @Override 164 public void close() throws IOException { 165 bos.close(); 166 } 167 168 169 @Override 170 public void flush() throws IOException { 171 bos.flush(); 172 } 173 174 @Override 175 public void write( byte[] b, int off, int len ) throws IOException { 176 bos.write( b, off, len ); 177 } 178 179 @Override 180 public void write( byte[] b ) throws IOException { 181 bos.write( b ); 182 } 183 184 @Override 185 public void write( int v ) throws IOException { 186 bos.write( v ); 187 } 188 189 /** 190 * @return the actual bytes of the stream. 191 */ 192 public byte[] toByteArray() { 193 return bos.toByteArray(); 194 } 195 196 /** 197 * @param enc encoding to which the bytes must encoded. 198 * @return a string representation of the byte array with the given encoding. 199 * @throws UnsupportedEncodingException if the encoding is not supported 200 */ 201 public String toString( String enc ) throws UnsupportedEncodingException{ 202 return bos.toString( enc ); 203 } 204 } 205 }