Request.java
上传用户:demmber
上传日期:2007-12-22
资源大小:717k
文件大小:7k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * @(#)Request.java 0.3-3 06/05/2001
  3.  *
  4.  *  This file is part of the HTTPClient package
  5.  *  Copyright (C) 1996-2001 Ronald Tschal鋜
  6.  *
  7.  *  This library is free software; you can redistribute it and/or
  8.  *  modify it under the terms of the GNU Lesser General Public
  9.  *  License as published by the Free Software Foundation; either
  10.  *  version 2 of the License, or (at your option) any later version.
  11.  *
  12.  *  This library is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  *  Lesser General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU Lesser General Public
  18.  *  License along with this library; if not, write to the Free
  19.  *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20.  *  MA 02111-1307, USA
  21.  *
  22.  *  For questions, suggestions, bug-reports, enhancement-requests etc.
  23.  *  I may be contacted at:
  24.  *
  25.  *  ronald@innovation.ch
  26.  *
  27.  *  The HTTPClient's home page is located at:
  28.  *
  29.  *  http://www.innovation.ch/java/HTTPClient/ 
  30.  *
  31.  */
  32. package HTTPClient;
  33. /**
  34.  * This class represents an http request. It's used by classes which
  35.  * implement the HTTPClientModule interface.
  36.  *
  37.  * @version 0.3-3  06/05/2001
  38.  * @author Ronald Tschal鋜
  39.  */
  40. public final class Request implements RoRequest, Cloneable
  41. {
  42.     /** null headers */
  43.     private static final NVPair[] empty = new NVPair[0];
  44.     /** the current HTTPConnection */
  45.     private HTTPConnection connection;
  46.     /** the request method to be used (e.g. GET, POST, etc) */
  47.     private String         method;
  48.     /** the request-uri */
  49.     private String         req_uri;
  50.     /** the headers to be used */
  51.     private NVPair[]       headers;
  52.     /** the entity (if any) */
  53.     private byte[]         data;
  54.     /** or an output stream on which the entity will be written */
  55.     private HttpOutputStream stream;
  56.     /** are modules allowed to popup windows or otherwise prompt user? */
  57.     private boolean        allow_ui;
  58.     /** number of millisecs to wait for an error from the server before sending
  59. the entity (used when retrying requests) */
  60.             long           delay_entity = 0;
  61.     /** number of retries so far */
  62.             int            num_retries = 0;
  63.     /** disable pipelining of following request */
  64.             boolean        dont_pipeline = false;
  65.     /** was this request aborted by the user? */
  66.             boolean        aborted = false;
  67.     /** is this an internally generated subrequest? */
  68.             boolean        internal_subrequest = false;
  69.     // Constructors
  70.     /**
  71.      * Creates a new request structure.
  72.      *
  73.      * @param con      the current HTTPConnection
  74.      * @param method   the request method
  75.      * @param req_uri  the request-uri
  76.      * @param headers  the request headers
  77.      * @param data     the entity as a byte[]
  78.      * @param stream   the entity as a stream
  79.      * @param allow_ui allow user interaction
  80.      */
  81.     public Request(HTTPConnection con, String method, String req_uri,
  82.    NVPair[] headers, byte[] data, HttpOutputStream stream,
  83.    boolean allow_ui)
  84.     {
  85. this.connection = con;
  86. this.method     = method;
  87. setRequestURI(req_uri);
  88. setHeaders(headers);
  89. this.data       = data;
  90. this.stream     = stream;
  91. this.allow_ui   = allow_ui;
  92.     }
  93.     // Methods
  94.     /**
  95.      * @return the HTTPConnection this request is associated with
  96.      */
  97.     public HTTPConnection getConnection()
  98.     {
  99. return connection;
  100.     }
  101.     /**
  102.      * @param con the HTTPConnection this request is associated with
  103.      */
  104.     public void setConnection(HTTPConnection  con)
  105.     {
  106. this.connection = con;
  107.     }
  108.     /**
  109.      * @return the request method
  110.      */
  111.     public String getMethod()
  112.     {
  113. return method;
  114.     }
  115.     /**
  116.      * @param method the request method (e.g. GET, POST, etc)
  117.      */
  118.     public void setMethod(String method)
  119.     {
  120. this.method = method;
  121.     }
  122.     /**
  123.      * @return the request-uri
  124.      */
  125.     public String getRequestURI()
  126.     {
  127. return req_uri;
  128.     }
  129.     /**
  130.      * @param req_uri the request-uri
  131.      */
  132.     public void setRequestURI(String req_uri)
  133.     {
  134. if (req_uri != null  &&  req_uri.trim().length() > 0)
  135. {
  136.     req_uri = req_uri.trim();
  137.     if (req_uri.charAt(0) != '/'  &&  !req_uri.equals("*")  &&
  138. !method.equals("CONNECT")  &&  !isAbsolute(req_uri))
  139. req_uri = "/" + req_uri;
  140.     this.req_uri = req_uri;
  141. }
  142. else
  143.     this.req_uri = "/";
  144.     }
  145.     private static final boolean isAbsolute(String uri)
  146.     {
  147. char ch = '';
  148. int  pos = 0, len = uri.length();
  149. while (pos < len  &&  (ch = uri.charAt(pos)) != ':'  &&
  150.        ch != '/'  &&  ch != '?'  &&  ch != '#')
  151.   pos++;
  152. return (ch == ':');
  153.     }
  154.     /**
  155.      * @return the headers making up this request
  156.      */
  157.     public NVPair[] getHeaders()
  158.     {
  159. return headers;
  160.     }
  161.     /**
  162.      * @param headers the headers for this request
  163.      */
  164.     public void setHeaders(NVPair[] headers)
  165.     {
  166. if (headers != null)
  167.     this.headers = headers;
  168. else
  169.     this.headers = empty;
  170.     }
  171.     /**
  172.      * @return the body of this request
  173.      */
  174.     public byte[] getData()
  175.     {
  176. return data;
  177.     }
  178.     /**
  179.      * @param data the entity for this request
  180.      */
  181.     public void setData(byte[] data)
  182.     {
  183. this.data = data;
  184.     }
  185.     /**
  186.      * @return the output stream on which the body is written
  187.      */
  188.     public HttpOutputStream getStream()
  189.     {
  190. return stream;
  191.     }
  192.     /**
  193.      * @param stream an output stream on which the entity is written
  194.      */
  195.     public void setStream(HttpOutputStream stream)
  196.     {
  197. this.stream = stream;
  198.     }
  199.     /**
  200.      * @return true if the modules or handlers for this request may popup
  201.      *         windows or otherwise interact with the user
  202.      */
  203.     public boolean allowUI()
  204.     {
  205. return allow_ui;
  206.     }
  207.     /**
  208.      * @param allow_ui are modules and handlers allowed to popup windows or
  209.      *                otherwise interact with the user?
  210.      */
  211.     public void setAllowUI(boolean allow_ui)
  212.     {
  213. this.allow_ui = allow_ui;
  214.     }
  215.     /**
  216.      * @return a clone of this request object
  217.      */
  218.     public Object clone()
  219.     {
  220. Request cl;
  221. try
  222.     { cl = (Request) super.clone(); }
  223. catch (CloneNotSupportedException cnse)
  224.     { throw new InternalError(cnse.toString()); /* shouldn't happen */ }
  225. cl.headers = new NVPair[headers.length];
  226. System.arraycopy(headers, 0, cl.headers, 0, headers.length);
  227. return cl;
  228.     }
  229.     /**
  230.      * Copy all the fields from <var>other</var> to this request.
  231.      *
  232.      * @param other the Request to copy from
  233.      */
  234.     public void copyFrom(Request other)
  235.     {
  236. this.connection          = other.connection;
  237. this.method              = other.method;
  238. this.req_uri             = other.req_uri;
  239. this.headers             = other.headers;
  240. this.data                = other.data;
  241. this.stream              = other.stream;
  242. this.allow_ui            = other.allow_ui;
  243. this.delay_entity        = other.delay_entity;
  244. this.num_retries         = other.num_retries;
  245. this.dont_pipeline       = other.dont_pipeline;
  246. this.aborted             = other.aborted;
  247. this.internal_subrequest = other.internal_subrequest;
  248.     }
  249.     /**
  250.      * @return a string containing the method and request-uri
  251.      */
  252.     public String toString()
  253.     {
  254. return getClass().getName() + ": " + method + " " + req_uri;
  255.     }
  256. }