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 public final String httpVerb;
45
46 public final String httpURI;
47
48 private String httpVersion;
49
50 private String host;
51
52 private Map<String, List<String>> headers;
53
54 private transient Map<String, String> properties;
55
56 public HttpProxyRequest(final InetSocketAddress endpointAddress) {
57 this(HttpProxyConstants.CONNECT, endpointAddress.getHostName() + ":"
58 + endpointAddress.getPort(), HttpProxyConstants.HTTP_1_0, null);
59 }
60
61 public HttpProxyRequest(final InetSocketAddress endpointAddress,
62 final String httpVersion) {
63 this(HttpProxyConstants.CONNECT, endpointAddress.getHostName() + ":"
64 + endpointAddress.getPort(), httpVersion, null);
65 }
66
67 public HttpProxyRequest(final InetSocketAddress endpointAddress,
68 final String httpVersion, final Map<String, List<String>> headers) {
69 this(HttpProxyConstants.CONNECT, endpointAddress.getHostName() + ":"
70 + endpointAddress.getPort(), httpVersion, headers);
71 }
72
73 public HttpProxyRequest(final String httpURI) {
74 this(HttpProxyConstants.GET, httpURI, HttpProxyConstants.HTTP_1_0, null);
75 }
76
77 public HttpProxyRequest(final String httpURI, final String httpVersion) {
78 this(HttpProxyConstants.GET, httpURI, httpVersion, null);
79 }
80
81 public HttpProxyRequest(final String httpVerb, final String httpURI,
82 final String httpVersion) {
83 this(httpVerb, httpURI, httpVersion, null);
84 }
85
86 public HttpProxyRequest(final String httpVerb, final String httpURI,
87 final String httpVersion, final Map<String, List<String>> headers) {
88 this.httpVerb = httpVerb;
89 this.httpURI = httpURI;
90 this.httpVersion = httpVersion;
91 this.headers = headers;
92 }
93
94
95
96
97 public final String getHttpVerb() {
98 return httpVerb;
99 }
100
101
102
103
104 public String getHttpVersion() {
105 return httpVersion;
106 }
107
108
109
110
111 public void setHttpVersion(String httpVersion) {
112 this.httpVersion = httpVersion;
113 }
114
115
116
117
118 public synchronized final String getHost() {
119 if (host == null) {
120 if (getEndpointAddress() != null) {
121 host = getEndpointAddress().getHostName();
122 }
123
124 if (host == null && httpURI != null) {
125 try {
126 host = (new URL(httpURI)).getHost();
127 } catch (MalformedURLException e) {
128 logger.debug("Malformed URL", e);
129 }
130 }
131 }
132
133 return host;
134 }
135
136
137
138
139 public final String getHttpURI() {
140 return httpURI;
141 }
142
143
144
145
146 public final Map<String, List<String>> getHeaders() {
147 return headers;
148 }
149
150
151
152
153 public final void setHeaders(Map<String, List<String>> headers) {
154 this.headers = headers;
155 }
156
157
158
159
160 public Map<String, String> getProperties() {
161 return properties;
162 }
163
164
165
166
167 public void setProperties(Map<String, String> properties) {
168 this.properties = properties;
169 }
170
171
172
173
174 public void checkRequiredProperty(String propertyName)
175 throws ProxyAuthException {
176 if (properties.get(propertyName) == null) {
177 StringBuilder sb = new StringBuilder("'");
178 sb.append(propertyName).append(
179 "' property not provided in the request properties");
180 throw new ProxyAuthException(sb.toString());
181 }
182 }
183
184
185
186
187 public String toHttpString() {
188 StringBuilder sb = new StringBuilder();
189
190 sb.append(getHttpVerb()).append(' ').append(getHttpURI()).append(' ')
191 .append(getHttpVersion()).append(HttpProxyConstants.CRLF);
192
193 boolean hostHeaderFound = false;
194
195 if (getHeaders() != null) {
196 for (Map.Entry<String, List<String>> header : getHeaders()
197 .entrySet()) {
198 if (!hostHeaderFound) {
199 hostHeaderFound = header.getKey().equalsIgnoreCase("host");
200 }
201
202 for (String value : header.getValue()) {
203 sb.append(header.getKey()).append(": ").append(value)
204 .append(HttpProxyConstants.CRLF);
205 }
206 }
207
208 if (!hostHeaderFound
209 && getHttpVersion() == HttpProxyConstants.HTTP_1_1) {
210 sb.append("Host: ").append(getHost()).append(
211 HttpProxyConstants.CRLF);
212 }
213 }
214
215 sb.append(HttpProxyConstants.CRLF);
216
217 return sb.toString();
218 }
219 }