| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.acegisecurity.ui.savedrequest; |
| 17 |
|
|
| 18 |
|
import org.acegisecurity.util.PortResolver; |
| 19 |
|
import org.acegisecurity.util.UrlUtils; |
| 20 |
|
import org.apache.commons.logging.Log; |
| 21 |
|
import org.apache.commons.logging.LogFactory; |
| 22 |
|
import org.springframework.util.Assert; |
| 23 |
|
|
| 24 |
|
import javax.servlet.http.Cookie; |
| 25 |
|
import javax.servlet.http.HttpServletRequest; |
| 26 |
|
import java.util.ArrayList; |
| 27 |
|
import java.util.Enumeration; |
| 28 |
|
import java.util.Iterator; |
| 29 |
|
import java.util.List; |
| 30 |
|
import java.util.Locale; |
| 31 |
|
import java.util.Map; |
| 32 |
|
import java.util.TreeMap; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
public class SavedRequest implements java.io.Serializable { |
| 50 |
|
|
| 51 |
|
|
| 52 |
2 |
protected static final Log logger = LogFactory.getLog(SavedRequest.class); |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
5 |
private ArrayList cookies = new ArrayList(); |
| 57 |
5 |
private ArrayList locales = new ArrayList(); |
| 58 |
5 |
private Map headers = new TreeMap(String.CASE_INSENSITIVE_ORDER); |
| 59 |
5 |
private Map parameters = new TreeMap(String.CASE_INSENSITIVE_ORDER); |
| 60 |
|
private String contextPath; |
| 61 |
|
private String method; |
| 62 |
|
private String pathInfo; |
| 63 |
|
private String queryString; |
| 64 |
|
private String requestURI; |
| 65 |
|
private String requestURL; |
| 66 |
|
private String scheme; |
| 67 |
|
private String serverName; |
| 68 |
|
private String servletPath; |
| 69 |
|
private int serverPort; |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
5 |
public SavedRequest(HttpServletRequest request, PortResolver portResolver) { |
| 74 |
5 |
Assert.notNull(request, "Request required"); |
| 75 |
5 |
Assert.notNull(portResolver, "PortResolver required"); |
| 76 |
|
|
| 77 |
|
|
| 78 |
5 |
Cookie[] cookies = request.getCookies(); |
| 79 |
|
|
| 80 |
5 |
if (cookies != null) { |
| 81 |
0 |
for (int i = 0; i < cookies.length; i++) { |
| 82 |
0 |
this.addCookie(cookies[i]); |
| 83 |
|
} |
| 84 |
|
} |
| 85 |
|
|
| 86 |
|
|
| 87 |
5 |
Enumeration names = request.getHeaderNames(); |
| 88 |
|
|
| 89 |
6 |
while (names.hasMoreElements()) { |
| 90 |
1 |
String name = (String) names.nextElement(); |
| 91 |
1 |
Enumeration values = request.getHeaders(name); |
| 92 |
|
|
| 93 |
2 |
while (values.hasMoreElements()) { |
| 94 |
1 |
String value = (String) values.nextElement(); |
| 95 |
1 |
this.addHeader(name, value); |
| 96 |
1 |
} |
| 97 |
1 |
} |
| 98 |
|
|
| 99 |
|
|
| 100 |
5 |
Enumeration locales = request.getLocales(); |
| 101 |
|
|
| 102 |
10 |
while (locales.hasMoreElements()) { |
| 103 |
5 |
Locale locale = (Locale) locales.nextElement(); |
| 104 |
5 |
this.addLocale(locale); |
| 105 |
5 |
} |
| 106 |
|
|
| 107 |
|
|
| 108 |
5 |
Map parameters = request.getParameterMap(); |
| 109 |
5 |
Iterator paramNames = parameters.keySet().iterator(); |
| 110 |
|
|
| 111 |
6 |
while (paramNames.hasNext()) { |
| 112 |
1 |
String paramName = (String) paramNames.next(); |
| 113 |
1 |
Object o = parameters.get(paramName); |
| 114 |
1 |
if (o instanceof String[]) { |
| 115 |
1 |
String[] paramValues = (String[]) o; |
| 116 |
1 |
this.addParameter(paramName, paramValues); |
| 117 |
1 |
} else { |
| 118 |
0 |
if (logger.isWarnEnabled()) { |
| 119 |
0 |
logger.warn("ServletRequest.getParameterMap() returned non-String array"); |
| 120 |
|
} |
| 121 |
|
} |
| 122 |
1 |
} |
| 123 |
|
|
| 124 |
|
|
| 125 |
5 |
this.method = request.getMethod(); |
| 126 |
5 |
this.pathInfo = request.getPathInfo(); |
| 127 |
5 |
this.queryString = request.getQueryString(); |
| 128 |
5 |
this.requestURI = request.getRequestURI(); |
| 129 |
5 |
this.serverPort = portResolver.getServerPort(request); |
| 130 |
5 |
this.requestURL = request.getRequestURL().toString(); |
| 131 |
5 |
this.scheme = request.getScheme(); |
| 132 |
5 |
this.serverName = request.getServerName(); |
| 133 |
5 |
this.contextPath = request.getContextPath(); |
| 134 |
5 |
this.servletPath = request.getServletPath(); |
| 135 |
5 |
} |
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
private void addCookie(Cookie cookie) { |
| 140 |
0 |
cookies.add(new SavedCookie(cookie)); |
| 141 |
0 |
} |
| 142 |
|
|
| 143 |
|
private void addHeader(String name, String value) { |
| 144 |
1 |
ArrayList values = (ArrayList) headers.get(name); |
| 145 |
|
|
| 146 |
1 |
if (values == null) { |
| 147 |
1 |
values = new ArrayList(); |
| 148 |
1 |
headers.put(name, values); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
1 |
values.add(value); |
| 152 |
1 |
} |
| 153 |
|
|
| 154 |
|
private void addLocale(Locale locale) { |
| 155 |
5 |
locales.add(locale); |
| 156 |
5 |
} |
| 157 |
|
|
| 158 |
|
private void addParameter(String name, String[] values) { |
| 159 |
1 |
parameters.put(name, values); |
| 160 |
1 |
} |
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
public boolean doesRequestMatch(HttpServletRequest request, PortResolver portResolver) { |
| 171 |
0 |
Assert.notNull(request, "Request required"); |
| 172 |
0 |
Assert.notNull(portResolver, "PortResolver required"); |
| 173 |
|
|
| 174 |
0 |
if (!propertyEquals("pathInfo", this.pathInfo, request.getPathInfo())) { |
| 175 |
0 |
return false; |
| 176 |
|
} |
| 177 |
|
|
| 178 |
0 |
if (!propertyEquals("queryString", this.queryString, request.getQueryString())) { |
| 179 |
0 |
return false; |
| 180 |
|
} |
| 181 |
|
|
| 182 |
0 |
if (!propertyEquals("requestURI", this.requestURI, request.getRequestURI())) { |
| 183 |
0 |
return false; |
| 184 |
|
} |
| 185 |
|
|
| 186 |
0 |
if (!propertyEquals("serverPort", new Integer(this.serverPort), new Integer(portResolver.getServerPort(request)))) |
| 187 |
|
{ |
| 188 |
0 |
return false; |
| 189 |
|
} |
| 190 |
|
|
| 191 |
0 |
if (!propertyEquals("requestURL", this.requestURL, request.getRequestURL().toString())) { |
| 192 |
0 |
return false; |
| 193 |
|
} |
| 194 |
|
|
| 195 |
0 |
if (!propertyEquals("scheme", this.scheme, request.getScheme())) { |
| 196 |
0 |
return false; |
| 197 |
|
} |
| 198 |
|
|
| 199 |
0 |
if (!propertyEquals("serverName", this.serverName, request.getServerName())) { |
| 200 |
0 |
return false; |
| 201 |
|
} |
| 202 |
|
|
| 203 |
0 |
if (!propertyEquals("contextPath", this.contextPath, request.getContextPath())) { |
| 204 |
0 |
return false; |
| 205 |
|
} |
| 206 |
|
|
| 207 |
0 |
if (!propertyEquals("servletPath", this.servletPath, request.getServletPath())) { |
| 208 |
0 |
return false; |
| 209 |
|
} |
| 210 |
|
|
| 211 |
0 |
return true; |
| 212 |
|
} |
| 213 |
|
|
| 214 |
|
public String getContextPath() { |
| 215 |
3 |
return contextPath; |
| 216 |
|
} |
| 217 |
|
|
| 218 |
|
public List getCookies() { |
| 219 |
0 |
List cookieList = new ArrayList(cookies.size()); |
| 220 |
0 |
for (Iterator iterator = cookies.iterator(); iterator.hasNext();) { |
| 221 |
0 |
SavedCookie savedCookie = (SavedCookie) iterator.next(); |
| 222 |
0 |
cookieList.add(savedCookie.getCookie()); |
| 223 |
0 |
} |
| 224 |
0 |
return cookieList; |
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
public String getFullRequestUrl() { |
| 233 |
3 |
return UrlUtils.getFullRequestUrl(this); |
| 234 |
|
} |
| 235 |
|
|
| 236 |
|
public Iterator getHeaderNames() { |
| 237 |
0 |
return (headers.keySet().iterator()); |
| 238 |
|
} |
| 239 |
|
|
| 240 |
|
public Iterator getHeaderValues(String name) { |
| 241 |
1 |
ArrayList values = (ArrayList) headers.get(name); |
| 242 |
|
|
| 243 |
1 |
if (values == null) { |
| 244 |
0 |
return ((new ArrayList()).iterator()); |
| 245 |
|
} else { |
| 246 |
1 |
return (values.iterator()); |
| 247 |
|
} |
| 248 |
|
} |
| 249 |
|
|
| 250 |
|
public Iterator getLocales() { |
| 251 |
0 |
return (locales.iterator()); |
| 252 |
|
} |
| 253 |
|
|
| 254 |
|
public String getMethod() { |
| 255 |
0 |
return (this.method); |
| 256 |
|
} |
| 257 |
|
|
| 258 |
|
public Map getParameterMap() { |
| 259 |
0 |
return parameters; |
| 260 |
|
} |
| 261 |
|
|
| 262 |
|
public Iterator getParameterNames() { |
| 263 |
0 |
return (parameters.keySet().iterator()); |
| 264 |
|
} |
| 265 |
|
|
| 266 |
|
public String[] getParameterValues(String name) { |
| 267 |
1 |
return ((String[]) parameters.get(name)); |
| 268 |
|
} |
| 269 |
|
|
| 270 |
|
public String getPathInfo() { |
| 271 |
3 |
return pathInfo; |
| 272 |
|
} |
| 273 |
|
|
| 274 |
|
public String getQueryString() { |
| 275 |
3 |
return (this.queryString); |
| 276 |
|
} |
| 277 |
|
|
| 278 |
|
public String getRequestURI() { |
| 279 |
3 |
return (this.requestURI); |
| 280 |
|
} |
| 281 |
|
|
| 282 |
|
public String getRequestURL() { |
| 283 |
3 |
return requestURL; |
| 284 |
|
} |
| 285 |
|
|
| 286 |
|
|
| 287 |
|
|
| 288 |
|
|
| 289 |
|
|
| 290 |
|
|
| 291 |
|
public String getRequestUrl() { |
| 292 |
0 |
return UrlUtils.getRequestUrl(this); |
| 293 |
|
} |
| 294 |
|
|
| 295 |
|
public String getScheme() { |
| 296 |
3 |
return scheme; |
| 297 |
|
} |
| 298 |
|
|
| 299 |
|
public String getServerName() { |
| 300 |
3 |
return serverName; |
| 301 |
|
} |
| 302 |
|
|
| 303 |
|
public int getServerPort() { |
| 304 |
3 |
return serverPort; |
| 305 |
|
} |
| 306 |
|
|
| 307 |
|
public String getServletPath() { |
| 308 |
3 |
return servletPath; |
| 309 |
|
} |
| 310 |
|
|
| 311 |
|
private boolean propertyEquals(String log, Object arg1, Object arg2) { |
| 312 |
0 |
if ((arg1 == null) && (arg2 == null)) { |
| 313 |
0 |
if (logger.isDebugEnabled()) { |
| 314 |
0 |
logger.debug(log + ": both null (property equals)"); |
| 315 |
|
} |
| 316 |
|
|
| 317 |
0 |
return true; |
| 318 |
|
} |
| 319 |
|
|
| 320 |
0 |
if (((arg1 == null) && (arg2 != null)) || ((arg1 != null) && (arg2 == null))) { |
| 321 |
0 |
if (logger.isDebugEnabled()) { |
| 322 |
0 |
logger.debug(log + ": arg1=" + arg1 + "; arg2=" + arg2 + " (property not equals)"); |
| 323 |
|
} |
| 324 |
|
|
| 325 |
0 |
return false; |
| 326 |
|
} |
| 327 |
|
|
| 328 |
0 |
if (arg1.equals(arg2)) { |
| 329 |
0 |
if (logger.isDebugEnabled()) { |
| 330 |
0 |
logger.debug(log + ": arg1=" + arg1 + "; arg2=" + arg2 + " (property equals)"); |
| 331 |
|
} |
| 332 |
|
|
| 333 |
0 |
return true; |
| 334 |
|
} else { |
| 335 |
0 |
if (logger.isDebugEnabled()) { |
| 336 |
0 |
logger.debug(log + ": arg1=" + arg1 + "; arg2=" + arg2 + " (property not equals)"); |
| 337 |
|
} |
| 338 |
|
|
| 339 |
0 |
return false; |
| 340 |
|
} |
| 341 |
|
} |
| 342 |
|
|
| 343 |
|
public String toString() { |
| 344 |
0 |
return "SavedRequest[" + getFullRequestUrl() + "]"; |
| 345 |
|
} |
| 346 |
|
} |