001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.wrapper;
017:
018: import org.acegisecurity.ui.AbstractProcessingFilter;
019: import org.acegisecurity.ui.savedrequest.Enumerator;
020: import org.acegisecurity.ui.savedrequest.FastHttpDateFormat;
021: import org.acegisecurity.ui.savedrequest.SavedRequest;
022:
023: import org.acegisecurity.util.PortResolver;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import java.text.SimpleDateFormat;
029:
030: import java.util.ArrayList;
031: import java.util.Enumeration;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Locale;
035: import java.util.Map;
036: import java.util.TimeZone;
037:
038: import javax.servlet.http.Cookie;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpSession;
041:
042: /**
043: * Provides request parameters, headers and cookies from either an original request or a saved request.<p>Note that
044: * not all request parameters in the original request are emulated by this wrapper. Nevertheless, the important data
045: * from the original request is emulated and this should prove adequate for most purposes (in particular standard HTTP
046: * GET and POST operations).</p>
047: * <p>Added into a request by {@link org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter}.</p>
048: *
049: * @author Andrey Grebnev
050: * @author Ben Alex
051: * @version $Id: SavedRequestAwareWrapper.java 1859 2007-05-24 23:20:40Z vishalpuri $
052: */
053: public class SavedRequestAwareWrapper extends
054: SecurityContextHolderAwareRequestWrapper {
055: //~ Static fields/initializers =====================================================================================
056:
057: protected static final Log logger = LogFactory
058: .getLog(SavedRequestAwareWrapper.class);
059: protected static final TimeZone GMT_ZONE = TimeZone
060: .getTimeZone("GMT");
061:
062: /** The default Locale if none are specified. */
063: protected static Locale defaultLocale = Locale.getDefault();
064:
065: //~ Instance fields ================================================================================================
066:
067: protected SavedRequest savedRequest = null;
068:
069: /**
070: * The set of SimpleDateFormat formats to use in getDateHeader(). Notice that because SimpleDateFormat is
071: * not thread-safe, we can't declare formats[] as a static variable.
072: */
073: protected SimpleDateFormat[] formats = new SimpleDateFormat[3];
074:
075: //~ Constructors ===================================================================================================
076:
077: public SavedRequestAwareWrapper(HttpServletRequest request,
078: PortResolver portResolver) {
079: super (request, portResolver);
080:
081: HttpSession session = request.getSession(false);
082:
083: if (session == null) {
084: if (logger.isDebugEnabled()) {
085: logger
086: .debug("Wrapper not replaced; no session available for SavedRequest extraction");
087: }
088:
089: return;
090: }
091:
092: SavedRequest saved = (SavedRequest) session
093: .getAttribute(AbstractProcessingFilter.ACEGI_SAVED_REQUEST_KEY);
094:
095: if ((saved != null)
096: && saved.doesRequestMatch(request, portResolver)) {
097: if (logger.isDebugEnabled()) {
098: logger.debug("Wrapper replaced; SavedRequest was: "
099: + saved);
100: }
101:
102: savedRequest = saved;
103: session
104: .removeAttribute(AbstractProcessingFilter.ACEGI_SAVED_REQUEST_KEY);
105:
106: formats[0] = new SimpleDateFormat(
107: "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
108: formats[1] = new SimpleDateFormat(
109: "EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US);
110: formats[2] = new SimpleDateFormat(
111: "EEE MMMM d HH:mm:ss yyyy", Locale.US);
112:
113: formats[0].setTimeZone(GMT_ZONE);
114: formats[1].setTimeZone(GMT_ZONE);
115: formats[2].setTimeZone(GMT_ZONE);
116: } else {
117: if (logger.isDebugEnabled()) {
118: logger.debug("Wrapper not replaced; SavedRequest was: "
119: + saved);
120: }
121: }
122: }
123:
124: //~ Methods ========================================================================================================
125:
126: /**
127: * The default behavior of this method is to return getCookies() on the wrapped request object.
128: *
129: * @return DOCUMENT ME!
130: */
131: public Cookie[] getCookies() {
132: if (savedRequest == null) {
133: return super .getCookies();
134: } else {
135: List cookies = savedRequest.getCookies();
136:
137: return (Cookie[]) cookies
138: .toArray(new Cookie[cookies.size()]);
139: }
140: }
141:
142: /**
143: * The default behavior of this method is to return getDateHeader(String name) on the wrapped request
144: * object.
145: *
146: * @param name DOCUMENT ME!
147: *
148: * @return DOCUMENT ME!
149: *
150: * @throws IllegalArgumentException DOCUMENT ME!
151: */
152: public long getDateHeader(String name) {
153: if (savedRequest == null) {
154: return super .getDateHeader(name);
155: } else {
156: String value = getHeader(name);
157:
158: if (value == null) {
159: return (-1L);
160: }
161:
162: // Attempt to convert the date header in a variety of formats
163: long result = FastHttpDateFormat.parseDate(value, formats);
164:
165: if (result != (-1L)) {
166: return result;
167: }
168:
169: throw new IllegalArgumentException(value);
170: }
171: }
172:
173: /**
174: * The default behavior of this method is to return getHeader(String name) on the wrapped request object.
175: *
176: * @param name DOCUMENT ME!
177: *
178: * @return DOCUMENT ME!
179: */
180: public String getHeader(String name) {
181: if (savedRequest == null) {
182: return super .getHeader(name);
183: } else {
184: String header = null;
185: Iterator iterator = savedRequest.getHeaderValues(name);
186:
187: while (iterator.hasNext()) {
188: header = (String) iterator.next();
189:
190: break;
191: }
192:
193: return header;
194: }
195: }
196:
197: /**
198: * The default behavior of this method is to return getHeaderNames() on the wrapped request object.
199: *
200: * @return DOCUMENT ME!
201: */
202: public Enumeration getHeaderNames() {
203: if (savedRequest == null) {
204: return super .getHeaderNames();
205: } else {
206: return new Enumerator(savedRequest.getHeaderNames());
207: }
208: }
209:
210: /**
211: * The default behavior of this method is to return getHeaders(String name) on the wrapped request object.
212: *
213: * @param name DOCUMENT ME!
214: *
215: * @return DOCUMENT ME!
216: */
217: public Enumeration getHeaders(String name) {
218: if (savedRequest == null) {
219: return super .getHeaders(name);
220: } else {
221: return new Enumerator(savedRequest.getHeaderValues(name));
222: }
223: }
224:
225: /**
226: * The default behavior of this method is to return getIntHeader(String name) on the wrapped request
227: * object.
228: *
229: * @param name DOCUMENT ME!
230: *
231: * @return DOCUMENT ME!
232: */
233: public int getIntHeader(String name) {
234: if (savedRequest == null) {
235: return super .getIntHeader(name);
236: } else {
237: String value = getHeader(name);
238:
239: if (value == null) {
240: return (-1);
241: } else {
242: return (Integer.parseInt(value));
243: }
244: }
245: }
246:
247: /**
248: * The default behavior of this method is to return getLocale() on the wrapped request object.
249: *
250: * @return DOCUMENT ME!
251: */
252: public Locale getLocale() {
253: if (savedRequest == null) {
254: return super .getLocale();
255: } else {
256: Locale locale = null;
257: Iterator iterator = savedRequest.getLocales();
258:
259: while (iterator.hasNext()) {
260: locale = (Locale) iterator.next();
261:
262: break;
263: }
264:
265: if (locale == null) {
266: return defaultLocale;
267: } else {
268: return locale;
269: }
270: }
271: }
272:
273: /**
274: * The default behavior of this method is to return getLocales() on the wrapped request object.
275: *
276: * @return DOCUMENT ME!
277: */
278: public Enumeration getLocales() {
279: if (savedRequest == null) {
280: return super .getLocales();
281: } else {
282: Iterator iterator = savedRequest.getLocales();
283:
284: if (iterator.hasNext()) {
285: return new Enumerator(iterator);
286: } else {
287: ArrayList results = new ArrayList();
288: results.add(defaultLocale);
289:
290: return new Enumerator(results.iterator());
291: }
292: }
293: }
294:
295: /**
296: * The default behavior of this method is to return getMethod() on the wrapped request object.
297: *
298: * @return DOCUMENT ME!
299: */
300: public String getMethod() {
301: if (savedRequest == null) {
302: return super .getMethod();
303: } else {
304: return savedRequest.getMethod();
305: }
306: }
307:
308: /**
309: * The default behavior of this method is to return getParameter(String name) on the wrapped request
310: * object.
311: *
312: * @param name DOCUMENT ME!
313: *
314: * @return DOCUMENT ME!
315: */
316: public String getParameter(String name) {
317: /*
318: if (savedRequest == null) {
319: return super.getParameter(name);
320: } else {
321: String value = null;
322: String[] values = savedRequest.getParameterValues(name);
323: if (values == null)
324: return null;
325: for (int i = 0; i < values.length; i++) {
326: value = values[i];
327: break;
328: }
329: return value;
330: }
331: */
332:
333: //we do not get value from super.getParameter because there is a bug in Jetty servlet-container
334: String value = null;
335: String[] values = null;
336:
337: if (savedRequest == null) {
338: values = super .getParameterValues(name);
339: } else {
340: values = savedRequest.getParameterValues(name);
341: }
342:
343: if (values == null) {
344: return null;
345: }
346:
347: for (int i = 0; i < values.length; i++) {
348: value = values[i];
349:
350: break;
351: }
352:
353: return value;
354: }
355:
356: /**
357: * The default behavior of this method is to return getParameterMap() on the wrapped request object.
358: *
359: * @return DOCUMENT ME!
360: */
361: public Map getParameterMap() {
362: if (savedRequest == null) {
363: return super .getParameterMap();
364: } else {
365: return savedRequest.getParameterMap();
366: }
367: }
368:
369: /**
370: * The default behavior of this method is to return getParameterNames() on the wrapped request object.
371: *
372: * @return DOCUMENT ME!
373: */
374: public Enumeration getParameterNames() {
375: if (savedRequest == null) {
376: return super .getParameterNames();
377: } else {
378: return new Enumerator(savedRequest.getParameterNames());
379: }
380: }
381:
382: /**
383: * The default behavior of this method is to return getParameterValues(String name) on the wrapped request
384: * object.
385: *
386: * @param name DOCUMENT ME!
387: *
388: * @return DOCUMENT ME!
389: */
390: public String[] getParameterValues(String name) {
391: if (savedRequest == null) {
392: return super.getParameterValues(name);
393: } else {
394: return savedRequest.getParameterValues(name);
395: }
396: }
397: }
|