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.Iterator;
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.HttpVersion;
37  import org.apache.hc.core5.http.ProtocolVersion;
38  import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
39  import org.apache.hc.core5.http.io.entity.StringEntity;
40  import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
41  import org.apache.hc.core5.http.message.BasicHeader;
42  import org.apache.hc.core5.http.message.HeaderGroup;
43  import org.apache.hc.core5.util.Args;
44  
45  /**
46   * Builder for {@link ClassicHttpResponse} instances.
47   *
48   * @since 5.0
49   */
50  public class ClassicResponseBuilder {
51  
52      private int status;
53      private ProtocolVersion version;
54      private HeaderGroup headerGroup;
55      private HttpEntity entity;
56  
57      ClassicResponseBuilder() {
58      }
59  
60      ClassicResponseBuilder(final int status) {
61          super();
62          this.status = status;
63      }
64  
65      public static ClassicResponseBuilder create(final int status) {
66          Args.checkRange(status, 100, 599, "HTTP status code");
67          return new ClassicResponseBuilder(status);
68      }
69  
70      public ProtocolVersion getVersion() {
71          return version;
72      }
73  
74      public ClassicResponseBuilder setVersion(final ProtocolVersion version) {
75          this.version = version;
76          return this;
77      }
78  
79      public Header[] getHeaders(final String name) {
80          return headerGroup != null ? headerGroup.getHeaders(name) : null;
81      }
82  
83      public ClassicResponseBuilder setHeaders(final Header... headers) {
84          if (headerGroup == null) {
85              headerGroup = new HeaderGroup();
86          }
87          headerGroup.setHeaders(headers);
88          return this;
89      }
90  
91      public Header getFirstHeader(final String name) {
92          return headerGroup != null ? headerGroup.getFirstHeader(name) : null;
93      }
94  
95      public Header getLastHeader(final String name) {
96          return headerGroup != null ? headerGroup.getLastHeader(name) : null;
97      }
98  
99      public ClassicResponseBuilder addHeader(final Header header) {
100         if (headerGroup == null) {
101             headerGroup = new HeaderGroup();
102         }
103         headerGroup.addHeader(header);
104         return this;
105     }
106 
107     public ClassicResponseBuilder addHeader(final String name, final String value) {
108         if (headerGroup == null) {
109             headerGroup = new HeaderGroup();
110         }
111         this.headerGroup.addHeader(new BasicHeader(name, value));
112         return this;
113     }
114 
115     public ClassicResponseBuilder removeHeader(final Header header) {
116         if (headerGroup == null) {
117             headerGroup = new HeaderGroup();
118         }
119         headerGroup.removeHeader(header);
120         return this;
121     }
122 
123     public ClassicResponseBuilder removeHeaders(final String name) {
124         if (name == null || headerGroup == null) {
125             return this;
126         }
127         for (final Iterator<Header> i = headerGroup.headerIterator(); i.hasNext(); ) {
128             final Header header = i.next();
129             if (name.equalsIgnoreCase(header.getName())) {
130                 i.remove();
131             }
132         }
133         return this;
134     }
135 
136     public ClassicResponseBuilder setHeader(final Header header) {
137         if (headerGroup == null) {
138             headerGroup = new HeaderGroup();
139         }
140         this.headerGroup.setHeader(header);
141         return this;
142     }
143 
144     public ClassicResponseBuilder setHeader(final String name, final String value) {
145         if (headerGroup == null) {
146             headerGroup = new HeaderGroup();
147         }
148         this.headerGroup.setHeader(new BasicHeader(name, value));
149         return this;
150     }
151 
152     public HttpEntity getEntity() {
153         return entity;
154     }
155 
156     public ClassicResponseBuilder setEntity(final HttpEntity entity) {
157         this.entity = entity;
158         return this;
159     }
160 
161     public ClassicResponseBuilder setEntity(final String content, final ContentType contentType) {
162         this.entity = new StringEntity(content, contentType);
163         return this;
164     }
165 
166     public ClassicResponseBuilder setEntity(final String content) {
167         this.entity = new StringEntity(content);
168         return this;
169     }
170 
171     public ClassicResponseBuilder setEntity(final byte[] content, final ContentType contentType) {
172         this.entity = new ByteArrayEntity(content, contentType);
173         return this;
174     }
175 
176     public ClassicHttpResponse build() {
177         final ClassicHttpResponse result = new BasicClassicHttpResponse(status);
178         result.setVersion(this.version != null ? this.version : HttpVersion.HTTP_1_1);
179         if (this.headerGroup != null) {
180             result.setHeaders(this.headerGroup.getHeaders());
181         }
182         result.setEntity(entity);
183         return result;
184     }
185 
186     @Override
187     public String toString() {
188         final StringBuilder builder = new StringBuilder();
189         builder.append("ClassicResponseBuilder [method=");
190         builder.append(status);
191         builder.append(", status=");
192         builder.append(status);
193         builder.append(", version=");
194         builder.append(version);
195         builder.append(", headerGroup=");
196         builder.append(headerGroup);
197         builder.append(", entity=");
198         builder.append(entity != null ? entity.getClass() : null);
199         builder.append("]");
200         return builder.toString();
201     }
202 
203 }