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.HttpVersion;
32  import org.apache.http.ProtocolVersion;
33  import org.apache.http.RequestLine;
34  import org.apache.http.util.Args;
35  
36  /**
37   * Basic implementation of {@link HttpRequest}.
38   *
39   * @since 4.0
40   */
41  public class BasicHttpRequest extends AbstractHttpMessage implements HttpRequest {
42  
43      private final String method;
44      private final String uri;
45  
46      private RequestLine requestline;
47  
48      /**
49       * Creates an instance of this class using the given request method
50       * and URI.
51       *
52       * @param method request method.
53       * @param uri request URI.
54       */
55      public BasicHttpRequest(final String method, final String uri) {
56          super();
57          this.method = Args.notNull(method, "Method name");
58          this.uri = Args.notNull(uri, "Request URI");
59          this.requestline = null;
60      }
61  
62      /**
63       * Creates an instance of this class using the given request method, URI
64       * and the HTTP protocol version.
65       *
66       * @param method request method.
67       * @param uri request URI.
68       * @param ver HTTP protocol version.
69       */
70      public BasicHttpRequest(final String method, final String uri, final ProtocolVersion ver) {
71          this(new BasicRequestLine(method, uri, ver));
72      }
73  
74      /**
75       * Creates an instance of this class using the given request line.
76       *
77       * @param requestline request line.
78       */
79      public BasicHttpRequest(final RequestLine requestline) {
80          super();
81          this.requestline = Args.notNull(requestline, "Request line");
82          this.method = requestline.getMethod();
83          this.uri = requestline.getUri();
84      }
85  
86      /**
87       * Returns the HTTP protocol version to be used for this request.
88       *
89       * @see #BasicHttpRequest(String, String)
90       */
91      @Override
92      public ProtocolVersion getProtocolVersion() {
93          return getRequestLine().getProtocolVersion();
94      }
95  
96      /**
97       * Returns the request line of this request.
98       *
99       * @see #BasicHttpRequest(String, String)
100      */
101     @Override
102     public RequestLine getRequestLine() {
103         if (this.requestline == null) {
104             this.requestline = new BasicRequestLine(this.method, this.uri, HttpVersion.HTTP_1_1);
105         }
106         return this.requestline;
107     }
108 
109     @Override
110     public String toString() {
111         return this.method + ' ' + this.uri + ' ' + this.headergroup;
112     }
113 
114 }