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