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.hc.client5.http.impl.routing;
29  
30  import java.net.InetAddress;
31  
32  import org.apache.hc.client5.http.HttpRoute;
33  import org.apache.hc.client5.http.SchemePortResolver;
34  import org.apache.hc.client5.http.config.RequestConfig;
35  import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
36  import org.apache.hc.client5.http.protocol.HttpClientContext;
37  import org.apache.hc.client5.http.routing.HttpRoutePlanner;
38  import org.apache.hc.client5.http.routing.RoutingSupport;
39  import org.apache.hc.core5.annotation.Contract;
40  import org.apache.hc.core5.annotation.ThreadingBehavior;
41  import org.apache.hc.core5.http.HttpException;
42  import org.apache.hc.core5.http.HttpHost;
43  import org.apache.hc.core5.http.ProtocolException;
44  import org.apache.hc.core5.http.URIScheme;
45  import org.apache.hc.core5.http.protocol.HttpContext;
46  
47  /**
48   * Default implementation of an {@link HttpRoutePlanner}. It will not make use of
49   * any Java system properties, nor of system or browser proxy settings.
50   *
51   * @since 4.3
52   */
53  @Contract(threading = ThreadingBehavior.STATELESS)
54  public class DefaultRoutePlanner implements HttpRoutePlanner {
55  
56      private final SchemePortResolver schemePortResolver;
57  
58      public DefaultRoutePlanner(final SchemePortResolver schemePortResolver) {
59          super();
60          this.schemePortResolver = schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE;
61      }
62  
63      @Override
64      public final HttpRoute determineRoute(final HttpHost host, final HttpContext context) throws HttpException {
65          if (host == null) {
66              throw new ProtocolException("Target host is not specified");
67          }
68          final HttpClientContext clientContext = HttpClientContext.adapt(context);
69          final RequestConfig config = clientContext.getRequestConfig();
70          @SuppressWarnings("deprecation")
71          HttpHost proxy = config.getProxy();
72          if (proxy == null) {
73              proxy = determineProxy(host, context);
74          }
75          final HttpHost target = RoutingSupport.normalize(host, schemePortResolver);
76          if (target.getPort() < 0) {
77              throw new ProtocolException("Unroutable protocol scheme: " + target);
78          }
79          final boolean secure = target.getSchemeName().equalsIgnoreCase(URIScheme.HTTPS.getId());
80          if (proxy == null) {
81              return new HttpRoute(target, determineLocalAddress(target, context), secure);
82          }
83          return new HttpRoute(target, determineLocalAddress(proxy, context), proxy, secure);
84      }
85  
86      /**
87       * This implementation returns null.
88       *
89       * @throws HttpException may be thrown if overridden
90       */
91      protected HttpHost determineProxy(
92              final HttpHost target,
93              final HttpContext context) throws HttpException {
94          return null;
95      }
96  
97      /**
98       * This implementation returns null.
99       *
100      * @throws HttpException may be thrown if overridden
101      */
102     protected InetAddress determineLocalAddress(
103             final HttpHost firstHop,
104             final HttpContext context) throws HttpException {
105         return null;
106     }
107 
108 }