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