001: /*
002: * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source 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, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: *
028: * $Id: ServletRequestWrapper.java,v 1.2 2004/09/29 00:12:46 cvs Exp $
029: */
030:
031: package javax.servlet;
032:
033: import java.io.BufferedReader;
034: import java.io.IOException;
035: import java.io.UnsupportedEncodingException;
036: import java.util.Enumeration;
037: import java.util.Locale;
038: import java.util.Map;
039:
040: /**
041: * Wraps a servlet request in another request. Filters may
042: * use ServletRequestWrapper to modify the headers passed to the servlet.
043: *
044: * <p/>The default methods just call the wrapped request methods.
045: *
046: * @since servlet 2.3
047: */
048: public class ServletRequestWrapper implements ServletRequest {
049: // the wrapped request
050: private ServletRequest _request;
051:
052: /**
053: * Create a new ServletRequestWrapper wrapping the enclosed request.
054: */
055: public ServletRequestWrapper(ServletRequest request) {
056: if (request == null)
057: throw new IllegalArgumentException();
058:
059: _request = request;
060: }
061:
062: /**
063: * Sets the request object being wrapped.
064: *
065: * @exception IllegalArgumentException if the request is null
066: */
067: public void setRequest(ServletRequest request) {
068: if (request == null)
069: throw new IllegalArgumentException();
070:
071: _request = request;
072: }
073:
074: /**
075: * Gets the request object being wrapped.
076: *
077: * @return the wrapped response
078: */
079: public ServletRequest getRequest() {
080: return _request;
081: }
082:
083: /**
084: * Returns the prococol, e.g. "HTTP/1.1"
085: */
086: public String getProtocol() {
087: return _request.getProtocol();
088: }
089:
090: /**
091: * Returns the request scheme, e.g. "http"
092: */
093: public String getScheme() {
094: return _request.getScheme();
095: }
096:
097: /**
098: * Returns the server name handling the request. When using virtual hosts,
099: * this returns the virtual host name, e.g. "vhost1.caucho.com".
100: */
101: public String getServerName() {
102: return _request.getServerName();
103: }
104:
105: /**
106: * Returns the server port handling the request, e.g. 80.
107: */
108: public int getServerPort() {
109: return _request.getServerPort();
110: }
111:
112: /**
113: * Returns the IP address of the remote host, i.e. the client browser.
114: */
115: public String getRemoteAddr() {
116: return _request.getRemoteAddr();
117: }
118:
119: /**
120: * Returns the DNS hostname of the remote host, i.e. the client browser.
121: */
122: public String getRemoteHost() {
123: return _request.getRemoteHost();
124: }
125:
126: /**
127: * Returns the remote port
128: *
129: * @since 2.4
130: */
131: public int getRemotePort() {
132: return _request.getRemotePort();
133: }
134:
135: /**
136: * Returns the IP address of the local host, i.e. the server.
137: */
138: public String getLocalAddr() {
139: return _request.getLocalAddr();
140: }
141:
142: /**
143: * Returns the local host name.
144: */
145: public String getLocalName() {
146: return _request.getLocalName();
147: }
148:
149: /**
150: * Returns the local port
151: */
152: public int getLocalPort() {
153: return _request.getLocalPort();
154: }
155:
156: /**
157: * Returns a form parameter. When the form contains several parameters
158: * of the same name, <code>getParameter</code> returns the first.
159: *
160: * <p>For example, calling <code>getParameter("a")</code> with the
161: * the query string <code>a=1&a=2</code> will return "1".
162: *
163: * @param name the form parameter to return
164: * @return the form value or null if none matches.
165: */
166: public String getParameter(String name) {
167: return _request.getParameter(name);
168: }
169:
170: /**
171: * Returns the parameter map request parameters. By default, returns
172: * the underlying request's map.
173: */
174: public Map getParameterMap() {
175: return _request.getParameterMap();
176: }
177:
178: /**
179: * Returns all values of a form parameter.
180: *
181: * <p>For example, calling <code>getParameterValues("a")</code>
182: * with the the query string <code>a=1&a=2</code> will
183: * return ["1", "2"].
184: *
185: * @param name the form parameter to return
186: * @return an array of matching form values or null if none matches.
187: */
188: public String[] getParameterValues(String name) {
189: return _request.getParameterValues(name);
190: }
191:
192: /**
193: * Returns an enumeration of all form parameter names.
194: *
195: * <code><pre>
196: * Enumeration e = _request.getParameterNames();
197: * while (e.hasMoreElements()) {
198: * String name = (String) e.nextElement();
199: * out.println(name + ": " + request.getParameter(name));
200: * }
201: * </pre></code>
202: */
203: public Enumeration getParameterNames() {
204: return _request.getParameterNames();
205: }
206:
207: /**
208: * Returns an InputStream to retrieve POST data from the request.
209: * The stream will automatically end when the end of the POST data
210: * is complete.
211: */
212: public ServletInputStream getInputStream() throws IOException {
213: return _request.getInputStream();
214: }
215:
216: /**
217: * Returns a reader to read POSTed data. Character encoding is
218: * based on the request data and is the same as
219: * <code>getCharacterEncoding()</code>
220: */
221: public BufferedReader getReader() throws IOException,
222: IllegalStateException {
223: return _request.getReader();
224: }
225:
226: /**
227: * Returns the character encoding of the POSTed data.
228: */
229: public String getCharacterEncoding() {
230: return _request.getCharacterEncoding();
231: }
232:
233: /**
234: * Sets the character encoding to be used for forms and getReader.
235: */
236: public void setCharacterEncoding(String encoding)
237: throws UnsupportedEncodingException {
238: _request.setCharacterEncoding(encoding);
239: }
240:
241: /**
242: * Returns the content length of the data. This value may differ from
243: * the actual length of the data. For newer browsers, i.e.
244: * those supporting HTTP/1.1, can support "chunked" encoding which does
245: * not make the content length available.
246: *
247: * <p>The upshot is, rely on the input stream to end when the data
248: * completes.
249: */
250: public int getContentLength() {
251: return _request.getContentLength();
252: }
253:
254: /**
255: * Returns the request's mime-type.
256: */
257: public String getContentType() {
258: return _request.getContentType();
259: }
260:
261: /**
262: * Returns the request's preferred locale.
263: */
264: public Locale getLocale() {
265: return _request.getLocale();
266: }
267:
268: /**
269: * Returns an enumeration of all locales acceptable by the client.
270: */
271: public Enumeration getLocales() {
272: return _request.getLocales();
273: }
274:
275: /**
276: * Returns true if the connection is secure, e.g. it uses SSL.
277: */
278: public boolean isSecure() {
279: return _request.isSecure();
280: }
281:
282: /**
283: * Returns an attribute value.
284: *
285: * @param name the attribute name
286: * @return the attribute value
287: */
288: public Object getAttribute(String name) {
289: return _request.getAttribute(name);
290: }
291:
292: /**
293: * Sets an attribute value.
294: *
295: * @param name the attribute name
296: * @param o the attribute value
297: */
298: public void setAttribute(String name, Object o) {
299: _request.setAttribute(name, o);
300: }
301:
302: /**
303: * Enumerates all attribute names in the request.
304: */
305: public Enumeration getAttributeNames() {
306: return _request.getAttributeNames();
307: }
308:
309: /**
310: * Removes the given attribute.
311: *
312: * @param name the attribute name
313: */
314: public void removeAttribute(String name) {
315: _request.removeAttribute(name);
316: }
317:
318: /**
319: * Returns a request dispatcher for later inclusion or forwarding. This
320: * is the servlet API equivalent to SSI includes. <code>uri</code>
321: * is relative to the request URI. Absolute URIs are relative to
322: * the application prefix (<code>getContextPath()</code>).
323: *
324: * <p>If <code>getRequestURI()</code> is /myapp/dir/test.jsp and the
325: * <code>uri</code> is "inc.jsp", the resulting page is
326: * /myapp/dir/inc.jsp.
327:
328: * <code><pre>
329: * RequestDispatcher disp;
330: * disp = getRequestDispatcher("inc.jsp?a=b");
331: * disp.include(request, response);
332: * </pre></code>
333: *
334: * @param uri path relative to <code>getRequestURI()</code>
335: * (including query string) for the included file.
336: * @return RequestDispatcher for later inclusion or forwarding.
337: */
338: public RequestDispatcher getRequestDispatcher(String uri) {
339: return _request.getRequestDispatcher(uri);
340: }
341:
342: /**
343: * Returns the real path.
344: */
345: public String getRealPath(String uri) {
346: return _request.getRealPath(uri);
347: }
348: }
|