| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| SavedRequestAwareWrapper |
|
| 4.357142857142857;4.357 |
| 1 | /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited |
|
| 2 | * |
|
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 4 | * you may not use this file except in compliance with the License. |
|
| 5 | * You may obtain a copy of the License at |
|
| 6 | * |
|
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 8 | * |
|
| 9 | * Unless required by applicable law or agreed to in writing, software |
|
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 12 | * See the License for the specific language governing permissions and |
|
| 13 | * limitations under the License. |
|
| 14 | */ |
|
| 15 | ||
| 16 | package org.acegisecurity.wrapper; |
|
| 17 | ||
| 18 | import org.acegisecurity.ui.AbstractProcessingFilter; |
|
| 19 | import org.acegisecurity.ui.savedrequest.Enumerator; |
|
| 20 | import org.acegisecurity.ui.savedrequest.FastHttpDateFormat; |
|
| 21 | import org.acegisecurity.ui.savedrequest.SavedRequest; |
|
| 22 | ||
| 23 | import org.acegisecurity.util.PortResolver; |
|
| 24 | ||
| 25 | import org.apache.commons.logging.Log; |
|
| 26 | import org.apache.commons.logging.LogFactory; |
|
| 27 | ||
| 28 | import java.text.SimpleDateFormat; |
|
| 29 | ||
| 30 | import java.util.ArrayList; |
|
| 31 | import java.util.Enumeration; |
|
| 32 | import java.util.Iterator; |
|
| 33 | import java.util.List; |
|
| 34 | import java.util.Locale; |
|
| 35 | import java.util.Map; |
|
| 36 | import java.util.TimeZone; |
|
| 37 | ||
| 38 | import javax.servlet.http.Cookie; |
|
| 39 | import javax.servlet.http.HttpServletRequest; |
|
| 40 | import javax.servlet.http.HttpSession; |
|
| 41 | ||
| 42 | ||
| 43 | /** |
|
| 44 | * Provides request parameters, headers and cookies from either an original request or a saved request.<p>Note that |
|
| 45 | * not all request parameters in the original request are emulated by this wrapper. Nevertheless, the important data |
|
| 46 | * from the original request is emulated and this should prove adequate for most purposes (in particular standard HTTP |
|
| 47 | * GET and POST operations).</p> |
|
| 48 | * <p>Added into a request by {@link org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter}.</p> |
|
| 49 | * |
|
| 50 | * @author Andrey Grebnev |
|
| 51 | * @author Ben Alex |
|
| 52 | * @version $Id: SavedRequestAwareWrapper.java 1859 2007-05-24 23:20:40Z vishalpuri $ |
|
| 53 | */ |
|
| 54 | public class SavedRequestAwareWrapper extends SecurityContextHolderAwareRequestWrapper { |
|
| 55 | //~ Static fields/initializers ===================================================================================== |
|
| 56 | ||
| 57 | 2 | protected static final Log logger = LogFactory.getLog(SavedRequestAwareWrapper.class); |
| 58 | 1 | protected static final TimeZone GMT_ZONE = TimeZone.getTimeZone("GMT"); |
| 59 | ||
| 60 | /** The default Locale if none are specified. */ |
|
| 61 | 1 | protected static Locale defaultLocale = Locale.getDefault(); |
| 62 | ||
| 63 | //~ Instance fields ================================================================================================ |
|
| 64 | ||
| 65 | 2 | protected SavedRequest savedRequest = null; |
| 66 | ||
| 67 | /** |
|
| 68 | * The set of SimpleDateFormat formats to use in getDateHeader(). Notice that because SimpleDateFormat is |
|
| 69 | * not thread-safe, we can't declare formats[] as a static variable. |
|
| 70 | */ |
|
| 71 | 2 | protected SimpleDateFormat[] formats = new SimpleDateFormat[3]; |
| 72 | ||
| 73 | //~ Constructors =================================================================================================== |
|
| 74 | ||
| 75 | public SavedRequestAwareWrapper(HttpServletRequest request, PortResolver portResolver) { |
|
| 76 | 2 | super(request,portResolver); |
| 77 | ||
| 78 | 2 | HttpSession session = request.getSession(false); |
| 79 | ||
| 80 | 2 | if (session == null) { |
| 81 | 2 | if (logger.isDebugEnabled()) { |
| 82 | 0 | logger.debug("Wrapper not replaced; no session available for SavedRequest extraction"); |
| 83 | } |
|
| 84 | ||
| 85 | 2 | return; |
| 86 | } |
|
| 87 | ||
| 88 | 0 | SavedRequest saved = (SavedRequest) session.getAttribute(AbstractProcessingFilter.ACEGI_SAVED_REQUEST_KEY); |
| 89 | ||
| 90 | 0 | if ((saved != null) && saved.doesRequestMatch(request, portResolver)) { |
| 91 | 0 | if (logger.isDebugEnabled()) { |
| 92 | 0 | logger.debug("Wrapper replaced; SavedRequest was: " + saved); |
| 93 | } |
|
| 94 | ||
| 95 | 0 | savedRequest = saved; |
| 96 | 0 | session.removeAttribute(AbstractProcessingFilter.ACEGI_SAVED_REQUEST_KEY); |
| 97 | ||
| 98 | 0 | formats[0] = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); |
| 99 | 0 | formats[1] = new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US); |
| 100 | 0 | formats[2] = new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy", Locale.US); |
| 101 | ||
| 102 | 0 | formats[0].setTimeZone(GMT_ZONE); |
| 103 | 0 | formats[1].setTimeZone(GMT_ZONE); |
| 104 | 0 | formats[2].setTimeZone(GMT_ZONE); |
| 105 | } else { |
|
| 106 | 0 | if (logger.isDebugEnabled()) { |
| 107 | 0 | logger.debug("Wrapper not replaced; SavedRequest was: " + saved); |
| 108 | } |
|
| 109 | } |
|
| 110 | 0 | } |
| 111 | ||
| 112 | //~ Methods ======================================================================================================== |
|
| 113 | ||
| 114 | /** |
|
| 115 | * The default behavior of this method is to return getCookies() on the wrapped request object. |
|
| 116 | * |
|
| 117 | * @return DOCUMENT ME! |
|
| 118 | */ |
|
| 119 | public Cookie[] getCookies() { |
|
| 120 | 0 | if (savedRequest == null) { |
| 121 | 0 | return super.getCookies(); |
| 122 | } else { |
|
| 123 | 0 | List cookies = savedRequest.getCookies(); |
| 124 | ||
| 125 | 0 | return (Cookie[]) cookies.toArray(new Cookie[cookies.size()]); |
| 126 | } |
|
| 127 | } |
|
| 128 | ||
| 129 | /** |
|
| 130 | * The default behavior of this method is to return getDateHeader(String name) on the wrapped request |
|
| 131 | * object. |
|
| 132 | * |
|
| 133 | * @param name DOCUMENT ME! |
|
| 134 | * |
|
| 135 | * @return DOCUMENT ME! |
|
| 136 | * |
|
| 137 | * @throws IllegalArgumentException DOCUMENT ME! |
|
| 138 | */ |
|
| 139 | public long getDateHeader(String name) { |
|
| 140 | 0 | if (savedRequest == null) { |
| 141 | 0 | return super.getDateHeader(name); |
| 142 | } else { |
|
| 143 | 0 | String value = getHeader(name); |
| 144 | ||
| 145 | 0 | if (value == null) { |
| 146 | 0 | return (-1L); |
| 147 | } |
|
| 148 | ||
| 149 | // Attempt to convert the date header in a variety of formats |
|
| 150 | 0 | long result = FastHttpDateFormat.parseDate(value, formats); |
| 151 | ||
| 152 | 0 | if (result != (-1L)) { |
| 153 | 0 | return result; |
| 154 | } |
|
| 155 | ||
| 156 | 0 | throw new IllegalArgumentException(value); |
| 157 | } |
|
| 158 | } |
|
| 159 | ||
| 160 | /** |
|
| 161 | * The default behavior of this method is to return getHeader(String name) on the wrapped request object. |
|
| 162 | * |
|
| 163 | * @param name DOCUMENT ME! |
|
| 164 | * |
|
| 165 | * @return DOCUMENT ME! |
|
| 166 | */ |
|
| 167 | public String getHeader(String name) { |
|
| 168 | 0 | if (savedRequest == null) { |
| 169 | 0 | return super.getHeader(name); |
| 170 | } else { |
|
| 171 | 0 | String header = null; |
| 172 | 0 | Iterator iterator = savedRequest.getHeaderValues(name); |
| 173 | ||
| 174 | 0 | while (iterator.hasNext()) { |
| 175 | 0 | header = (String) iterator.next(); |
| 176 | ||
| 177 | break; |
|
| 178 | } |
|
| 179 | ||
| 180 | 0 | return header; |
| 181 | } |
|
| 182 | } |
|
| 183 | ||
| 184 | /** |
|
| 185 | * The default behavior of this method is to return getHeaderNames() on the wrapped request object. |
|
| 186 | * |
|
| 187 | * @return DOCUMENT ME! |
|
| 188 | */ |
|
| 189 | public Enumeration getHeaderNames() { |
|
| 190 | 0 | if (savedRequest == null) { |
| 191 | 0 | return super.getHeaderNames(); |
| 192 | } else { |
|
| 193 | 0 | return new Enumerator(savedRequest.getHeaderNames()); |
| 194 | } |
|
| 195 | } |
|
| 196 | ||
| 197 | /** |
|
| 198 | * The default behavior of this method is to return getHeaders(String name) on the wrapped request object. |
|
| 199 | * |
|
| 200 | * @param name DOCUMENT ME! |
|
| 201 | * |
|
| 202 | * @return DOCUMENT ME! |
|
| 203 | */ |
|
| 204 | public Enumeration getHeaders(String name) { |
|
| 205 | 0 | if (savedRequest == null) { |
| 206 | 0 | return super.getHeaders(name); |
| 207 | } else { |
|
| 208 | 0 | return new Enumerator(savedRequest.getHeaderValues(name)); |
| 209 | } |
|
| 210 | } |
|
| 211 | ||
| 212 | /** |
|
| 213 | * The default behavior of this method is to return getIntHeader(String name) on the wrapped request |
|
| 214 | * object. |
|
| 215 | * |
|
| 216 | * @param name DOCUMENT ME! |
|
| 217 | * |
|
| 218 | * @return DOCUMENT ME! |
|
| 219 | */ |
|
| 220 | public int getIntHeader(String name) { |
|
| 221 | 0 | if (savedRequest == null) { |
| 222 | 0 | return super.getIntHeader(name); |
| 223 | } else { |
|
| 224 | 0 | String value = getHeader(name); |
| 225 | ||
| 226 | 0 | if (value == null) { |
| 227 | 0 | return (-1); |
| 228 | } else { |
|
| 229 | 0 | return (Integer.parseInt(value)); |
| 230 | } |
|
| 231 | } |
|
| 232 | } |
|
| 233 | ||
| 234 | /** |
|
| 235 | * The default behavior of this method is to return getLocale() on the wrapped request object. |
|
| 236 | * |
|
| 237 | * @return DOCUMENT ME! |
|
| 238 | */ |
|
| 239 | public Locale getLocale() { |
|
| 240 | 0 | if (savedRequest == null) { |
| 241 | 0 | return super.getLocale(); |
| 242 | } else { |
|
| 243 | 0 | Locale locale = null; |
| 244 | 0 | Iterator iterator = savedRequest.getLocales(); |
| 245 | ||
| 246 | 0 | while (iterator.hasNext()) { |
| 247 | 0 | locale = (Locale) iterator.next(); |
| 248 | ||
| 249 | break; |
|
| 250 | } |
|
| 251 | ||
| 252 | 0 | if (locale == null) { |
| 253 | 0 | return defaultLocale; |
| 254 | } else { |
|
| 255 | 0 | return locale; |
| 256 | } |
|
| 257 | } |
|
| 258 | } |
|
| 259 | ||
| 260 | /** |
|
| 261 | * The default behavior of this method is to return getLocales() on the wrapped request object. |
|
| 262 | * |
|
| 263 | * @return DOCUMENT ME! |
|
| 264 | */ |
|
| 265 | public Enumeration getLocales() { |
|
| 266 | 0 | if (savedRequest == null) { |
| 267 | 0 | return super.getLocales(); |
| 268 | } else { |
|
| 269 | 0 | Iterator iterator = savedRequest.getLocales(); |
| 270 | ||
| 271 | 0 | if (iterator.hasNext()) { |
| 272 | 0 | return new Enumerator(iterator); |
| 273 | } else { |
|
| 274 | 0 | ArrayList results = new ArrayList(); |
| 275 | 0 | results.add(defaultLocale); |
| 276 | ||
| 277 | 0 | return new Enumerator(results.iterator()); |
| 278 | } |
|
| 279 | } |
|
| 280 | } |
|
| 281 | ||
| 282 | /** |
|
| 283 | * The default behavior of this method is to return getMethod() on the wrapped request object. |
|
| 284 | * |
|
| 285 | * @return DOCUMENT ME! |
|
| 286 | */ |
|
| 287 | public String getMethod() { |
|
| 288 | 0 | if (savedRequest == null) { |
| 289 | 0 | return super.getMethod(); |
| 290 | } else { |
|
| 291 | 0 | return savedRequest.getMethod(); |
| 292 | } |
|
| 293 | } |
|
| 294 | ||
| 295 | /** |
|
| 296 | * The default behavior of this method is to return getParameter(String name) on the wrapped request |
|
| 297 | * object. |
|
| 298 | * |
|
| 299 | * @param name DOCUMENT ME! |
|
| 300 | * |
|
| 301 | * @return DOCUMENT ME! |
|
| 302 | */ |
|
| 303 | public String getParameter(String name) { |
|
| 304 | /* |
|
| 305 | if (savedRequest == null) { |
|
| 306 | return super.getParameter(name); |
|
| 307 | } else { |
|
| 308 | String value = null; |
|
| 309 | String[] values = savedRequest.getParameterValues(name); |
|
| 310 | if (values == null) |
|
| 311 | return null; |
|
| 312 | for (int i = 0; i < values.length; i++) { |
|
| 313 | value = values[i]; |
|
| 314 | break; |
|
| 315 | } |
|
| 316 | return value; |
|
| 317 | } |
|
| 318 | */ |
|
| 319 | ||
| 320 | //we do not get value from super.getParameter because there is a bug in Jetty servlet-container |
|
| 321 | 0 | String value = null; |
| 322 | 0 | String[] values = null; |
| 323 | ||
| 324 | 0 | if (savedRequest == null) { |
| 325 | 0 | values = super.getParameterValues(name); |
| 326 | } else { |
|
| 327 | 0 | values = savedRequest.getParameterValues(name); |
| 328 | } |
|
| 329 | ||
| 330 | 0 | if (values == null) { |
| 331 | 0 | return null; |
| 332 | } |
|
| 333 | ||
| 334 | 0 | for (int i = 0; i < values.length; i++) { |
| 335 | 0 | value = values[i]; |
| 336 | ||
| 337 | break; |
|
| 338 | } |
|
| 339 | ||
| 340 | 0 | return value; |
| 341 | } |
|
| 342 | ||
| 343 | /** |
|
| 344 | * The default behavior of this method is to return getParameterMap() on the wrapped request object. |
|
| 345 | * |
|
| 346 | * @return DOCUMENT ME! |
|
| 347 | */ |
|
| 348 | public Map getParameterMap() { |
|
| 349 | 0 | if (savedRequest == null) { |
| 350 | 0 | return super.getParameterMap(); |
| 351 | } else { |
|
| 352 | 0 | return savedRequest.getParameterMap(); |
| 353 | } |
|
| 354 | } |
|
| 355 | ||
| 356 | /** |
|
| 357 | * The default behavior of this method is to return getParameterNames() on the wrapped request object. |
|
| 358 | * |
|
| 359 | * @return DOCUMENT ME! |
|
| 360 | */ |
|
| 361 | public Enumeration getParameterNames() { |
|
| 362 | 0 | if (savedRequest == null) { |
| 363 | 0 | return super.getParameterNames(); |
| 364 | } else { |
|
| 365 | 0 | return new Enumerator(savedRequest.getParameterNames()); |
| 366 | } |
|
| 367 | } |
|
| 368 | ||
| 369 | /** |
|
| 370 | * The default behavior of this method is to return getParameterValues(String name) on the wrapped request |
|
| 371 | * object. |
|
| 372 | * |
|
| 373 | * @param name DOCUMENT ME! |
|
| 374 | * |
|
| 375 | * @return DOCUMENT ME! |
|
| 376 | */ |
|
| 377 | public String[] getParameterValues(String name) { |
|
| 378 | 0 | if (savedRequest == null) { |
| 379 | 0 | return super.getParameterValues(name); |
| 380 | } else { |
|
| 381 | 0 | return savedRequest.getParameterValues(name); |
| 382 | } |
|
| 383 | } |
|
| 384 | } |