1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.proxy.handlers.http;
21
22 import java.net.InetSocketAddress;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.mina.proxy.ProxyAuthException;
29 import org.apache.mina.proxy.handlers.ProxyRequest;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39
40 public class HttpProxyRequest extends ProxyRequest {
41 private final static Logger logger = LoggerFactory
42 .getLogger(HttpProxyRequest.class);
43
44
45
46
47 public final String httpVerb;
48
49
50
51
52 public final String httpURI;
53
54
55
56
57 private String httpVersion;
58
59
60
61
62 private String host;
63
64
65
66
67 private Map<String, List<String>> headers;
68
69
70
71
72
73 private transient Map<String, String> properties;
74
75
76
77
78
79
80
81 public HttpProxyRequest(final InetSocketAddress endpointAddress) {
82 this(endpointAddress, HttpProxyConstants.HTTP_1_0, null);
83 }
84
85
86
87
88
89
90
91
92 public HttpProxyRequest(final InetSocketAddress endpointAddress,
93 final String httpVersion) {
94 this(endpointAddress, httpVersion, null);
95 }
96
97
98
99
100
101
102
103
104
105 public HttpProxyRequest(final InetSocketAddress endpointAddress,
106 final String httpVersion, final Map<String, List<String>> headers) {
107 this.httpVerb = HttpProxyConstants.CONNECT;
108 if (!endpointAddress.isUnresolved()) {
109 this.httpURI = endpointAddress.getHostName() + ":"
110 + endpointAddress.getPort();
111 } else {
112 this.httpURI = endpointAddress.getAddress().getHostAddress() + ":"
113 + endpointAddress.getPort();
114 }
115
116 this.httpVersion = httpVersion;
117 this.headers = headers;
118 }
119
120
121
122
123
124
125
126 public HttpProxyRequest(final String httpURI) {
127 this(HttpProxyConstants.GET, httpURI, HttpProxyConstants.HTTP_1_0, null);
128 }
129
130
131
132
133
134
135
136
137 public HttpProxyRequest(final String httpURI, final String httpVersion) {
138 this(HttpProxyConstants.GET, httpURI, httpVersion, null);
139 }
140
141
142
143
144
145
146
147
148
149 public HttpProxyRequest(final String httpVerb, final String httpURI,
150 final String httpVersion) {
151 this(httpVerb, httpURI, httpVersion, null);
152 }
153
154
155
156
157
158
159
160
161
162
163
164 public HttpProxyRequest(final String httpVerb, final String httpURI,
165 final String httpVersion, final Map<String, List<String>> headers) {
166 this.httpVerb = httpVerb;
167 this.httpURI = httpURI;
168 this.httpVersion = httpVersion;
169 this.headers = headers;
170 }
171
172
173
174
175 public final String getHttpVerb() {
176 return httpVerb;
177 }
178
179
180
181
182 public String getHttpVersion() {
183 return httpVersion;
184 }
185
186
187
188
189
190
191 public void setHttpVersion(String httpVersion) {
192 this.httpVersion = httpVersion;
193 }
194
195
196
197
198 public synchronized final String getHost() {
199 if (host == null) {
200 if (getEndpointAddress() != null &&
201 !getEndpointAddress().isUnresolved()) {
202 host = getEndpointAddress().getHostName();
203 }
204
205 if (host == null && httpURI != null) {
206 try {
207 host = (new URL(httpURI)).getHost();
208 } catch (MalformedURLException e) {
209 logger.debug("Malformed URL", e);
210 }
211 }
212 }
213
214 return host;
215 }
216
217
218
219
220 public final String getHttpURI() {
221 return httpURI;
222 }
223
224
225
226
227 public final Map<String, List<String>> getHeaders() {
228 return headers;
229 }
230
231
232
233
234 public final void setHeaders(Map<String, List<String>> headers) {
235 this.headers = headers;
236 }
237
238
239
240
241 public Map<String, String> getProperties() {
242 return properties;
243 }
244
245
246
247
248 public void setProperties(Map<String, String> properties) {
249 this.properties = properties;
250 }
251
252
253
254
255
256 public void checkRequiredProperties(String... propNames) throws ProxyAuthException {
257 StringBuilder sb = new StringBuilder();
258 for (String propertyName : propNames) {
259 if (properties.get(propertyName) == null) {
260 sb.append(propertyName).append(' ');
261 }
262 }
263 if (sb.length() > 0) {
264 sb.append("property(ies) missing in request");
265 throw new ProxyAuthException(sb.toString());
266 }
267 }
268
269
270
271
272 public String toHttpString() {
273 StringBuilder sb = new StringBuilder();
274
275 sb.append(getHttpVerb()).append(' ').append(getHttpURI()).append(' ')
276 .append(getHttpVersion()).append(HttpProxyConstants.CRLF);
277
278 boolean hostHeaderFound = false;
279
280 if (getHeaders() != null) {
281 for (Map.Entry<String, List<String>> header : getHeaders()
282 .entrySet()) {
283 if (!hostHeaderFound) {
284 hostHeaderFound = header.getKey().equalsIgnoreCase("host");
285 }
286
287 for (String value : header.getValue()) {
288 sb.append(header.getKey()).append(": ").append(value)
289 .append(HttpProxyConstants.CRLF);
290 }
291 }
292
293 if (!hostHeaderFound
294 && getHttpVersion() == HttpProxyConstants.HTTP_1_1) {
295 sb.append("Host: ").append(getHost()).append(
296 HttpProxyConstants.CRLF);
297 }
298 }
299
300 sb.append(HttpProxyConstants.CRLF);
301
302 return sb.toString();
303 }
304 }