00001
00025 package org.openmobileis.test.mock.embedded;
00026
00027 import java.io.BufferedReader;
00028 import java.io.IOException;
00029 import java.io.InputStreamReader;
00030 import java.io.UnsupportedEncodingException;
00031 import java.security.Principal;
00032 import java.util.Enumeration;
00033 import java.util.Hashtable;
00034 import java.util.Locale;
00035 import java.util.Map;
00036 import java.util.Vector;
00037
00038 import javax.servlet.RequestDispatcher;
00039 import javax.servlet.ServletInputStream;
00040 import javax.servlet.http.Cookie;
00041 import javax.servlet.http.HttpServletRequest;
00042 import javax.servlet.http.HttpSession;
00043
00044 import org.openmobileis.common.context.SessionContext;
00045 import org.openmobileis.common.context.SessionContextManager;
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 public class MockHttpServletRequest implements HttpServletRequest {
00056 public String authType;
00057 public String contextPath;
00058 public Cookie[] cookies;
00059 public long dateHeader = 0;
00060 public String method;
00061 public String pathinfo;
00062 public String query;
00063 public String requestURI;
00064 public String servletPath;
00065 public String characterEncoding;
00066 public String contentType;
00067 public ServletInputStream inputStream;
00068 public Locale locale;
00069 public String protocol;
00070 public String realPath;
00071
00072 private Hashtable attributes = new Hashtable();
00073 private Hashtable parameters = new Hashtable();;
00074
00075 public Vector headerNames = new Vector();
00076 public Vector headerValues = new Vector();
00077
00078
00082 public MockHttpServletRequest() {
00083 authType = HttpServletRequest.BASIC_AUTH;
00084 contextPath = "";
00085 cookies = new Cookie[0];
00086 locale = Locale.US;
00087 }
00088
00089
00090
00091
00092 public String getAuthType() {
00093 return authType;
00094 }
00095
00096
00097
00098
00099 public String getContextPath() {
00100 return contextPath;
00101 }
00102
00103
00104
00105
00106 public Cookie[] getCookies() {
00107 return cookies;
00108 }
00109
00110
00111
00112
00113 public long getDateHeader(String arg0) {
00114 return dateHeader;
00115 }
00116
00117
00118
00119
00120 public String getHeader(String name) {
00121 int i = headerNames.indexOf(name.toLowerCase());
00122 if (i == -1)
00123 return null;
00124 return (String) headerValues.elementAt(i);
00125 }
00126
00127
00128
00129
00130 public Enumeration getHeaderNames() {
00131 return headerNames.elements();
00132 }
00133
00134
00135
00136
00137 public Enumeration getHeaders(String arg0) {
00138 return headerValues.elements();
00139 }
00140
00141
00142
00143
00144 public int getIntHeader(String name) {
00145 String val = getHeader(name);
00146 if (val == null)
00147 return -1;
00148
00149
00150
00151
00152 return Integer.parseInt(val);
00153 }
00154
00155
00156
00157
00158 public String getMethod() {
00159 return method;
00160 }
00161
00162
00163
00164
00165 public String getPathInfo() {
00166 return pathinfo;
00167 }
00168
00169
00170
00171
00172 public String getPathTranslated() {
00173 return getRealPath(getPathInfo());
00174 }
00175
00176
00177
00178
00179 public String getQueryString() {
00180 return query;
00181 }
00182
00183
00184
00185
00186 public String getRemoteUser() {
00187 return null;
00188 }
00189
00190
00191
00192
00193 public String getRequestURI() {
00194 return requestURI;
00195 }
00196
00197
00198
00199
00200 public StringBuffer getRequestURL() {
00201 return new StringBuffer("http://localhost:8080").append(getRequestURI());
00202 }
00203
00204
00205
00206
00207 public String getRequestedSessionId() {
00208 return this.getSession().getId();
00209 }
00210
00211
00212
00213
00214 public String getServletPath() {
00215 return servletPath;
00216 }
00217
00218
00219
00220
00221 public HttpSession getSession() {
00222 return this.getSession(true);
00223 }
00224
00225
00226
00227
00228 public HttpSession getSession(boolean arg0) {
00229
00230 SessionContext session = SessionContextManager.getManager().getSessionContext("OPENMOBILEISSESSION");
00231 if (session == null) {
00232 synchronized (SessionContextManager.getManager()) {
00233 session = SessionContextManager.getManager().getSessionContext("OPENMOBILEISSESSION");
00234 if (session == null) {
00235 session = SessionContextManager.getManager().createSessionContext("OPENMOBILEISSESSION");
00236 }
00237 }
00238 }
00239 SessionContextManager.getManager().joinSessionContext(session.getId());
00240 return SessionContextManager.getManager().getSessionContext();
00241 }
00242
00243
00244
00245
00246 public Principal getUserPrincipal() {
00247 return null;
00248 }
00249
00250
00251
00252
00253 public boolean isRequestedSessionIdFromCookie() {
00254 return false;
00255 }
00256
00257
00258
00259
00260 public boolean isRequestedSessionIdFromURL() {
00261 return false;
00262 }
00263
00264
00265
00266
00267 public boolean isRequestedSessionIdFromUrl() {
00268 return false;
00269 }
00270
00271
00272
00273
00274 public boolean isRequestedSessionIdValid() {
00275 return false;
00276 }
00277
00278
00279
00280
00281 public boolean isUserInRole(String arg0) {
00282 return false;
00283 }
00284
00285 public Object getAttribute(String name) {
00286 return attributes.get(name);
00287 }
00288
00289
00290
00291
00292 public void setAttribute(String name, Object o) {
00293 attributes.put(name, o);
00294 }
00295
00296
00297
00298
00299 public Enumeration getAttributeNames() {
00300 return attributes.keys();
00301 }
00302
00303
00304
00305
00306 public String getCharacterEncoding() {
00307 return characterEncoding;
00308 }
00309
00310
00311
00312
00313 public int getContentLength() {
00314 return 0;
00315 }
00316
00317
00318
00319
00320 public String getContentType() {
00321 return contentType;
00322 }
00323
00324
00325
00326
00327 public ServletInputStream getInputStream() throws IOException {
00328 return inputStream;
00329 }
00330
00331
00332
00333
00334 public String getLocalAddr() {
00335 return null;
00336 }
00337
00338
00339
00340
00341 public String getLocalName() {
00342 return null;
00343 }
00344
00345
00346
00347
00348 public int getLocalPort() {
00349 return 8080;
00350 }
00351
00352
00353
00354
00355 public Locale getLocale() {
00356 return locale;
00357 }
00358
00359
00360
00361
00362 public Enumeration getLocales() {
00363 return null;
00364 }
00365
00366
00367
00368
00369 public String getParameter(String name) {
00370 String[] params = getParameterValues(name);
00371 if (params == null || params.length == 0)
00372 return null;
00373
00374 return params[0];
00375 }
00376
00377
00378
00379
00380 public Map getParameterMap() {
00381 return (java.util.Map) parameters;
00382 }
00383
00384
00385
00386
00387 public Enumeration getParameterNames() {
00388 return parameters.keys();
00389 }
00390
00391
00392
00393
00394 public String[] getParameterValues(String name) {
00395 if (parameters == null)
00396 getParameterNames();
00397
00398 return (String[]) parameters.get(name);
00399 }
00400
00401 public void addParameter(String name, String value) {
00402 String[] params = this.getParameterValues(name);
00403 if (params == null) {
00404 params = new String[]{value};
00405 parameters.put(name, params);
00406 } else {
00407 String[] newparam = new String[params.length+1];
00408 System.arraycopy(params, 0, newparam, 0, params.length);
00409 newparam[params.length] = value;
00410 parameters.put(name, newparam);
00411 }
00412 }
00413
00414
00415
00416
00417 public String getProtocol() {
00418 return protocol;
00419 }
00420
00421
00422
00423
00424 public BufferedReader getReader() throws IOException {
00425 if (characterEncoding != null)
00426 try {
00427 return new BufferedReader(new InputStreamReader(inputStream, characterEncoding));
00428 } catch (UnsupportedEncodingException uee) {
00429 }
00430 return new BufferedReader(new InputStreamReader(inputStream));
00431 }
00432
00433
00434
00435
00436 public String getRealPath(String arg0) {
00437 return realPath;
00438 }
00439
00440
00441
00442
00443 public String getRemoteAddr() {
00444 return null;
00445 }
00446
00447
00448
00449
00450 public String getRemoteHost() {
00451 return "localhost";
00452 }
00453
00454
00455
00456
00457 public int getRemotePort() {
00458 return 0;
00459 }
00460
00461
00462
00463
00464 public RequestDispatcher getRequestDispatcher(String arg0) {
00465 return null;
00466 }
00467
00468
00469
00470
00471 public String getScheme() {
00472 return "http";
00473 }
00474
00475
00476
00477
00478 public String getServerName() {
00479 return "localhost";
00480 }
00481
00482
00483
00484
00485 public int getServerPort() {
00486 return 8080;
00487 }
00488
00489
00490
00491
00492 public boolean isSecure() {
00493 return false;
00494 }
00495
00496
00497
00498
00499 public void removeAttribute(String arg0) {
00500 }
00501
00502
00503
00504
00505 public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException {
00506 characterEncoding = arg0;
00507 }
00508
00509 }