001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/enterprise/servlet/ServletRequestWrapper.java $
002 /*---------------- FILE HEADER ------------------------------------------
003 This file is part of deegree.
004 Copyright (C) 2001-2007 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
039 package org.deegree.enterprise.servlet;
040
041 import java.io.BufferedInputStream;
042 import java.io.BufferedReader;
043 import java.io.ByteArrayInputStream;
044 import java.io.ByteArrayOutputStream;
045 import java.io.IOException;
046 import java.io.InputStream;
047 import java.io.InputStreamReader;
048 import java.security.Principal;
049 import java.util.Arrays;
050 import java.util.Enumeration;
051 import java.util.HashMap;
052 import java.util.Iterator;
053 import java.util.Map;
054 import java.util.ResourceBundle;
055
056 import javax.servlet.ServletInputStream;
057 import javax.servlet.http.HttpServletRequest;
058 import javax.servlet.http.HttpServletRequestWrapper;
059
060 import org.deegree.framework.log.ILogger;
061 import org.deegree.framework.log.LoggerFactory;
062 import org.deegree.framework.util.CharsetUtils;
063 import org.deegree.framework.util.StringTools;
064
065 /**
066 * TODO describe function and usage of the class here.
067 *
068 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
069 * @author last edited by: $Author: mays$
070 *
071 * @version $Revision: 7994 $, $Date: 23.05.2007 18:09:52$
072 */
073 public class ServletRequestWrapper extends HttpServletRequestWrapper {
074
075 private static ILogger LOG = LoggerFactory.getLogger( ServletRequestWrapper.class );
076
077 private static final String BUNDLE_NAME = "org.deegree.enterprise.servlet.ServletRequestWrapper";
078
079 static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle( BUNDLE_NAME );
080
081 private HttpServletRequest origReq = null;
082
083 private byte[] bytes = null;
084
085 private Map<String, Object> paramMap;
086
087 private String queryString;
088
089 /**
090 * @param request
091 */
092 public ServletRequestWrapper( HttpServletRequest request ) {
093 super( request );
094
095 this.origReq = request;
096
097 ByteArrayOutputStream bos = new ByteArrayOutputStream( 10000 );
098 try {
099 InputStream is = origReq.getInputStream();
100 int c = 0;
101 while ( ( c = is.read() ) > -1 ) {
102 bos.write( c );
103 }
104 bytes = bos.toByteArray();
105 LOG.logDebug( "The constructor created a new bytearray in the HttpServletRequestWrapper" );
106 } catch ( IOException ioe ) {
107 LOG.logError( "An error occured while creating a byte-buffered inputstream from the HttpServletRequest inputstream because: " + ioe.getMessage(),
108 ioe );
109 bytes = null;
110 }
111 queryString = request.getQueryString();
112 }
113
114
115
116 // /**
117 // * creates a new ServletInputStream with a copy of the content of the original one
118 // *
119 // * @return
120 // * @throws IOException
121 // */
122 // private ServletInputStream createInputStream()
123 // throws IOException {
124 //
125 // if ( bytes == null ) {
126 // LOG.logDebug( "Creating new bytearray in the HttpServletRequestWrapper" );
127 // ByteArrayOutputStream bos = new ByteArrayOutputStream( 10000 );
128 // InputStream is = origReq.getInputStream();
129 // int c = 0;
130 // while ( ( c = is.read() ) > -1 ) {
131 // bos.write( c );
132 // }
133 // bytes = bos.toByteArray();
134 // }
135 //
136 // return new ProxyServletInputStream( new ByteArrayInputStream( bytes ), bytes.length );
137 // }
138
139 @Override
140 public Map getParameterMap() {
141 if ( paramMap == null ) {
142 paramMap = super.getParameterMap();
143 }
144 return paramMap;
145 }
146
147 @Override
148 public String getParameter( String key ) {
149 if ( paramMap == null ) {
150 paramMap = super.getParameterMap();
151 }
152 Object o = paramMap.get( key );
153 String tmp = null;
154 if ( o != null && o.getClass() == String[].class ) {
155 tmp = StringTools.arrayToString( (String[])o, ',' );
156 } else {
157 tmp = (String)o;
158 }
159 return tmp;
160 }
161
162
163 @Override
164 public String[] getParameterValues( String arg0 ) {
165 if ( paramMap == null ) {
166 paramMap = super.getParameterMap();
167 }
168 Object o = paramMap.get( arg0 );
169 if ( o instanceof String) {
170 return new String[] { (String)o };
171 }
172 return (String[])o;
173 }
174
175 /**
176 *
177 * @param param
178 */
179 public void setParameter(Map<String,String> param) {
180 this.paramMap = new HashMap<String, Object>(param.size());
181
182 Iterator<String> iter = param.keySet().iterator();
183 StringBuffer sb = new StringBuffer(500);
184 while ( iter.hasNext() ) {
185 String key = iter.next();
186 String value = param.get( key);
187 sb.append( key ).append( '=' ).append( value );
188 if ( iter.hasNext() ) {
189 sb.append( '&' );
190 }
191 this.paramMap.put( key, StringTools.toArray( value, ",", false ) );
192 }
193 this.queryString = sb.toString();
194 }
195
196
197 @Override
198 public String getQueryString() {
199 return queryString;
200 }
201
202
203
204 /**
205 * sets the content of the inputstream returned by the
206 *
207 * @see #getReader() and the
208 * @see #getInputStream() method as a byte array. Calling this method will override the content that may has been
209 * read from the <code>HttpServletRequest</code> that has been passed to the constructor
210 *
211 * @param b
212 */
213 public void setInputStreamAsByteArray( byte[] b ) {
214 LOG.logDebug( "ServletRequestWrapper: setting inputstream#byteArray to given bytearra" );
215 this.bytes = b;
216 }
217
218 @Override
219 public BufferedReader getReader() throws IOException {
220 return new BufferedReader( new InputStreamReader( getInputStream(), CharsetUtils.getSystemCharset() ) );
221 }
222
223 /**
224 * @see javax.servlet.ServletRequest#getInputStream()
225 */
226 @Override
227 public ServletInputStream getInputStream() throws IOException {
228 if ( bytes == null ) {
229 LOG.logDebug( "Creating new bytearray in the HttpServletRequestWrapper#getInputStream" );
230 ByteArrayOutputStream bos = new ByteArrayOutputStream( 10000 );
231 InputStream is = origReq.getInputStream();
232 int c = 0;
233 while ( ( c = is.read() ) > -1 ) {
234 bos.write( c );
235 }
236 bytes = bos.toByteArray();
237 }
238
239 return new ProxyServletInputStream( new ByteArrayInputStream( bytes ), bytes.length );
240 }
241
242 @Override
243 public Principal getUserPrincipal() {
244 if ( origReq.getUserPrincipal() != null ) {
245 return origReq.getUserPrincipal();
246 }
247 return new Principal() {
248 public String getName() {
249 return RESOURCE_BUNDLE.getString( "defaultuser" );
250 }
251 };
252
253 }
254
255 // ///////////////////////////////////////////////////////////////////////
256 // inner classes //
257 // ///////////////////////////////////////////////////////////////////////
258
259 /**
260 * @author Administrator
261 *
262 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code
263 * Templates
264 */
265 private class ProxyServletInputStream extends ServletInputStream {
266
267 private BufferedInputStream buffered;
268
269 /**
270 * @param in
271 * the InputStream which will be buffered.
272 * @param length
273 */
274 public ProxyServletInputStream( InputStream in, int length ) {
275 if ( length > 0 )
276 buffered = new BufferedInputStream( in, length );
277 else
278 buffered = new BufferedInputStream( in );
279 }
280
281 @Override
282 public synchronized int read() throws IOException {
283 return buffered.read();
284 }
285
286 @Override
287 public synchronized int read( byte b[], int off, int len ) throws IOException {
288 return buffered.read( b, off, len );
289 }
290
291 @Override
292 public synchronized long skip( long n ) throws IOException {
293 return buffered.skip( n );
294 }
295
296 @Override
297 public synchronized int available() throws IOException {
298 return buffered.available();
299 }
300
301 @Override
302 public synchronized void mark( int readlimit ) {
303 buffered.mark( readlimit );
304 }
305
306 @Override
307 public synchronized void reset() throws IOException {
308 buffered.reset();
309 }
310
311 @Override
312 public boolean markSupported() {
313 return buffered.markSupported();
314 }
315
316 @Override
317 public void close() throws IOException {
318 buffered.close();
319 }
320 }
321
322 }