1 package servletunit; 2 3 import javax.servlet.RequestDispatcher ; 4 import javax.servlet.ServletContext ; 5 import javax.servlet.ServletInputStream ; 6 import javax.servlet.ServletRequest ; 7 import javax.servlet.http.Cookie ; 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpSession ; 10 import java.io.BufferedReader ; 11 import java.io.IOException ; 12 import java.io.File ; 13 import java.security.Principal ; 14 import java.util.*; 15 import java.text.SimpleDateFormat ; 16 import java.text.DateFormat ; 17 import java.text.ParseException ; 18 19 20 36 public class HttpServletRequestSimulator implements HttpServletRequest 37 { 38 private Hashtable attributes; 39 private String scheme; 40 private String protocol = "HTTP/1.1"; 41 private String requestURI; 42 private String requestURL; 43 private String contextPath = ""; 44 private String servletPath; 45 private String pathInfo; 46 private String queryString; 47 private String method; 48 private String contentType; 49 private Locale locale; 50 private Principal principal; 51 String remoteAddr; 52 String localAddr; 53 String remoteHost; 54 String localName; 55 int remotePort; 56 int localPort; 57 private String remoteUser; 58 private String userRole; 59 private String reqSessionId; 60 String authType; 61 String charEncoding; 62 private String serverName; 63 private int port; 64 65 private Hashtable parameters; 66 private Hashtable headers; 67 private Vector cookies; 68 69 private HttpSession session; 70 private ServletContext context; 71 72 76 77 public final static int GET = 0; 78 79 83 public final static int POST = 1; 84 85 89 public final static int PUT = 2; 90 91 public HttpServletRequestSimulator(ServletContext context) 92 { 93 scheme = "http"; 94 attributes = new Hashtable(); 95 parameters = new Hashtable(); 96 headers = new Hashtable(); 97 cookies = new Vector(); 98 this.context = context; 99 } 102 103 109 public void addParameter( String key, String value ) 110 { 111 if ((key != null) && (value != null)) 112 this.parameters.put( key, value ); 113 } 114 115 118 public void addParameter(String name, String [] values) { 119 if ((name != null) && (values != null)) 120 parameters.put(name,values); 121 } 122 123 135 public Map getParameterMap() { 136 return this.parameters; 137 } 138 139 165 public Object getAttribute(String s) 166 { 167 return attributes.get(s); 168 } 169 170 182 public Enumeration getAttributeNames() 183 { 184 return attributes.keys(); 185 } 186 187 205 public String getAuthType() 206 { 207 return authType; 208 } 209 210 220 public String getCharacterEncoding() 221 { 222 return charEncoding; 223 } 224 225 233 public int getContentLength() 234 { 235 return -1; 236 } 237 238 248 public String getContentType() 249 { 250 return contentType; 251 } 252 253 268 public String getContextPath() 269 { 270 return contextPath; 271 } 272 273 282 public void addCookie(Cookie cookie) { 283 cookies.addElement(cookie); 284 } 285 286 295 public void setCookies(Cookie [] cookies) { 296 for (int i = 0; i < cookies.length; i++) 297 this.cookies.addElement(cookies[i]); 298 } 299 300 312 public Cookie [] getCookies() 313 { 314 if (cookies.isEmpty()) 315 return null; 316 else { 317 Cookie [] cookieArray = new Cookie [cookies.size()]; 318 return (Cookie []) cookies.toArray(cookieArray); 319 } 320 } 321 322 332 public long getDateHeader(String name) 333 { 334 String s1 = getHeader(name); 335 if(s1 == null) 336 return -1L; 337 try 338 { 339 DateFormat dateFormat = new SimpleDateFormat ("EEE, d MMM yyyy HH:mm:ss z"); 340 return dateFormat.parse(s1).getTime(); 341 } 342 catch(ParseException exception) { 343 throw new IllegalArgumentException ("Cannot parse date: " + s1); 344 } 345 } 346 347 352 public void setDateHeader(String name, long millis) 353 { 354 String dateString = new SimpleDateFormat ("EEE, d MMM yyyy HH:mm:ss z").format(new Date(millis)); 355 setHeader(name, dateString); 356 } 357 358 376 public String getHeader(String s) 377 { 378 return (String ) headers.get(s); 379 } 380 381 401 public Enumeration getHeaderNames() 402 { 403 return headers.keys(); 404 } 405 406 409 public Enumeration getHeaders(String s) 410 { 411 throw new UnsupportedOperationException ("getHeaders operation is not supported!"); 412 } 413 414 417 public ServletInputStream getInputStream() throws IOException { 418 throw new UnsupportedOperationException ("getInputStream operation is not supported!"); 419 } 420 421 443 public int getIntHeader(String s) 444 { 445 Object header = headers.get(s); 446 if (header != null) { 447 try { 448 Integer intHeader = (Integer ) header; 449 return intHeader.intValue(); 450 } catch (ClassCastException e) { 451 throw new NumberFormatException ("header '" + s + "' cannot be converted to number format."); 452 } 453 } else 454 return -1; 455 } 456 457 470 public Locale getLocale() 471 { 472 if (this.locale == null) 473 return Locale.US; 474 else 475 return this.locale; 476 } 477 478 482 483 484 485 public Enumeration getLocales() 486 { 487 return java.util.Collections.enumeration(Collections.singleton(getLocale())); 488 } 489 490 502 public String getMethod() 503 { 504 if (method == null) 505 return "POST"; 506 else 507 return method; 508 } 509 510 538 public String getParameter( String s ) 539 { 540 if (s == null) 541 return null; 542 543 Object param = parameters.get(s); 544 if( null == param ) 545 return null; 546 if( param.getClass().isArray() ) 547 return ((String []) param)[0]; 548 return (String )param; 549 } 550 551 566 public Enumeration getParameterNames() 567 { 568 return parameters.keys(); 569 } 570 571 588 public String [] getParameterValues( String s ) 589 { 590 if (s == null) 591 return null; 592 Object param = parameters.get( s ); 593 if( null == param ) 594 return null; 595 else { 596 if (param.getClass().isArray()) { 597 return (String []) param; 598 } else { 599 return new String [] {(String ) param}; 600 } 601 } 602 } 603 604 625 public String getPathInfo() 626 { 627 return pathInfo; 628 } 629 630 633 public String getPathTranslated() 634 { 635 throw new UnsupportedOperationException ("getPathTranslated operation is not supported!"); 636 } 637 638 649 public String getProtocol() 650 { 651 return protocol; 652 } 653 654 667 public String getQueryString() 668 { 669 return queryString; 670 } 671 672 675 public BufferedReader getReader() throws IOException { 676 throw new UnsupportedOperationException ("getReader operation is not supported!"); 677 } 678 679 685 public String getRealPath(String path) 686 { 687 File contextDirectory = ((ServletContextSimulator) context).getContextDirectory(); 688 if ((contextDirectory == null) || (path == null)) 689 return null; 690 else 691 return (new File (contextDirectory, path)).getAbsolutePath(); 692 } 693 694 703 public String getRemoteAddr() { 704 return remoteAddr; 705 } 706 707 718 public String getRemoteHost() { 719 return remoteHost; 720 } 721 722 733 public String getRemoteUser() 734 { 735 return remoteUser; 736 } 737 738 767 public RequestDispatcher getRequestDispatcher( String url ) 768 { 769 return context.getRequestDispatcher(url); 770 } 771 772 790 public String getRequestedSessionId() 791 { 792 return reqSessionId; 793 } 794 795 819 public String getRequestURI() 820 { 821 return requestURI; 822 } 823 824 825 833 834 public StringBuffer getRequestURL() 835 { 836 return new StringBuffer (requestURL); 837 } 838 839 850 public String getScheme() 851 { 852 return scheme; 853 } 854 855 861 public String getServerName() { 862 return serverName; 863 } 864 865 869 public int getServerPort() { 870 return this.port; 871 } 872 873 876 public void setServerPort(int port) { 877 this.port = port; 878 } 879 880 896 public String getServletPath() 897 { 898 return servletPath; 899 } 900 901 912 public HttpSession getSession() 913 { 914 return getSession(true); 915 } 916 917 952 public HttpSession getSession(boolean b) 953 { 954 if ((session == null) && (b)) 955 this.session = new HttpSessionSimulator(context); 956 else if ((session != null) && (!((HttpSessionSimulator) session).isValid()) && (b)) 957 this.session = new HttpSessionSimulator(context); 958 if ((session != null) && (((HttpSessionSimulator) session).isValid())) 959 return this.session; 960 else 961 return null; 962 } 963 964 976 public Principal getUserPrincipal() 977 { 978 return this.principal; 979 } 980 981 990 public boolean isRequestedSessionIdFromCookie() 991 { 992 return true; 993 } 994 995 1002 public boolean isRequestedSessionIdFromUrl() 1003 { 1004 return isRequestedSessionIdFromURL(); 1005 } 1006 1007 1017 public boolean isRequestedSessionIdFromURL() 1018 { 1019 return false; 1020 } 1021 1022 1035 public boolean isRequestedSessionIdValid() 1036 { 1037 if (session != null) { 1038 try { 1039 session.getId(); 1040 return true; 1041 } catch (IllegalStateException e) { 1042 return false; 1043 } 1044 } else 1045 return false; 1046 } 1047 1048 1057 public boolean isSecure() 1058 { 1059 if(scheme==null){ 1060 return false; 1061 } else{ 1062 return scheme.equalsIgnoreCase("HTTPS"); 1063 } 1064 } 1065 1066 1079 public boolean isUserInRole(String s) 1080 { 1081 return s.equals(userRole); 1082 } 1083 1084 1087 public void setUserRole(String role) { 1088 this.userRole = role; 1089 } 1090 1091 1107 public void removeAttribute(String s) 1108 { 1109 attributes.remove(s); 1110 } 1111 1112 1133 public void setAttribute(String name, Object o) 1134 { 1135 if (o == null) 1136 attributes.remove(name); 1137 else 1138 attributes.put(name, o); 1139 } 1140 1141 1142 1145 public void setAuthType(String s) 1146 { 1147 authType = s; 1148 } 1149 1150 1153 public void setCharacterEncoding(String s) 1154 { 1155 charEncoding = s; 1156 } 1157 1158 1161 public void setContentType(String s) { 1162 contentType = s; 1163 } 1164 1165 1168 public void setHeader(String key, String value) 1169 { 1170 headers.put(key,value); 1171 } 1172 1173 1183 public void setMethod(int methodType) 1184 { 1185 switch (methodType) 1186 { 1187 case GET:method="GET";break; 1188 case PUT:method="PUT";break; 1189 case POST:method="POST";break; 1190 default:method="POST"; 1191 } 1192 } 1193 1194 1197 public void setParameterValue( String key, String [] value ) 1198 { 1199 parameters.put( key, value ); 1200 } 1201 1202 1205 public void setPathInfo(String s) 1206 { 1207 pathInfo = s; 1208 } 1209 1210 1213 public void setQueryString(String s) { 1214 this.queryString = s; 1215 } 1216 1217 1220 public void setRemoteUser(String remoteUser) 1221 { 1222 this.remoteUser = remoteUser; 1223 } 1224 1225 1228 public void setRemoteAddr(String remoteAddr) { 1229 this.remoteAddr = remoteAddr; 1230 } 1231 1232 1235 public void setRemoteHost(String remoteHost) { 1236 this.remoteHost = remoteHost; 1237 } 1238 1239 1242 public void setRequestedSessionId(String s) 1243 { 1244 reqSessionId = s; 1245 } 1246 1247 1250 public void setRequestURI(String requestURI) 1251 { 1252 this.requestURI = requestURI; 1253 } 1254 1255 1260 public void setRequestURL(String url) { 1261 1262 int queryIndex = url.lastIndexOf('?'); 1264 if (queryIndex < 0) 1265 queryIndex = url.length(); 1266 this.requestURL = url.substring(0,queryIndex); 1267 1268 if (queryIndex != url.length()) 1270 setQueryString(url.substring(queryIndex + 1)); 1271 1272 int schemeIndex = url.lastIndexOf("://"); 1274 setScheme(url.substring(0,schemeIndex)); 1275 1276 setRequestURI(url.substring(url.indexOf('/',schemeIndex + 3),queryIndex)); 1278 1279 int portIndex = url.indexOf(':',schemeIndex + 2); 1281 if (portIndex > 0) { 1282 setServerName(url.substring(schemeIndex + 3, portIndex)); 1283 setServerPort(Integer.parseInt(url.substring(portIndex + 1,url.indexOf('/',schemeIndex + 3)))); 1284 } else { 1285 setServerName(url.substring(schemeIndex + 3,url.indexOf('/',schemeIndex + 3))); 1286 if (isSecure()) 1287 setServerPort(443); 1288 else 1289 setServerPort(80); 1290 } 1291 } 1292 1293 1296 public void setScheme(String s) 1297 { 1298 scheme = s; 1299 } 1300 1301 1304 public void setServletPath(String s) 1305 { 1306 servletPath = s; 1307 } 1308 1309 1312 public void setServerName(String s) 1313 { 1314 serverName = s; 1315 } 1316 1317 1320 public void setContextPath(String s) 1321 { 1322 contextPath = s; 1323 } 1324 1325 1326 1329 public void setLocale(Locale locale) { 1330 this.locale = locale; 1331 } 1332 1333 1336 public void setUserPrincipal(Principal principal) { 1337 this.principal = principal; 1338 } 1339 1340 public int getRemotePort() { 1341 return remotePort; 1342 } 1343 1344 public void setRemotePort(int remotePort) { 1345 this.remotePort = remotePort; 1346 } 1347 1348 public String getLocalAddr() { 1349 return localAddr; 1350 } 1351 1352 public void setLocalAddr(String localAddr) { 1353 this.localAddr = localAddr; 1354 } 1355 1356 public String getLocalName() { 1357 return localName; 1358 } 1359 1360 public void setLocalName(String localName) { 1361 this.localName = localName; 1362 } 1363 1364 public int getLocalPort() { 1365 return localPort; 1366 } 1367 1368 public void setLocalPort(int localPort) { 1369 this.localPort = localPort; 1370 } 1371 1372 1373} 1374 | Popular Tags |