~james-page/ubuntu/oneiric/jenkins-winstone/827651

« back to all changes in this revision

Viewing changes to src/java/winstone/auth/RetryRequestWrapper.java

  • Committer: Bazaar Package Importer
  • Author(s): James Page
  • Date: 2011-06-29 12:16:17 UTC
  • Revision ID: james.westby@ubuntu.com-20110629121617-vd3ha6lp4nqvxkbr
Tags: upstream-0.9.10-jenkins-25+dfsg
ImportĀ upstreamĀ versionĀ 0.9.10-jenkins-25+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
6
 */
 
7
package winstone.auth;
 
8
 
 
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;
 
20
import java.util.Map;
 
21
import java.util.TimeZone;
 
22
import java.util.Vector;
 
23
 
 
24
import javax.servlet.ServletInputStream;
 
25
import javax.servlet.http.HttpServletRequest;
 
26
import javax.servlet.http.HttpServletRequestWrapper;
 
27
 
 
28
import winstone.Launcher;
 
29
import winstone.Logger;
 
30
import winstone.WinstoneException;
 
31
import winstone.WinstoneInputStream;
 
32
import winstone.WinstoneRequest;
 
33
 
 
34
 
 
35
/**
 
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
 
38
 * by the ACL filter.
 
39
 *
 
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 $
 
42
 */
 
43
public class RetryRequestWrapper extends HttpServletRequestWrapper {
 
44
    protected static final DateFormat headerDF = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
 
45
 
 
46
    static {
 
47
        headerDF.setTimeZone(TimeZone.getTimeZone("GMT"));
 
48
    }
 
49
 
 
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";
 
54
 
 
55
    private RetryRequestParams oldRequest;
 
56
 
 
57
    // PARAMETER/BODY RELATED FUNCTIONS
 
58
    private String encoding;
 
59
    private Map parsedParams;
 
60
    private ServletInputStream inData;
 
61
 
 
62
    /**
 
63
     * Constructor - this populates the wrapper from the object in session
 
64
     */
 
65
    public RetryRequestWrapper(HttpServletRequest request, RetryRequestParams oldRequest)
 
66
                                 throws IOException {
 
67
        super(request);
 
68
        this.oldRequest = oldRequest;
 
69
        this.encoding = this.oldRequest.getEncoding();
 
70
    }
 
71
 
 
72
    private boolean hasBeenForwarded() {
 
73
        return (super.getAttribute("javax.servlet.forward.request_uri") != null);
 
74
    }
 
75
    
 
76
    public String getScheme() {
 
77
        if (hasBeenForwarded()) {
 
78
            return super.getScheme();
 
79
        } else {
 
80
            return this.oldRequest.getScheme();
 
81
        }
 
82
    }
 
83
 
 
84
    public String getMethod() {
 
85
        if (hasBeenForwarded()) {
 
86
            return super.getMethod();
 
87
        } else {
 
88
            return this.oldRequest.getMethod();
 
89
        }
 
90
    }
 
91
 
 
92
    public String getContextPath() {
 
93
        if (hasBeenForwarded()) {
 
94
            return super.getContextPath();
 
95
        } else {
 
96
            return this.oldRequest.getContextPath();
 
97
        }
 
98
    }
 
99
 
 
100
    public String getServletPath() {
 
101
        if (hasBeenForwarded()) {
 
102
            return super.getServletPath();
 
103
        } else {
 
104
            return this.oldRequest.getServletPath();
 
105
        }
 
106
    }
 
107
 
 
108
    public String getPathInfo() {
 
109
        if (hasBeenForwarded()) {
 
110
            return super.getPathInfo();
 
111
        } else {
 
112
            return this.oldRequest.getPathInfo();
 
113
        }
 
114
    }
 
115
 
 
116
    public String getQueryString() {
 
117
        if (hasBeenForwarded()) {
 
118
            return super.getQueryString();
 
119
        } else {
 
120
            return this.oldRequest.getQueryString();
 
121
        }
 
122
    }
 
123
 
 
124
    public String getRequestURI() {
 
125
        if (hasBeenForwarded()) {
 
126
            return super.getRequestURI();
 
127
        } else {
 
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));
 
134
        }
 
135
    }
 
136
 
 
137
    public String getCharacterEncoding() {
 
138
        if (hasBeenForwarded()) {
 
139
            return super.getCharacterEncoding();
 
140
        } else {
 
141
            return this.oldRequest.getEncoding();
 
142
        }
 
143
    }
 
144
 
 
145
    public void setCharacterEncoding(String encoding) throws UnsupportedEncodingException {
 
146
        if (hasBeenForwarded()) {
 
147
            super.setCharacterEncoding(encoding);
 
148
        } else {
 
149
            this.encoding = encoding;
 
150
        }
 
151
    }
 
152
 
 
153
    public int getContentLength() {
 
154
        if (hasBeenForwarded()) {
 
155
            return super.getContentLength();
 
156
        } else {
 
157
            return this.oldRequest.getContentLength();
 
158
        }
 
159
    }
 
160
 
 
161
    public String getContentType() {
 
162
        if (hasBeenForwarded()) {
 
163
            return super.getContentType();
 
164
        } else {
 
165
            return this.oldRequest.getContentType();
 
166
        }
 
167
    }
 
168
 
 
169
    public Locale getLocale() {
 
170
        if (hasBeenForwarded()) {
 
171
            return super.getLocale();
 
172
        } else {
 
173
            return this.oldRequest.getLocale();
 
174
        }
 
175
    }
 
176
 
 
177
    public Enumeration getLocales() {
 
178
        if (hasBeenForwarded()) {
 
179
            return super.getLocales();
 
180
        } else {
 
181
            return this.oldRequest.getLocales().elements();
 
182
        }
 
183
    }
 
