1 16 17 package org.apache.coyote.tomcat4; 18 19 20 import java.io.BufferedReader ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 import java.io.UnsupportedEncodingException ; 25 import java.net.InetAddress ; 26 import java.net.Socket ; 27 import java.security.AccessController ; 28 import java.security.Principal ; 29 import java.security.PrivilegedAction ; 30 import java.text.ParseException ; 31 import java.text.SimpleDateFormat ; 32 import java.util.ArrayList ; 33 import java.util.Date ; 34 import java.util.Enumeration ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.Locale ; 38 import java.util.Map ; 39 import java.util.TreeMap ; 40 41 import javax.servlet.RequestDispatcher ; 42 import javax.servlet.ServletContext ; 43 import javax.servlet.ServletInputStream ; 44 import javax.servlet.ServletRequest ; 45 import javax.servlet.http.Cookie ; 46 import javax.servlet.http.HttpServletRequest ; 47 import javax.servlet.http.HttpServletResponse ; 48 import javax.servlet.http.HttpSession ; 49 50 import org.apache.catalina.Connector; 51 import org.apache.catalina.Context; 52 import org.apache.catalina.Globals; 53 import org.apache.catalina.HttpRequest; 54 import org.apache.catalina.Manager; 55 import org.apache.catalina.Realm; 56 import org.apache.catalina.Session; 57 import org.apache.catalina.Wrapper; 58 import org.apache.catalina.util.Enumerator; 59 import org.apache.catalina.util.ParameterMap; 60 import org.apache.catalina.util.RequestUtil; 61 import org.apache.catalina.util.StringManager; 62 import org.apache.catalina.util.StringParser; 63 import org.apache.coyote.ActionCode; 64 import org.apache.coyote.Request; 65 import org.apache.tomcat.util.buf.B2CConverter; 66 import org.apache.tomcat.util.http.Parameters; 67 68 75 76 public class CoyoteRequest 77 implements HttpRequest, HttpServletRequest { 78 79 80 82 83 protected class PrivilegedGetSession 84 implements PrivilegedAction { 85 86 private boolean create; 87 88 PrivilegedGetSession(boolean create) { 89 this.create = create; 90 } 91 92 public Object run() { 93 return doGetSession(create); 94 } 95 96 } 97 98 99 101 102 105 protected Request coyoteRequest; 106 107 112 public void setCoyoteRequest(Request coyoteRequest) { 113 this.coyoteRequest = coyoteRequest; 114 inputStream.setRequest(coyoteRequest); 115 } 116 117 120 public Request getCoyoteRequest() { 121 return (this.coyoteRequest); 122 } 123 124 125 127 128 131 protected static StringManager sm = 132 StringManager.getManager(Constants.Package); 133 134 135 138 protected Cookie [] cookies = null; 139 140 141 144 protected SimpleDateFormat formats[] = { 145 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US), 146 new SimpleDateFormat ("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US), 147 new SimpleDateFormat ("EEE MMMM d HH:mm:ss yyyy", Locale.US) 148 }; 149 150 151 154 protected static Locale defaultLocale = Locale.getDefault(); 155 156 157 160 protected HashMap attributes = new HashMap (); 161 162 163 166 protected ArrayList locales = new ArrayList (); 167 168 169 173 private transient HashMap notes = new HashMap (); 174 175 176 179 protected String authType = null; 180 181 182 188 protected BufferedReader reader = null; 189 190 191 194 protected CoyoteInputStream inputStream = new CoyoteInputStream(); 195 196 197 200 protected boolean usingInputStream = false; 201 202 203 206 protected boolean usingReader = false; 207 208 209 212 protected String contextPath = ""; 213 214 215 218 protected String pathInfo = null; 219 220 221 224 protected String servletPath = null; 225 226 227 230 protected Principal userPrincipal = null; 231 232 233 236 protected boolean sessionParsed = false; 237 238 239 242 protected boolean requestParametersParsed = false; 243 244 245 248 protected boolean secure = false; 249 250 251 254 protected static int CACHED_POST_LEN = 8192; 255 protected byte[] postData = null; 256 257 258 261 protected ParameterMap parameterMap = new ParameterMap(); 262 263 264 267 protected Session session = null; 268 269 270 273 protected boolean requestedSessionCookie = false; 274 275 276 279 protected String requestedSessionId = null; 280 281 282 285 protected boolean requestedSessionURL = false; 286 287 288 291 protected Socket socket = null; 292 293 294 297 protected boolean localesParsed = false; 298 299 300 303 private StringParser parser = new StringParser(); 304 305 306 309 protected String remoteAddr = null; 310 311 312 315 protected String remoteHost = null; 316 317 318 320 321 325 public void recycle() { 326 327 context = null; 328 wrapper = null; 329 330 authorization = null; 331 authType = null; 332 usingInputStream = false; 333 usingReader = false; 334 contextPath = ""; 335 pathInfo = null; 336 servletPath = null; 337 reader = null; 338 inputStream.recycle(); 339 userPrincipal = null; 340 sessionParsed = false; 341 authorization = null; 342 requestParametersParsed = false; 343 locales.clear(); 344 localesParsed = false; 345 secure = false; 346 remoteAddr = null; 347 remoteHost = null; 348 349 attributes.clear(); 350 notes.clear(); 351 cookies = null; 352 353 session = null; 354 requestedSessionCookie = false; 355 requestedSessionId = null; 356 requestedSessionURL = false; 357 358 parameterMap.setLocked(false); 359 parameterMap.clear(); 360 361 if (facade != null) { 362 facade.clear(); 363 facade = null; 364 } 365 366 } 367 368 369 371 372 375 protected String authorization = null; 376 377 380 public String getAuthorization() { 381 382 return (this.authorization); 383 384 } 385 386 391 public void setAuthorization(String authorization) { 392 this.authorization = authorization; 393 } 394 395 396 399 protected CoyoteConnector connector; 400 401 404 public Connector getConnector() { 405 return (this.connector); 406 } 407 408 413 public void setConnector(Connector connector) { 414 this.connector = (CoyoteConnector) connector; 415 } 416 417 420 protected Context context = null; 421 422 425 public Context getContext() { 426 return (this.context); 427 } 428 429 437 public void setContext(Context context) { 438 this.context = context; 439 } 440 441 442 445 protected static final String info = 446 "org.apache.coyote.catalina.CoyoteRequest/1.0"; 447 448 453 public String getInfo() { 454 return (info); 455 } 456 457 458 461 protected CoyoteRequestFacade facade = null; 462 463 467 public ServletRequest getRequest() { 468 if (facade == null) { 469 facade = new CoyoteRequestFacade(this); 470 } 471 return (facade); 472 } 473 474 475 478 protected org.apache.catalina.Response response = null; 479 480 483 public org.apache.catalina.Response getResponse() { 484 return (this.response); 485 } 486 487 492 public void setResponse(org.apache.catalina.Response response) { 493 this.response = response; 494 } 495 496 497 503 public Socket getSocket() { 504 return (socket); 505 } 506 507 512 public void setSocket(Socket socket) { 513 this.socket = socket; 514 remoteHost = null; 515 remoteAddr = null; 516 } 517 518 519 522 public InputStream getStream() { 523 return inputStream; 524 } 525 526 531 public void setStream(InputStream stream) { 532 } 534 535 536 539 protected B2CConverter URIConverter = null; 540 541 544 protected B2CConverter getURIConverter() { 545 return URIConverter; 546 } 547 548 553 protected void setURIConverter(B2CConverter URIConverter) { 554 this.URIConverter = URIConverter; 555 } 556 557 558 561 protected Wrapper wrapper = null; 562 563 566 public Wrapper getWrapper() { 567 return (this.wrapper); 568 } 569 570 577 public void setWrapper(Wrapper wrapper) { 578 this.wrapper = wrapper; 579 } 580 581 582 584 585 591 public ServletInputStream createInputStream() 592 throws IOException { 593 return inputStream; 594 } 595 596 597 603 public void finishRequest() throws IOException { 604 } 606 607 608 614 public Object getNote(String name) { 615 return (notes.get(name)); 616 } 617 618 619 623 public Iterator getNoteNames() { 624 return (notes.keySet().iterator()); 625 } 626 627 628 634 public void removeNote(String name) { 635 notes.remove(name); 636 } 637 638 639 646 public void setNote(String name, Object value) { 647 notes.put(name, value); 648 } 649 650 651 656 public void setContentLength(int length) { 657 } 659 660 661 668 public void setContentType(String type) { 669 } 671 672 673 678 public void setProtocol(String protocol) { 679 } 681 682 683 688 public void setRemoteAddr(String remoteAddr) { 689 } 691 692 693 699 public void setRemoteHost(String remoteHost) { 700 } 702 703 704 710 public void setScheme(String scheme) { 711 } 713 714 715 721 public void setSecure(boolean secure) { 722 this.secure = secure; 723 } 724 725 726 731 public void setServerName(String name) { 732 coyoteRequest.serverName().setString(name); 733 } 734 735 736 741 public void setServerPort(int port) { 742 coyoteRequest.setServerPort(port); 743 } 744 745 746 748 749 755 public Object getAttribute(String name) { 756 Object attr=attributes.get(name); 757 758 if(attr!=null) 759 return(attr); 760 761 attr = coyoteRequest.getAttribute(name); 762 if(attr != null) { 763 attributes.put(name, attr); 764 return attr; 765 } 766 if(Constants.SSL_CERTIFICATE_ATTR.equals(name)) { 768 coyoteRequest.action(ActionCode.ACTION_REQ_SSL_CERTIFICATE, null); 769 attr = getAttribute(Globals.CERTIFICATES_ATTR); 770 if(attr != null) 771 attributes.put(name, attr); 772 } 773 return attr; 774 } 775 776 780 public Enumeration getAttributeNames() { 781 return (new Enumerator(attributes.keySet())); 782 } 783 784 785 788 public String getCharacterEncoding() { 789 return (coyoteRequest.getCharacterEncoding()); 790 } 791 792 793 796 public int getContentLength() { 797 return (coyoteRequest.getContentLength()); 798 } 799 800 801 804 public String getContentType() { 805 return (coyoteRequest.getContentType()); 806 } 807 808 809 818 public ServletInputStream getInputStream() throws IOException { 819 820 if (usingReader) 821 throw new IllegalStateException 822 (sm.getString("coyoteRequest.getInputStream.ise")); 823 824 usingInputStream = true; 825 return inputStream; 826 827 } 828 829 830 836 public Locale getLocale() { 837 838 if (!localesParsed) 839 parseLocales(); 840 841 if (locales.size() > 0) { 842 return ((Locale ) locales.get(0)); 843 } else { 844 return (defaultLocale); 845 } 846 847 } 848 849 850 856 public Enumeration getLocales() { 857 858 if (!localesParsed) 859 parseLocales(); 860 861 if (locales.size() > 0) 862 return (new Enumerator(locales)); 863 ArrayList results = new ArrayList (); 864 results.add(defaultLocale); 865 return (new Enumerator(results)); 866 867 } 868 869 870 877 public String getParameter(String name) { 878 879 if (!requestParametersParsed) 880 parseRequestParameters(); 881 882 return coyoteRequest.getParameters().getParameter(name); 883 884 } 885 886 887 888 897 public Map getParameterMap() { 898 899 if (parameterMap.isLocked()) 900 return parameterMap; 901 902 Enumeration paramNames = getParameterNames(); 903 while (paramNames.hasMoreElements()) { 904 String name = paramNames.nextElement().toString(); 905 String [] values = getParameterValues(name); 906 parameterMap.put(name, values); 907 } 908 909 parameterMap.setLocked(true); 910 911 return parameterMap; 912 913 } 914 915 916 919 public Enumeration getParameterNames() { 920 921 if (!requestParametersParsed) 922 parseRequestParameters(); 923 924 return coyoteRequest.getParameters().getParameterNames(); 925 926 } 927 928 929 935 public String [] getParameterValues(String name) { 936 937 if (!requestParametersParsed) 938 parseRequestParameters(); 939 940 return coyoteRequest.getParameters().getParameterValues(name); 941 942 } 943 944 945 948 public String getProtocol() { 949 return coyoteRequest.protocol().toString(); 950 } 951 952 953 962 public BufferedReader getReader() throws IOException { 963 964 if (usingInputStream) 965 throw new IllegalStateException 966 (sm.getString("coyoteRequest.getReader.ise")); 967 968 usingReader = true; 969 if (reader == null) { 970 String encoding = getCharacterEncoding(); 971 if (encoding == null) { 972 encoding = 973 org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING; 974 } 975 InputStreamReader r = new InputStreamReader (inputStream, encoding); 976 reader = new BufferedReader (r); 977 } 978 return (reader); 979 980 } 981 982 983 991 public String getRealPath(String path) { 992 993 if (context == null) 994 return (null); 995 ServletContext servletContext = context.getServletContext(); 996 if (servletContext == null) 997 return (null); 998 else { 999 try { 1000 return (servletContext.getRealPath(path)); 1001 } catch (IllegalArgumentException e) { 1002 return (null); 1003 } 1004 } 1005 1006 } 1007 1008 1009 1012 public String getRemoteAddr() { 1013 if (remoteAddr == null) { 1014 if (socket != null) { 1015 InetAddress inet = socket.getInetAddress(); 1016 remoteAddr = inet.getHostAddress(); 1017 } else { 1018 coyoteRequest.action 1019 (ActionCode.ACTION_REQ_HOST_ADDR_ATTRIBUTE, coyoteRequest); 1020 remoteAddr = coyoteRequest.remoteAddr().toString(); 1021 } 1022 } 1023 return remoteAddr; 1024 } 1025 1026 1027 1030 public String getRemoteHost() { 1031 if (remoteHost == null) { 1032 if (!connector.getEnableLookups()) { 1033 remoteHost = getRemoteAddr(); 1034 } else if (socket != null) { 1035 InetAddress inet = socket.getInetAddress(); 1036 remoteHost = inet.getHostName(); 1037 } else { 1038 coyoteRequest.action 1039 (ActionCode.ACTION_REQ_HOST_ATTRIBUTE, coyoteRequest); 1040 remoteHost = coyoteRequest.remoteHost().toString(); 1041 } 1042 } 1043 return remoteHost; 1044 } 1045 1046 1047 1053 public RequestDispatcher getRequestDispatcher(String path) { 1054 1055 if (context == null) 1056 return (null); 1057 1058 if (path == null) 1060 return (null); 1061 else if (path.startsWith("/")) 1062 return (context.getServletContext().getRequestDispatcher(path)); 1063 1064 String servletPath = (String ) getAttribute(Globals.SERVLET_PATH_ATTR); 1066 if (servletPath == null) 1067 servletPath = getServletPath(); 1068 1069 String pathInfo = getPathInfo(); 1071 String requestPath = null; 1072 1073 if (pathInfo == null) { 1074 requestPath = servletPath; 1075 } else { 1076 requestPath = servletPath + pathInfo; 1077 } 1078 1079 int pos = requestPath.lastIndexOf('/'); 1080 String relative = null; 1081 if (pos >= 0) { 1082 relative = RequestUtil.normalize 1083 (requestPath.substring(0, pos + 1) + path); 1084 } else { 1085 relative = RequestUtil.normalize(requestPath + path); 1086 } 1087 1088 return (context.getServletContext().getRequestDispatcher(relative)); 1089 1090 } 1091 1092 1093 1096 public String getScheme() { 1097 return (coyoteRequest.scheme().toString()); 1098 } 1099 1100 1101 1104 public String getServerName() { 1105 return (coyoteRequest.serverName().toString()); 1106 } 1107 1108 1109 1112 public int getServerPort() { 1113 return (coyoteRequest.getServerPort()); 1114 } 1115 1116 1117 1120 public boolean isSecure() { 1121 return (secure); 1122 } 1123 1124 1125 1130 public void removeAttribute(String name) { 1131 attributes.remove(name); 1132 } 1133 1134 1135 1141 public void setAttribute(String name, Object value) { 1142 1143 if (name == null) 1145 throw new IllegalArgumentException 1146 (sm.getString("coyoteRequest.setAttribute.namenull")); 1147 1148 if (value == null) { 1150 removeAttribute(name); 1151 return; 1152 } 1153 1154 attributes.put(name, value); 1155 1156 } 1157 1158 1159 1171 public void setCharacterEncoding(String enc) 1172 throws UnsupportedEncodingException { 1173 1174 byte buffer[] = new byte[1]; 1176 buffer[0] = (byte) 'a'; 1177 String dummy = new String (buffer, enc); 1178 1179 coyoteRequest.setCharacterEncoding(enc); 1181 1182 } 1183 1184 1185 1187 1188 1193 public void addCookie(Cookie cookie) { 1194 1195 1197 int size = 0; 1198 if (cookies != null) { 1199 size = cookies.length; 1200 } 1201 1202 Cookie [] newCookies = new Cookie [size + 1]; 1203 for (int i = 0; i < size; i++) { 1204 newCookies[i] = cookies[i]; 1205 } 1206 newCookies[size] = cookie; 1207 1208 cookies = newCookies; 1209 1210 } 1211 1212 1213 1219 public void addHeader(String name, String value) { 1220 } 1222 1223 1224 1230 public void addLocale(Locale locale) { 1231 locales.add(locale); 1232 } 1233 1234 1235 1243 public void addParameter(String name, String values[]) { 1244 coyoteRequest.getParameters().addParameterValues(name, values); 1245 } 1246 1247 1248 1251 public void clearCookies() { 1252 cookies = null; 1253 } 1254 1255 1256 1259 public void clearHeaders() { 1260 } 1262 1263 1264 1267 public void clearLocales() { 1268 locales.clear(); 1269 } 1270 1271 1272 1275 public void clearParameters() { 1276 } 1278 1279 1280 1287 public void setAuthType(String type) { 1288 this.authType = type; 1289 } 1290 1291 1292 1299 public void setContextPath(String path) { 1300 1301 if (path == null) { 1302 this.contextPath = ""; 1303 } else { 1304 this.contextPath = path; 1305 } 1306 1307 } 1308 1309 1310 1315 public void setMethod(String method) { 1316 } 1318 1319 1320 1326 public void setQueryString(String query) { 1327 } 1329 1330 1331 1338 public void setPathInfo(String path) { 1339 this.pathInfo = path; 1340 } 1341 1342 1343 1350 public void setRequestedSessionCookie(boolean flag) { 1351 1352 this.requestedSessionCookie = flag; 1353 1354 } 1355 1356 1357 1363 public void setRequestedSessionId(String id) { 1364 1365 this.requestedSessionId = id; 1366 1367 } 1368 1369 1370 1377 public void setRequestedSessionURL(boolean flag) { 1378 1379 this.requestedSessionURL = flag; 1380 1381 } 1382 1383 1384 1390 public void setRequestURI(String uri) { 1391 } 1393 1394 1395 1400 public void setDecodedRequestURI(String uri) { 1401 } 1403 1404 1405 1410 public String getDecodedRequestURI() { 1411 return (coyoteRequest.decodedURI().toString()); 1412 } 1413 1414 1415 1422 public void setServletPath(String path) { 1423 this.servletPath = path; 1424 } 1425 1426 1427 1434 public void setUserPrincipal(Principal principal) { 1435 this.userPrincipal = principal; 1436 } 1437 1438 1439 1441 1442 1445 public String getAuthType() { 1446 return (authType); 1447 } 1448 1449 1450 1454 public String getContextPath() { 1455 return (contextPath); 1456 } 1457 1458 1459 1462 public Cookie [] getCookies() { 1463 1464 return cookies; 1465 1466 } 1467 1468 1469 1472 public void setCookies(Cookie [] cookies) { 1473 1474 this.cookies = cookies; 1475 1476 } 1477 1478 1479 1488 public long getDateHeader(String name) { 1489 1490 String value = getHeader(name); 1491 if (value == null) 1492 return (-1L); 1493 1494 value += " "; 1497 1498 for (int i = 0; i < formats.length; i++) { 1500 try { 1501 Date date = formats[i].parse(value); 1502 return (date.getTime()); 1503 } catch (ParseException e) { 1504 ; 1505 } 1506 } 1507 throw new IllegalArgumentException (value); 1508 1509 } 1510 1511 1512 1518 public String getHeader(String name) { 1519 return coyoteRequest.getHeader(name); 1520 } 1521 1522 1523 1529 public Enumeration getHeaders(String name) { 1530 return coyoteRequest.getMimeHeaders().values(name); 1531 } 1532 1533 1534 1537 public Enumeration getHeaderNames() { 1538 return coyoteRequest.getMimeHeaders().names(); 1539 } 1540 1541 1542 1551 public int getIntHeader(String name) { 1552 1553 String value = getHeader(name); 1554 if (value == null) { 1555 return (-1); 1556 } else { 1557 return (Integer.parseInt(value)); 1558 } 1559 1560 } 1561 1562 1563 1566 public String getMethod() { 1567 return coyoteRequest.method().toString(); 1568 } 1569 1570 1571 1574 public String getPathInfo() { 1575 return (pathInfo); 1576 } 1577 1578 1579 1583 public String getPathTranslated() { 1584 1585 if (context == null) 1586 return (null); 1587 1588 if (pathInfo == null) { 1589 return (null); 1590 } else { 1591 return (context.getServletContext().getRealPath(pathInfo)); 1592 } 1593 1594 } 1595 1596 1597 1600 public String getQueryString() { 1601 String queryString = coyoteRequest.queryString().toString(); 1602 if (queryString.equals("")) { 1603 return (null); 1604 } else { 1605 return queryString; 1606 } 1607 } 1608 1609 1610 1614 public String getRemoteUser() { 1615 1616 if (userPrincipal != null) { 1617 return (userPrincipal.getName()); 1618 } else { 1619 return (null); 1620 } 1621 1622 } 1623 1624 1625 1628 public String getRequestedSessionId() { 1629 return (requestedSessionId); 1630 } 1631 1632 1633 1636 public String getRequestURI() { 1637 return coyoteRequest.requestURI().toString(); 1638 } 1639 1640 1641 1657 public StringBuffer getRequestURL() { 1658 1659 StringBuffer url = new StringBuffer (); 1660 String scheme = getScheme(); 1661 int port = getServerPort(); 1662 if (port < 0) 1663 port = 80; 1665 url.append(scheme); 1666 url.append("://"); 1667 url.append(getServerName()); 1668 if ((scheme.equals("http") && (port != 80)) 1669 || (scheme.equals("https") && (port != 443))) { 1670 url.append(':'); 1671 url.append(port); 1672 } 1673 url.append(getRequestURI()); 1674 1675 return (url); 1676 1677 } 1678 1679 1680 1684 public String getServletPath() { 1685 return (servletPath); 1686 } 1687 1688 1689 1693 public HttpSession getSession() { 1694 return (getSession(true)); 1695 } 1696 1697 1698 1704 public HttpSession getSession(boolean create) { 1705 1706 if (System.getSecurityManager() != null) { 1707 PrivilegedGetSession dp = new PrivilegedGetSession(create); 1708 return (HttpSession ) AccessController.doPrivileged(dp); 1709 } 1710 return doGetSession(create); 1711 1712 } 1713 1714 1715 1719 public boolean isRequestedSessionIdFromCookie() { 1720 1721 if (requestedSessionId != null) 1722 return (requestedSessionCookie); 1723 else 1724 return (false); 1725 1726 } 1727 1728 1729 1733 public boolean isRequestedSessionIdFromURL() { 1734 1735 if (requestedSessionId != null) 1736 return (requestedSessionURL); 1737 else 1738 return (false); 1739 1740 } 1741 1742 1743 1750 public boolean isRequestedSessionIdFromUrl() { 1751 return (isRequestedSessionIdFromURL()); 1752 } 1753 1754 1755 1759 public boolean isRequestedSessionIdValid() { 1760 1761 if (requestedSessionId == null) 1762 return (false); 1763 if (context == null) 1764 return (false); 1765 Manager manager = context.getManager(); 1766 if (manager == null) 1767 return (false); 1768 Session session = null; 1769 try { 1770 session = manager.findSession(requestedSessionId); 1771 } catch (IOException e) { 1772 session = null; 1773 } 1774 if ((session != null) && session.isValid()) 1775 return (true); 1776 else 1777 return (false); 1778 1779 } 1780 1781 1782 1788 public boolean isUserInRole(String role) { 1789 1790 if (userPrincipal == null) 1792 return (false); 1793 1794 if (context == null) 1796 return (false); 1797 Realm realm = context.getRealm(); 1798 if (realm == null) 1799 return (false); 1800 1801 if (wrapper != null) { 1803 String realRole = wrapper.findSecurityReference(role); 1804 if ((realRole != null) && 1805 realm.hasRole(userPrincipal, realRole)) 1806 return (true); 1807 } 1808 1809 return (realm.hasRole(userPrincipal, role)); 1811 1812 } 1813 1814 1815 1818 public Principal getUserPrincipal() { 1819 return (userPrincipal); 1820 } 1821 1822 1823 1825 1826 protected HttpSession doGetSession(boolean create) { 1827 1828 if (context == null) 1830 return (null); 1831 1832 if ((session != null) && !session.isValid()) 1834 session = null; 1835 if (session != null) 1836 return (session.getSession()); 1837 1838 Manager manager = null; 1840 if (context != null) 1841 manager = context.getManager(); 1842 if (manager == null) 1843 return (null); if (requestedSessionId != null) { 1845 try { 1846 session = manager.findSession(requestedSessionId); 1847 } catch (IOException e) { 1848 session = null; 1849 } 1850 if ((session != null) && !session.isValid()) 1851 session = null; 1852 if (session != null) { 1853 return (session.getSession()); 1854 } 1855 } 1856 1857 if (!create) 1859 return (null); 1860 if ((context != null) && (response != null) && 1861 context.getCookies() && 1862 response.getResponse().isCommitted()) { 1863 throw new IllegalStateException 1864 (sm.getString("coyoteRequest.sessionCreateCommitted")); 1865 } 1866 1867 session = manager.createSession(); 1868 1869 if ((session != null) && (getContext() != null) 1871 && getContext().getCookies()) { 1872 Cookie cookie = new Cookie (Globals.SESSION_COOKIE_NAME, 1873 session.getId()); 1874 cookie.setMaxAge(-1); 1875 String contextPath = null; 1876 if (context != null) 1877 contextPath = context.getPath(); 1878 if ((contextPath != null) && (contextPath.length() > 0)) 1879 cookie.setPath(contextPath); 1880 else 1881 cookie.setPath("/"); 1882 if (isSecure()) 1883 cookie.setSecure(true); 1884 ((HttpServletResponse ) response).addCookie(cookie); 1885 } 1886 1887 if (session != null) 1888 return (session.getSession()); 1889 else 1890 return (null); 1891 1892 } 1893 1894 1895 1898 protected void parseRequestParameters() { 1899 1900 requestParametersParsed = true; 1901 1902 Parameters parameters = coyoteRequest.getParameters(); 1903 1904 String enc = coyoteRequest.getCharacterEncoding(); 1905 boolean useBodyEncodingForURI = connector.getUseBodyEncodingForURI(); 1906 if (enc != null) { 1907 parameters.setEncoding(enc); 1908 if (useBodyEncodingForURI) { 1909 parameters.setQueryStringEncoding(enc); 1910 } 1911 } else { 1912 parameters.setEncoding 1913 (org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING); 1914 if (useBodyEncodingForURI) { 1915 parameters.setQueryStringEncoding 1916 (org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING); 1917 } 1918 } 1919 1920 parameters.handleQueryParameters(); 1921 1922 if (usingInputStream || usingReader) 1923 return; 1924 1925 if (!getMethod().equalsIgnoreCase("POST")) 1926 return; 1927 1928 String contentType = getContentType(); 1929 if (contentType == null) 1930 contentType = ""; 1931 int semicolon = contentType.indexOf(';'); 1932 if (semicolon >= 0) { 1933 contentType = contentType.substring(0, semicolon).trim(); 1934 } else { 1935 contentType = contentType.trim(); 1936 } 1937 if (!("application/x-www-form-urlencoded".equals(contentType))) 1938 return; 1939 1940 int len = getContentLength(); 1941 1942 if (len > 0) { 1943 try { 1944 byte[] formData = null; 1945 if (len < CACHED_POST_LEN) { 1946 if (postData == null) 1947 postData = new byte[CACHED_POST_LEN]; 1948 formData = postData; 1949 } else { 1950 formData = new byte[len]; 1951 } 1952 int actualLen = readPostBody(formData, len); 1953 if (actualLen == len) { 1954 parameters.processParameters(formData, 0, len); 1955 } 1956 } catch (Throwable t) { 1957 ; } 1959 } 1960 1961 } 1962 1963 1964 1967 protected int readPostBody(byte body[], int len) 1968 throws IOException { 1969 1970 int offset = 0; 1971 do { 1972 int inputLen = getStream().read(body, offset, len - offset); 1973 if (inputLen <= 0) { 1974 return offset; 1975 } 1976 offset += inputLen; 1977 } while ((len - offset) > 0); 1978 return len; 1979 1980 } 1981 1982 1983 1986 protected void parseLocales() { 1987 1988 localesParsed = true; 1989 1990 Enumeration values = getHeaders("accept-language"); 1991 1992 while (values.hasMoreElements()) { 1993 String value = values.nextElement().toString(); 1994 parseLocalesHeader(value); 1995 } 1996 1997 } 1998 1999 2000 2003 protected void parseLocalesHeader(String value) { 2004 2005 TreeMap locales = new TreeMap (); 2010 2011 int white = value.indexOf(' '); 2013 if (white < 0) 2014 white = value.indexOf('\t'); 2015 if (white >= 0) { 2016 StringBuffer sb = new StringBuffer (); 2017 int len = value.length(); 2018 for (int i = 0; i < len; i++) { 2019 char ch = value.charAt(i); 2020 if ((ch != ' ') && (ch != '\t')) 2021 sb.append(ch); 2022 } 2023 value = sb.toString(); 2024 } 2025 2026 parser.setString(value); int length = parser.getLength(); 2029 while (true) { 2030 2031 int start = parser.getIndex(); 2033 if (start >= length) 2034 break; 2035 int end = parser.findChar(','); 2036 String entry = parser.extract(start, end).trim(); 2037 parser.advance(); 2039 double quality = 1.0; 2041 int semi = entry.indexOf(";q="); 2042 if (semi >= 0) { 2043 try { 2044 quality = Double.parseDouble(entry.substring(semi + 3)); 2045 } catch (NumberFormatException e) { 2046 quality = 0.0; 2047 } 2048 entry = entry.substring(0, semi); 2049 } 2050 2051 if (quality < 0.00005) 2053 continue; if ("*".equals(entry)) 2055 continue; 2057 String language = null; 2059 String country = null; 2060 String variant = null; 2061 int dash = entry.indexOf('-'); 2062 if (dash < 0) { 2063 language = entry; 2064 country = ""; 2065 variant = ""; 2066 } else { 2067 language = entry.substring(0, dash); 2068 country = entry.substring(dash + 1); 2069 int vDash = country.indexOf('-'); 2070 if (vDash > 0) { 2071 String cTemp = country.substring(0, vDash); 2072 variant = country.substring(vDash + 1); 2073 country = cTemp; 2074 } else { 2075 variant = ""; 2076 } 2077 } 2078 2079 Locale locale = new Locale (language, country, variant); 2081 Double key = new Double (-quality); ArrayList values = (ArrayList ) locales.get(key); 2083 if (values == null) { 2084 values = new ArrayList (); 2085 locales.put(key, values); 2086 } 2087 values.add(locale); 2088 2089 } 2090 2091 Iterator keys = locales.keySet().iterator(); 2094 while (keys.hasNext()) { 2095 Double key = (Double ) keys.next(); 2096 ArrayList list = (ArrayList ) locales.get(key); 2097 Iterator values = list.iterator(); 2098 while (values.hasNext()) { 2099 Locale locale = (Locale ) values.next(); 2100 addLocale(locale); 2101 } 2102 } 2103 2104 } 2105 2106 2107} 2108
| Popular Tags
|