/sigmah/src/test/java/org/sigmah/server/mock/MockHttpServletRequest.java
Java | 384 lines | 289 code | 91 blank | 4 comment | 22 complexity | 7ac16269d614a6846344ac9a7f3047d0 MD5 | raw file
1/* 2 * All Sigmah code is released under the GNU General Public License v3 3 * See COPYRIGHT.txt and LICENSE.txt. 4 */ 5 6package org.sigmah.server.mock; 7 8import javax.servlet.RequestDispatcher; 9import javax.servlet.ServletInputStream; 10import javax.servlet.http.Cookie; 11import javax.servlet.http.HttpServletRequest; 12import javax.servlet.http.HttpSession; 13import java.io.BufferedReader; 14import java.security.Principal; 15import java.util.*; 16 17 18public class MockHttpServletRequest implements HttpServletRequest { 19 20 private HashMap<String, Object> attributes = new HashMap<String, Object>(); 21 private String contextPath = null; 22 private Locale locale = null; 23 private HashMap<String, String[]> parameters = new HashMap<String, String[]>(); 24 private String pathInfo = null; 25 private Principal principal = null; 26 private String queryString = null; 27 private String servletPath = null; 28 private HttpSession session = null; 29 private String method = null; 30 private String contentType = null; 31 private ArrayList<Cookie> cookies = new ArrayList<Cookie>(0); 32 private String server = "localhost"; 33 private int port = 8080; 34 private boolean secure = false; 35 private StringBuffer requestURL; 36 37 38 public MockHttpServletRequest() { 39 super(); 40 } 41 42 public MockHttpServletRequest(HttpSession session) { 43 super(); 44 setHttpSession(session); 45 } 46 47 public void setParameter(String name, String value) { 48 parameters.remove(name); 49 addParameter(name, value); 50 } 51 52 public void addParameter(String name, String value) { 53 String[] values = (String[]) parameters.get(name); 54 55 if (values == null) { 56 String[] results = new String[]{value}; 57 58 parameters.put(name, results); 59 60 return; 61 } 62 63 String[] results = new String[values.length + 1]; 64 65 System.arraycopy(values, 0, results, 0, values.length); 66 results[values.length] = value; 67 parameters.put(name, results); 68 } 69 70 public void setHttpSession(HttpSession session) { 71 this.session = session; 72 } 73 74 public void setLocale(Locale locale) { 75 this.locale = locale; 76 } 77 78 public void setMethod(String method) { 79 this.method = method; 80 } 81 82 public void setContentType(String contentType) { 83 this.contentType = contentType; 84 } 85 86 public void setContextPath(String contextPath) { 87 this.contextPath = contextPath; 88 } 89 90 public void setPathInfo(String pathInfo) { 91 this.pathInfo = pathInfo; 92 } 93 94 public void setQueryString(String queryString) { 95 this.queryString = queryString; 96 } 97 98 public void setServletPath(String servletPath) { 99 this.servletPath = servletPath; 100 } 101 102 public void setServer(String server) { 103 this.server = server; 104 } 105 106 public void setPort(int port) { 107 this.port = port; 108 } 109 110 public void setSecure(boolean secure) { 111 this.secure = secure; 112 } 113 114 public void setUserPrincipal(Principal principal) { 115 this.principal = principal; 116 } 117 118 public String getAuthType() { 119 throw new UnsupportedOperationException(); 120 } 121 122 public String getContextPath() { 123 return (contextPath); 124 } 125 126 public Cookie[] getCookies() { 127 return cookies.toArray(new Cookie[0]); 128 } 129 130 public void addCookie(String name, String value) { 131 cookies.add(new Cookie(name, value)); 132 } 133 134 public long getDateHeader(String name) { 135 throw new UnsupportedOperationException(); 136 } 137 138 public String getHeader(String name) { 139 throw new UnsupportedOperationException(); 140 } 141 142 public Enumeration getHeaderNames() { 143 throw new UnsupportedOperationException(); 144 } 145 146 public Enumeration getHeaders(String name) { 147 throw new UnsupportedOperationException(); 148 } 149 150 public int getIntHeader(String name) { 151 throw new UnsupportedOperationException(); 152 } 153 154 public String getMethod() { 155 return method; 156 } 157 158 public String getPathInfo() { 159 return pathInfo; 160 } 161 162 public String getPathTranslated() { 163 throw new UnsupportedOperationException(); 164 } 165 166 public String getQueryString() { 167 return queryString; 168 } 169 170 public String getRemoteUser() { 171 if (principal != null) { 172 return (principal.getName()); 173 } else { 174 return (null); 175 } 176 } 177 178 public String getRequestedSessionId() { 179 throw new UnsupportedOperationException(); 180 } 181 182 183 public String getRequestURI() { 184 StringBuffer sb = new StringBuffer(); 185 186 if (contextPath != null) { 187 sb.append(contextPath); 188 } 189 190 if (servletPath != null) { 191 sb.append(servletPath); 192 } 193 194 if (pathInfo != null) { 195 sb.append(pathInfo); 196 } 197 198 return (sb.toString()); 199 } 200 201 public StringBuffer getRequestURL() { 202 if (requestURL == null) { 203 throw new IllegalStateException("RequestURL has not been set"); 204 } 205 206 return requestURL; 207 } 208 209 public void setRequestURL(String url) { 210 requestURL = new StringBuffer(url); 211 } 212 213 public String getServletPath() { 214 return servletPath; 215 } 216 217 public HttpSession getSession() { 218 return getSession(true); 219 } 220 221 @Override 222 public int getRemotePort() { 223 throw new UnsupportedOperationException(); 224 } 225 226 @Override 227 public String getLocalName() { 228 throw new UnsupportedOperationException(); 229 } 230 231 @Override 232 public String getLocalAddr() { 233 throw new UnsupportedOperationException(); 234 } 235 236 @Override 237 public int getLocalPort() { 238 return 0; 239 } 240 241 public HttpSession getSession(boolean create) { 242 if (create && (session == null)) { 243 session = new MockHttpSession(); 244 } 245 246 return session; 247 } 248 249 public Principal getUserPrincipal() { 250 return (principal); 251 } 252 253 public boolean isRequestedSessionIdFromCookie() { 254 throw new UnsupportedOperationException(); 255 } 256 257 @Deprecated 258 public boolean isRequestedSessionIdFromUrl() { 259 throw new UnsupportedOperationException(); 260 } 261 262 public boolean isRequestedSessionIdFromURL() { 263 throw new UnsupportedOperationException(); 264 } 265 266 public boolean isRequestedSessionIdValid() { 267 throw new UnsupportedOperationException(); 268 } 269 270 public boolean isUserInRole(String role) { 271 return false; 272 } 273 274 public Object getAttribute(String name) { 275 return (attributes.get(name)); 276 } 277 278 public Enumeration getAttributeNames() { 279 throw new UnsupportedOperationException(); 280 } 281 282 public String getCharacterEncoding() { 283 throw new UnsupportedOperationException(); 284 } 285 286 public int getContentLength() { 287 throw new UnsupportedOperationException(); 288 } 289 290 public String getContentType() { 291 return (contentType); 292 } 293 294 public ServletInputStream getInputStream() { 295 throw new UnsupportedOperationException(); 296 } 297 298 public Locale getLocale() { 299 return (locale); 300 } 301 302 public Enumeration getLocales() { 303 throw new UnsupportedOperationException(); 304 } 305 306 public String getParameter(String name) { 307 String[] values = (String[]) parameters.get(name); 308 309 if (values != null) { 310 return (values[0]); 311 } else { 312 return (null); 313 } 314 } 315 316 public Map getParameterMap() { 317 return (parameters); 318 } 319 320 public Enumeration getParameterNames() { 321 return Collections.enumeration(parameters.keySet()); 322 } 323 324 public String[] getParameterValues(String name) { 325 return ((String[]) parameters.get(name)); 326 } 327 328 public String getProtocol() { 329 throw new UnsupportedOperationException(); 330 } 331 332 public BufferedReader getReader() { 333 throw new UnsupportedOperationException(); 334 } 335 336 @Deprecated 337 public String getRealPath(String path) { 338 throw new UnsupportedOperationException(); 339 } 340 341 public String getRemoteAddr() { 342 throw new UnsupportedOperationException(); 343 } 344 345 public String getRemoteHost() { 346 throw new UnsupportedOperationException(); 347 } 348 349 public RequestDispatcher getRequestDispatcher(String path) { 350 throw new UnsupportedOperationException(); 351 } 352 353 public String getScheme() { 354 return "http"; 355 } 356 357 public String getServerName() { 358 return server; 359 } 360 361 public int getServerPort() { 362 return port; 363 } 364 365 public boolean isSecure() { 366 return secure; 367 } 368 369 public void removeAttribute(String name) { 370 attributes.remove(name); 371 } 372 373 public void setAttribute(String name, Object value) { 374 if (value == null) { 375 attributes.remove(name); 376 } else { 377 attributes.put(name, value); 378 } 379 } 380 381 public void setCharacterEncoding(String name) { 382 throw new UnsupportedOperationException(); 383 } 384}