1 5 6 13 package org.exoplatform.test.mocks.servlet; 14 15 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.Cookie ; 18 import javax.servlet.http.HttpSession ; 19 import javax.servlet.ServletInputStream ; 20 import javax.servlet.RequestDispatcher ; 21 22 import java.util.*; 23 import java.security.Principal ; 24 import java.io.UnsupportedEncodingException ; 25 import java.io.IOException ; 26 import java.io.BufferedReader ; 27 28 public class MockServletRequest implements HttpServletRequest { 29 30 private Map parameters ; 31 private Map attributes ; 32 private HttpSession session; 33 private Locale locale; 34 private boolean secure; 35 private Map headers; 36 private String enc = "ISO-8859-1"; 37 38 private String remoteUser = "REMOTE USER FROM MOCK"; 39 40 public MockServletRequest(HttpSession session, Locale locale) { 41 this.session = session; 42 this.locale = locale; 43 headers = new HashMap(); 44 Collection headersMultiple = new ArrayList(); 45 headersMultiple.add("header-value3-1"); 46 headersMultiple.add("header-value3-2"); 47 headersMultiple.add("header-value3-3"); 48 headers.put("header1", "header-value1"); 49 headers.put("header2", "header-value2"); 50 headers.put("header3", headersMultiple); 51 parameters = new HashMap(); 52 attributes = new HashMap(); 53 } 54 55 public void reset() { 56 parameters = new HashMap(); 57 attributes = new HashMap(); 58 } 59 60 public MockServletRequest(HttpSession session, Locale locale, boolean secure) { 61 this(session, locale); 62 this.secure = secure; 63 } 64 65 public String getAuthType() { 66 return DIGEST_AUTH; 67 } 68 69 public Cookie [] getCookies() { 70 return new Cookie [0]; 71 } 72 73 public long getDateHeader(String s) { 74 return 0; 75 } 76 77 public String getHeader(String s) { 78 return (String ) headers.get(s); 79 } 80 81 public Enumeration getHeaders(String s) { 82 if(headers.get(s) instanceof Collection) 83 return Collections.enumeration((Collection)headers.get(s)); 84 else { 85 Vector v = new Vector(); 86 v.add(headers.get(s)); 87 return v.elements(); 88 } 89 } 90 91 public Enumeration getHeaderNames() { 92 return Collections.enumeration(headers.keySet()); 93 } 94 95 public int getIntHeader(String s) { 96 return 0; 97 } 98 99 public String getMethod() { 100 return null; 101 } 102 103 public String getPathInfo() { 104 return null; 105 } 106 107 public String getPathTranslated() { 108 return null; 109 } 110 111 public String getContextPath() { 112 return null; 113 } 114 115 public String getQueryString() { 116 return null; 117 } 118 119 public String getRemoteUser() { 120 return remoteUser; 121 } 122 123 public void setRemoteUser(String remoteUser) { 124 this.remoteUser = remoteUser; 125 } 126 127 public boolean isUserInRole(String s) { 128 if("auth-user".equals(s) ) 129 return true; 130 else 131 return false; 132 } 133 134 public Principal getUserPrincipal() { 135 return new MockPrincipal(); 136 } 137 138 public String getRequestedSessionId() { 139 return null; 140 } 141 142 public String getRequestURI() { 143 return null; 144 } 145 146 public StringBuffer getRequestURL() { 147 return null; 148 } 149 150 public String getServletPath() { 151 return null; 152 } 153 154 public HttpSession getSession(boolean b) { 155 return session; 156 } 157 158 public HttpSession getSession() { 159 return session; 160 } 161 162 public boolean isRequestedSessionIdValid() { 163 return false; 164 } 165 166 public boolean isRequestedSessionIdFromCookie() { 167 return false; 168 } 169 170 public boolean isRequestedSessionIdFromURL() { 171 return false; 172 } 173 174 public boolean isRequestedSessionIdFromUrl() { 175 return false; 176 } 177 178 public Object getAttribute(String s) { 179 return attributes.get(s); 180 } 181 182 public Enumeration getAttributeNames() { 183 return new Vector(attributes.keySet()).elements(); 184 } 185 186 public String getCharacterEncoding() { 187 return enc; 188 } 189 190 public void setCharacterEncoding(String s) throws UnsupportedEncodingException { 191 enc = s; 192 } 193 194 public int getContentLength() { 195 return 0; 196 } 197 198 public String getContentType() { 199 return null; 200 } 201 202 public ServletInputStream getInputStream() throws IOException { 203 return null; 204 } 205 206 public String getParameter(String s) { 207 return (String ) parameters.get(s); 208 } 209 210 public void setParameter(String s, Object value) { 211 parameters.put(s, value); 212 } 213 214 public Enumeration getParameterNames() { 215 return new Vector(parameters.keySet()).elements(); 216 } 217 218 public String [] getParameterValues(String s) { 219 return new String [0]; 220 } 221 222 public Map getParameterMap() { 223 return parameters; 224 } 225 226 public String getProtocol() { 227 return null; 228 } 229 230 public String getScheme() { 231 return null; 232 } 233 234 public String getServerName() { 235 return null; 236 } 237 238 public int getServerPort() { 239 return 0; 240 } 241 242 public BufferedReader getReader() throws IOException { 243 return null; 244 } 245 246 public String getRemoteAddr() { 247 return null; 248 } 249 250 public String getRemoteHost() { 251 return null; 252 } 253 254 public void setAttribute(String s, Object o) { 255 attributes.put(s, o); 256 } 257 258 public void removeAttribute(String s) { 259 attributes.remove(s); 260 } 261 262 public Locale getLocale() { 263 return locale; 264 } 265 266 public Enumeration getLocales() { 267 System.out.println("MOCK get Locale : " + locale); 268 Vector v = new Vector(); 269 v.add(locale); 270 return v.elements(); 271 } 272 273 public boolean isSecure() { 274 return secure; 275 } 276 277 public RequestDispatcher getRequestDispatcher(String s) { 278 return null; 279 } 280 281 public String getRealPath(String s) { 282 return null; 283 } 284 285 public int getLocalPort(){ 287 return 0; 288 } 289 290 public String getLocalAddr(){ 291 return "127.0.0.1"; 292 } 293 294 public String getLocalName(){ 295 return "localhost"; 296 } 297 298 public int getRemotePort(){ 299 return 0; 300 } 301 } 302 | Popular Tags |