1 16 17 package org.apache.coyote.tomcat4; 18 19 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.io.PrintWriter ; 23 import java.net.MalformedURLException ; 24 import java.text.SimpleDateFormat ; 25 import java.util.ArrayList ; 26 import java.util.Date ; 27 import java.util.Enumeration ; 28 import java.util.Locale ; 29 import java.util.TimeZone ; 30 import java.util.Vector ; 31 32 import javax.servlet.ServletOutputStream ; 33 import javax.servlet.ServletResponse ; 34 import javax.servlet.http.Cookie ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 import javax.servlet.http.HttpSession ; 38 39 import org.apache.catalina.Connector; 40 import org.apache.catalina.Context; 41 import org.apache.catalina.HttpResponse; 42 import org.apache.catalina.util.CharsetMapper; 43 import org.apache.catalina.util.StringManager; 44 import org.apache.coyote.Response; 45 import org.apache.tomcat.util.buf.CharChunk; 46 import org.apache.tomcat.util.buf.UEncoder; 47 import org.apache.tomcat.util.http.MimeHeaders; 48 import org.apache.tomcat.util.http.ServerCookie; 49 import org.apache.tomcat.util.net.URL; 50 51 52 59 60 public class CoyoteResponse 61 implements HttpResponse, HttpServletResponse { 62 63 64 66 67 public CoyoteResponse() { 68 69 format.setTimeZone(TimeZone.getTimeZone("GMT")); 70 urlEncoder.addSafeCharacter('/'); 71 72 } 73 74 75 77 78 81 protected final SimpleDateFormat format = 82 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); 83 84 85 88 protected static final String info = 89 "org.apache.coyote.tomcat4.CoyoteResponse/1.0"; 90 91 92 95 protected static StringManager sm = 96 StringManager.getManager(Constants.Package); 97 98 99 101 102 105 protected Connector connector; 106 107 110 public Connector getConnector() { 111 return (this.connector); 112 } 113 114 119 public void setConnector(Connector connector) { 120 this.connector = connector; 121 } 122 123 124 127 protected Response coyoteResponse; 128 129 134 public void setCoyoteResponse(Response coyoteResponse) { 135 this.coyoteResponse = coyoteResponse; 136 outputBuffer.setResponse(coyoteResponse); 137 } 138 139 142 public Response getCoyoteResponse() { 143 return (coyoteResponse); 144 } 145 146 147 150 protected Context context = null; 151 152 155 public Context getContext() { 156 return (this.context); 157 } 158 159 167 public void setContext(Context context) { 168 this.context = context; 169 } 170 171 172 175 protected OutputBuffer outputBuffer = new OutputBuffer(); 176 177 178 181 protected CoyoteOutputStream outputStream = 182 new CoyoteOutputStream(outputBuffer); 183 184 185 188 protected CoyoteWriter writer = new CoyoteWriter(outputBuffer); 189 190 191 194 protected boolean appCommitted = false; 195 196 197 200 protected boolean included = false; 201 202 203 206 protected boolean error = false; 207 208 209 212 protected ArrayList cookies = new ArrayList (); 213 214 215 218 protected boolean usingOutputStream = false; 219 220 221 224 protected boolean usingWriter = false; 225 226 227 230 protected UEncoder urlEncoder = new UEncoder(); 231 232 233 236 protected CharChunk redirectURLCC = new CharChunk(); 237 238 239 241 242 246 public void recycle() { 247 248 outputBuffer.recycle(); 249 usingOutputStream = false; 250 usingWriter = false; 251 appCommitted = false; 252 included = false; 253 error = false; 254 cookies.clear(); 255 256 if (facade != null) { 257 facade.clear(); 258 facade = null; 259 } 260 261 writer.recycle(); 262 263 } 264 265 266 268 269 272 public int getContentCount() { 273 return outputBuffer.getContentWritten(); 274 } 275 276 277 282 public void setAppCommitted(boolean appCommitted) { 283 this.appCommitted = appCommitted; 284 } 285 286 287 290 public boolean isAppCommitted() { 291 return (this.appCommitted || isCommitted() || isSuspended()); 292 } 293 294 295 298 public boolean getIncluded() { 299 return included; 300 } 301 302 303 309 public void setIncluded(boolean included) { 310 this.included = included; 311 } 312 313 314 319 public String getInfo() { 320 return (info); 321 } 322 323 324 327 protected CoyoteRequest request = null; 328 329 332 public org.apache.catalina.Request getRequest() { 333 return (this.request); 334 } 335 336 341 public void setRequest(org.apache.catalina.Request request) { 342 this.request = (CoyoteRequest) request; 343 } 344 345 346 349 protected CoyoteResponseFacade facade = null; 350 351 355 public ServletResponse getResponse() { 356 if (facade == null) { 357 facade = new CoyoteResponseFacade(this); 358 } 359 return (facade); 360 } 361 362 363 366 public OutputStream getStream() { 367 return outputStream; 368 } 369 370 371 376 public void setStream(OutputStream stream) { 377 } 379 380 381 386 public void setSuspended(boolean suspended) { 387 outputBuffer.setSuspended(suspended); 388 } 389 390 391 394 public boolean isSuspended() { 395 return outputBuffer.isSuspended(); 396 } 397 398 399 402 public void setError() { 403 error = true; 404 } 405 406 407 410 public boolean isError() { 411 return error; 412 } 413 414 415 421 public ServletOutputStream createOutputStream() 422 throws IOException { 423 return outputStream; 425 } 426 427 428 434 public void finishResponse() 435 throws IOException { 436 try { 438 outputBuffer.close(); 439 } catch(IOException e) { 440 ; 441 } catch(Throwable t) { 442 t.printStackTrace(); 443 } 444 } 445 446 447 450 public int getContentLength() { 451 return (coyoteResponse.getContentLength()); 452 } 453 454 455 459 public String getContentType() { 460 return (coyoteResponse.getContentType()); 461 } 462 463 464 474 public PrintWriter getReporter() { 475 if (outputBuffer.isNew()) { 476 return writer; 477 } else { 478 return null; 479 } 480 } 481 482 483 485 486 491 public void flushBuffer() 492 throws IOException { 493 outputBuffer.flush(); 494 } 495 496 497 500 public int getBufferSize() { 501 return outputBuffer.getBufferSize(); 502 } 503 504 505 508 public String getCharacterEncoding() { 509 return (coyoteResponse.getCharacterEncoding()); 510 } 511 512 513 520 public ServletOutputStream getOutputStream() 521 throws IOException { 522 523 if (usingWriter) 524 throw new IllegalStateException 525 (sm.getString("coyoteResponse.getOutputStream.ise")); 526 527 usingOutputStream = true; 528 return outputStream; 529 530 } 531 532 533 536 public Locale getLocale() { 537 return (coyoteResponse.getLocale()); 538 } 539 540 541 548 public PrintWriter getWriter() 549 throws IOException { 550 551 if (usingOutputStream) 552 throw new IllegalStateException 553 (sm.getString("coyoteResponse.getWriter.ise")); 554 555 usingWriter = true; 556 return writer; 557 558 } 559 560 561 564 public boolean isCommitted() { 565 return (coyoteResponse.isCommitted()); 566 } 567 568 569 575 public void reset() { 576 577 if (included) 578 return; 580 coyoteResponse.reset(); 581 outputBuffer.reset(); 582 583 } 584 585 586 592 public void resetBuffer() { 593 594 if (isCommitted()) 595 throw new IllegalStateException 596 (sm.getString("coyoteResponse.resetBuffer.ise")); 597 598 outputBuffer.reset(); 599 600 } 601 602 603 611 public void setBufferSize(int size) { 612 613 if (isCommitted() || !outputBuffer.isNew()) 614 throw new IllegalStateException 615 (sm.getString("coyoteResponse.setBufferSize.ise")); 616 617 outputBuffer.setBufferSize(size); 618 619 } 620 621 622 627 public void setContentLength(int length) { 628 629 if (isCommitted()) 630 return; 631 632 if (included) 634 return; 635 636 coyoteResponse.setContentLength(length); 637 638 } 639 640 641 646 public void setContentType(String type) { 647 648 if (isCommitted()) 649 return; 650 651 if (included) 653 return; 654 655 coyoteResponse.setContentType(type); 656 657 } 658 659 660 666 public void setLocale(Locale locale) { 667 668 if (isCommitted()) 669 return; 670 671 if (included) 673 return; 674 675 coyoteResponse.setLocale(locale); 676 677 CharsetMapper cm = context.getCharsetMapper(); 679 String charset = cm.getCharset(locale); 680 681 if (charset != null) { 682 coyoteResponse.setCharacterEncoding(charset); 683 } 684 685 } 686 687 688 690 691 695 public Cookie [] getCookies() { 696 return ((Cookie []) cookies.toArray(new Cookie [cookies.size()])); 697 } 698 699 700 708 public String getHeader(String name) { 709 return coyoteResponse.getMimeHeaders().getHeader(name); 710 } 711 712 713 717 public String [] getHeaderNames() { 718 719 MimeHeaders headers = coyoteResponse.getMimeHeaders(); 720 int n = headers.size(); 721 String [] result = new String [n]; 722 for (int i = 0; i < n; i++) { 723 result[i] = headers.getName(i).toString(); 724 } 725 return result; 726 727 } 728 729 730 737 public String [] getHeaderValues(String name) { 738 739 Enumeration headerValues = coyoteResponse.getMimeHeaders().values(name); 740 Vector result = new Vector (); 741 while (headerValues.hasMoreElements()) { 742 result.addElement(headerValues.nextElement()); 743 } 744 String [] resultArray = new String [result.size()]; 745 result.copyInto(resultArray); 746 return resultArray; 747 748 } 749 750 751 755 public String getMessage() { 756 return coyoteResponse.getMessage(); 757 } 758 759 760 763 public int getStatus() { 764 return coyoteResponse.getStatus(); 765 } 766 767 768 775 public void reset(int status, String message) { 776 reset(); 777 setStatus(status, message); 778 } 779 780 781 783 784 790 public void addCookie(Cookie cookie) { 791 792 if (isCommitted()) 793 return; 794 795 if (included) 797 return; 798 799 cookies.add(cookie); 800 801 StringBuffer sb = new StringBuffer (); 802 ServerCookie.appendCookieValue 803 (sb, cookie.getVersion(), cookie.getName(), cookie.getValue(), 804 cookie.getPath(), cookie.getDomain(), cookie.getComment(), 805 cookie.getMaxAge(), cookie.getSecure()); 806 addHeader("Set-Cookie", sb.toString()); 810 811 } 812 813 814 820 public void addDateHeader(String name, long value) { 821 822 if (isCommitted()) 823 return; 824 825 if (included) 827 return; 828 829 addHeader(name, format.format(new Date (value))); 830 831 } 832 833 834 840 public void addHeader(String name, String value) { 841 842 if (isCommitted()) 843 return; 844 845 if (included) 847 return; 848 849 coyoteResponse.addHeader(name, value); 850 851 } 852 853 854 860 public void addIntHeader(String name, int value) { 861 862 if (isCommitted()) 863 return; 864 865 if (included) 867 return; 868 869 addHeader(name, "" + value); 870 871 } 872 873 874 879 public boolean containsHeader(String name) { 880 return coyoteResponse.containsHeader(name); 881 } 882 883 884 890 public String encodeRedirectURL(String url) { 891 892 if (isEncodeable(toAbsolute(url))) { 893 HttpServletRequest hreq = 894 (HttpServletRequest ) request.getRequest(); 895 return (toEncoded(url, hreq.getSession().getId())); 896 } else { 897 return (url); 898 } 899 900 } 901 902 903 912 public String encodeRedirectUrl(String url) { 913 return (encodeRedirectURL(url)); 914 } 915 916 917 923 public String encodeURL(String url) { 924 925 String absolute = toAbsolute(url); 926 if (isEncodeable(absolute)) { 927 HttpServletRequest hreq = 928 (HttpServletRequest ) request.getRequest(); 929 930 if (url.equalsIgnoreCase("")){ 932 url = absolute; 933 } 934 return (toEncoded(url, hreq.getSession().getId())); 935 } else { 936 return (url); 937 } 938 939 } 940 941 942 951 public String encodeUrl(String url) { 952 return (encodeURL(url)); 953 } 954 955 956 961 public void sendAcknowledgement() 962 throws IOException { 963 964 if (isCommitted()) 965 return; 966 967 if (included) 969 return; 970 971 coyoteResponse.acknowledge(); 972 973 } 974 975 976 986 public void sendError(int status) 987 throws IOException { 988 sendError(status, null); 989 } 990 991 992 1002 public void sendError(int status, String message) 1003 throws IOException { 1004 1005 if (isCommitted()) 1006 throw new IllegalStateException 1007 (sm.getString("coyoteResponse.sendError.ise")); 1008 1009 if (included) 1011 return; 1012 1013 setError(); 1014 1015 coyoteResponse.setStatus(status); 1016 coyoteResponse.setMessage(message); 1017 1018 resetBuffer(); 1020 1021 setSuspended(true); 1023 1024 } 1025 1026 1027 1036 public void sendRedirect(String location) 1037 throws IOException { 1038 1039 if (isCommitted()) 1040 throw new IllegalStateException 1041 (sm.getString("coyoteResponse.sendRedirect.ise")); 1042 1043 if (included) 1045 return; 1046 1047 resetBuffer(); 1049 1050 try { 1052 String absolute = toAbsolute(location); 1053 setStatus(SC_MOVED_TEMPORARILY); 1054 setHeader("Location", absolute); 1055 } catch (IllegalArgumentException e) { 1056 setStatus(SC_NOT_FOUND); 1057 } 1058 1059 setSuspended(true); 1061 1062 } 1063 1064 1065 1071 public void setDateHeader(String name, long value) { 1072 1073 if (isCommitted()) 1074 return; 1075 1076 if (included) 1078 return; 1079 1080 setHeader(name, format.format(new Date (value))); 1081 1082 } 1083 1084 1085 1091 public void setHeader(String name, String value) { 1092 1093 if (isCommitted()) 1094 return; 1095 1096 if (included) 1098 return; 1099 1100 coyoteResponse.setHeader(name, value); 1101 1102 } 1103 1104 1105 1111 public void setIntHeader(String name, int value) { 1112 1113 if (isCommitted()) 1114 return; 1115 1116 if (included) 1118 return; 1119 1120 setHeader(name, "" + value); 1121 1122 } 1123 1124 1125 1130 public void setStatus(int status) { 1131 setStatus(status, null); 1132 } 1133 1134 1135 1145 public void setStatus(int status, String message) { 1146 1147 if (isCommitted()) 1148 return; 1149 1150 if (included) 1152 return; 1153 1154 coyoteResponse.setStatus(status); 1155 coyoteResponse.setMessage(message); 1156 1157 } 1158 1159 1160 1162 1163 1176 protected boolean isEncodeable(String location) { 1177 1178 if (location == null) 1179 return (false); 1180 1181 if (location.startsWith("#")) 1183 return (false); 1184 1185 HttpServletRequest hreq = (HttpServletRequest ) request.getRequest(); 1187 HttpSession session = hreq.getSession(false); 1188 if (session == null) 1189 return (false); 1190 if (hreq.isRequestedSessionIdFromCookie()) 1191 return (false); 1192 1193 URL url = null; 1195 try { 1196 url = new URL(location); 1197 } catch (MalformedURLException e) { 1198 return (false); 1199 } 1200 1201 if (!hreq.getScheme().equalsIgnoreCase(url.getProtocol())) 1203 return (false); 1204 if (!hreq.getServerName().equalsIgnoreCase(url.getHost())) 1205 return (false); 1206 int serverPort = hreq.getServerPort(); 1207 if (serverPort == -1) { 1208 if ("https".equals(hreq.getScheme())) 1209 serverPort = 443; 1210 else 1211 serverPort = 80; 1212 } 1213 int urlPort = url.getPort(); 1214 if (urlPort == -1) { 1215 if ("https".equals(url.getProtocol())) 1216 urlPort = 443; 1217 else 1218 urlPort = 80; 1219 } 1220 if (serverPort != urlPort) 1221 return (false); 1222 1223 String contextPath = getContext().getPath(); 1224 if (contextPath != null) { 1225 String file = url.getFile(); 1226 if ((file == null) || !file.startsWith(contextPath)) 1227 return (false); 1228 if( file.indexOf(";jsessionid=" + session.getId()) >= 0 ) 1229 return (false); 1230 } 1231 1232 return (true); 1234 1235 } 1236 1237 1238 1248 private String toAbsolute(String location) { 1249 1250 if (location == null) 1251 return (location); 1252 1253 URL url = null; 1256 try { 1257 url = new URL(location); 1258 1259 if (url.getAuthority() == null) 1260 return location; 1261 1262 } catch (MalformedURLException e1) { 1263 HttpServletRequest hreq = 1264 (HttpServletRequest ) request.getRequest(); 1265 String requrl = request.getRequestURL().toString(); 1266 try { 1267 url = new URL(new URL(requrl), location); 1268 } catch (MalformedURLException e2) { 1269 throw new IllegalArgumentException (location); 1270 } 1271 } 1272 return (url.toExternalForm()); 1273 } 1274 1275 1276 1283 private String toEncoded(String url, String sessionId) { 1284 1285 if ((url == null) || (sessionId == null)) 1286 return (url); 1287 1288 String path = url; 1289 String query = ""; 1290 String anchor = ""; 1291 int question = url.indexOf('?'); 1292 if (question >= 0) { 1293 path = url.substring(0, question); 1294 query = url.substring(question); 1295 } 1296 int pound = path.indexOf('#'); 1297 if (pound >= 0) { 1298 anchor = path.substring(pound); 1299 path = path.substring(0, pound); 1300 } 1301 StringBuffer sb = new StringBuffer (path); 1302 if( sb.length() > 0 ) { sb.append(";jsessionid="); 1304 sb.append(sessionId); 1305 } 1306 sb.append(anchor); 1307 sb.append(query); 1308 return (sb.toString()); 1309 1310 } 1311 1312 1313} 1314
| Popular Tags
|