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.core5.http.io.support;
29  
30  import java.util.Arrays;
31  
32  import org.apache.hc.core5.http.ClassicHttpResponse;
33  import org.apache.hc.core5.http.ContentType;
34  import org.apache.hc.core5.http.Header;
35  import org.apache.hc.core5.http.HttpEntity;
36  import org.apache.hc.core5.http.ProtocolVersion;
37  import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
38  import org.apache.hc.core5.http.io.entity.StringEntity;
39  import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
40  import org.apache.hc.core5.http.support.AbstractResponseBuilder;
41  import org.apache.hc.core5.util.Args;
42  
43  /**
44   * Builder for {@link ClassicHttpResponse} instances.
45   *
46   * @since 5.0
47   */
48  public class ClassicResponseBuilder extends AbstractResponseBuilder<ClassicHttpResponse> {
49  
50      private HttpEntity entity;
51  
52      ClassicResponseBuilder(final int status) {
53          super(status);
54      }
55  
56      public static ClassicResponseBuilder create(final int status) {
57          Args.checkRange(status, 100, 599, "HTTP status code");
58          return new ClassicResponseBuilder(status);
59      }
60  
61      /**
62       * @since 5.1
63       */
64      public static ClassicResponseBuilder copy(final ClassicHttpResponse response) {
65          Args.notNull(response, "HTTP response");
66          final ClassicResponseBuilderClassicResponseBuilder.html#ClassicResponseBuilder">ClassicResponseBuilder builder = new ClassicResponseBuilder(response.getCode());
67          builder.digest(response);
68          return builder;
69      }
70  
71      protected void digest(final ClassicHttpResponse response) {
72          super.digest(response);
73          setEntity(response.getEntity());
74      }
75  
76      @Override
77      public ClassicResponseBuilder setVersion(final ProtocolVersion version) {
78          super.setVersion(version);
79          return this;
80      }
81  
82      @Override
83      public ClassicResponseBuilder setHeaders(final Header... headers) {
84          super.setHeaders(headers);
85          return this;
86      }
87  
88      @Override
89      public ClassicResponseBuilder addHeader(final Header header) {
90          super.addHeader(header);
91          return this;
92      }
93  
94      @Override
95      public ClassicResponseBuilder addHeader(final String name, final    String value) {
96          super.addHeader(name, value);
97          return this;
98      }
99  
100     @Override
101     public ClassicResponseBuilder removeHeader(final Header header) {
102         super.removeHeader(header);
103         return this;
104     }
105 
106     @Override
107     public ClassicResponseBuilder removeHeaders(final String name) {
108         super.removeHeaders(name);
109         return this;
110     }
111 
112     @Override
113     public ClassicResponseBuilder setHeader(final Header header) {
114         super.setHeader(header);
115         return this;
116     }
117 
118     @Override
119     public ClassicResponseBuilder setHeader(final String name, final String value) {
120         super.setHeader(name, value);
121         return this;
122     }
123 
124     public HttpEntity getEntity() {
125         return entity;
126     }
127 
128     public ClassicResponseBuilder setEntity(final HttpEntity entity) {
129         this.entity = entity;
130         return this;
131     }
132 
133     public ClassicResponseBuilder setEntity(final String content, final ContentType contentType) {
134         this.entity = new StringEntity(content, contentType);
135         return this;
136     }
137 
138     public ClassicResponseBuilder setEntity(final String content) {
139         this.entity = new StringEntity(content);
140         return this;
141     }
142 
143     public ClassicResponseBuilder setEntity(final byte[] content, final ContentType contentType) {
144         this.entity = new ByteArrayEntity(content, contentType);
145         return this;
146     }
147 
148     public ClassicHttpResponse build() {
149         final BasicClassicHttpResponsecClassicHttpResponse.html#BasicClassicHttpResponse">BasicClassicHttpResponse result = new BasicClassicHttpResponse(getStatus());
150         result.setVersion(getVersion());
151         result.setHeaders(getHeaders());
152         result.setEntity(entity);
153         return result;
154     }
155 
156     @Override
157     public String toString() {
158         final StringBuilder builder = new StringBuilder();
159         builder.append("ClassicResponseBuilder [status=");
160         builder.append(getStatus());
161         builder.append(", headerGroup=");
162         builder.append(Arrays.toString(getHeaders()));
163         builder.append(", entity=");
164         builder.append(entity != null ? entity.getClass() : null);
165         builder.append("]");
166         return builder.toString();
167     }
168 
169 }