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