View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.conn;
29  
30  import java.net.InetAddress;
31  
32  import org.apache.http.HttpException;
33  import org.apache.http.HttpHost;
34  import org.apache.http.HttpRequest;
35  import org.apache.http.annotation.Immutable;
36  import org.apache.http.client.config.RequestConfig;
37  import org.apache.http.client.protocol.HttpClientContext;
38  import org.apache.http.conn.SchemePortResolver;
39  import org.apache.http.conn.UnsupportedSchemeException;
40  import org.apache.http.conn.routing.HttpRoute;
41  import org.apache.http.conn.routing.HttpRoutePlanner;
42  import org.apache.http.protocol.HttpContext;
43  import org.apache.http.util.Args;
44  
45  /**
46   * Default implementation of an {@link HttpRoutePlanner}. It will not make use of
47   * any Java system properties, nor of system or browser proxy settings.
48   *
49   * @since 4.3
50   */
51  @Immutable
52  public class DefaultRoutePlanner implements HttpRoutePlanner {
53  
54      private final SchemePortResolver schemePortResolver;
55  
56      public DefaultRoutePlanner(final SchemePortResolver schemePortResolver) {
57          super();
58          this.schemePortResolver = schemePortResolver != null ? schemePortResolver :
59              DefaultSchemePortResolver.INSTANCE;
60      }
61  
62      public HttpRoute determineRoute(
63              final HttpHost host,
64              final HttpRequest request,
65              final HttpContext context) throws HttpException {
66          Args.notNull(host, "Target host");
67          Args.notNull(request, "Request");
68          final HttpClientContext clientContext = HttpClientContext.adapt(context);
69          final RequestConfig config = clientContext.getRequestConfig();
70          final InetAddress local = config.getLocalAddress();
71          HttpHost proxy = config.getProxy();
72          if (proxy == null) {
73              proxy = determineProxy(host, request, context);
74          }
75  
76          final HttpHost target;
77          if (host.getPort() <= 0) {
78              try {
79                  target = new HttpHost(
80                          host.getHostName(),
81                          this.schemePortResolver.resolve(host),
82                          host.getSchemeName());
83              } catch (final UnsupportedSchemeException ex) {
84                  throw new HttpException(ex.getMessage());
85              }
86          } else {
87              target = host;
88          }
89          final boolean secure = target.getSchemeName().equalsIgnoreCase("https");
90          if (proxy == null) {
91              return new HttpRoute(target, local, secure);
92          } else {
93              return new HttpRoute(target, local, proxy, secure);
94          }
95      }
96  
97      protected HttpHost determineProxy(
98              final HttpHost target,
99              final HttpRequest request,
100             final HttpContext context) throws HttpException {
101         return null;
102     }
103 
104 }