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.client5.http.async.methods;
29  
30  import java.net.URI;
31  import java.util.Iterator;
32  
33  import org.apache.hc.core5.http.ContentType;
34  import org.apache.hc.core5.http.Header;
35  import org.apache.hc.core5.http.HttpHost;
36  import org.apache.hc.core5.http.HttpRequest;
37  import org.apache.hc.core5.http.Method;
38  import org.apache.hc.core5.net.URIAuthority;
39  import org.apache.hc.core5.util.Args;
40  
41  /**
42   * HTTP request that can enclose a body represented as a simple text string or an array of bytes.
43   * <p>
44   * IMPORTANT: {@link SimpleHttpRequest}s are intended for simple scenarios where entities inclosed
45   * in requests are known to be small. It is generally recommended to use
46   * {@link org.apache.hc.core5.http.nio.support.AsyncRequestBuilder} and streaming
47   * {@link org.apache.hc.core5.http.nio.AsyncEntityProducer}s.
48   *
49   * @since 5.0
50   *
51   * @see SimpleBody
52   * @see org.apache.hc.core5.http.nio.support.AsyncRequestBuilder
53   * @see org.apache.hc.core5.http.nio.AsyncEntityProducer
54   */
55  public final class SimpleHttpRequest extends ConfigurableHttpRequest {
56  
57      private static final long serialVersionUID = 1L;
58      private SimpleBody body;
59  
60      /**
61       * Creates a new request message with the given method and request path.
62       *
63       * @param method request method.
64       * @param uri request URI.
65       * @return a new SimpleHttpRequest.
66       * @since 5.1
67       */
68      public static SimpleHttpRequest create(final String method, final String uri) {
69          return new SimpleHttpRequest(method, uri);
70      }
71  
72      /**
73       * Creates a new request message with the given method and request path.
74       *
75       * @param method request method.
76       * @param uri request URI.
77       * @return a new SimpleHttpRequest.
78       * @since 5.1
79       */
80      public static SimpleHttpRequest create(final String method, final URI uri) {
81          return new SimpleHttpRequest(method, uri);
82      }
83  
84      /**
85       * Creates a new request message with the given method and request path.
86       *
87       * @param method request method.
88       * @param uri request URI.
89       * @return a new SimpleHttpRequest.
90       * @since 5.1
91       */
92      public static SimpleHttpRequest create(final Method method, final URI uri) {
93          return new SimpleHttpRequest(method, uri);
94      }
95  
96      /**
97       * Creates a new request message with the given method, host, and request path.
98       *
99       * @param method request method.
100      * @param host request host.
101      * @param path request path.
102      * @return a new SimpleHttpRequest.
103      * @since 5.1
104      */
105     public static SimpleHttpRequest create(final Method method, final HttpHost host, final String path) {
106         return new SimpleHttpRequest(method, host, path);
107     }
108 
109     /**
110      * Creates a new request message with the given method, scheme, authority, and request path.
111      *
112      * @param method request method.
113      * @param scheme request host.
114      * @param authority request URI authority.
115      * @param path request path.
116      * @return a new SimpleHttpRequest.
117      * @since 5.1
118      */
119     public static SimpleHttpRequest create(final String method, final String scheme, final URIAuthority authority, final String path) {
120         return new SimpleHttpRequest(method, scheme, authority, path);
121     }
122 
123     /**
124      * Copies the given HttpRequest.
125      *
126      * @param original the source to copy.
127      * @return a new SimpleHttpRequest.
128      * @deprecated Use {@link SimpleRequestBuilder}
129      */
130     @Deprecated
131     public static SimpleHttpRequest copy(final HttpRequest original) {
132         Args.notNull(original, "HTTP request");
133         final SimpleHttpRequest copy = new SimpleHttpRequest(original.getMethod(), original.getRequestUri());
134         copy.setVersion(original.getVersion());
135         for (final Iterator<Header> it = original.headerIterator(); it.hasNext(); ) {
136             copy.addHeader(it.next());
137         }
138         copy.setScheme(original.getScheme());
139         copy.setAuthority(original.getAuthority());
140         return copy;
141     }
142 
143     /**
144      * Constructs a new request message with the given method and request path.
145      *
146      * @param method request method.
147      * @param path request path.
148      */
149     public SimpleHttpRequest(final String method, final String path) {
150         super(method, path);
151     }
152 
153     /**
154      * Constructs a new request message with the given method, host, and request path.
155      *
156      * @param method request method.
157      * @param host request host.
158      * @param path request path.
159      */
160     public SimpleHttpRequest(final String method, final HttpHost host, final String path) {
161         super(method, host, path);
162     }
163 
164     /**
165      * Constructs a new request message with the given method, and request URI.
166      *
167      * @param method request method.
168      * @param requestUri request URI.
169      * @since 5.1
170      */
171     public SimpleHttpRequest(final String method, final URI requestUri) {
172         super(method, requestUri);
173     }
174 
175     /**
176      * Constructs a new request message with the given method, and request URI.
177      *
178      * @param method request method.
179      * @param requestUri request URI.
180      * @since 5.1
181      */
182     public SimpleHttpRequest(final Method method, final URI requestUri) {
183         this(method.name(), requestUri);
184     }
185 
186     /**
187      * Constructs a new request message with the given method, host, and request path.
188      *
189      * @param method request method.
190      * @param host request host.
191      * @param path request path.
192      * @since 5.1
193      */
194     public SimpleHttpRequest(final Method method, final HttpHost host, final String path) {
195         this(method.name(), host, path);
196     }
197 
198     /**
199      * Constructs a new request message with the given method, scheme, authority, and request path.
200      *
201      * @param method request method.
202      * @param scheme request host.
203      * @param authority request URI authority.
204      * @param path request path.
205      * @since 5.1
206      */
207     public SimpleHttpRequest(final String method, final String scheme, final URIAuthority authority, final String path) {
208         super(method, scheme, authority, path);
209     }
210 
211     /**
212      * Sets the message body and content type.
213      *
214      * @param body request body.
215      */
216     public void setBody(final SimpleBody body) {
217         this.body = body;
218     }
219 
220     /**
221      * Sets the message body and content type.
222      *
223      * @param bodyBytes request body.
224      * @param contentType request content type.
225      */
226     public void setBody(final byte[] bodyBytes, final ContentType contentType) {
227         this.body = SimpleBody.create(bodyBytes, contentType);
228     }
229 
230     /**
231      * Sets the message body and content type.
232      *
233      * @param bodyText request body.
234      * @param contentType request content type.
235      */
236     public void setBody(final String bodyText, final ContentType contentType) {
237         this.body = SimpleBody.create(bodyText, contentType);
238     }
239 
240     /**
241      * Gets the request body.
242      *
243      * @return the request body.
244      */
245     public SimpleBody getBody() {
246         return body;
247     }
248 
249     /**
250      * Gets the request content type.
251      *
252      * @return the request content type.
253      */
254     public ContentType getContentType() {
255         return body != null ? body.getContentType() : null;
256     }
257 
258     /**
259      * Gets the request body as a String.
260      *
261      * @return the request body.
262      */
263     public String getBodyText() {
264         return body != null ? body.getBodyText() : null;
265     }
266 
267     /**
268      * Gets the request body as a byte array.
269      *
270      * @return the request body.
271      */
272     public byte[] getBodyBytes() {
273         return body != null ? body.getBodyBytes() : null;
274     }
275 
276 }
277