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.ProtocolException;
36  import org.apache.http.annotation.Immutable;
37  import org.apache.http.client.config.RequestConfig;
38  import org.apache.http.client.protocol.HttpClientContext;
39  import org.apache.http.conn.SchemePortResolver;
40  import org.apache.http.conn.UnsupportedSchemeException;
41  import org.apache.http.conn.routing.HttpRoute;
42  import org.apache.http.conn.routing.HttpRoutePlanner;
43  import org.apache.http.protocol.HttpContext;
44  import org.apache.http.util.Args;
45  
46  /**
47   * Default implementation of an {@link HttpRoutePlanner}. It will not make use of
48   * any Java system properties, nor of system or browser proxy settings.
49   *
50   * @since 4.3
51   */
52  @Immutable
53  public class DefaultRoutePlanner implements HttpRoutePlanner {
54  
55      private final SchemePortResolver schemePortResolver;
56  
57      public DefaultRoutePlanner(final SchemePortResolver schemePortResolver) {
58          super();
59          this.schemePortResolver = schemePortResolver != null ? schemePortResolver :
60              DefaultSchemePortResolver.INSTANCE;
61      }
62  
63      @Override
64      public HttpRoute determineRoute(
65              final HttpHost host,
66              final HttpRequest request,
67              final HttpContext context) throws HttpException {
68          Args.notNull(request, "Request");
69          if (host == null) {
70              throw new ProtocolException("Target host is not specified");
71          }
72          final HttpClientContext clientContext = HttpClientContext.adapt(context);
73          final RequestConfig config = clientContext.getRequestConfig();
74          final InetAddress local = config.getLocalAddress();
75          HttpHost proxy = config.getProxy();
76          if (proxy == null) {
77              proxy = determineProxy(host, request, context);
78          }
79  
80          final HttpHost target;
81          if (host.getPort() <= 0) {
82              try {
83                  target = new HttpHost(
84                          host.getHostName(),
85                          this.schemePortResolver.resolve(host),
86                          host.getSchemeName());
87              } catch (final UnsupportedSchemeException ex) {
88                  throw new HttpException(ex.getMessage());
89              }
90          } else {
91              target = host;
92          }
93          final boolean secure = target.getSchemeName().equalsIgnoreCase("https");
94          if (proxy == null) {
95              return new HttpRoute(target, local, secure);
96          } else {
97              return new HttpRoute(target, local, proxy, secure);
98          }
99      }
100 
101     /**
102      * This implementation returns null.
103      *
104      * @throws HttpException may be thrown if overridden
105      */
106     protected HttpHost determineProxy(
107             final HttpHost target,
108             final HttpRequest request,
109             final HttpContext context) throws HttpException {
110         return null;
111     }
112 
113 }