184
 
 
185
    // -------------------------------------------------------------------
 
186
    // HEADER RELATED FUNCTIONS
 
187
    public long getDateHeader(String name) {
 
188
        if (hasBeenForwarded()) {
 
189
            return super.getDateHeader(name);
 
190
        } else {
 
191
            String dateHeader = getHeader(name);
 
192
            if (dateHeader == null) {
 
193
                return -1;
 
194
            } else {
 
195
                try {
 
196
                    synchronized (headerDF) {
 
197
                        return headerDF.parse(dateHeader).getTime();
 
198
                    }
 
199
                } catch (java.text.ParseException err) {
 
200
                    throw new IllegalArgumentException("Illegal date format: " + dateHeader);
 
201
                }
 
202
            }
 
203
        }
 
204
    }
 
205
    
 
206
    public int getIntHeader(String name) {
 
207
        if (hasBeenForwarded()) {
 
208
            return super.getIntHeader(name);
 
209
        } else {
 
210
            String header = getHeader(name);
 
211
            return header == null ? -1 : Integer.parseInt(header);
 
212
        }
 
213
    }
 
214
 
 
215
    public String getHeader(String name) {
 
216
        if (hasBeenForwarded()) {
 
217
            return super.getHeader(name);
 
218
        } else {
 
219
            Enumeration e = getHeaders(name);
 
220
            return (e != null) && e.hasMoreElements() ? (String) e.nextElement() : null;
 
221
        }
 
222
    }
 
223
 
 
224
    public Enumeration getHeaderNames() {
 
225
        if (hasBeenForwarded()) {
 
226
            return super.getHeaderNames();
 
227
        } else {
 
228
            return Collections.enumeration(this.oldRequest.getHeaders().keySet());
 
229
        }
 
230
    }
 
231
 
 
232
    public Enumeration getHeaders(String name) {
 
233
        if (hasBeenForwarded()) {
 
234
            return super.getHeaders(name);
 
235
        } else {
 
236
            Vector result = (Vector) this.oldRequest.getHeaders().get(name.toLowerCase()); 
 
237
            return result == null ? null : result.elements();
 
238
        }
 
239
    }
 
240
 
 
241
    public String getParameter(String name) {
 
242
        if (hasBeenForwarded()) {
 
243
            return super.getParameter(name);
 
244
        } else {
 
245
            parseRequestParameters();
 
246
            Object param = this.parsedParams.get(name);
 
247
            if (param == null) {
 
248
                return null;
 
249
            } else if (param instanceof String) {
 
250
                return (String) param;
 
251
            } else if (param instanceof String[]) {
 
252
                return ((String[]) param)[0];
 
253
            } else {
 
254
                return param.toString();
 
255
            }
 
256
        }
 
257
    }
 
258
 
 
259
    public Enumeration getParameterNames() {
 
260
        if (hasBeenForwarded()) {
 
261
            return super.getParameterNames();
 
262
        } else {
 
263
            parseRequestParameters();
 
264
            return Collections.enumeration(this.parsedParams.keySet());
 
265
        }
 
266
    }
 
267
 
 
268
    public String[] getParameterValues(String name) {
 
269
        if (hasBeenForwarded()) {
 
270
            return super.getParameterValues(name);
 
271
        } else {
 
272
            parseRequestParameters();
 
273
            Object param = this.parsedParams.get(name);
 
274
            if (param == null) {
 
275
                return null;
 
276
            } else if (param instanceof String) {
 
277
                return new String[] {(String) param};
 
278
            } else if (param instanceof String[]) {
 
279
                return (String[]) param;
 
280
            } else {
 
281
                throw new WinstoneException(Launcher.RESOURCES.getString(
 
282
                        "WinstoneRequest.UnknownParameterType", name + " - "
 
283
                                + param.getClass()));
 
284
            }
 
285
        }
 
286
    }
 
287
 
 
288
    public Map getParameterMap() {
 
289
        if (hasBeenForwarded()) {
 
290
            return super.getParameterMap();
 
291
        } else {
 
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));
 
296
            }
 
297
            return paramMap;
 
298
        }
 
299
    }
 
300
 
 
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));
 
306
        } else {
 
307
            return new BufferedReader(new InputStreamReader(getInputStream()));
 
308
        }
 
309
    }
 
310
 
 
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");
 
316
        }
 
317
 
 
318
        if (this.inData == null) {
 
319
            this.inData = new WinstoneInputStream(this.oldRequest.getBodyContent());
 
320
        }
 
321
 
 
322
        return this.inData;
 
323
    }
 
324
 
 
325
    // -------------------------------------------------------------------
 
326
 
 
327
    /**
 
328
     * This takes the parameters in the body of the request and puts them into
 
329
     * the parameters map.
 
330
     */
 
331
    private void parseRequestParameters() {
 
332
        if (inData != null) {
 
333
            Logger.log(Logger.WARNING, Launcher.RESOURCES, "WinstoneRequest.BothMethods");
 
334
        }
 
335
 
 
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();
 
341
            try {
 
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);
 
346
                }
 
347
                
 
348
                if (method.equals(METHOD_POST) && (contentType != null)
 
349
                        && (contentType.equals(POST_PARAMETERS) || contentType.startsWith(POST_PARAMETERS + ";"))) {
 
350
                    // Parse params
 
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);
 
354
                } 
 
355
                
 
356
                this.parsedParams = workingParameters;
 
357
            } catch (UnsupportedEncodingException err) {
 
358
                Logger.log(Logger.ERROR, Launcher.RESOURCES, "WinstoneRequest.ErrorBodyParameters", err);
 
359
                this.parsedParams = null;
 
360
            }
 
361
        }
 
362
    }
 
363
}