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