001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/enterprise/servlet/ServletRequestWrapper.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:
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: 9338 $, $Date: 23.05.2007 18:09:52$
072: */
073: public class ServletRequestWrapper extends HttpServletRequestWrapper {
074:
075: private static ILogger LOG = LoggerFactory
076: .getLogger(ServletRequestWrapper.class);
077:
078: private static final String BUNDLE_NAME = "org.deegree.enterprise.servlet.ServletRequestWrapper";
079:
080: static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
081: .getBundle(BUNDLE_NAME);
082:
083: private HttpServletRequest origReq = null;
084:
085: private byte[] bytes = null;
086:
087: private Map<String, Object> paramMap;
088:
089: private String queryString;
090:
091: /**
092: * @param request
093: */
094: public ServletRequestWrapper(HttpServletRequest request) {
095: super (request);
096:
097: this .origReq = request;
098:
099: ByteArrayOutputStream bos = new ByteArrayOutputStream(10000);
100: try {
101: InputStream is = origReq.getInputStream();
102: int c = 0;
103: while ((c = is.read()) > -1) {
104: bos.write(c);
105: }
106: bytes = bos.toByteArray();
107: LOG
108: .logDebug("The constructor created a new bytearray in the HttpServletRequestWrapper");
109: } catch (IOException ioe) {
110: LOG
111: .logError(
112: "An error occured while creating a byte-buffered inputstream from the HttpServletRequest inputstream because: "
113: + ioe.getMessage(), ioe);
114: bytes = null;
115: }
116: queryString = request.getQueryString();
117: }
118:
119: // /**
120: // * creates a new ServletInputStream with a copy of the content of the original one
121: // *
122: // * @return
123: // * @throws IOException
124: // */
125: // private ServletInputStream createInputStream()
126: // throws IOException {
127: //
128: // if ( bytes == null ) {
129: // LOG.logDebug( "Creating new bytearray in the HttpServletRequestWrapper" );
130: // ByteArrayOutputStream bos = new ByteArrayOutputStream( 10000 );
131: // InputStream is = origReq.getInputStream();
132: // int c = 0;
133: // while ( ( c = is.read() ) > -1 ) {
134: // bos.write( c );
135: // }
136: // bytes = bos.toByteArray();
137: // }
138: //
139: // return new ProxyServletInputStream( new ByteArrayInputStream( bytes ), bytes.length );
140: // }
141:
142: @Override
143: public Map getParameterMap() {
144: if (paramMap == null) {
145: paramMap = super .getParameterMap();
146: }
147: return paramMap;
148: }
149:
150: @Override
151: public String getParameter(String key) {
152: if (paramMap == null) {
153: paramMap = super .getParameterMap();
154: }
155: Object o = paramMap.get(key);
156: String tmp = null;
157: if (o != null && o.getClass() == String[].class) {
158: tmp = StringTools.arrayToString((String[]) o, ',');
159: } else {
160: tmp = (String) o;
161: }
162: return tmp;
163: }
164:
165: @Override
166: public String[] getParameterValues(String arg0) {
167: if (paramMap == null) {
168: paramMap = super .getParameterMap();
169: }
170: Object o = paramMap.get(arg0);
171: if (o instanceof String) {
172: return new String[] { (String) o };
173: }
174: return (String[]) o;
175: }
176:
177: /**
178: *
179: * @param param
180: */
181: public void setParameter(Map<String, String> param) {
182: this .paramMap = new HashMap<String, Object>(param.size());
183:
184: Iterator<String> iter = param.keySet().iterator();
185: StringBuffer sb = new StringBuffer(500);
186: while (iter.hasNext()) {
187: String key = iter.next();
188: String value = param.get(key);
189: sb.append(key).append('=').append(value);
190: if (iter.hasNext()) {
191: sb.append('&');
192: }
193: this .paramMap.put(key, StringTools.toArray(value, ",",
194: false));
195: }
196: this .queryString = sb.toString();
197: }
198:
199: @Override
200: public String getQueryString() {
201: return queryString;
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
215: .logDebug("ServletRequestWrapper: setting inputstream#byteArray to given bytearra");
216: this .bytes = b;
217: }
218:
219: @Override
220: public BufferedReader getReader() throws IOException {
221: return new BufferedReader(new InputStreamReader(
222: getInputStream(), CharsetUtils.getSystemCharset()));
223: }
224:
225: /**
226: * @see javax.servlet.ServletRequest#getInputStream()
227: */
228: @Override
229: public ServletInputStream getInputStream() throws IOException {
230: if (bytes == null) {
231: LOG
232: .logDebug("Creating new bytearray in the HttpServletRequestWrapper#getInputStream");
233: ByteArrayOutputStream bos = new ByteArrayOutputStream(10000);
234: InputStream is = origReq.getInputStream();
235: int c = 0;
236: while ((c = is.read()) > -1) {
237: bos.write(c);
238: }
239: bytes = bos.toByteArray();
240: }
241:
242: return new ProxyServletInputStream(new ByteArrayInputStream(
243: bytes), bytes.length);
244: }
245:
246: @Override
247: public Principal getUserPrincipal() {
248: if (origReq.getUserPrincipal() != null) {
249: return origReq.getUserPrincipal();
250: }
251: return new Principal() {
252: public String getName() {
253: return RESOURCE_BUNDLE.getString("defaultuser");
254: }
255: };
256:
257: }
258:
259: // ///////////////////////////////////////////////////////////////////////
260: // inner classes //
261: // ///////////////////////////////////////////////////////////////////////
262:
263: /**
264: * @author Administrator
265: *
266: * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code
267: * Templates
268: */
269: private class ProxyServletInputStream extends ServletInputStream {
270:
271: private BufferedInputStream buffered;
272:
273: /**
274: * @param in
275: * the InputStream which will be buffered.
276: * @param length
277: */
278: public ProxyServletInputStream(InputStream in, int length) {
279: if (length > 0)
280: buffered = new BufferedInputStream(in, length);
281: else
282: buffered = new BufferedInputStream(in);
283: }
284:
285: @Override
286: public synchronized int read() throws IOException {
287: return buffered.read();
288: }
289:
290: @Override
291: public synchronized int read(byte b[], int off, int len)
292: throws IOException {
293: return buffered.read(b, off, len);
294: }
295:
296: @Override
297: public synchronized long skip(long n) throws IOException {
298: return buffered.skip(n);
299: }
300:
301: @Override
302: public synchronized int available() throws IOException {
303: return buffered.available();
304: }
305:
306: @Override
307: public synchronized void mark(int readlimit) {
308: buffered.mark(readlimit);
309: }
310:
311: @Override
312: public synchronized void reset() throws IOException {
313: buffered.reset();
314: }
315:
316: @Override
317: public boolean markSupported() {
318: return buffered.markSupported();
319: }
320:
321: @Override
322: public void close() throws IOException {
323: buffered.close();
324: }
325: }
326:
327: }
|