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.util.Iterator;
31  
32  import org.apache.hc.core5.http.ContentType;
33  import org.apache.hc.core5.http.Header;
34  import org.apache.hc.core5.http.HttpResponse;
35  import org.apache.hc.core5.http.message.BasicHttpResponse;
36  import org.apache.hc.core5.util.Args;
37  
38  /**
39   * HTTP response that can enclose a body represented as a simple text string or an array of bytes.
40   * <p>
41   * IMPORTANT: {@link SimpleHttpResponse}s are intended for simple scenarios where entities inclosed
42   * in responses are known to be small. It is generally recommended to use streaming
43   * {@link org.apache.hc.core5.http.nio.AsyncResponseConsumer}s, for instance, such as based on
44   * {@link AbstractCharResponseConsumer} or {@link AbstractBinResponseConsumer}.
45   *
46   * @since 5.0
47   *
48   * @see SimpleBody
49   * @see AbstractCharResponseConsumer
50   * @see AbstractBinResponseConsumer
51   */
52  public final class SimpleHttpResponse extends BasicHttpResponse {
53  
54      private static final long serialVersionUID = 1L;
55      private SimpleBody body;
56  
57      public SimpleHttpResponse(final int code) {
58          super(code);
59      }
60  
61      public SimpleHttpResponse(final int code, final String reasonPhrase) {
62          super(code, reasonPhrase);
63      }
64  
65      public static SimpleHttpResponse copy(final HttpResponse original) {
66          Args.notNull(original, "HTTP response");
67          final SimpleHttpResponse copy = new SimpleHttpResponse(original.getCode());
68          copy.setVersion(original.getVersion());
69          for (final Iterator<Header> it = original.headerIterator(); it.hasNext(); ) {
70              copy.addHeader(it.next());
71          }
72          return copy;
73      }
74  
75      public static SimpleHttpResponse create(final int code) {
76          return new SimpleHttpResponse(code);
77      }
78  
79      public static SimpleHttpResponse create(final int code, final String content, final ContentType contentType) {
80          final SimpleHttpResponse response = new SimpleHttpResponse(code);
81          if (content != null) {
82              response.setBody(content, contentType);
83          }
84          return response;
85      }
86  
87      public static SimpleHttpResponse create(final int code, final String content) {
88          return create(code, content, ContentType.TEXT_PLAIN);
89      }
90  
91      public static SimpleHttpResponse create(final int code, final byte[] content, final ContentType contentType) {
92          final SimpleHttpResponse response = new SimpleHttpResponse(code);
93          if (content != null) {
94              response.setBody(content, contentType);
95          }
96          return response;
97      }
98  
99      public static SimpleHttpResponse create(final int code, final byte[] content) {
100         return create(code, content, ContentType.TEXT_PLAIN);
101     }
102 
103     public void setBody(final SimpleBody body) {
104         this.body = body;
105     }
106 
107     public void setBody(final byte[] bodyBytes, final ContentType contentType) {
108         this.body = SimpleBody.create(bodyBytes, contentType);
109     }
110 
111     public void setBody(final String bodyText, final ContentType contentType) {
112         this.body = SimpleBody.create(bodyText, contentType);
113     }
114 
115     public SimpleBody getBody() {
116         return body;
117     }
118 
119     public ContentType getContentType() {
120         return body != null ? body.getContentType() : null;
121     }
122 
123     public String getBodyText() {
124         return body != null ? body.getBodyText() : null;
125     }
126 
127     public byte[] getBodyBytes() {
128         return body != null ? body.getBodyBytes() : null;
129     }
130 
131 }
132