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  
58      /**
59       * Creates request message with the given method and request path.
60       *
61       * @param method request method.
62       * @param path request path.
63       */
64      public BasicHttpRequest(final String method, final String path) {
65          super();
66          this.method = method;
67          if (path != null) {
68              try {
69                  setUri(new URI(path));
70              } catch (final URISyntaxException ex) {
71                  this.path = path;
72              }
73          }
74      }
75  
76      /**
77       * Creates request message with the given method, host and request path.
78       *
79       * @param method request method.
80       * @param host request host.
81       * @param path request path.
82       *
83       * @since 5.0
84       */
85      public BasicHttpRequest(final String method, final HttpHost host, final String path) {
86          super();
87          this.method = Args.notNull(method, "Method name");
88          this.scheme = host != null ? host.getSchemeName() : null;
89          this.authority = host != null ? new URIAuthority(host) : null;
90          this.path = path;
91      }
92  
93      /**
94       * Creates request message with the given method, request URI.
95       *
96       * @param method request method.
97       * @param requestUri request URI.
98       *
99       * @since 5.0
100      */
101     public BasicHttpRequest(final String method, final URI requestUri) {
102         super();
103         this.method = Args.notNull(method, "Method name");
104         setUri(Args.notNull(requestUri, "Request URI"));
105     }
106 
107     /**
108      * Creates request message with the given method and request path.
109      *
110      * @param method request method.
111      * @param path request path.
112      *
113      * @since 5.0
114      */
115     public BasicHttpRequest(final Method method, final String path) {
116         super();
117         this.method = Args.notNull(method, "Method").name();
118         if (path != null) {
119             try {
120                 setUri(new URI(path));
121             } catch (final URISyntaxException ex) {
122                 this.path = path;
123             }
124         }
125     }
126 
127     /**
128      * Creates request message with the given method, host and request path.
129      *
130      * @param method request method.
131      * @param host request host.
132      * @param path request path.
133      *
134      * @since 5.0
135      */
136     public BasicHttpRequest(final Method method, final HttpHost host, final String path) {
137         super();
138         this.method = Args.notNull(method, "Method").name();
139         this.scheme = host != null ? host.getSchemeName() : null;
140         this.authority = host != null ? new URIAuthority(host) : null;
141         this.path = path;
142     }
143 
144     /**
145      * Creates request message with the given method, request URI.
146      *
147      * @param method request method.
148      * @param requestUri request URI.
149      *
150      * @since 5.0
151      */
152     public BasicHttpRequest(final Method method, final URI requestUri) {
153         super();
154         this.method = Args.notNull(method, "Method").name();
155         setUri(Args.notNull(requestUri, "Request URI"));
156     }
157 
158     @Override
159     public void addHeader(final String name, final Object value) {
160         Args.notNull(name, "Header name");
161         addHeader(new BasicHeader(name, value));
162     }
163 
164     @Override
165     public void setHeader(final String name, final Object value) {
166         Args.notNull(name, "Header name");
167         setHeader(new BasicHeader(name, value));
168     }
169 
170     @Override
171     public void setVersion(final ProtocolVersion version) {
172         this.version = version;
173     }
174 
175     @Override
176     public ProtocolVersion getVersion() {
177         return this.version;
178     }
179 
180     @Override
181     public String getMethod() {
182         return this.method;
183     }
184 
185     @Override
186     public String getPath() {
187         return this.path;
188     }
189 
190     @Override
191     public void setPath(final String path) {
192         this.path = path;
193         this.requestUri = null;
194     }
195 
196     @Override
197     public String getScheme() {
198         return this.scheme;
199     }
200 
201     @Override
202     public void setScheme(final String scheme) {
203         this.scheme = scheme;
204         this.requestUri = null;
205     }
206 
207     @Override
208     public URIAuthority getAuthority() {
209         return this.authority;
210     }
211 
212     @Override
213     public void setAuthority(final URIAuthority authority) {
214         this.authority = authority;
215         this.requestUri = null;
216     }
217 
218     @Override
219     public String getRequestUri() {
220         return getPath();
221     }
222 
223     @Override
224     public void setUri(final URI requestUri) {
225         this.scheme = requestUri.getScheme();
226         this.authority = requestUri.getHost() != null ? new URIAuthority(
227                 requestUri.getRawUserInfo(),
228                 requestUri.getHost(),
229                 requestUri.getPort()) : null;
230         final StringBuilder buf = new StringBuilder();
231         final String rawPath = requestUri.getRawPath();
232         if (!TextUtils.isBlank(rawPath)) {
233             buf.append(rawPath);
234         } else {
235             buf.append("/");
236         }
237         final String query = requestUri.getRawQuery();
238         if (query != null) {
239             buf.append('?').append(query);
240         }
241         this.path = buf.toString();
242     }
243 
244     @Override
245     public URI getUri() throws URISyntaxException {
246         if (this.requestUri == null) {
247             final StringBuilder buf = new StringBuilder();
248             if (this.authority != null) {
249                 buf.append(this.scheme != null ? this.scheme : URIScheme.HTTP.id).append("://");
250                 buf.append(this.authority.getHostName());
251                 if (this.authority.getPort() >= 0) {
252                     buf.append(":").append(this.authority.getPort());
253                 }
254             }
255             if (this.path == null) {
256                 buf.append("/");
257             } else {
258                 if (buf.length() > 0 && !this.path.startsWith("/")) {
259                     buf.append("/");
260                 }
261                 buf.append(this.path);
262             }
263             this.requestUri = new URI(buf.toString());
264         }
265         return this.requestUri;
266     }
267 
268     @Override
269     public String toString() {
270         final StringBuilder sb = new StringBuilder();
271         sb.append(this.method).append(" ").append(this.scheme).append("://").append(this.authority).append(this.path);
272         return sb.toString();
273     }
274 
275 }