View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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   * HttpProxyRequest.java - Wrapper class for HTTP requests.
35   * 
36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
37   * @since MINA 2.0.0-M3
38   */
39  public class HttpProxyRequest extends ProxyRequest {
40      private static final Logger LOGGER = LoggerFactory.getLogger(HttpProxyRequest.class);
41  
42      /**
43       * The HTTP verb.
44       */
45      private final String httpVerb;
46  
47      /**
48       * The HTTP URI.
49       */
50      private final String httpURI;
51  
52      /**
53       * The HTTP protocol version.
54       */
55      private String httpVersion;
56  
57      /**
58       * The target hostname.
59       */
60      private String host;
61  
62      /**
63       * The request headers.
64       */
65      private Map<String, List<String>> headers;
66  
67      /**
68       * The additionnal properties supplied to use with the proxy for 
69       * authentication for example. 
70       */
71      private Map<String, String> properties;
72  
73      /**
74       * Constructor which creates a HTTP/1.0 CONNECT request to the specified 
75       * endpoint.
76       *  
77       * @param endpointAddress the endpoint to connect to
78       */
79      public HttpProxyRequest(final InetSocketAddress endpointAddress) {
80          this(endpointAddress, HttpProxyConstants.HTTP_1_0, null);
81      }
82  
83      /**
84       * Constructor which creates a CONNECT request to the specified endpoint
85       * using the provided protocol version.
86       *  
87       * @param endpointAddress the endpoint to connect to
88       * @param httpVersion the HTTP protocol version
89       */
90      public HttpProxyRequest(final InetSocketAddress endpointAddress, final String httpVersion) {
91          this(endpointAddress, httpVersion, null);
92      }
93  
94      /**
95       * Constructor which creates a CONNECT request to the specified endpoint
96       * using the provided protocol version and setting the requested headers.
97       *  
98       * @param endpointAddress the endpoint to connect to
99       * @param httpVersion the HTTP protocol version
100      * @param headers the additionnal http headers
101      */
102     public HttpProxyRequest(final InetSocketAddress endpointAddress, final String httpVersion,
103             final Map<String, List<String>> headers) {
104         this.httpVerb = HttpProxyConstants.CONNECT;
105         if (endpointAddress.isUnresolved()) {
106             this.httpURI = endpointAddress.getHostName() + ":" + endpointAddress.getPort();
107         } else {
108             this.httpURI = endpointAddress.getAddress().getHostAddress() + ":" + endpointAddress.getPort();
109         }
110 
111         this.httpVersion = httpVersion;
112         this.headers = headers;
113     }
114 
115     /**
116      * Constructor which creates a HTTP/1.0 GET request to the specified 
117      * http URI.
118      *  
119      * @param httpURI the target URI
120      */
121     public HttpProxyRequest(final String httpURI) {
122         this(HttpProxyConstants.GET, httpURI, HttpProxyConstants.HTTP_1_0, null);
123     }
124 
125     /**
126      * Constructor which creates a GET request to the specified http URI
127      * using the provided protocol version
128      *  
129      * @param httpURI the target URI
130      * @param httpVersion the HTTP protocol version
131      */
132     public HttpProxyRequest(final String httpURI, final String httpVersion) {
133         this(HttpProxyConstants.GET, httpURI, httpVersion, null);
134     }
135 
136     /**
137      * Constructor which creates a request using the provided HTTP verb targeted at
138      * the specified http URI using the provided protocol version.
139      * 
140      * @param httpVerb the HTTP verb to use 
141      * @param httpURI the target URI
142      * @param httpVersion the HTTP protocol version
143      */
144     public HttpProxyRequest(final String httpVerb, final String httpURI, final String httpVersion) {
145         this(httpVerb, httpURI, httpVersion, null);
146     }
147 
148     /**
149      * Constructor which creates a request using the provided HTTP verb targeted at
150      * the specified http URI using the provided protocol version and setting the 
151      * requested headers.
152      * 
153      * @param httpVerb the HTTP verb to use 
154      * @param httpURI the target URI
155      * @param httpVersion the HTTP protocol version
156      * @param headers the additional http headers
157      */
158     public HttpProxyRequest(final String httpVerb, final String httpURI, final String httpVersion,
159             final Map<String, List<String>> headers) {
160         this.httpVerb = httpVerb;
161         this.httpURI = httpURI;
162         this.httpVersion = httpVersion;
163         this.headers = headers;
164     }
165 
166     /**
167      * @return the HTTP request verb.
168      */
169     public final String getHttpVerb() {
170         return httpVerb;
171     }
172 
173     /**
174      * @return the HTTP version.
175      */
176     public String getHttpVersion() {
177         return httpVersion;
178     }
179 
180     /**
181      * Sets the HTTP version.
182      * 
183      * @param httpVersion the HTTP protocol version
184      */
185     public void setHttpVersion(String httpVersion) {
186         this.httpVersion = httpVersion;
187     }
188 
189     /**
190      * @return the host to which we are connecting.
191      */
192     public final synchronized  String getHost() {
193         if (host == null) {
194             if (getEndpointAddress() != null && !getEndpointAddress().isUnresolved()) {
195                 host = getEndpointAddress().getHostName();
196             }
197 
198             if (host == null && httpURI != null) {
199                 try {
200                     host = (new URL(httpURI)).getHost();
201                 } catch (MalformedURLException e) {
202                     if (LOGGER.isDebugEnabled()) {
203                         LOGGER.debug("Malformed URL", e);
204                     }
205                 }
206             }
207         }
208 
209         return host;
210     }
211 
212     /**
213      * @return the request HTTP URI.
214      */
215     public final String getHttpURI() {
216         return httpURI;
217     }
218 
219     /**
220      * @return the HTTP headers.
221      */
222     public final Map<String, List<String>> getHeaders() {
223         return headers;
224     }
225 
226     /**
227      * Set the HTTP headers.
228      * 
229      * @param headers The HTTP headers to set
230      */
231     public final void setHeaders(Map<String, List<String>> headers) {
232         this.headers = headers;
233     }
234 
235     /**
236      * @return additional properties for the request.
237      */
238     public Map<String, String> getProperties() {
239         return properties;
240     }
241 
242     /**
243      * Set additional properties for the request.
244      * 
245      * @param properties The properties to add to the reqyest
246      */
247     public void setProperties(Map<String, String> properties) {
248         this.properties = properties;
249     }
250 
251     /**
252      * Check if the given property(ies) is(are) set. Otherwise throws a 
253      * {@link ProxyAuthException}.
254      * 
255      * @param propNames The list of property name to check
256      * @throws ProxyAuthException If we get an error during the proxy authentication
257      */
258     public void checkRequiredProperties(String... propNames) throws ProxyAuthException {
259         StringBuilder sb = new StringBuilder();
260         for (String propertyName : propNames) {
261             if (properties.get(propertyName) == null) {
262                 sb.append(propertyName).append(' ');
263             }
264         }
265         if (sb.length() > 0) {
266             sb.append("property(ies) missing in request");
267             throw new ProxyAuthException(sb.toString());
268         }
269     }
270 
271     /**
272      * @return the string representation of the HTTP request .
273      */
274     public String toHttpString() {
275         StringBuilder sb = new StringBuilder();
276 
277         sb.append(getHttpVerb()).append(' ').append(getHttpURI()).append(' ').append(getHttpVersion())
278                 .append(HttpProxyConstants.CRLF);
279 
280         boolean hostHeaderFound = false;
281 
282         if (getHeaders() != null) {
283             for (Map.Entry<String, List<String>> header : getHeaders().entrySet()) {
284                 if (!hostHeaderFound) {
285                     hostHeaderFound = "host".equalsIgnoreCase(header.getKey());
286                 }
287 
288                 for (String value : header.getValue()) {
289                     sb.append(header.getKey()).append(": ").append(value).append(HttpProxyConstants.CRLF);
290                 }
291             }
292 
293             if (!hostHeaderFound && HttpProxyConstants.HTTP_1_1.equals(getHttpVersion())) {
294                 sb.append("Host: ").append(getHost()).append(HttpProxyConstants.CRLF);
295             }
296         }
297 
298         sb.append(HttpProxyConstants.CRLF);
299 
300         return sb.toString();
301     }
302 }