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.client.methods;
29  
30  import java.net.URI;
31  
32  import org.apache.http.ProtocolVersion;
33  import org.apache.http.RequestLine;
34  import org.apache.http.client.config.RequestConfig;
35  import org.apache.http.message.BasicRequestLine;
36  import org.apache.http.params.HttpProtocolParams;
37  
38  /**
39   * Base implementation of {@link HttpUriRequest}.
40   *
41   * @since 4.0
42   */
43  @SuppressWarnings("deprecation")
44  public abstract class HttpRequestBase extends AbstractExecutionAwareRequest
45      implements HttpUriRequest, Configurable {
46  
47      private ProtocolVersion version;
48      private URI uri;
49      private RequestConfig config;
50  
51      @Override
52      public abstract String getMethod();
53  
54      /**
55       * @since 4.3
56       */
57      public void setProtocolVersion(final ProtocolVersion version) {
58          this.version = version;
59      }
60  
61      @Override
62      public ProtocolVersion getProtocolVersion() {
63          return version != null ? version : HttpProtocolParams.getVersion(getParams());
64      }
65  
66      /**
67       * Returns the original request URI.
68       * <p>
69       * Please note URI remains unchanged in the course of request execution and
70       * is not updated if the request is redirected to another location.
71       */
72      @Override
73      public URI getURI() {
74          return this.uri;
75      }
76  
77      @Override
78      public RequestLine getRequestLine() {
79          final String method = getMethod();
80          final ProtocolVersion ver = getProtocolVersion();
81          final URI uriCopy = getURI(); // avoids possible window where URI could be changed
82          String uritext = null;
83          if (uriCopy != null) {
84              uritext = uriCopy.toASCIIString();
85          }
86          if (uritext == null || uritext.isEmpty()) {
87              uritext = "/";
88          }
89          return new BasicRequestLine(method, uritext, ver);
90      }
91  
92  
93      @Override
94      public RequestConfig getConfig() {
95          return config;
96      }
97  
98      public void setConfig(final RequestConfig config) {
99          this.config = config;
100     }
101 
102     public void setURI(final URI uri) {
103         this.uri = uri;
104     }
105 
106     /**
107      * @since 4.2
108      */
109     public void started() {
110     }
111 
112     /**
113      * A convenience method to simplify migration from HttpClient 3.1 API. This method is
114      * equivalent to {@link #reset()}.
115      *
116      * @since 4.2
117      */
118     public void releaseConnection() {
119         reset();
120     }
121 
122     @Override
123     public String toString() {
124         return getMethod() + " " + getURI() + " " + getProtocolVersion();
125     }
126 
127 }