View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  
16  package org.esigate.util;
17  
18  import java.util.List;
19  
20  import org.apache.http.Header;
21  import org.apache.http.HttpHeaders;
22  import org.apache.http.HttpHost;
23  import org.apache.http.HttpRequest;
24  import org.apache.http.NameValuePair;
25  import org.esigate.impl.DriverRequest;
26  
27  public final class HttpRequestHelper {
28  
29      private HttpRequestHelper() {
30      }
31  
32      public static String getFirstHeader(String name, HttpRequest request) {
33          final Header header = request.getFirstHeader(name);
34          String headerValue = null;
35          if (header != null) {
36              headerValue = header.getValue();
37          }
38          return headerValue;
39      }
40  
41      public static String getParameter(DriverRequest request, String name) {
42          String characterEncoding = request.getCharacterEncoding();
43          if (characterEncoding == null) {
44              characterEncoding = "ISO-8859-1";
45          }
46          List<NameValuePair> parameters =
47                  UriUtils.parse(request.getOriginalRequest().getRequestLine().getUri(), characterEncoding);
48          for (NameValuePair nameValuePair : parameters) {
49              if (nameValuePair.getName().equals(name)) {
50                  return nameValuePair.getValue();
51              }
52          }
53          return null;
54      }
55  
56      /**
57       * Returns the target host as defined in the Host header or extracted from the request URI.
58       * 
59       * Usefull to generate Host header in a HttpRequest
60       * 
61       * @param request
62       * @return the host formatted as host:port
63       */
64      public static HttpHost getHost(HttpRequest request) {
65          HttpHost httpHost = UriUtils.extractHost(request.getRequestLine().getUri());
66          String scheme = httpHost.getSchemeName();
67          String host = httpHost.getHostName();
68          int port = httpHost.getPort();
69          Header[] headers = request.getHeaders(HttpHeaders.HOST);
70          if (headers != null && headers.length != 0) {
71              String headerValue = headers[0].getValue();
72              String[] splitted = headerValue.split(":");
73              host = splitted[0];
74              if (splitted.length > 1) {
75                  port = Integer.parseInt(splitted[1]);
76              } else {
77                  port = -1;
78              }
79          }
80          return new HttpHost(host, port, scheme);
81      }
82  
83  }