1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
45
46
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 }