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.ProtocolVersion;
36  import org.apache.hc.core5.net.URIAuthority;
37  import org.apache.hc.core5.util.Args;
38  import org.apache.hc.core5.util.TextUtils;
39  
40  /**
41   * Basic implementation of {@link HttpRequest}.
42   *
43   * @since 4.0
44   */
45  public class BasicHttpRequest extends HeaderGroup implements HttpRequest {
46  
47      private static final long serialVersionUID = 1L;
48  
49      private final String method;
50      private String path;
51      private String scheme;
52      private URIAuthority authority;
53      private ProtocolVersion version;
54      private URI requestUri;
55  
56      /**
57       * Creates request message with the given method and request path.
58       *
59       * @param method request method.
60       * @param path request path.
61       */
62      public BasicHttpRequest(final String method, final String path) {
63          super();
64          this.method = method;
65          if (path != null) {
66              try {
67                  setUri(new URI(path));
68              } catch (final URISyntaxException ex) {
69                  this.path = path;
70              }
71          }
72      }
73  
74      /**
75       * Creates request message with the given method, host and request path.
76       *
77       * @param method request method.
78       * @param host request host.
79       * @param path request path.
80       *
81       * @since 5.0
82       */
83      public BasicHttpRequest(final String method, final HttpHost host, final String path) {
84          super();
85          this.method = Args.notNull(method, "Method name");
86          this.scheme = host != null ? host.getSchemeName() : null;
87          this.authority = host != null ? new URIAuthority(host) : null;
88          this.path = path;
89      }
90  
91      /**
92       * Creates request message with the given method, request URI.
93       *
94       * @param method request method.
95       * @param requestUri request URI.
96       *
97       * @since 5.0
98       */
99      public BasicHttpRequest(final String method, final URI requestUri) {
100         super();
101         this.method = Args.notNull(method, "Method name");
102         setUri(Args.notNull(requestUri, "Request URI"));
103     }
104 
105     @Override
106     public void addHeader(final String name, final Object value) {
107         Args.notNull(name, "Header name");
108         addHeader(new BasicHeader(name, value));
109     }
110 
111     @Override
112     public void setHeader(final String name, final Object value) {
113         Args.notNull(name, "Header name");
114         setHeader(new BasicHeader(name, value));
115     }
116 
117     @Override
118     public void setVersion(final ProtocolVersion version) {
119         this.version = version;
120     }
121 
122     @Override
123     public ProtocolVersion getVersion() {
124         return this.version;
125     }
126 
127     @Override
128     public String getMethod() {
129         return this.method;
130     }
131 
132     @Override
133     public String getPath() {
134         return this.path;
135     }
136 
137     @Override
138     public void setPath(final String path) {
139         this.path = path;
140         this.requestUri = null;
141     }
142 
143     @Override
144     public String getScheme() {
145         return this.scheme;
146     }
147 
148     @Override
149     public void setScheme(final String scheme) {
150         this.scheme = scheme;
151         this.requestUri = null;
152     }
153 
154     @Override
155     public URIAuthority getAuthority() {
156         return this.authority;
157     }
158 
159     @Override
160     public void setAuthority(final URIAuthority authority) {
161         this.authority = authority;
162         this.requestUri = null;
163     }
164 
165     @Override
166     public String getRequestUri() {
167         return getPath();
168     }
169 
170     void setUri(final URI requestUri) {
171         this.scheme = requestUri.getScheme();
172         this.authority = requestUri.getHost() != null ? new URIAuthority(
173                 requestUri.getRawUserInfo(),
174                 requestUri.getHost(),
175                 requestUri.getPort()) : null;
176         final StringBuilder buf = new StringBuilder();
177         final String path = requestUri.getRawPath();
178         if (!TextUtils.isBlank(path)) {
179             buf.append(path);
180         } else {
181             buf.append("/");
182         }
183         final String query = requestUri.getRawQuery();
184         if (query != null) {
185             buf.append('?').append(query);
186         }
187         this.path = buf.toString();
188     }
189 
190     @Override
191     public URI getUri() throws URISyntaxException {
192         if (this.requestUri == null) {
193             final StringBuilder buf = new StringBuilder();
194             if (this.authority != null) {
195                 buf.append(this.scheme != null ? this.scheme : "http").append("://");
196                 buf.append(this.authority.getHostName());
197                 if (this.authority.getPort() >= 0) {
198                     buf.append(":").append(this.authority.getPort());
199                 }
200             }
201             if (this.path == null) {
202                 buf.append("/");
203             } else {
204                 if (buf.length() > 0 && !this.path.startsWith("/")) {
205                     buf.append("/");
206                 }
207                 buf.append(this.path);
208             }
209             this.requestUri = new URI(buf.toString());
210         }
211         return this.requestUri;
212     }
213 
214     @Override
215     public String toString() {
216         final StringBuilder sb = new StringBuilder();
217         sb.append(this.method).append(" ").append(this.scheme).append("://").append(this.authority).append(this.path);
218         return sb.toString();
219     }
220 
221 }