View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.message;
29  
30  import org.apache.http.HttpRequest;
31  import org.apache.http.ProtocolVersion;
32  import org.apache.http.RequestLine;
33  import org.apache.http.annotation.NotThreadSafe;
34  import org.apache.http.params.HttpParams;
35  import org.apache.http.params.HttpProtocolParams;
36  
37  /**
38   * Basic implementation of {@link HttpRequest}.
39   * <p>
40   * The following parameters can be used to customize the behavior of this class:
41   * <ul>
42   *  <li>{@link org.apache.http.params.CoreProtocolPNames#PROTOCOL_VERSION}</li>
43   * </ul>
44   *
45   * @since 4.0
46   */
47  @NotThreadSafe
48  public class BasicHttpRequest extends AbstractHttpMessage implements HttpRequest {
49  
50      private final String method;
51      private final String uri;
52  
53      private RequestLine requestline;
54  
55      /**
56       * Creates an instance of this class using the given request method
57       * and URI. The HTTP protocol version will be obtained from the
58       * {@link HttpParams} instance associated with the object.
59       * The initialization will be deferred
60       * until {@link #getRequestLine()} is accessed for the first time.
61       *
62       * @param method request method.
63       * @param uri request URI.
64       */
65      public BasicHttpRequest(final String method, final String uri) {
66          super();
67          if (method == null) {
68              throw new IllegalArgumentException("Method name may not be null");
69          }
70          if (uri == null) {
71              throw new IllegalArgumentException("Request URI may not be null");
72          }
73          this.method = method;
74          this.uri = uri;
75          this.requestline = null;
76      }
77  
78      /**
79       * Creates an instance of this class using the given request method, URI
80       * and the HTTP protocol version.
81       *
82       * @param method request method.
83       * @param uri request URI.
84       * @param ver HTTP protocol version.
85       */
86      public BasicHttpRequest(final String method, final String uri, final ProtocolVersion ver) {
87          this(new BasicRequestLine(method, uri, ver));
88      }
89  
90      /**
91       * Creates an instance of this class using the given request line.
92       *
93       * @param requestline request line.
94       */
95      public BasicHttpRequest(final RequestLine requestline) {
96          super();
97          if (requestline == null) {
98              throw new IllegalArgumentException("Request line may not be null");
99          }
100         this.requestline = requestline;
101         this.method = requestline.getMethod();
102         this.uri = requestline.getUri();
103     }
104 
105     /**
106      * Returns the HTTP protocol version to be used for this request. If an
107      * HTTP protocol version was not explicitly set at the construction time,
108      * this method will obtain it from the {@link HttpParams} instance
109      * associated with the object.
110      *
111      * @see #BasicHttpRequest(String, String)
112      */
113     public ProtocolVersion getProtocolVersion() {
114         return getRequestLine().getProtocolVersion();
115     }
116 
117     /**
118      * Returns the request line of this request. If an HTTP protocol version
119      * was not explicitly set at the construction time, this method will obtain
120      * it from the {@link HttpParams} instance associated with the object.
121      *
122      * @see #BasicHttpRequest(String, String)
123      */
124     public RequestLine getRequestLine() {
125         if (this.requestline == null) {
126             ProtocolVersion ver = HttpProtocolParams.getVersion(getParams());
127             this.requestline = new BasicRequestLine(this.method, this.uri, ver);
128         }
129         return this.requestline;
130     }
131 
132     @Override
133     public String toString() {
134         return this.method + " " + this.uri + " " + this.headergroup;
135     }
136 
137 }