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       * @since 5.1
62       */
63      public static SimpleHttpRequest create(final String method, final String uri) {
64          return new SimpleHttpRequest(method, uri);
65      }
66  
67      /**
68       * @since 5.1
69       */
70      public static SimpleHttpRequest create(final String method, final URI uri) {
71          return new SimpleHttpRequest(method, uri);
72      }
73  
74      /**
75       * @since 5.1
76       */
77      public static SimpleHttpRequest create(final Method method, final URI uri) {
78          return new SimpleHttpRequest(method, uri);
79      }
80  
81      /**
82       * @since 5.1
83       */
84      public static SimpleHttpRequest create(final Method method, final HttpHost host, final String path) {
85          return new SimpleHttpRequest(method, host, path);
86      }
87  
88      /**
89       * @since 5.1
90       */
91      public static SimpleHttpRequest create(final String method, final String scheme, final URIAuthority authority, final String path) {
92          return new SimpleHttpRequest(method, scheme, authority, path);
93      }
94  
95      /**
96       * @deprecated Use {@link SimpleRequestBuilder}
97       */
98      @Deprecated
99      public static SimpleHttpRequest copy(final HttpRequest original) {
100         Args.notNull(original, "HTTP request");
101         final SimpleHttpRequest copy = new SimpleHttpRequest(original.getMethod(), original.getRequestUri());
102         copy.setVersion(original.getVersion());
103         for (final Iterator<Header> it = original.headerIterator(); it.hasNext(); ) {
104             copy.addHeader(it.next());
105         }
106         copy.setScheme(original.getScheme());
107         copy.setAuthority(original.getAuthority());
108         return copy;
109     }
110 
111     public SimpleHttpRequest(final String method, final String path) {
112         super(method, path);
113     }
114 
115     public SimpleHttpRequest(final String method, final HttpHost host, final String path) {
116         super(method, host, path);
117     }
118 
119     public SimpleHttpRequest(final String method, final URI requestUri) {
120         super(method, requestUri);
121     }
122 
123     /**
124      * @since 5.1
125      */
126     public SimpleHttpRequest(final Method method, final URI requestUri) {
127         this(method.name(), requestUri);
128     }
129 
130     /**
131      * @since 5.1
132      */
133     public SimpleHttpRequest(final Method method, final HttpHost host, final String path) {
134         this(method.name(), host, path);
135     }
136 
137     /**
138      * @since 5.1
139      */
140     public SimpleHttpRequest(final String method, final String scheme, final URIAuthority authority, final String path) {
141         super(method, scheme, authority, path);
142     }
143 
144     public void setBody(final SimpleBody body) {
145         this.body = body;
146     }
147 
148     public void setBody(final byte[] bodyBytes, final ContentType contentType) {
149         this.body = SimpleBody.create(bodyBytes, contentType);
150     }
151 
152     public void setBody(final String bodyText, final ContentType contentType) {
153         this.body = SimpleBody.create(bodyText, contentType);
154     }
155 
156     public SimpleBody getBody() {
157         return body;
158     }
159 
160     public ContentType getContentType() {
161         return body != null ? body.getContentType() : null;
162     }
163 
164     public String getBodyText() {
165         return body != null ? body.getBodyText() : null;
166     }
167 
168     public byte[] getBodyBytes() {
169         return body != null ? body.getBodyBytes() : null;
170     }
171 
172 }
173