2
* Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3
* Distributed under the terms of either:
4
* - the common development and distribution license (CDDL), v1.0; or
5
* - the GNU Lesser General Public License, v2.1 or later
9
import java.io.BufferedReader;
10
import java.io.IOException;
11
import java.io.InputStreamReader;
12
import java.io.UnsupportedEncodingException;
13
import java.text.DateFormat;
14
import java.text.SimpleDateFormat;
15
import java.util.Collections;
16
import java.util.Enumeration;
17
import java.util.HashMap;
18
import java.util.Hashtable;
19
import java.util.Locale;
21
import java.util.TimeZone;
22
import java.util.Vector;
24
import javax.servlet.ServletInputStream;
25
import javax.servlet.http.HttpServletRequest;
26
import javax.servlet.http.HttpServletRequestWrapper;
28
import winstone.Launcher;
29
import winstone.Logger;
30
import winstone.WinstoneException;
31
import winstone.WinstoneInputStream;
32
import winstone.WinstoneRequest;
36
* This is used by the ACL filter to allow a retry by using a key lookup
37
* on old request. It's only used when retrying an old request that was blocked
40
* @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
41
* @version $Id: RetryRequestWrapper.java,v 1.3 2007/02/26 00:28:05 rickknowles Exp $
43
public class RetryRequestWrapper extends HttpServletRequestWrapper {
44
protected static final DateFormat headerDF = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
47
headerDF.setTimeZone(TimeZone.getTimeZone("GMT"));
50
private final static String METHOD_HEAD = "GET";
51
private final static String METHOD_GET = "GET";
52
private final static String METHOD_POST = "POST";
53
private final static String POST_PARAMETERS = "application/x-www-form-urlencoded";
55
private RetryRequestParams oldRequest;
57
// PARAMETER/BODY RELATED FUNCTIONS
58
private String encoding;
59
private Map parsedParams;
60
private ServletInputStream inData;
63
* Constructor - this populates the wrapper from the object in session
65
public RetryRequestWrapper(HttpServletRequest request, RetryRequestParams oldRequest)
68
this.oldRequest = oldRequest;
69
this.encoding = this.oldRequest.getEncoding();
72
private boolean hasBeenForwarded() {
73
return (super.getAttribute("javax.servlet.forward.request_uri") != null);
76
public String getScheme() {
77
if (hasBeenForwarded()) {
78
return super.getScheme();
80
return this.oldRequest.getScheme();
84
public String getMethod() {
85
if (hasBeenForwarded()) {
86
return super.getMethod();
88
return this.oldRequest.getMethod();
92
public String getContextPath() {
93
if (hasBeenForwarded()) {
94
return super.getContextPath();
96
return this.oldRequest.getContextPath();
100
public String getServletPath() {
101
if (hasBeenForwarded()) {
102
return super.getServletPath();
104
return this.oldRequest.getServletPath();
108
public String getPathInfo() {
109
if (hasBeenForwarded()) {
110
return super.getPathInfo();
112
return this.oldRequest.getPathInfo();
116
public String getQueryString() {
117
if (hasBeenForwarded()) {
118
return super.getQueryString();
120
return this.oldRequest.getQueryString();
124
public String getRequestURI() {
125
if (hasBeenForwarded()) {
126
return super.getRequestURI();
128
String contextPath = this.oldRequest.getContextPath();
129
String servletPath = this.oldRequest.getServletPath();
130
String pathInfo = this.oldRequest.getPathInfo();
131
String queryString = this.oldRequest.getQueryString();
132
return contextPath + servletPath + ((pathInfo == null) ? "" : pathInfo)
133
+ ((queryString == null) ? "" : ("?" + queryString));
137
public String getCharacterEncoding() {
138
if (hasBeenForwarded()) {
139
return super.getCharacterEncoding();
141
return this.oldRequest.getEncoding();
145
public void setCharacterEncoding(String encoding) throws UnsupportedEncodingException {
146
if (hasBeenForwarded()) {
147
super.setCharacterEncoding(encoding);
149
this.encoding = encoding;
153
public int getContentLength() {
154
if (hasBeenForwarded()) {
155
return super.getContentLength();
157
return this.oldRequest.getContentLength();
161
public String getContentType() {
162
if (hasBeenForwarded()) {
163
return super.getContentType();
165
return this.oldRequest.getContentType();
169
public Locale getLocale() {
170
if (hasBeenForwarded()) {
171
return super.getLocale();
173
return this.oldRequest.getLocale();
177
public Enumeration getLocales() {
178
if (hasBeenForwarded()) {
179
return super.getLocales();
181
return this.oldRequest.getLocales().elements();
185
// -------------------------------------------------------------------
186
// HEADER RELATED FUNCTIONS
187
public long getDateHeader(String name) {
188
if (hasBeenForwarded()) {
189
return super.getDateHeader(name);
191
String dateHeader = getHeader(name);
192
if (dateHeader == null) {
196
synchronized (headerDF) {
197
return headerDF.parse(dateHeader).getTime();
199
} catch (java.text.ParseException err) {
200
throw new IllegalArgumentException("Illegal date format: " + dateHeader);
206
public int getIntHeader(String name) {
207
if (hasBeenForwarded()) {
208
return super.getIntHeader(name);
210
String header = getHeader(name);
211
return header == null ? -1 : Integer.parseInt(header);
215
public String getHeader(String name) {
216
if (hasBeenForwarded()) {
217
return super.getHeader(name);
219
Enumeration e = getHeaders(name);
220
return (e != null) && e.hasMoreElements() ? (String) e.nextElement() : null;
224
public Enumeration getHeaderNames() {
225
if (hasBeenForwarded()) {
226
return super.getHeaderNames();
228
return Collections.enumeration(this.oldRequest.getHeaders().keySet());
232
public Enumeration getHeaders(String name) {
233
if (hasBeenForwarded()) {
234
return super.getHeaders(name);
236
Vector result = (Vector) this.oldRequest.getHeaders().get(name.toLowerCase());
237
return result == null ? null : result.elements();
241
public String getParameter(String name) {
242
if (hasBeenForwarded()) {
243
return super.getParameter(name);
245
parseRequestParameters();
246
Object param = this.parsedParams.get(name);
249
} else if (param instanceof String) {
250
return (String) param;
251
} else if (param instanceof String[]) {
252
return ((String[]) param)[0];
254
return param.toString();
259
public Enumeration getParameterNames() {
260
if (hasBeenForwarded()) {
261
return super.getParameterNames();
263
parseRequestParameters();
264
return Collections.enumeration(this.parsedParams.keySet());
268
public String[] getParameterValues(String name) {
269
if (hasBeenForwarded()) {
270
return super.getParameterValues(name);
272
parseRequestParameters();
273
Object param = this.parsedParams.get(name);
276
} else if (param instanceof String) {
277
return new String[] {(String) param};
278
} else if (param instanceof String[]) {
279
return (String[]) param;
281
throw new WinstoneException(Launcher.RESOURCES.getString(
282
"WinstoneRequest.UnknownParameterType", name + " - "
283
+ param.getClass()));
288
public Map getParameterMap() {
289
if (hasBeenForwarded()) {
290
return super.getParameterMap();
292
Hashtable paramMap = new Hashtable();
293
for (Enumeration names = this.getParameterNames(); names.hasMoreElements();) {
294
String name = (String) names.nextElement();
295
paramMap.put(name, getParameterValues(name));
301
public BufferedReader getReader() throws IOException {
302
if (hasBeenForwarded()) {
303
return super.getReader();
304
} else if (getCharacterEncoding() != null) {
305
return new BufferedReader(new InputStreamReader(getInputStream(), this.encoding));
307
return new BufferedReader(new InputStreamReader(getInputStream()));
311
public ServletInputStream getInputStream() throws IOException {
312
if (hasBeenForwarded()) {
313
return super.getInputStream();
314
} else if (this.parsedParams != null) {
315
Logger.log(Logger.WARNING, Launcher.RESOURCES, "WinstoneRequest.BothMethods");
318
if (this.inData == null) {
319
this.inData = new WinstoneInputStream(this.oldRequest.getBodyContent());
325
// -------------------------------------------------------------------
328
* This takes the parameters in the body of the request and puts them into
329
* the parameters map.
331
private void parseRequestParameters() {
332
if (inData != null) {
333
Logger.log(Logger.WARNING, Launcher.RESOURCES, "WinstoneRequest.BothMethods");
336
if (this.parsedParams == null) {
337
String contentType = this.oldRequest.getContentType();
338
String queryString = this.oldRequest.getQueryString();
339
String method = this.oldRequest.getMethod();
340
Map workingParameters = new HashMap();
342
// Parse query string from request
343
if ((method.equals(METHOD_GET) || method.equals(METHOD_HEAD) ||
344
method.equals(METHOD_POST)) && (queryString != null)) {
345
WinstoneRequest.extractParameters(queryString, this.encoding, workingParameters, false);
348
if (method.equals(METHOD_POST) && (contentType != null)
349
&& (contentType.equals(POST_PARAMETERS) || contentType.startsWith(POST_PARAMETERS + ";"))) {
351
String paramLine = (this.encoding == null ? new String(this.oldRequest.getBodyContent())
352
: new String(this.oldRequest.getBodyContent(), this.encoding));
353
WinstoneRequest.extractParameters(paramLine.trim(), this.encoding, workingParameters, false);
356
this.parsedParams = workingParameters;
357
} catch (UnsupportedEncodingException err) {
358
Logger.log(Logger.ERROR, Launcher.RESOURCES, "WinstoneRequest.ErrorBodyParameters", err);
359
this.parsedParams = null;