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