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 The Apache MINA Project (dev@mina.apache.org)
37   * @version $Rev: 685703 $, $Date: 2008-08-14 00:14:47 +0200 (Thu, 14 Aug 2008) $
38   * @since MINA 2.0.0-M3
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       * The request verb.
96       */
97      public final String getHttpVerb() {
98          return httpVerb;
99      }
100 
101     /**
102      * The HTTP version.
103      */
104     public String getHttpVersion() {
105         return httpVersion;
106     }
107 
108     /**
109      * Sets the HTTP version.
110      */
111     public void setHttpVersion(String httpVersion) {
112         this.httpVersion = httpVersion;
113     }
114 
115     /**
116      * Returns the host to which we are connecting.
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      * The request URI.
138      */
139     public final String getHttpURI() {
140         return httpURI;
141     }
142 
143     /**
144      * HTTP headers.
145      */
146     public final Map<String, List<String>> getHeaders() {
147         return headers;
148     }
149 
150     /**
151      * Set the HTTP headers.
152      */
153     public final void setHeaders(Map<String, List<String>> headers) {
154         this.headers = headers;
155     }
156 
157     /**
158      * Get the additional properties.
159      */
160     public Map<String, String> getProperties() {
161         return properties;
162     }
163 
164     /**
165      * Set the additional properties.
166      */
167     public void setProperties(Map<String, String> properties) {
168         this.properties = properties;
169     }
170 
171     /**
172      * Check if the property is set otherwise throw a{@link ProxyAuthException}.
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      * Returns the string representation of the HTTP request .
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 }