1 15 16 package com.sun.facelets.mock; 17 18 import java.io.BufferedReader ; 19 import java.io.IOException ; 20 import java.io.InputStreamReader ; 21 import java.io.Reader ; 22 import java.io.UnsupportedEncodingException ; 23 import java.net.URI ; 24 import java.security.Principal ; 25 import java.text.DateFormat ; 26 import java.text.ParseException ; 27 import java.util.Arrays ; 28 import java.util.Collections ; 29 import java.util.Enumeration ; 30 import java.util.Hashtable ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 import java.util.Vector ; 36 37 import javax.servlet.RequestDispatcher ; 38 import javax.servlet.ServletContext ; 39 import javax.servlet.ServletInputStream ; 40 import javax.servlet.http.Cookie ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpSession ; 43 44 49 public class MockHttpServletRequest implements HttpServletRequest { 50 51 private final ServletContext servletContext; 52 53 private final URI uri; 54 55 private final String method; 56 57 private Cookie [] cookies = new Cookie [0]; 58 59 private final Hashtable headers = new Hashtable (); 60 61 private String remoteUser; 62 63 private String servletPath; 64 65 private HttpSession session; 66 67 private final Hashtable attributes = new Hashtable (); 68 69 private final Properties param = new Properties (); 70 71 private String characterEncoding = "ISO-8859-1"; 72 73 private String contentType = "text/html"; 74 75 private int contentLength = 0; 76 77 private String protocol = "HTTP/1.1"; 78 79 private String localName = "localhost"; 80 81 private int localPort = 80; 82 83 private String remoteAddr = "127.0.0.1"; 84 85 private String remoteHost = "localhost"; 86 87 private Locale locale = Locale.getDefault(); 88 89 private Vector locales = new Vector (Arrays.asList(Locale 90 .getAvailableLocales())); 91 92 private boolean secure = false; 93 94 private int remotePort = 1024; 95 96 private String localAddr = "127.0.0.1"; 97 98 private ServletInputStream inputStream = new MockServletInputStream(); 99 100 public MockHttpServletRequest(ServletContext servletContext, URI uri) { 101 this(servletContext, "GET", uri); 102 } 103 104 public MockHttpServletRequest(ServletContext servletContext, String uri) { 105 this(servletContext, "GET", uri); 106 } 107 108 public MockHttpServletRequest(ServletContext servletContext, String method, 109 String uri) { 110 this(servletContext, method, URI.create(uri)); 111 } 112 113 public MockHttpServletRequest(ServletContext servletContext, String method, 114 URI uri) { 115 this.servletContext = servletContext; 116 this.uri = uri; 117 this.method = method; 118 119 String q = this.uri.getRawQuery(); 120 if (q != null) { 121 String [] p = q.split("(&|=)"); 122 for (int i = 0; i < p.length; i += 2) { 123 this.param.put(p[i], p[i + 1]); 124 } 125 } 126 } 127 128 public String getAuthType() { 129 return BASIC_AUTH; 130 } 131 132 public Cookie [] getCookies() { 133 return this.cookies; 134 } 135 136 public long getDateHeader(String name) { 137 String hdr = this.getHeader(name); 138 if (hdr != null) { 139 try { 140 return DateFormat.getDateInstance(DateFormat.FULL).parse(hdr) 141 .getTime(); 142 } catch (ParseException e) { 143 throw new IllegalArgumentException ("Header " + name + ": " 144 + hdr); 145 } 146 } 147 return -1; 148 } 149 150 public String getHeader(String name) { 151 Object obj = this.headers.get(name); 152 if (obj instanceof List ) { 153 return ((List ) obj).get(0).toString(); 154 } else if (obj instanceof String ) { 155 return (String ) obj; 156 } 157 return null; 158 } 159 160 public Enumeration getHeaders(String name) { 161 Object obj = this.headers.get(name); 162 if (obj instanceof Vector ) { 163 return ((Vector ) obj).elements(); 164 } else if (obj instanceof String ) { 165 Vector v = new Vector (); 166 v.add(obj); 167 return v.elements(); 168 } 169 return null; 170 } 171 172 public Enumeration getHeaderNames() { 173 return this.headers.keys(); 174 } 175 176 public int getIntHeader(String name) { 177 String hdr = this.getHeader(name); 178 if (hdr != null) { 179 try { 180 return Integer.parseInt(hdr); 181 } catch (Exception e) { 182 throw new IllegalArgumentException ("Header " + name + ": " 183 + hdr); 184 } 185 } 186 return -1; 187 } 188 189 public String getMethod() { 190 return this.method; 191 } 192 193 public String getPathInfo() { 194 return this.uri.getPath(); 195 } 196 197 public String getPathTranslated() { 198 return this.servletContext.getRealPath(this.uri.getPath()); 199 } 200 201 public String getContextPath() { 202 return this.uri.getPath(); 203 } 204 205 public String getQueryString() { 206 return this.uri.getQuery(); 207 } 208 209 public String getRemoteUser() { 210 return this.remoteUser; 211 } 212 213 public boolean isUserInRole(String role) { 214 throw new UnsupportedOperationException (); 215 } 216 217 public Principal getUserPrincipal() { 218 throw new UnsupportedOperationException (); 219 } 220 221 public String getRequestedSessionId() { 222 return this.getParameter("jsessionid"); 223 } 224 225 public String getRequestURI() { 226 return this.uri.getPath(); 227 } 228 229 public StringBuffer getRequestURL() { 230 return new StringBuffer (this.uri.toString()); 231 } 232 233 public String getServletPath() { 234 return this.servletPath; 235 } 236 237 public HttpSession getSession(boolean create) { 238 if (this.session == null && create) { 239 this.session = new MockHttpSession(this.servletContext); 240 } 241 return this.session; 242 } 243 244 public HttpSession getSession() { 245 return this.getSession(true); 246 } 247 248 public boolean isRequestedSessionIdValid() { 249 throw new UnsupportedOperationException (); 250 } 251 252 public boolean isRequestedSessionIdFromCookie() { 253 throw new UnsupportedOperationException (); 254 } 255 256 public boolean isRequestedSessionIdFromURL() { 257 throw new UnsupportedOperationException (); 258 } 259 260 public boolean isRequestedSessionIdFromUrl() { 261 throw new UnsupportedOperationException (); 262 } 263 264 public Object getAttribute(String name) { 265 return this.attributes.get(name); 266 } 267 268 public Enumeration getAttributeNames() { 269 return this.attributes.keys(); 270 } 271 272 public String getCharacterEncoding() { 273 return this.characterEncoding; 274 } 275 276 public void setCharacterEncoding(String characterEncoding) 277 throws UnsupportedEncodingException { 278 this.characterEncoding = characterEncoding; 279 } 280 281 public int getContentLength() { 282 return this.contentLength; 283 } 284 285 public String getContentType() { 286 return this.contentType; 287 } 288 289 public ServletInputStream getInputStream() throws IOException { 290 return this.inputStream; 291 } 292 293 public String getParameter(String name) { 294 return this.param.getProperty(name); 295 } 296 297 public Enumeration getParameterNames() { 298 return this.param.keys(); 299 } 300 301 public String [] getParameterValues(String name) { 302 String p = this.param.getProperty(name); 303 if (p != null) { 304 return p.split(","); 305 } 306 return null; 307 } 308 309 public void setParameter(String name, String value) { 310 this.param.put(name, value); 311 } 312 313 public Map getParameterMap() { 314 return Collections.unmodifiableMap(this.param); 315 } 316 317 public String getProtocol() { 318 return this.protocol; 319 } 320 321 public String getScheme() { 322 return this.uri.getScheme(); 323 } 324 325 public String getServerName() { 326 return this.localName; 327 } 328 329 public int getServerPort() { 330 return this.localPort; 331 } 332 333 public BufferedReader getReader() throws IOException { 334 if (this.inputStream != null) { 335 Reader sourceReader = (this.characterEncoding != null) ? new InputStreamReader ( 336 this.inputStream, this.characterEncoding) 337 : new InputStreamReader (this.inputStream); 338 return new BufferedReader (sourceReader); 339 } else { 340 return null; 341 } 342 } 343 344 public String getRemoteAddr() { 345 return this.remoteAddr; 346 } 347 348 public String getRemoteHost() { 349 return this.remoteHost; 350 } 351 352 public void setAttribute(String name, Object value) { 353 this.attributes.put(name, value); 354 } 355 356 public void removeAttribute(String name) { 357 this.attributes.remove(name); 358 } 359 360 public Locale getLocale() { 361 return this.locale; 362 } 363 364 public Enumeration getLocales() { 365 return this.locales.elements(); 366 } 367 368 public boolean isSecure() { 369 return this.secure; 370 } 371 372 public RequestDispatcher getRequestDispatcher(String path) { 373 return this.servletContext.getRequestDispatcher(path); 374 } 375 376 public String getRealPath(String path) { 377 return this.servletContext.getRealPath(path); 378 } 379 380 public int getRemotePort() { 381 return this.remotePort; 382 } 383 384 public String getLocalName() { 385 return this.localName; 386 } 387 388 public String getLocalAddr() { 389 return this.localAddr; 390 } 391 392 public int getLocalPort() { 393 return this.localPort; 394 } 395 396 } 397 | Popular Tags |