001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.util;
018:
019: import javax.servlet.http.Cookie;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpSession;
022: import javax.servlet.http.HttpServletRequestWrapper;
023: import java.text.SimpleDateFormat;
024: import java.util.*;
025:
026: /**
027: * <p>This class provides request parameters, headers, cookies from original requrest or saved request.</p>
028: *
029: * @author Andrey Grebnev <a href="mailto:andrey.grebnev@blandware.com"><andrey.grebnev@blandware.com></a>
030: * @author <a href="mailto:claude.brisson@gmail.com">Claude Brisson</a>
031: */
032:
033: public @SuppressWarnings("deprecation")
034: class SavedRequestWrapper extends HttpServletRequestWrapper {
035:
036: /** the saved request. */
037: private SavedRequest savedRequest = null;
038:
039: /** timezone. */
040: private static final TimeZone GMT_ZONE = TimeZone
041: .getTimeZone("GMT");
042:
043: /**
044: * The default Locale if none are specified.
045: */
046: private static final Locale defaultLocale = Locale.getDefault();
047:
048: /**
049: * The set of SimpleDateFormat formats to use in getDateHeader().
050: *
051: * Notice that because SimpleDateFormat is not thread-safe, we can't
052: * declare formats[] as a static variable.
053: */
054: private SimpleDateFormat formats[] = new SimpleDateFormat[3];
055:
056: /**
057: * Constructor
058: * @param request to save
059: */
060: public SavedRequestWrapper(HttpServletRequest request,
061: SavedRequest saved) {
062: super (request);
063:
064: HttpSession session = request.getSession(false);
065: if (session == null)
066: return;
067:
068: String requestURI = saved.getRequestURI();
069: if (requestURI == null)
070: return;
071:
072: savedRequest = saved;
073:
074: formats[0] = new SimpleDateFormat(
075: "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
076: formats[1] = new SimpleDateFormat(
077: "EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US);
078: formats[2] = new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy",
079: Locale.US);
080:
081: formats[0].setTimeZone(GMT_ZONE);
082: formats[1].setTimeZone(GMT_ZONE);
083: formats[2].setTimeZone(GMT_ZONE);
084: }
085:
086: /**
087: * The default behavior of this method is to return getMethod()
088: * on the wrapped request object.
089: * @return the HTTP method
090: */
091: public String getMethod() {
092: return savedRequest.getMethod();
093: }
094:
095: /**
096: * The default behavior of this method is to return getHeader(String name)
097: * on the wrapped request object.
098: * @param name header name
099: * @return header value or null
100: */
101: public String getHeader(String name) {
102: String header = null;
103: Iterator iterator = savedRequest.getHeaderValues(name);
104: while (iterator.hasNext()) {
105: header = (String) iterator.next();
106: break;
107: }
108: return header;
109: }
110:
111: /**
112: * The default behavior of this method is to return getIntHeader(String name)
113: * on the wrapped request object.
114: * @param name integer header name
115: * @return integer header value or -1
116: */
117:
118: public int getIntHeader(String name) {
119: String value = getHeader(name);
120: if (value == null) {
121: return (-1);
122: } else {
123: return (Integer.parseInt(value));
124: }
125: }
126:
127: /**
128: * The default behavior of this method is to return getDateHeader(String name)
129: * on the wrapped request object.
130: * @param name date header name
131: * @return date header value or null
132: */
133: public long getDateHeader(String name) {
134: String value = getHeader(name);
135: if (value == null)
136: return (-1L);
137:
138: // Attempt to convert the date header in a variety of formats
139: long result = FastHttpDateFormat.parseDate(value, formats);
140: if (result != (-1L)) {
141: return result;
142: }
143: throw new IllegalArgumentException(value);
144: }
145:
146: /**
147: * The default behavior of this method is to return getHeaderNames()
148: * on the wrapped request object.
149: * @return an enumeration of header names
150: */
151:
152: public Enumeration getHeaderNames() {
153: return new Enumerator(savedRequest.getHeaderNames());
154: }
155:
156: /**
157: * The default behavior of this method is to return getHeaders(String name)
158: * on the wrapped request object.
159: * @param name multivalued header
160: * @return enumeration of values for this header
161: */
162: public Enumeration getHeaders(String name) {
163: return new Enumerator(savedRequest.getHeaderValues(name));
164: }
165:
166: /**
167: * The default behavior of this method is to return getCookies()
168: * on the wrapped request object.
169: * @return cookies array
170: */
171: public Cookie[] getCookies() {
172: List cookies = savedRequest.getCookies();
173: return (Cookie[]) cookies.toArray(new Cookie[cookies.size()]);
174: }
175:
176: /**
177: * The default behavior of this method is to return getParameterValues(String name)
178: * on the wrapped request object.
179: * @param name parameter name
180: * @value array of values
181: */
182: public String[] getParameterValues(String name) {
183: return savedRequest.getParameterValues(name);
184: }
185:
186: /**
187: * The default behavior of this method is to return getParameterNames()
188: * on the wrapped request object.
189: * @return enumeration of parameter names
190: */
191:
192: public Enumeration getParameterNames() {
193: return new Enumerator(savedRequest.getParameterNames());
194: }
195:
196: /**
197: * The default behavior of this method is to return getParameterMap()
198: * on the wrapped request object.
199: * @return parameter map
200: */
201: public Map getParameterMap() {
202: return savedRequest.getParameterMap();
203: }
204:
205: /**
206: * The default behavior of this method is to return getParameter(String name)
207: * on the wrapped request object.
208: * @param name parameter name
209: * @return parameter value
210: */
211:
212: public String getParameter(String name) {
213: String[] values = savedRequest.getParameterValues(name);
214: if (values == null) {
215: return null;
216: } else {
217: return values[0];
218: }
219: }
220:
221: /**
222: * The default behavior of this method is to return getLocales()
223: * on the wrapped request object.
224: * @return enumeration of locales
225: */
226:
227: public Enumeration getLocales() {
228: Iterator iterator = savedRequest.getLocales();
229: if (iterator.hasNext()) {
230: return new Enumerator(iterator);
231: } else {
232: ArrayList results = new ArrayList();
233: results.add(defaultLocale);
234: return new Enumerator(results.iterator());
235: }
236: }
237:
238: /**
239: * The default behavior of this method is to return getLocale()
240: * on the wrapped request object.
241: * @return locale
242: */
243:
244: public Locale getLocale() {
245: Locale locale = null;
246: Iterator iterator = savedRequest.getLocales();
247: while (iterator.hasNext()) {
248: locale = (Locale) iterator.next();
249: break;
250: }
251: if (locale == null) {
252: return defaultLocale;
253: } else {
254: return locale;
255: }
256: }
257: }
|