1 17 package javax.servlet.http; 18 19 import java.io.IOException ; 20 import java.io.PrintWriter ; 21 import java.io.OutputStreamWriter ; 22 import java.io.UnsupportedEncodingException ; 23 import java.lang.reflect.Method ; 24 import java.text.MessageFormat ; 25 import java.util.Enumeration ; 26 import java.util.Locale ; 27 import java.util.ResourceBundle ; 28 29 import javax.servlet.GenericServlet ; 30 import javax.servlet.ServletException ; 31 import javax.servlet.ServletOutputStream ; 32 import javax.servlet.ServletRequest ; 33 import javax.servlet.ServletResponse ; 34 35 36 79 80 81 82 public abstract class HttpServlet extends GenericServlet 83 implements java.io.Serializable 84 { 85 private static final String METHOD_DELETE = "DELETE"; 86 private static final String METHOD_HEAD = "HEAD"; 87 private static final String METHOD_GET = "GET"; 88 private static final String METHOD_OPTIONS = "OPTIONS"; 89 private static final String METHOD_POST = "POST"; 90 private static final String METHOD_PUT = "PUT"; 91 private static final String METHOD_TRACE = "TRACE"; 92 93 private static final String HEADER_IFMODSINCE = "If-Modified-Since"; 94 private static final String HEADER_LASTMOD = "Last-Modified"; 95 96 private static final String LSTRING_FILE = 97 "javax.servlet.http.LocalStrings"; 98 private static ResourceBundle lStrings = 99 ResourceBundle.getBundle(LSTRING_FILE); 100 101 102 103 104 108 109 public HttpServlet() { } 110 111 112 113 181 182 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 183 throws ServletException , IOException 184 { 185 String protocol = req.getProtocol(); 186 String msg = lStrings.getString("http.method_get_not_supported"); 187 if (protocol.endsWith("1.1")) { 188 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); 189 } else { 190 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); 191 } 192 } 193 194 195 196 197 198 222 223 protected long getLastModified(HttpServletRequest req) { 224 return -1; 225 } 226 227 228 229 230 265 266 protected void doHead(HttpServletRequest req, HttpServletResponse resp) 267 throws ServletException , IOException 268 { 269 NoBodyResponse response = new NoBodyResponse(resp); 270 271 doGet(req, response); 272 response.setContentLength(); 273 } 274 275 276 277 278 279 340 341 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 342 throws ServletException , IOException 343 { 344 String protocol = req.getProtocol(); 345 String msg = lStrings.getString("http.method_post_not_supported"); 346 if (protocol.endsWith("1.1")) { 347 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); 348 } else { 349 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); 350 } 351 } 352 353 354 355 356 400 401 protected void doPut(HttpServletRequest req, HttpServletResponse resp) 402 throws ServletException , IOException 403 { 404 String protocol = req.getProtocol(); 405 String msg = lStrings.getString("http.method_put_not_supported"); 406 if (protocol.endsWith("1.1")) { 407 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); 408 } else { 409 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); 410 } 411 } 412 413 414 415 416 454 455 protected void doDelete(HttpServletRequest req, 456 HttpServletResponse resp) 457 throws ServletException , IOException 458 { 459 String protocol = req.getProtocol(); 460 String msg = lStrings.getString("http.method_delete_not_supported"); 461 if (protocol.endsWith("1.1")) { 462 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); 463 } else { 464 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); 465 } 466 } 467 468 469 private static Method [] getAllDeclaredMethods(Class c) { 470 471 if (c.equals(javax.servlet.http.HttpServlet .class)) { 472 return null; 473 } 474 475 Method [] parentMethods = getAllDeclaredMethods(c.getSuperclass()); 476 Method [] thisMethods = c.getDeclaredMethods(); 477 478 if ((parentMethods != null) && (parentMethods.length > 0)) { 479 Method [] allMethods = 480 new Method [parentMethods.length + thisMethods.length]; 481 System.arraycopy(parentMethods, 0, allMethods, 0, 482 parentMethods.length); 483 System.arraycopy(thisMethods, 0, allMethods, parentMethods.length, 484 thisMethods.length); 485 486 thisMethods = allMethods; 487 } 488 489 return thisMethods; 490 } 491 492 493 527 528 protected void doOptions(HttpServletRequest req, HttpServletResponse resp) 529 throws ServletException , IOException 530 { 531 Method [] methods = getAllDeclaredMethods(this.getClass()); 532 533 boolean ALLOW_GET = false; 534 boolean ALLOW_HEAD = false; 535 boolean ALLOW_POST = false; 536 boolean ALLOW_PUT = false; 537 boolean ALLOW_DELETE = false; 538 boolean ALLOW_TRACE = true; 539 boolean ALLOW_OPTIONS = true; 540 541 for (int i=0; i<methods.length; i++) { 542 Method m = methods[i]; 543 544 if (m.getName().equals("doGet")) { 545 ALLOW_GET = true; 546 ALLOW_HEAD = true; 547 } 548 if (m.getName().equals("doPost")) 549 ALLOW_POST = true; 550 if (m.getName().equals("doPut")) 551 ALLOW_PUT = true; 552 if (m.getName().equals("doDelete")) 553 ALLOW_DELETE = true; 554 555 } 556 557 String allow = null; 558 if (ALLOW_GET) 559 if (allow==null) allow=METHOD_GET; 560 if (ALLOW_HEAD) 561 if (allow==null) allow=METHOD_HEAD; 562 else allow += ", " + METHOD_HEAD; 563 if (ALLOW_POST) 564 if (allow==null) allow=METHOD_POST; 565 else allow += ", " + METHOD_POST; 566 if (ALLOW_PUT) 567 if (allow==null) allow=METHOD_PUT; 568 else allow += ", " + METHOD_PUT; 569 if (ALLOW_DELETE) 570 if (allow==null) allow=METHOD_DELETE; 571 else allow += ", " + METHOD_DELETE; 572 if (ALLOW_TRACE) 573 if (allow==null) allow=METHOD_TRACE; 574 else allow += ", " + METHOD_TRACE; 575 if (ALLOW_OPTIONS) 576 if (allow==null) allow=METHOD_OPTIONS; 577 else allow += ", " + METHOD_OPTIONS; 578 579 resp.setHeader("Allow", allow); 580 } 581 582 583 584 585 613 614 protected void doTrace(HttpServletRequest req, HttpServletResponse resp) 615 throws ServletException , IOException 616 { 617 618 int responseLength; 619 620 String CRLF = "\r\n"; 621 String responseString = "TRACE "+ req.getRequestURI()+ 622 " " + req.getProtocol(); 623 624 Enumeration reqHeaderEnum = req.getHeaderNames(); 625 626 while( reqHeaderEnum.hasMoreElements() ) { 627 String headerName = (String )reqHeaderEnum.nextElement(); 628 responseString += CRLF + headerName + ": " + 629 req.getHeader(headerName); 630 } 631 632 responseString += CRLF; 633 634 responseLength = responseString.length(); 635 636 resp.setContentType("message/http"); 637 resp.setContentLength(responseLength); 638 ServletOutputStream out = resp.getOutputStream(); 639 out.print(responseString); 640 out.close(); 641 return; 642 } 643 644 645 646 647 648 679 680 protected void service(HttpServletRequest req, HttpServletResponse resp) 681 throws ServletException , IOException 682 { 683 String method = req.getMethod(); 684 685 if (method.equals(METHOD_GET)) { 686 long lastModified = getLastModified(req); 687 if (lastModified == -1) { 688 doGet(req, resp); 691 } else { 692 long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); 693 if (ifModifiedSince < (lastModified / 1000 * 1000)) { 694 maybeSetLastModified(resp, lastModified); 698 doGet(req, resp); 699 } else { 700 resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 701 } 702 } 703 704 } else if (method.equals(METHOD_HEAD)) { 705 long lastModified = getLastModified(req); 706 maybeSetLastModified(resp, lastModified); 707 doHead(req, resp); 708 709 } else if (method.equals(METHOD_POST)) { 710 doPost(req, resp); 711 712 } else if (method.equals(METHOD_PUT)) { 713 doPut(req, resp); 714 715 } else if (method.equals(METHOD_DELETE)) { 716 doDelete(req, resp); 717 718 } else if (method.equals(METHOD_OPTIONS)) { 719 doOptions(req,resp); 720 721 } else if (method.equals(METHOD_TRACE)) { 722 doTrace(req,resp); 723 724 } else { 725 730 String errMsg = lStrings.getString("http.method_not_implemented"); 731 Object [] errArgs = new Object [1]; 732 errArgs[0] = method; 733 errMsg = MessageFormat.format(errMsg, errArgs); 734 735 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg); 736 } 737 } 738 739 740 741 742 743 750 751 private void maybeSetLastModified(HttpServletResponse resp, 752 long lastModified) { 753 if (resp.containsHeader(HEADER_LASTMOD)) 754 return; 755 if (lastModified >= 0) 756 resp.setDateHeader(HEADER_LASTMOD, lastModified); 757 } 758 759 760 761 762 790 791 public void service(ServletRequest req, ServletResponse res) 792 throws ServletException , IOException 793 { 794 HttpServletRequest request; 795 HttpServletResponse response; 796 797 try { 798 request = (HttpServletRequest ) req; 799 response = (HttpServletResponse ) res; 800 } catch (ClassCastException e) { 801 throw new ServletException ("non-HTTP request or response"); 802 } 803 service(request, response); 804 } 805 } 806 807 808 809 810 816 class NoBodyResponse implements HttpServletResponse { 818 private HttpServletResponse resp; 819 private NoBodyOutputStream noBody; 820 private PrintWriter writer; 821 private boolean didSetContentLength; 822 823 NoBodyResponse(HttpServletResponse r) { 825 resp = r; 826 noBody = new NoBodyOutputStream(); 827 } 828 829 void setContentLength() { 831 if (!didSetContentLength) 832 resp.setContentLength(noBody.getContentLength()); 833 } 834 835 836 838 public void setContentLength(int len) { 839 resp.setContentLength(len); 840 didSetContentLength = true; 841 } 842 843 public void setCharacterEncoding(String charset) 844 { resp.setCharacterEncoding(charset); } 845 846 public void setContentType(String type) 847 { resp.setContentType(type); } 848 849 public String getContentType() 850 { return resp.getContentType(); } 851 852 public ServletOutputStream getOutputStream() throws IOException 853 { return noBody; } 854 855 public String getCharacterEncoding() 856 { return resp.getCharacterEncoding(); } 857 858 public PrintWriter getWriter() throws UnsupportedEncodingException 859 { 860 if (writer == null) { 861 OutputStreamWriter w; 862 863 w = new OutputStreamWriter (noBody, getCharacterEncoding()); 864 writer = new PrintWriter (w); 865 } 866 return writer; 867 } 868 869 public void setBufferSize(int size) throws IllegalStateException 870 { resp.setBufferSize(size); } 871 872 public int getBufferSize() 873 { return resp.getBufferSize(); } 874 875 public void reset() throws IllegalStateException 876 { resp.reset(); } 877 878 public void resetBuffer() throws IllegalStateException 879 { resp.resetBuffer(); } 880 881 public boolean isCommitted() 882 { return resp.isCommitted(); } 883 884 public void flushBuffer() throws IOException 885 { resp.flushBuffer(); } 886 887 public void setLocale(Locale loc) 888 { resp.setLocale(loc); } 889 890 public Locale getLocale() 891 { return resp.getLocale(); } 892 893 894 896 public void addCookie(Cookie cookie) 897 { resp.addCookie(cookie); } 898 899 public boolean containsHeader(String name) 900 { return resp.containsHeader(name); } 901 902 903 public void setStatus(int sc, String sm) 904 { resp.setStatus(sc, sm); } 905 906 public void setStatus(int sc) 907 { resp.setStatus(sc); } 908 909 public void setHeader(String name, String value) 910 { resp.setHeader(name, value); } 911 912 public void setIntHeader(String name, int value) 913 { resp.setIntHeader(name, value); } 914 915 public void setDateHeader(String name, long date) 916 { resp.setDateHeader(name, date); } 917 918 public void sendError(int sc, String msg) throws IOException 919 { resp.sendError(sc, msg); } 920 921 public void sendError(int sc) throws IOException 922 { resp.sendError(sc); } 923 924 public void sendRedirect(String location) throws IOException 925 { resp.sendRedirect(location); } 926 927 public String encodeURL(String url) 928 { return resp.encodeURL(url); } 929 930 public String encodeRedirectURL(String url) 931 { return resp.encodeRedirectURL(url); } 932 933 public void addHeader(String name, String value) 934 { resp.addHeader(name, value); } 935 936 public void addDateHeader(String name, long value) 937 { resp.addDateHeader(name, value); } 938 939 public void addIntHeader(String name, int value) 940 { resp.addIntHeader(name, value); } 941 942 943 944 945 950 951 952 public String encodeUrl(String url) 953 { return this.encodeURL(url); } 954 955 956 957 958 959 960 961 962 967 968 969 public String encodeRedirectUrl(String url) 970 { return this.encodeRedirectURL(url); } 971 972 } 973 974 975 976 977 978 979 980 983 984 class NoBodyOutputStream extends ServletOutputStream { 986 987 private static final String LSTRING_FILE = 988 "javax.servlet.http.LocalStrings"; 989 private static ResourceBundle lStrings = 990 ResourceBundle.getBundle(LSTRING_FILE); 991 992 private int contentLength = 0; 993 994 NoBodyOutputStream() {} 996 997 int getContentLength() { 999 return contentLength; 1000 } 1001 1002 public void write(int b) { 1003 contentLength++; 1004 } 1005 1006 public void write(byte buf[], int offset, int len) 1007 throws IOException 1008 { 1009 if (len >= 0) { 1010 contentLength += len; 1011 } else { 1012 1015 String msg = lStrings.getString("err.io.negativelength"); 1016 throw new IOException ("negative length"); 1017 } 1018 } 1019} 1020 | Popular Tags |