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