1 16 package org.apache.cocoon.environment.mock; 17 18 import java.security.Principal ; 19 import java.util.Collections ; 20 import java.util.Enumeration ; 21 import java.util.Hashtable ; 22 import java.util.HashMap ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 import java.text.DateFormat ; 26 import java.text.SimpleDateFormat ; 27 import java.text.ParseException ; 28 29 import junit.framework.AssertionFailedError; 30 31 import org.apache.cocoon.environment.Cookie; 32 import org.apache.cocoon.environment.Request; 33 import org.apache.cocoon.environment.Session; 34 35 38 public class MockRequest implements Request { 39 40 private Hashtable attributes = new Hashtable (); 41 private String scheme; 42 private String protocol = "HTTP/1.1"; 43 private String requestURI; 44 private String contextPath = ""; 45 private String servletPath; 46 private String pathInfo; 47 private String queryString; 48 private String method = "GET"; 49 private String contentType; 50 private Locale locale; 51 private Principal principal; 52 private String remoteAddr; 53 private String remoteHost; 54 private String remoteUser; 55 private String userRole; 56 private String reqSessionId; 57 private String authType; 58 private String charEncoding; 59 private String serverName; 60 private int port = 80; 61 62 private Hashtable parameters = new Hashtable (); 63 private Hashtable headers = new Hashtable (); 64 private HashMap cookies = new HashMap (); 65 66 private MockSession session; 67 68 public Object get(String name) { 69 return getAttribute(name); 70 } 71 72 public Object getAttribute(String name) { 73 return attributes.get(name); 74 } 75 76 public Enumeration getAttributeNames() { 77 return attributes.keys(); 78 } 79 80 public void setAttribute(String name, Object o) { 81 if (o == null) 82 attributes.remove(name); 83 else 84 attributes.put(name, o); 85 } 86 87 public void removeAttribute(String name) { 88 attributes.remove(name); 89 } 90 91 public String getAuthType() { 92 return authType; 93 } 94 95 public String getCharacterEncoding() { 96 return charEncoding; 97 } 98 99 public void setCharacterEncoding(String enc) throws java.io.UnsupportedEncodingException { 100 charEncoding = enc; 101 } 102 103 public int getContentLength() { 104 return -1; 105 } 106 107 public String getContentType() { 108 return contentType; 109 } 110 111 public String getParameter(String name) { 112 return (String )parameters.get(name); 113 } 114 115 public Enumeration getParameterNames() { 116 return parameters.keys(); 117 } 118 119 public String [] getParameterValues(String name) { 120 Object param = parameters.get(name); 121 if( null == param ) 122 return null; 123 else { 124 if (param.getClass().isArray()) { 125 return (String []) param; 126 } else { 127 return new String [] {(String ) param}; 128 } 129 } 130 } 131 132 public void addParameter(String name, String value) { 133 parameters.put(name, value); 134 } 135 136 public String getProtocol() { 137 return protocol; 138 } 139 140 public String getScheme() { 141 return scheme; 142 } 143 144 public String getServerName() { 145 return serverName; 146 } 147 148 public int getServerPort() { 149 return port; 150 } 151 152 public String getRemoteAddr() { 153 return remoteAddr; 154 } 155 156 public String getRemoteHost() { 157 return remoteHost; 158 } 159 160 public Locale getLocale() { 161 return locale; 162 } 163 164 public Enumeration getLocales() { 165 return Collections.enumeration(Collections.singleton(getLocale())); 166 } 167 168 public boolean isSecure() { 169 if(scheme==null){ 170 return false; 171 } else{ 172 return scheme.equalsIgnoreCase("HTTPS"); 173 } 174 } 175 176 public Cookie[] getCookies() { 177 if (cookies.isEmpty()) 178 return null; 179 else { 180 Cookie[] cookieArray = new Cookie[cookies.size()]; 181 return (Cookie []) cookies.values().toArray(cookieArray); 182 } 183 } 184 185 public Map getCookieMap() { 186 return cookies; 187 } 188 189 public long getDateHeader(String name) { 190 String s1 = getHeader(name); 191 if(s1 == null) 192 return -1L; 193 try 194 { 195 DateFormat dateFormat = new SimpleDateFormat (); 196 return dateFormat.parse(s1).getTime(); 197 } 198 catch(ParseException exception) { 199 throw new IllegalArgumentException ("Cannot parse date: " + s1); 200 } 201 } 202 203 public String getHeader(String name) { 204 return (String ) headers.get(name); 205 } 206 207 public Enumeration getHeaders(String name) { 208 throw new AssertionFailedError("Not implemented"); 209 } 210 211 public Enumeration getHeaderNames() { 212 return headers.keys(); 213 } 214 215 public String getMethod() { 216 return method; 217 } 218 219 public String getPathInfo() { 220 return pathInfo; 221 } 222 223 public String getPathTranslated() { 224 throw new AssertionFailedError("Not implemented"); 225 } 226 227 public String getContextPath() { 228 return contextPath; 229 } 230 231 public void setContextPath(String path) { 232 contextPath = path; 233 } 234 235 public String getQueryString() { 236 return queryString; 237 } 238 239 public void setQueryString(String string) { 240 queryString = string; 241 } 242 243 public String getRemoteUser() { 244 return remoteUser; 245 } 246 247 public Principal getUserPrincipal() { 248 return principal; 249 } 250 251 public boolean isUserInRole(String role) { 252 return userRole.equals(role); 253 } 254 255 public String getRequestedSessionId() { 256 return reqSessionId; 257 } 258 259 public String getRequestURI() { 260 return requestURI; 261 } 262 263 public void setRequestURI(String uri) { 264 requestURI = uri; 265 } 266 267 public String getSitemapURI() { 268 return requestURI; 269 } 270 271 public String getSitemapURIPrefix() { 272 return ""; 273 } 274 275 public String getServletPath() { 276 return servletPath; 277 } 278 279 public Session getSession(boolean create) { 280 if ((session == null) && (create)) 281 this.session = new MockSession(); 282 else if ((session != null) && (!(session).isValid()) && (create)) 283 this.session = new MockSession(); 284 if ((session != null) && ((session).isValid())) 285 return this.session; 286 else 287 return null; 288 } 289 290 public Session getSession() { 291 return getSession(true); 292 } 293 294 public boolean isRequestedSessionIdValid() { 295 if (session != null) { 296 try { 297 session.getId(); 298 return true; 299 } catch (IllegalStateException e) { 300 return false; 301 } 302 } else 303 return false; 304 } 305 306 public boolean isRequestedSessionIdFromCookie() { 307 return true; 308 } 309 310 public boolean isRequestedSessionIdFromURL() { 311 return false; 312 } 313 314 public void reset() { 315 attributes.clear(); 316 scheme = null; 317 protocol = "HTTP/1.1"; 318 requestURI = null; 319 contextPath = null; 320 servletPath = null; 321 pathInfo = null; 322 queryString = null; 323 method = "GET"; 324 contentType = null; 325 locale = null; 326 principal = null; 327 remoteAddr = null; 328 remoteHost = null; 329 remoteUser = null; 330 userRole = null; 331 reqSessionId = null; 332 authType = null; 333 charEncoding = null; 334 serverName = null; 335 port = 80; 336 337 parameters.clear(); 338 headers.clear(); 339 } 340 341 public void setHeader( String key, String value ) { 342 this.headers.put(key, value ); 343 } 344 345 public void setMethod( String method ) { 346 this.method = method; 347 } 348 349 public void clearSession() { 350 this.session = null; 351 } 352 353 } 354 | Popular Tags |