home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / netscape / NSRequest.java next >
Encoding:
Java Source  |  1997-07-18  |  10.6 KB  |  447 lines

  1.  /*
  2.  * @(#)NSRequest.java    1.11 97/05/22
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. package sun.servlet.netscape;
  23.  
  24. import java.io.IOException;
  25. import java.util.Dictionary;
  26. import java.util.Hashtable;
  27. import java.util.Enumeration;
  28. import java.util.Date;
  29. import java.util.StringTokenizer;
  30. import javax.servlet.ServletInputStream;
  31. import javax.servlet.http.HttpServletRequest;
  32. import javax.servlet.http.HttpUtils;
  33. import sun.servlet.http.HttpInputStream;
  34. import netscape.server.applet.URIUtil;
  35.  
  36. /**
  37.  * This class represents a servlet request for servlets running in Netscape's
  38.  * Enterprise or FastTrack servers. The request object is passed to the servlet
  39.  * by a special Netscape "server applet".
  40.  *
  41.  * @version    1.11, 05/22/97
  42.  * @author    David Connelly
  43.  */
  44. public
  45. class NSRequest implements HttpServletRequest {
  46.     /*
  47.      * The server applet for running servlets.
  48.      */
  49.     private NSRunner sr;
  50.  
  51.     /*
  52.      * The servlet path for this request.
  53.      */
  54.     String servletPath;
  55.  
  56.     /*
  57.      * The path info for this request.
  58.      */
  59.     String pathInfo;
  60.  
  61.     /*
  62.      * The query string for this request.
  63.      */
  64.     private String queryString;
  65.  
  66.     /*
  67.      * The query parameters for this request.
  68.      */
  69.     private Dictionary params = null;
  70.  
  71.     /*
  72.      * The servlet input stream for the request.
  73.      */
  74.     private HttpInputStream in = new HttpInputStream();
  75.  
  76.     /*
  77.      * The headers for this request.
  78.      */
  79.     private Hashtable headers;
  80.  
  81.     /*
  82.      * Initializes the servlet request.
  83.      */
  84.     void init(NSRunner sr) throws IOException {
  85.     this.sr = sr;
  86.     in.init(sr.getInputStream());
  87.     in.next();
  88.     int len = getContentLength();
  89.     if (len != -1) {
  90.         in.setContentLength(len);
  91.     }
  92.     queryString = sr.getRequestProperty("query");
  93.     /*
  94.     if (queryString != null) {
  95.         params = URIUtil.splitFormData(queryString);
  96.     } else {
  97.         params = new Hashtable();
  98.     }
  99.     */
  100.     }
  101.  
  102.     /*
  103.      * Resets the request.
  104.      */
  105.     void reset() {
  106.     sr = null;
  107.     servletPath = null;
  108.     pathInfo = null;
  109.     queryString = null;
  110.     params = null;
  111.     in.resets();
  112.     headers = null;
  113.     }
  114.  
  115.     /**
  116.      * Returns the content length of the request.
  117.      */
  118.     public int getContentLength() {
  119.     String v = sr.getHeader("content-length");
  120.     try {
  121.         return v != null ? Integer.parseInt(v) : -1;
  122.     } catch (NumberFormatException e) {
  123.         return -1;
  124.     }
  125.     }
  126.  
  127.     /**
  128.      * Returns the content type of the request.
  129.      */
  130.     public String getContentType() {
  131.     return sr.getHeader("content-type");
  132.     }
  133.  
  134.     /**
  135.      * Returns the protocol for the request.
  136.      */
  137.     public String getProtocol() {
  138.     return sr.getRequestProperty("protocol");
  139.     }
  140.  
  141.     /**
  142.      * Returns the scheme of the request -- always "http" for now.
  143.      */
  144.     public String getScheme() {
  145.     return "http";        // XXX might be "https" too!
  146.     }
  147.  
  148.     /**
  149.      * Returns the host name of the server.
  150.      */
  151.     public String getServerName() {
  152.     return sr.getServer().getAddress().getHostName();
  153.     }
  154.  
  155.     /**
  156.      * Returns the port number on which this request was received.
  157.      */
  158.     public int getServerPort() {
  159.     return sr.getServer().getListeningPort();
  160.     }
  161.  
  162.     /**
  163.      * Returns the client host address.
  164.      */
  165.     public String getRemoteAddr() {
  166.     return sr.getClientProperty("ip");
  167.     }
  168.  
  169.     /**
  170.      * Returns the client host name.
  171.      */
  172.     public String getRemoteHost() {
  173.     return sr.getClientProperty("dns");
  174.     }
  175.  
  176.     /**
  177.      * Returns an input stream for reading the request.
  178.      * @exception IOException if an I/O error has occurred
  179.      */
  180.     public ServletInputStream getInputStream() {
  181.     return in;
  182.     }
  183.  
  184.     /**
  185.      * Returns an array of values for the specified query parameter.
  186.      */
  187.     public String [] getParameterValues(String name) {
  188.  
  189.     if (params == null)
  190.         params = getParametersFromRequest();
  191.  
  192.     return (String []) params.get(name);
  193.     }
  194.  
  195.     /**
  196.      * Returns the value of the specified query parameter.
  197.      */
  198.     public String getParameter(String name) {
  199.  
  200.     if (params == null)
  201.         params = getParametersFromRequest();
  202.  
  203.     String vals[] = (String []) params.get(name);
  204.  
  205.     if (vals == null)
  206.         return null;
  207.  
  208.     String hackVal = vals[0];
  209.     for (int i = 1; i < vals.length; i++)
  210.         hackVal = hackVal + "," + vals[i];
  211.         
  212.     return hackVal;
  213.  
  214.     }
  215.  
  216.     /**
  217.      * Returns an enumeration of query parameter names.
  218.      */
  219.     public Enumeration getParameterNames() {
  220.  
  221.     if (params == null)
  222.         params = getParametersFromRequest();
  223.  
  224.     return params.keys();
  225.     }
  226.  
  227.     /**
  228.      * Returns the request method.
  229.      */
  230.     public String getMethod() {
  231.     return sr.getRequestProperty("method");
  232.     }
  233.  
  234.     /**
  235.      * Returns the request URI.
  236.      */
  237.     public String getRequestURI() {
  238.     String file = sr.getRequestProperty("uri");
  239.     if (queryString != null) {
  240.         file = file + "?" + queryString;
  241.     }
  242.     return file;
  243.     }
  244.  
  245.     /**
  246.      * Returns the path info part of the request URI.
  247.      */
  248.     public String getPathInfo() {
  249.     return pathInfo;
  250.     }
  251.  
  252.     /**
  253.      * Returns the servlet path.
  254.      */
  255.     public String getServletPath() {
  256.     return servletPath;
  257.     }
  258.  
  259.     /**
  260.      * Returns path info translated.
  261.      */
  262.     public String getPathTranslated() {
  263.     return pathInfo != null ? sr.translateURI(pathInfo) : null;
  264.     }
  265.  
  266.     /**
  267.      * Returns corresponding real path for specified virtual path.
  268.      */
  269.     public String getRealPath(String path) {
  270.     return sr.translateURI(path);
  271.     }
  272.  
  273.     /**
  274.      * Returns the query string for the request.
  275.      */
  276.     public String getQueryString() {
  277.     return queryString;
  278.     }
  279.  
  280.     /**
  281.      * Returns the authenticated user if any.
  282.      */
  283.     public String getRemoteUser() {
  284.     return sr.getServerProperty("auth-user");
  285.     }
  286.  
  287.     /**
  288.      * Returns the authorization type for the request.
  289.      */
  290.     public String getAuthType() {
  291.     return sr.getServerProperty("auth-type");
  292.     }
  293.  
  294.     /**
  295.      * Returns the value of the header field with the specified name.
  296.      * @param name the header field name
  297.      * @return the header field value
  298.      */
  299.     public String getHeader(String name) {
  300.     return sr.getHeader(name.toLowerCase());
  301.     }
  302.  
  303.     /*
  304.      * Returns the integer value of the header field with the specified name.
  305.      * @param name the header field name
  306.      * @return the header field integer value, or -1 if not found
  307.      */
  308.     public int getIntHeader(String name) {
  309.     String v = getHeader(name);
  310.     try {
  311.         return v != null ? Integer.parseInt(v) : -1;
  312.     } catch (NumberFormatException e) {
  313.         return -1;
  314.     }
  315.     }
  316.  
  317.     /*
  318.      * Returns the date value of the header field with the specified name.
  319.      * @param name the header field name
  320.      * @return the header field date value, or -1 if not found
  321.      */
  322.     public long getDateHeader(String name) {
  323.     String v = getHeader(name);
  324.     try {
  325.         return v != null ? Date.parse(v) : -1;
  326.     } catch (IllegalArgumentException e) {
  327.         return -1;
  328.     }
  329.     }
  330.  
  331.     /**
  332.      * Returns an enumeration of header names for this request.
  333.      */
  334.     public Enumeration getHeaderNames() {
  335.     if (headers == null) {
  336.         headers = new Hashtable(HEADERS.length);
  337.         for (int i = 0; i < HEADERS.length; i++) {
  338.         String name = HEADERS[i];
  339.         String value = sr.getHeader(name.toLowerCase());
  340.         if (value != null) {
  341.             headers.put(name, value);
  342.         }
  343.         }
  344.     }
  345.     return headers.keys();
  346.     }
  347.  
  348.     /**
  349.      * Returns an attribute of the request for the specified key name.
  350.      * @param name the attribute name
  351.      */
  352.     public Object getAttribute(String name) {
  353.     return null;
  354.     }
  355.  
  356.     /*
  357.      * Possible request header field names.
  358.      */
  359.     private static String[] HEADERS = {
  360.     /* Standard HTTP 1.0 and 1.1 headers */
  361.     "Accept",        "Accept-Charset",    "Accept-Encoding",
  362.     "Accept-Language",    "Accept-Ranges",    "Age",
  363.     "Allow",        "Authorization",    "Cache-Control",
  364.     "Connection",        "Content-Base",        "Content-Encoding",
  365.     "Content-Language",    "Content-Length",    "Content-Location",
  366.     "Content-MD5",        "Content-Range",    "Content-Type",
  367.     "Date",            "ETag",            "Expires",
  368.     "From",            "Host",            "If-Modified-Since",
  369.     "If-Match",        "If-None-Match",    "If-Range",
  370.     "If-Unmodified-Since",    "Last-Modified",    "Location",
  371.     "Max-Forwards",        "Pragma",        "Proxy-Authenticate",
  372.     "Proxy-Authorization",    "Public",        "Range",
  373.     "Referer",        "Retry-After",        "Server",
  374.     "Transfer-Encoding",    "Upgrade",        "User-Agent",
  375.     "Vary",            "Via",            "Warning",
  376.     "WWW-Authenticate",
  377.     /* Popular extension headers */
  378.     "Alternates",        "Content-Version",    "Cookie",
  379.     "Derived-From",        "Keep-Alive",        "Link",
  380.     "MIME-Version",        "URI",            "Title"
  381.     };
  382.  
  383.     /**
  384.      * decode a URLencoded string, so we may use it as a string.
  385.      */
  386.     private String decode(String encoded) {
  387.         
  388.  
  389.         //speedily leave if we're not needed
  390.     if (encoded == null) return null;
  391.         if (encoded.indexOf('%') == -1 ) return encoded;
  392.  
  393.     //allocate the buffer - use byte[] to avoid calls to new.
  394.         byte holdbuffer[] = new byte[encoded.length()];
  395.  
  396.         char holdchar;
  397.         int bufcount = 0;
  398.  
  399.         for (int count = 0; count < encoded.length(); count++) {
  400.             if (encoded.charAt(count) == '%') {
  401.             holdbuffer[bufcount++] = 
  402.           (byte)Integer.parseInt(encoded.substring(count+1,count+3),16);
  403.                 if (count + 2 >= encoded.length()) 
  404.                     count = encoded.length();
  405.                 else
  406.                     count += 2;
  407.             } else {
  408.             holdbuffer[bufcount++] = (byte)encoded.charAt(count);
  409.             }
  410.         }
  411.     return new String(holdbuffer,0,0,bufcount);
  412.     }
  413.  
  414.     private static Hashtable nullHashtable = new Hashtable();
  415.  
  416.  
  417.     private Hashtable getParametersFromRequest() {
  418.  
  419.     Hashtable h = null;
  420.  
  421.     if (getMethod().equals("GET")) {
  422.         String s = getQueryString();
  423.         if (s != null) {
  424.         try {
  425.             h = HttpUtils.parseQueryString(s);
  426.         } catch (IllegalArgumentException e) {
  427.             h = null;
  428.         }
  429.         }
  430.     } else if (getMethod().equals("POST")) {
  431.         if (getContentType().equals("application/x-www-form-urlencoded")) {
  432.         try {
  433.             h = HttpUtils.parsePostData(getContentLength(),
  434.                              getInputStream());
  435.         } catch (Exception e) {
  436.             h = null;
  437.         }
  438.         }
  439.     }
  440.  
  441.     if (h == null)
  442.         h = nullHashtable;
  443.  
  444.     return h;
  445.     }
  446. }
  447.