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.util.Args;
39  
40  /**
41   * HTTP request that can enclose a body represented as a simple text string or an array of bytes.
42   *
43   * @since 5.0
44   *
45   * @see SimpleBody
46   */
47  public final class SimpleHttpRequest extends ConfigurableHttpRequest {
48  
49      private static final long serialVersionUID = 1L;
50      private SimpleBody body;
51  
52      public static SimpleHttpRequest copy(final HttpRequest original) {
53          Args.notNull(original, "HTTP request");
54          final SimpleHttpRequestsync/methods/SimpleHttpRequest.html#SimpleHttpRequest">SimpleHttpRequest copy = new SimpleHttpRequest(original.getMethod(), original.getRequestUri());
55          copy.setVersion(original.getVersion());
56          for (final Iterator<Header> it = original.headerIterator(); it.hasNext(); ) {
57              copy.addHeader(it.next());
58          }
59          copy.setScheme(original.getScheme());
60          copy.setAuthority(original.getAuthority());
61          return copy;
62      }
63  
64      public SimpleHttpRequest(final String method, final String path) {
65          super(method, path);
66      }
67  
68      public SimpleHttpRequest(final String method, final HttpHost host, final String path) {
69          super(method, host, path);
70      }
71  
72      public SimpleHttpRequest(final String method, final URI requestUri) {
73          super(method, requestUri);
74      }
75  
76      SimpleHttpRequest(final Method method, final URI requestUri) {
77          this(method.name(), requestUri);
78      }
79  
80      SimpleHttpRequest(final Method method, final HttpHost host, final String path) {
81          this(method.name(), host, path);
82      }
83  
84      public void setBody(final SimpleBody body) {
85          this.body = body;
86      }
87  
88      public void setBody(final byte[] bodyBytes, final ContentType contentType) {
89          this.body = SimpleBody.create(bodyBytes, contentType);
90      }
91  
92      public void setBody(final String bodyText, final ContentType contentType) {
93          this.body = SimpleBody.create(bodyText, contentType);
94      }
95  
96      public SimpleBody getBody() {
97          return body;
98      }
99  
100     public ContentType getContentType() {
101         return body != null ? body.getContentType() : null;
102     }
103 
104     public String getBodyText() {
105         return body != null ? body.getBodyText() : null;
106     }
107 
108     public byte[] getBodyBytes() {
109         return body != null ? body.getBodyBytes() : null;
110     }
111 
112 }
113