1 16 package org.apache.commons.chain.web.servlet; 17 18 19 import org.apache.commons.chain.web.MockEnumeration; 20 import org.apache.commons.chain.web.MockPrincipal; 21 22 import javax.servlet.RequestDispatcher ; 23 import javax.servlet.ServletInputStream ; 24 import javax.servlet.http.Cookie ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpSession ; 27 import java.io.BufferedReader ; 28 import java.security.Principal ; 29 import java.util.*; 30 31 32 public class MockHttpServletRequest implements HttpServletRequest { 34 35 36 public MockHttpServletRequest() { 37 super(); 38 } 39 40 41 public MockHttpServletRequest(HttpSession session) { 42 super(); 43 setHttpSession(session); 44 } 45 46 47 public MockHttpServletRequest(String contextPath, String servletPath, 48 String pathInfo, String queryString) { 49 super(); 50 setPathElements(contextPath, servletPath, pathInfo, queryString); 51 } 52 53 54 55 public MockHttpServletRequest(String contextPath, String servletPath, 56 String pathInfo, String queryString, 57 HttpSession session) { 58 super(); 59 setPathElements(contextPath, servletPath, pathInfo, queryString); 60 setHttpSession(session); 61 } 62 63 64 65 protected HashMap attributes = new HashMap(); 66 protected String contextPath = null; 67 protected HashMap headers = new HashMap(); 68 protected Locale locale = null; 69 protected HashMap parameters = new HashMap(); 70 protected String pathInfo = null; 71 protected Principal principal = null; 72 protected String queryString = null; 73 protected String servletPath = null; 74 protected HttpSession session = null; 75 76 77 79 80 public void addHeader(String name, String value) { 81 String values[] = (String []) headers.get(name); 82 if (values == null) { 83 String results[] = new String [] { value }; 84 headers.put(name, results); 85 return; 86 } 87 String results[] = new String [values.length + 1]; 88 System.arraycopy(values, 0, results, 0, values.length); 89 results[values.length] = value; 90 headers.put(name, results); 91 } 92 93 94 public void addParameter(String name, String value) { 95 String values[] = (String []) parameters.get(name); 96 if (values == null) { 97 String results[] = new String [] { value }; 98 parameters.put(name, results); 99 return; 100 } 101 String results[] = new String [values.length + 1]; 102 System.arraycopy(values, 0, results, 0, values.length); 103 results[values.length] = value; 104 parameters.put(name, results); 105 } 106 107 108 public void setHttpSession(HttpSession session) { 109 this.session = session; 110 } 111 112 113 public void setLocale(Locale locale) { 114 this.locale = locale; 115 } 116 117 118 public void setPathElements(String contextPath, String servletPath, 119 String pathInfo, String queryString) { 120 121 this.contextPath = contextPath; 122 this.servletPath = servletPath; 123 this.pathInfo = pathInfo; 124 this.queryString = queryString; 125 126 } 127 128 129 public void setUserPrincipal(Principal principal) { 130 this.principal = principal; 131 } 132 133 134 135 137 138 public String getAuthType() { 139 throw new UnsupportedOperationException (); 140 } 141 142 143 public String getContextPath() { 144 return (contextPath); 145 } 146 147 148 public Cookie [] getCookies() { 149 throw new UnsupportedOperationException (); 150 } 151 152 153 public long getDateHeader(String name) { 154 throw new UnsupportedOperationException (); 155 } 156 157 158 public String getHeader(String name) { 159 String values[] = (String []) headers.get(name); 160 if (values != null) { 161 return (values[0]); 162 } else { 163 return (null); 164 } 165 } 166 167 168 public Enumeration getHeaderNames() { 169 return (new MockEnumeration(headers.keySet().iterator())); 170 } 171 172 173 public Enumeration getHeaders(String name) { 174 String headers[] = (String []) this.headers.get(name); 175 if (headers == null) { 176 headers = new String [0]; 177 } 178 List list = new ArrayList(); 179 for (int i = 0; i < headers.length; i++) { 180 list.add(headers[i]); 181 } 182 return (new MockEnumeration(list.iterator())); 183 } 184 185 186 public int getIntHeader(String name) { 187 throw new UnsupportedOperationException (); 188 } 189 190 191 public String getMethod() { 192 throw new UnsupportedOperationException (); 193 } 194 195 196 public String getPathInfo() { 197 return (pathInfo); 198 } 199 200 201 public String getPathTranslated() { 202 throw new UnsupportedOperationException (); 203 } 204 205 206 public String getQueryString() { 207 return (queryString); 208 } 209 210 211 public String getRemoteUser() { 212 if (principal != null) { 213 return (principal.getName()); 214 } else { 215 return (null); 216 } 217 } 218 219 220 public String getRequestedSessionId() { 221 throw new UnsupportedOperationException (); 222 } 223 224 225 public String getRequestURI() { 226 StringBuffer sb = new StringBuffer (); 227 if (contextPath != null) { 228 sb.append(contextPath); 229 } 230 if (servletPath != null) { 231 sb.append(servletPath); 232 } 233 if (pathInfo != null) { 234 sb.append(pathInfo); 235 } 236 if (sb.length() > 0) { 237 return (sb.toString()); 238 } 239 throw new UnsupportedOperationException (); 240 } 241 242 243 public StringBuffer getRequestURL() { 244 throw new UnsupportedOperationException (); 245 } 246 247 248 public String getServletPath() { 249 return (servletPath); 250 } 251 252 253 public HttpSession getSession() { 254 return (getSession(true)); 255 } 256 257 258 public HttpSession getSession(boolean create) { 259 if (create && (session == null)) { 260 throw new UnsupportedOperationException (); 261 } 262 return (session); 263 } 264 265 266 public Principal getUserPrincipal() { 267 return (principal); 268 } 269 270 271 public boolean isRequestedSessionIdFromCookie() { 272 throw new UnsupportedOperationException (); 273 } 274 275 276 public boolean isRequestedSessionIdFromUrl() { 277 throw new UnsupportedOperationException (); 278 } 279 280 281 public boolean isRequestedSessionIdFromURL() { 282 throw new UnsupportedOperationException (); 283 } 284 285 286 public boolean isRequestedSessionIdValid() { 287 throw new UnsupportedOperationException (); 288 } 289 290 291 public boolean isUserInRole(String role) { 292 if ((principal != null) && (principal instanceof MockPrincipal)) { 293 return (((MockPrincipal) principal).isUserInRole(role)); 294 } else { 295 return (false); 296 } 297 } 298 299 300 302 303 public Object getAttribute(String name) { 304 return (attributes.get(name)); 305 } 306 307 308 public Enumeration getAttributeNames() { 309 return (new MockEnumeration(attributes.keySet().iterator())); 310 } 311 312 313 public String getCharacterEncoding() { 314 throw new UnsupportedOperationException (); 315 } 316 317 318 public int getContentLength() { 319 throw new UnsupportedOperationException (); 320 } 321 322 323 public String getContentType() { 324 throw new UnsupportedOperationException (); 325 } 326 327 328 public ServletInputStream getInputStream() { 329 throw new UnsupportedOperationException (); 330 } 331 332 333 public Locale getLocale() { 334 return (locale); 335 } 336 337 338 public Enumeration getLocales() { 339 throw new UnsupportedOperationException (); 340 } 341 342 343 public String getLocalAddr() { 344 throw new UnsupportedOperationException (); 345 } 346 347 348 public String getLocalName() { 349 throw new UnsupportedOperationException (); 350 } 351 352 353 public int getLocalPort() { 354 throw new UnsupportedOperationException (); 355 } 356 357 358 public String getParameter(String name) { 359 String values[] = (String []) parameters.get(name); 360 if (values != null) { 361 return (values[0]); 362 } else { 363 return (null); 364 } 365 } 366 367 368 public Map getParameterMap() { 369 return (parameters); 370 } 371 372 373 public Enumeration getParameterNames() { 374 return (new MockEnumeration(parameters.keySet().iterator())); 375 } 376 377 378 public String [] getParameterValues(String name) { 379 return ((String []) parameters.get(name)); 380 } 381 382 383 public String getProtocol() { 384 throw new UnsupportedOperationException (); 385 } 386 387 388 public BufferedReader getReader() { 389 throw new UnsupportedOperationException (); 390 } 391 392 393 public String getRealPath(String path) { 394 throw new UnsupportedOperationException (); 395 } 396 397 398 public String getRemoteAddr() { 399 throw new UnsupportedOperationException (); 400 } 401 402 403 public String getRemoteHost() { 404 throw new UnsupportedOperationException (); 405 } 406 407 408 public int getRemotePort() { 409 throw new UnsupportedOperationException (); 410 } 411 412 413 public RequestDispatcher getRequestDispatcher(String path) { 414 throw new UnsupportedOperationException (); 415 } 416 417 418 public String getScheme() { 419 return ("http"); 420 } 421 422 423 public String getServerName() { 424 return ("localhost"); 425 } 426 427 428 public int getServerPort() { 429 return (8080); 430 } 431 432 433 public boolean isSecure() { 434 return (false); 435 } 436 437 438 public void removeAttribute(String name) { 439 attributes.remove(name); 440 } 441 442 443 public void setAttribute(String name, Object value) { 444 if (value == null) { 445 attributes.remove(name); 446 } else { 447 attributes.put(name, value); 448 } 449 } 450 451 452 public void setCharacterEncoding(String name) { 453 throw new UnsupportedOperationException (); 454 } 455 456 457 } 458 | Popular Tags |