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.support;
29
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import java.nio.charset.Charset;
33 import java.util.ArrayList;
34 import java.util.LinkedList;
35 import java.util.List;
36
37 import org.apache.hc.core5.http.Header;
38 import org.apache.hc.core5.http.HttpHost;
39 import org.apache.hc.core5.http.HttpRequest;
40 import org.apache.hc.core5.http.Method;
41 import org.apache.hc.core5.http.NameValuePair;
42 import org.apache.hc.core5.http.ProtocolVersion;
43 import org.apache.hc.core5.http.URIScheme;
44 import org.apache.hc.core5.http.message.BasicHttpRequest;
45 import org.apache.hc.core5.http.message.BasicNameValuePair;
46 import org.apache.hc.core5.net.URIAuthority;
47 import org.apache.hc.core5.util.TextUtils;
48
49
50
51
52
53
54 public abstract class AbstractRequestBuilder<T> extends AbstractMessageBuilder<T> {
55
56 final private String method;
57 private String scheme;
58 private URIAuthority authority;
59 private String path;
60 private Charset charset;
61 private List<NameValuePair> parameters;
62 private boolean absoluteRequestUri;
63
64 protected AbstractRequestBuilder(final String method) {
65 super();
66 this.method = method;
67 }
68
69 protected AbstractRequestBuilder(final Method method) {
70 this(method.name());
71 }
72
73 protected AbstractRequestBuilder(final String method, final URI uri) {
74 super();
75 this.method = method;
76 setUri(uri);
77 }
78
79 protected AbstractRequestBuilder(final Method method, final URI uri) {
80 this(method.name(), uri);
81 }
82
83 protected AbstractRequestBuilder(final Method method, final String uri) {
84 this(method.name(), uri != null ? URI.create(uri) : null);
85 }
86
87 protected AbstractRequestBuilder(final String method, final String uri) {
88 this(method, uri != null ? URI.create(uri) : null);
89 }
90
91 protected void digest(final HttpRequest request) {
92 if (request == null) {
93 return;
94 }
95 setScheme(request.getScheme());
96 setAuthority(request.getAuthority());
97 setPath(request.getPath());
98 this.parameters = null;
99 super.digest(request);
100 }
101
102 public String getMethod() {
103 return method;
104 }
105
106 @Override
107 public AbstractRequestBuilder<T> setVersion(final ProtocolVersion version) {
108 super.setVersion(version);
109 return this;
110 }
111
112 public String getScheme() {
113 return scheme;
114 }
115
116 public AbstractRequestBuilder<T> setScheme(final String scheme) {
117 this.scheme = scheme;
118 return this;
119 }
120
121 public URIAuthority getAuthority() {
122 return authority;
123 }
124
125 public AbstractRequestBuilder<T> setAuthority(final URIAuthority authority) {
126 this.authority = authority;
127 return this;
128 }
129
130 public AbstractRequestBuilder<T> setHttpHost(final HttpHost httpHost) {
131 if (httpHost == null) {
132 return this;
133 }
134 this.authority = new URIAuthority(httpHost);
135 this.scheme = httpHost.getSchemeName();
136 return this;
137 }
138
139 public String getPath() {
140 return path;
141 }
142
143 public AbstractRequestBuilder<T> setPath(final String path) {
144 this.path = path;
145 return this;
146 }
147
148 public URI getUri() {
149 final StringBuilder buf = new StringBuilder();
150 if (this.authority != null) {
151 buf.append(this.scheme != null ? this.scheme : URIScheme.HTTP.id).append("://");
152 buf.append(this.authority.getHostName());
153 if (this.authority.getPort() >= 0) {
154 buf.append(":").append(this.authority.getPort());
155 }
156 }
157 if (this.path == null) {
158 buf.append("/");
159 } else {
160 if (buf.length() > 0 && !this.path.startsWith("/")) {
161 buf.append("/");
162 }
163 buf.append(this.path);
164 }
165 return URI.create(buf.toString());
166 }
167
168 public AbstractRequestBuilder<T> setUri(final URI uri) {
169 if (uri == null) {
170 this.scheme = null;
171 this.authority = null;
172 this.path = null;
173 } else {
174 this.scheme = uri.getScheme();
175 if (uri.getHost() != null) {
176 this.authority = new URIAuthority(uri.getRawUserInfo(), uri.getHost(), uri.getPort());
177 } else if (uri.getRawAuthority() != null) {
178 try {
179 this.authority = URIAuthority.create(uri.getRawAuthority());
180 } catch (final URISyntaxException ignore) {
181 this.authority = null;
182 }
183 } else {
184 this.authority = null;
185 }
186 final StringBuilder buf = new StringBuilder();
187 final String rawPath = uri.getRawPath();
188 if (!TextUtils.isBlank(rawPath)) {
189 buf.append(rawPath);
190 } else {
191 buf.append("/");
192 }
193 final String query = uri.getRawQuery();
194 if (query != null) {
195 buf.append('?').append(query);
196 }
197 this.path = buf.toString();
198 }
199 return this;
200 }
201
202 public AbstractRequestBuilder<T> setUri(final String uri) {
203 setUri(uri != null ? URI.create(uri) : null);
204 return this;
205 }
206
207 @Override
208 public AbstractRequestBuilder<T> setHeaders(final Header... headers) {
209 super.setHeaders(headers);
210 return this;
211 }
212
213 @Override
214 public AbstractRequestBuilder<T> addHeader(final Header header) {
215 super.addHeader(header);
216 return this;
217 }
218
219 @Override
220 public AbstractRequestBuilder<T> addHeader(final String name, final String value) {
221 super.addHeader(name, value);
222 return this;
223 }
224
225 @Override
226 public AbstractRequestBuilder<T> removeHeader(final Header header) {
227 super.removeHeader(header);
228 return this;
229 }
230
231 @Override
232 public AbstractRequestBuilder<T> removeHeaders(final String name) {
233 super.removeHeaders(name);
234 return this;
235 }
236
237 @Override
238 public AbstractRequestBuilder<T> setHeader(final Header header) {
239 super.setHeader(header);
240 return this;
241 }
242
243 @Override
244 public AbstractRequestBuilder<T> setHeader(final String name, final String value) {
245 super.setHeader(name, value);
246 return this;
247 }
248
249 public Charset getCharset() {
250 return charset;
251 }
252
253 public AbstractRequestBuilder<T> setCharset(final Charset charset) {
254 this.charset = charset;
255 return this;
256 }
257
258 public List<NameValuePair> getParameters() {
259 return parameters != null ? new ArrayList<>(parameters) : null;
260 }
261
262 public AbstractRequestBuilder<T> addParameter(final NameValuePair nvp) {
263 if (nvp == null) {
264 return this;
265 }
266 if (parameters == null) {
267 parameters = new LinkedList<>();
268 }
269 parameters.add(nvp);
270 return this;
271 }
272
273 public AbstractRequestBuilder<T> addParameter(final String name, final String value) {
274 return addParameter(new BasicNameValuePair(name, value));
275 }
276
277 public AbstractRequestBuilder<T> addParameters(final NameValuePair... nvps) {
278 for (final NameValuePair nvp : nvps) {
279 addParameter(nvp);
280 }
281 return this;
282 }
283
284 public boolean isAbsoluteRequestUri() {
285 return absoluteRequestUri;
286 }
287
288 public AbstractRequestBuilder<T> setAbsoluteRequestUri(final boolean absoluteRequestUri) {
289 this.absoluteRequestUri = absoluteRequestUri;
290 return this;
291 }
292
293 }