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