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   *
41   * @since 5.0
42   *
43   * @see SimpleBody
44   */
45  public final class SimpleHttpResponse extends BasicHttpResponse {
46  
47      private static final long serialVersionUID = 1L;
48      private SimpleBody body;
49  
50      public SimpleHttpResponse(final int code) {
51          super(code);
52      }
53  
54      public SimpleHttpResponse(final int code, final String reasonPhrase) {
55          super(code, reasonPhrase);
56      }
57  
58      public static SimpleHttpResponse copy(final HttpResponse original) {
59          Args.notNull(original, "HTTP response");
60          final SimpleHttpResponseync/methods/SimpleHttpResponse.html#SimpleHttpResponse">SimpleHttpResponse copy = new SimpleHttpResponse(original.getCode());
61          copy.setVersion(original.getVersion());
62          for (final Iterator<Header> it = original.headerIterator(); it.hasNext(); ) {
63              copy.addHeader(it.next());
64          }
65          return copy;
66      }
67  
68      public static SimpleHttpResponse create(final int code) {
69          return new SimpleHttpResponse(code);
70      }
71  
72      public static SimpleHttpResponse create(final int code, final String content, final ContentType contentType) {
73          final SimpleHttpResponsemethods/SimpleHttpResponse.html#SimpleHttpResponse">SimpleHttpResponse response = new SimpleHttpResponse(code);
74          if (content != null) {
75              response.setBody(content, contentType);
76          }
77          return response;
78      }
79  
80      public static SimpleHttpResponse create(final int code, final String content) {
81          return create(code, content, ContentType.TEXT_PLAIN);
82      }
83  
84      public static SimpleHttpResponse create(final int code, final byte[] content, final ContentType contentType) {
85          final SimpleHttpResponsemethods/SimpleHttpResponse.html#SimpleHttpResponse">SimpleHttpResponse response = new SimpleHttpResponse(code);
86          if (content != null) {
87              response.setBody(content, contentType);
88          }
89          return response;
90      }
91  
92      public static SimpleHttpResponse create(final int code, final byte[] content) {
93          return create(code, content, ContentType.TEXT_PLAIN);
94      }
95  
96      public void setBody(final SimpleBody body) {
97          this.body = body;
98      }
99  
100     public void setBody(final byte[] bodyBytes, final ContentType contentType) {
101         this.body = SimpleBody.create(bodyBytes, contentType);
102     }
103 
104     public void setBody(final String bodyText, final ContentType contentType) {
105         this.body = SimpleBody.create(bodyText, contentType);
106     }
107 
108     public SimpleBody getBody() {
109         return body;
110     }
111 
112     public ContentType getContentType() {
113         return body != null ? body.getContentType() : null;
114     }
115 
116     public String getBodyText() {
117         return body != null ? body.getBodyText() : null;
118     }
119 
120     public byte[] getBodyBytes() {
121         return body != null ? body.getBodyBytes() : null;
122     }
123 
124 }
125