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.Arrays;
31  
32  import org.apache.hc.core5.http.ContentType;
33  import org.apache.hc.core5.http.Header;
34  import org.apache.hc.core5.http.ProtocolVersion;
35  import org.apache.hc.core5.http.support.AbstractResponseBuilder;
36  import org.apache.hc.core5.util.Args;
37  
38  /**
39   * Builds {@link SimpleHttpResponse} instances.
40   *
41   * @since 5.1
42   */
43  public class SimpleResponseBuilder extends AbstractResponseBuilder<SimpleHttpResponse> {
44  
45      private SimpleBody body;
46  
47      SimpleResponseBuilder(final int status) {
48          super(status);
49      }
50  
51      /**
52       * Creates a new SimpleResponseBuilder with the given status.
53       *
54       * @param status response status.
55       * @return a new SimpleResponseBuilder.
56       */
57      public static SimpleResponseBuilder create(final int status) {
58          Args.checkRange(status, 100, 599, "HTTP status code");
59          return new SimpleResponseBuilder(status);
60      }
61  
62      /**
63       * Copies a source instance into a new one.
64       *
65       * @param response the source instance to copy.
66       * @return a new SimpleResponseBuilder.
67       */
68      public static SimpleResponseBuilder copy(final SimpleHttpResponse response) {
69          Args.notNull(response, "HTTP response");
70          final SimpleResponseBuilder builder = new SimpleResponseBuilder(response.getCode());
71          builder.digest(response);
72          return builder;
73      }
74  
75      protected void digest(final SimpleHttpResponse response) {
76          super.digest(response);
77          setBody(response.getBody());
78      }
79  
80      @Override
81      public SimpleResponseBuilder setVersion(final ProtocolVersion version) {
82          super.setVersion(version);
83          return this;
84      }
85  
86      @Override
87      public SimpleResponseBuilder setHeaders(final Header... headers) {
88          super.setHeaders(headers);
89          return this;
90      }
91  
92      @Override
93      public SimpleResponseBuilder addHeader(final Header header) {
94          super.addHeader(header);
95          return this;
96      }
97  
98      @Override
99      public SimpleResponseBuilder addHeader(final String name, final String value) {
100         super.addHeader(name, value);
101         return this;
102     }
103 
104     @Override
105     public SimpleResponseBuilder removeHeader(final Header header) {
106         super.removeHeader(header);
107         return this;
108     }
109 
110     @Override
111     public SimpleResponseBuilder removeHeaders(final String name) {
112         super.removeHeaders(name);
113         return this;
114     }
115 
116     @Override
117     public SimpleResponseBuilder setHeader(final Header header) {
118         super.setHeader(header);
119         return this;
120     }
121 
122     @Override
123     public SimpleResponseBuilder setHeader(final String name, final String value) {
124         super.setHeader(name, value);
125         return this;
126     }
127 
128     /**
129      * Gets the body.
130      *
131      * @return the body.
132      */
133     public SimpleBody getBody() {
134         return body;
135     }
136 
137     /**
138      * Sets the body.
139      *
140      * @param body the body.
141      * @return this instance.
142      */
143     public SimpleResponseBuilder setBody(final SimpleBody body) {
144         this.body = body;
145         return this;
146     }
147 
148     /**
149      * Sets the body.
150      *
151      * @param content the body content.
152      * @param contentType the body content-type.
153      * @return this instance.
154      */
155     public SimpleResponseBuilder setBody(final String content, final ContentType contentType) {
156         this.body = SimpleBody.create(content, contentType);
157         return this;
158     }
159 
160     /**
161      * Sets the body.
162      *
163      * @param content the body content.
164      * @param contentType the body content-type.
165      * @return this instance.
166      */
167     public SimpleResponseBuilder setBody(final byte[] content, final ContentType contentType) {
168         this.body = SimpleBody.create(content, contentType);
169         return this;
170     }
171 
172     @Override
173     public SimpleHttpResponse build() {
174         final SimpleHttpResponse result = new SimpleHttpResponse(getStatus());
175         result.setVersion(getVersion());
176         result.setHeaders(getHeaders());
177         result.setBody(body);
178         return result;
179     }
180 
181     @Override
182     public String toString() {
183         final StringBuilder builder = new StringBuilder();
184         builder.append("SimpleResponseBuilder [status=");
185         builder.append(getStatus());
186         builder.append(", headerGroup=");
187         builder.append(Arrays.toString(getHeaders()));
188         builder.append(", body=");
189         builder.append(body);
190         builder.append("]");
191         return builder.toString();
192     }
193 
194 }