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.hc.core5.http.message;
29  
30  import java.net.URI;
31  import java.net.URISyntaxException;
32  
33  import org.apache.hc.core5.http.HttpHost;
34  import org.apache.hc.core5.http.HttpRequest;
35  import org.apache.hc.core5.http.Method;
36  import org.apache.hc.core5.http.ProtocolVersion;
37  import org.apache.hc.core5.http.URIScheme;
38  import org.apache.hc.core5.net.URIAuthority;
39  import org.apache.hc.core5.util.Args;
40  import org.apache.hc.core5.util.TextUtils;
41  
42  /**
43   * Basic implementation of {@link HttpRequest}.
44   *
45   * @since 4.0
46   */
47  public class BasicHttpRequest extends HeaderGroup implements HttpRequest {
48  
49      private static final long serialVersionUID = 1L;
50  
51      private final String method;
52      private String path;
53      private String scheme;
54      private URIAuthority authority;
55      private ProtocolVersion version;
56      private URI requestUri;
57      private boolean absoluteRequestUri;
58  
59      /**
60       * Creates request message with the given method, host and request path.
61       *
62       * @param method request method.
63       * @param scheme request scheme.
64       * @param authority request authority.
65       * @param path request path.
66       *
67       * @since 5.1
68       */
69      public BasicHttpRequest(final String method, final String scheme, final URIAuthority authority, final String path) {
70          super();
71          this.method = Args.notNull(method, "Method name");
72          this.scheme = scheme;
73          this.authority = authority;
74          this.path = path;
75      }
76  
77      /**
78       * Creates request message with the given method and request path.
79       *
80       * @param method request method.
81       * @param path request path.
82       */
83      public BasicHttpRequest(final String method, final String path) {
84          super();
85          this.method = method;
86          if (path != null) {
87              try {
88                  setUri(new URI(path));
89              } catch (final URISyntaxException ex) {
90                  this.path = path;
91              }
92          }
93      }
94  
95      /**
96       * Creates request message with the given method, host and request path.
97       *
98       * @param method request method.
99       * @param host request host.
100      * @param path request path.
101      *
102      * @since 5.0
103      */
104     public BasicHttpRequest(final String method, final HttpHost host, final String path) {
105         super();
106         this.method = Args.notNull(method, "Method name");
107         this.scheme = host != null ? host.getSchemeName() : null;
108         this.authority = host != null ? new URIAuthority(host) : null;
109         this.path = path;
110     }
111 
112     /**
113      * Creates request message with the given method, request URI.
114      *
115      * @param method request method.
116      * @param requestUri request URI.
117      *
118      * @since 5.0
119      */
120     public BasicHttpRequest(final String method, final URI requestUri) {
121         super();
122         this.method = Args.notNull(method, "Method name");
123         setUri(Args.notNull(requestUri, "Request URI"));
124     }
125 
126     /**
127      * Creates request message with the given method and request path.
128      *
129      * @param method request method.
130      * @param path request path.
131      *
132      * @since 5.0
133      */
134     public BasicHttpRequest(final Method method, final String path) {
135         super();
136         this.method = Args.notNull(method, "Method").name();
137         if (path != null) {
138             try {
139                 setUri(new URI(path));
140             } catch (final URISyntaxException ex) {
141                 this.path = path;
142             }
143         }
144     }
145 
146     /**
147      * Creates request message with the given method, host and request path.
148      *
149      * @param method request method.
150      * @param host request host.
151      * @param path request path.
152      *
153      * @since 5.0
154      */
155     public BasicHttpRequest(final Method method, final HttpHost host, final String path) {
156         super();
157         this.method = Args.notNull(method, "Method").name();
158         this.scheme = host != null ? host.getSchemeName() : null;
159         this.authority = host != null ? new URIAuthority(host) : null;
160         this.path = path;
161     }
162 
163     /**
164      * Creates request message with the given method, request URI.
165      *
166      * @param method request method.
167      * @param requestUri request URI.
168      *
169      * @since 5.0
170      */
171     public BasicHttpRequest(final Method method, final URI requestUri) {
172         super();
173         this.method = Args.notNull(method, "Method").name();
174         setUri(Args.notNull(requestUri, "Request URI"));
175     }
176 
177     @Override
178     public void addHeader(final String name, final Object value) {
179         Args.notNull(name, "Header name");
180         addHeader(new BasicHeader(name, value));
181     }
182 
183     @Override
184     public void setHeader(final String name, final Object value) {
185         Args.notNull(name, "Header name");
186         setHeader(new BasicHeader(name, value));
187     }
188 
189     @Override
190     public void setVersion(final ProtocolVersion version) {
191         this.version = version;
192     }
193 
194     @Override
195     public ProtocolVersion getVersion() {
196         return this.version;
197     }
198 
199     @Override
200     public String getMethod() {
201         return this.method;
202     }
203 
204     @Override
205     public String getPath() {
206         return this.path;
207     }
208 
209     @Override
210     public void setPath(final String path) {
211         this.path = path;
212         this.requestUri = null;
213     }
214 
215     @Override
216     public String getScheme() {
217         return this.scheme;
218     }
219 
220     @Override
221     public void setScheme(final String scheme) {
222         this.scheme = scheme;
223         this.requestUri = null;
224     }
225 
226     @Override
227     public URIAuthority getAuthority() {
228         return this.authority;
229     }
230 
231     @Override
232     public void setAuthority(final URIAuthority authority) {
233         this.authority = authority;
234         this.requestUri = null;
235     }
236 
237     /**
238      * Sets a flag that the {@link #getRequestUri()} method should return the request URI
239      * in an absolute form.
240      * <p>
241      * This flag can used when the request is going to be transmitted via an HTTP/1.1 proxy.
242      *
243      * @since 5.1
244      */
245     public void setAbsoluteRequestUri(final boolean absoluteRequestUri) {
246         this.absoluteRequestUri = absoluteRequestUri;
247     }
248 
249     @Override
250     public String getRequestUri() {
251         if (absoluteRequestUri) {
252             final StringBuilder buf = new StringBuilder();
253             assembleRequestUri(buf);
254             return buf.toString();
255         } else {
256             return getPath();
257         }
258     }
259 
260     @Override
261     public void setUri(final URI requestUri) {
262         this.scheme = requestUri.getScheme();
263         if (requestUri.getHost() != null) {
264             this.authority = new URIAuthority(
265                     requestUri.getRawUserInfo(), requestUri.getHost(), requestUri.getPort());
266         } else if (requestUri.getRawAuthority() != null) {
267             try {
268                 this.authority = URIAuthority.create(requestUri.getRawAuthority());
269             } catch (final URISyntaxException ignore) {
270                 this.authority = null;
271             }
272         } else {
273             this.authority = null;
274         }
275         final StringBuilder buf = new StringBuilder();
276         final String rawPath = requestUri.getRawPath();
277         if (!TextUtils.isBlank(rawPath)) {
278             buf.append(rawPath);
279         } else {
280             buf.append("/");
281         }
282         final String query = requestUri.getRawQuery();
283         if (query != null) {
284             buf.append('?').append(query);
285         }
286         this.path = buf.toString();
287         this.requestUri = null;
288     }
289 
290     private void assembleRequestUri(final StringBuilder buf) {
291         if (this.authority != null) {
292             buf.append(this.scheme != null ? this.scheme : URIScheme.HTTP.id).append("://");
293             buf.append(this.authority.getHostName());
294             if (this.authority.getPort() >= 0) {
295                 buf.append(":").append(this.authority.getPort());
296             }
297         }
298         if (this.path == null) {
299             buf.append("/");
300         } else {
301             if (buf.length() > 0 && !this.path.startsWith("/")) {
302                 buf.append("/");
303             }
304             buf.append(this.path);
305         }
306     }
307 
308     @Override
309     public URI getUri() throws URISyntaxException {
310         if (this.requestUri == null) {
311             final StringBuilder buf = new StringBuilder();
312             assembleRequestUri(buf);
313             this.requestUri = new URI(buf.toString());
314         }
315         return this.requestUri;
316     }
317 
318     @Override
319     public String toString() {
320         final StringBuilder buf = new StringBuilder();
321         buf.append(method).append(" ");
322         assembleRequestUri(buf);
323         return buf.toString();
324     }
325 
326 }