home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-tomcat-addon-1.4.9-installer.exe / servlet-api.jar / javax / servlet / http / HttpServlet.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-08-28  |  7.9 KB  |  275 lines

  1. package javax.servlet.http;
  2.  
  3. import java.io.IOException;
  4. import java.io.Serializable;
  5. import java.lang.reflect.Method;
  6. import java.text.MessageFormat;
  7. import java.util.Enumeration;
  8. import java.util.ResourceBundle;
  9. import javax.servlet.GenericServlet;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.ServletOutputStream;
  12. import javax.servlet.ServletRequest;
  13. import javax.servlet.ServletResponse;
  14.  
  15. public abstract class HttpServlet extends GenericServlet implements Serializable {
  16.    private static final String METHOD_DELETE = "DELETE";
  17.    private static final String METHOD_HEAD = "HEAD";
  18.    private static final String METHOD_GET = "GET";
  19.    private static final String METHOD_OPTIONS = "OPTIONS";
  20.    private static final String METHOD_POST = "POST";
  21.    private static final String METHOD_PUT = "PUT";
  22.    private static final String METHOD_TRACE = "TRACE";
  23.    private static final String HEADER_IFMODSINCE = "If-Modified-Since";
  24.    private static final String HEADER_LASTMOD = "Last-Modified";
  25.    private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings";
  26.    private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings");
  27.    // $FF: synthetic field
  28.    static Class class$javax$servlet$http$HttpServlet;
  29.  
  30.    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  31.       String protocol = req.getProtocol();
  32.       String msg = lStrings.getString("http.method_get_not_supported");
  33.       if (protocol.endsWith("1.1")) {
  34.          resp.sendError(405, msg);
  35.       } else {
  36.          resp.sendError(400, msg);
  37.       }
  38.  
  39.    }
  40.  
  41.    protected long getLastModified(HttpServletRequest req) {
  42.       return -1L;
  43.    }
  44.  
  45.    protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  46.       NoBodyResponse response = new NoBodyResponse(resp);
  47.       this.doGet(req, response);
  48.       response.setContentLength();
  49.    }
  50.  
  51.    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  52.       String protocol = req.getProtocol();
  53.       String msg = lStrings.getString("http.method_post_not_supported");
  54.       if (protocol.endsWith("1.1")) {
  55.          resp.sendError(405, msg);
  56.       } else {
  57.          resp.sendError(400, msg);
  58.       }
  59.  
  60.    }
  61.  
  62.    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  63.       String protocol = req.getProtocol();
  64.       String msg = lStrings.getString("http.method_put_not_supported");
  65.       if (protocol.endsWith("1.1")) {
  66.          resp.sendError(405, msg);
  67.       } else {
  68.          resp.sendError(400, msg);
  69.       }
  70.  
  71.    }
  72.  
  73.    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  74.       String protocol = req.getProtocol();
  75.       String msg = lStrings.getString("http.method_delete_not_supported");
  76.       if (protocol.endsWith("1.1")) {
  77.          resp.sendError(405, msg);
  78.       } else {
  79.          resp.sendError(400, msg);
  80.       }
  81.  
  82.    }
  83.  
  84.    private static Method[] getAllDeclaredMethods(Class c) {
  85.       if (c.equals(class$javax$servlet$http$HttpServlet == null ? (class$javax$servlet$http$HttpServlet = class$("javax.servlet.http.HttpServlet")) : class$javax$servlet$http$HttpServlet)) {
  86.          return null;
  87.       } else {
  88.          Method[] parentMethods = getAllDeclaredMethods(c.getSuperclass());
  89.          Method[] thisMethods = c.getDeclaredMethods();
  90.          if (parentMethods != null && parentMethods.length > 0) {
  91.             Method[] allMethods = new Method[parentMethods.length + thisMethods.length];
  92.             System.arraycopy(parentMethods, 0, allMethods, 0, parentMethods.length);
  93.             System.arraycopy(thisMethods, 0, allMethods, parentMethods.length, thisMethods.length);
  94.             thisMethods = allMethods;
  95.          }
  96.  
  97.          return thisMethods;
  98.       }
  99.    }
  100.  
  101.    protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  102.       Method[] methods = getAllDeclaredMethods(this.getClass());
  103.       boolean ALLOW_GET = false;
  104.       boolean ALLOW_HEAD = false;
  105.       boolean ALLOW_POST = false;
  106.       boolean ALLOW_PUT = false;
  107.       boolean ALLOW_DELETE = false;
  108.       boolean ALLOW_TRACE = true;
  109.       boolean ALLOW_OPTIONS = true;
  110.  
  111.       for(int i = 0; i < methods.length; ++i) {
  112.          Method m = methods[i];
  113.          if (m.getName().equals("doGet")) {
  114.             ALLOW_GET = true;
  115.             ALLOW_HEAD = true;
  116.          }
  117.  
  118.          if (m.getName().equals("doPost")) {
  119.             ALLOW_POST = true;
  120.          }
  121.  
  122.          if (m.getName().equals("doPut")) {
  123.             ALLOW_PUT = true;
  124.          }
  125.  
  126.          if (m.getName().equals("doDelete")) {
  127.             ALLOW_DELETE = true;
  128.          }
  129.       }
  130.  
  131.       String allow = null;
  132.       if (ALLOW_GET && allow == null) {
  133.          allow = "GET";
  134.       }
  135.  
  136.       if (ALLOW_HEAD) {
  137.          if (allow == null) {
  138.             allow = "HEAD";
  139.          } else {
  140.             allow = allow + ", HEAD";
  141.          }
  142.       }
  143.  
  144.       if (ALLOW_POST) {
  145.          if (allow == null) {
  146.             allow = "POST";
  147.          } else {
  148.             allow = allow + ", POST";
  149.          }
  150.       }
  151.  
  152.       if (ALLOW_PUT) {
  153.          if (allow == null) {
  154.             allow = "PUT";
  155.          } else {
  156.             allow = allow + ", PUT";
  157.          }
  158.       }
  159.  
  160.       if (ALLOW_DELETE) {
  161.          if (allow == null) {
  162.             allow = "DELETE";
  163.          } else {
  164.             allow = allow + ", DELETE";
  165.          }
  166.       }
  167.  
  168.       if (ALLOW_TRACE) {
  169.          if (allow == null) {
  170.             allow = "TRACE";
  171.          } else {
  172.             allow = allow + ", TRACE";
  173.          }
  174.       }
  175.  
  176.       if (ALLOW_OPTIONS) {
  177.          if (allow == null) {
  178.             allow = "OPTIONS";
  179.          } else {
  180.             allow = allow + ", OPTIONS";
  181.          }
  182.       }
  183.  
  184.       resp.setHeader("Allow", allow);
  185.    }
  186.  
  187.    protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  188.       String CRLF = "\r\n";
  189.       String responseString = "TRACE " + req.getRequestURI() + " " + req.getProtocol();
  190.  
  191.       String headerName;
  192.       for(Enumeration reqHeaderEnum = req.getHeaderNames(); reqHeaderEnum.hasMoreElements(); responseString = responseString + CRLF + headerName + ": " + req.getHeader(headerName)) {
  193.          headerName = (String)reqHeaderEnum.nextElement();
  194.       }
  195.  
  196.       responseString = responseString + CRLF;
  197.       int responseLength = responseString.length();
  198.       resp.setContentType("message/http");
  199.       resp.setContentLength(responseLength);
  200.       ServletOutputStream out = resp.getOutputStream();
  201.       out.print(responseString);
  202.       out.close();
  203.    }
  204.  
  205.    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  206.       String method = req.getMethod();
  207.       if (method.equals("GET")) {
  208.          long lastModified = this.getLastModified(req);
  209.          if (lastModified == -1L) {
  210.             this.doGet(req, resp);
  211.          } else {
  212.             long ifModifiedSince = req.getDateHeader("If-Modified-Since");
  213.             if (ifModifiedSince < lastModified / 1000L * 1000L) {
  214.                this.maybeSetLastModified(resp, lastModified);
  215.                this.doGet(req, resp);
  216.             } else {
  217.                resp.setStatus(304);
  218.             }
  219.          }
  220.       } else if (method.equals("HEAD")) {
  221.          long lastModified = this.getLastModified(req);
  222.          this.maybeSetLastModified(resp, lastModified);
  223.          this.doHead(req, resp);
  224.       } else if (method.equals("POST")) {
  225.          this.doPost(req, resp);
  226.       } else if (method.equals("PUT")) {
  227.          this.doPut(req, resp);
  228.       } else if (method.equals("DELETE")) {
  229.          this.doDelete(req, resp);
  230.       } else if (method.equals("OPTIONS")) {
  231.          this.doOptions(req, resp);
  232.       } else if (method.equals("TRACE")) {
  233.          this.doTrace(req, resp);
  234.       } else {
  235.          String errMsg = lStrings.getString("http.method_not_implemented");
  236.          Object[] errArgs = new Object[1];
  237.          errArgs[0] = method;
  238.          errMsg = MessageFormat.format(errMsg, errArgs);
  239.          resp.sendError(501, errMsg);
  240.       }
  241.  
  242.    }
  243.  
  244.    private void maybeSetLastModified(HttpServletResponse resp, long lastModified) {
  245.       if (!resp.containsHeader("Last-Modified")) {
  246.          if (lastModified >= 0L) {
  247.             resp.setDateHeader("Last-Modified", lastModified);
  248.          }
  249.  
  250.       }
  251.    }
  252.  
  253.    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
  254.       HttpServletRequest request;
  255.       HttpServletResponse response;
  256.       try {
  257.          request = (HttpServletRequest)req;
  258.          response = (HttpServletResponse)res;
  259.       } catch (ClassCastException var6) {
  260.          throw new ServletException("non-HTTP request or response");
  261.       }
  262.  
  263.       this.service(request, response);
  264.    }
  265.  
  266.    // $FF: synthetic method
  267.    static Class class$(String x0) {
  268.       try {
  269.          return Class.forName(x0);
  270.       } catch (ClassNotFoundException x1) {
  271.          throw new NoClassDefFoundError(x1.getMessage());
  272.       }
  273.    }
  274. }
  275.