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  import java.util.Objects;
32  
33  import org.apache.hc.client5.http.HttpRoute;
34  import org.apache.hc.client5.http.SchemePortResolver;
35  import org.apache.hc.client5.http.config.RequestConfig;
36  import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
37  import org.apache.hc.client5.http.protocol.HttpClientContext;
38  import org.apache.hc.client5.http.routing.HttpRoutePlanner;
39  import org.apache.hc.client5.http.routing.RoutingSupport;
40  import org.apache.hc.core5.annotation.Contract;
41  import org.apache.hc.core5.annotation.ThreadingBehavior;
42  import org.apache.hc.core5.http.HttpException;
43  import org.apache.hc.core5.http.HttpHost;
44  import org.apache.hc.core5.http.HttpRequest;
45  import org.apache.hc.core5.http.ProtocolException;
46  import org.apache.hc.core5.http.URIScheme;
47  import org.apache.hc.core5.http.protocol.HttpContext;
48  import org.apache.hc.core5.net.NamedEndpoint;
49  import org.apache.hc.core5.net.URIAuthority;
50  
51  /**
52   * Default implementation of an {@link HttpRoutePlanner}. It will not make use of
53   * any Java system properties, nor of system or browser proxy settings.
54   *
55   * @since 4.3
56   */
57  @Contract(threading = ThreadingBehavior.STATELESS)
58  public class DefaultRoutePlanner implements HttpRoutePlanner {
59  
60      private final SchemePortResolver schemePortResolver;
61  
62      public DefaultRoutePlanner(final SchemePortResolver schemePortResolver) {
63          super();
64          this.schemePortResolver = schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE;
65      }
66  
67      private static boolean sameNamedEndpoint(final NamedEndpoint n1, final NamedEndpoint n2) {
68          if (n1 == null || n2 == null) {
69              return false;
70          }
71          return Objects.equals(n1.getHostName(), n2.getHostName()) && n1.getPort() == n2.getPort();
72      }
73  
74      @Override
75      public final HttpRoute determineRoute(final HttpHost host, final HttpRequest request, final HttpContext context) throws HttpException {
76          if (host == null) {
77              throw new ProtocolException("Target host is not specified");
78          }
79          final HttpClientContext clientContext = HttpClientContext.cast(context);
80          final RequestConfig config = clientContext.getRequestConfigOrDefault();
81          @SuppressWarnings("deprecation")
82          HttpHost proxy = config.getProxy();
83          if (proxy == null) {
84              proxy = determineProxy(host, context);
85          }
86          final HttpHost target = RoutingSupport.normalize(host, schemePortResolver);
87          if (target.getPort() < 0) {
88              throw new ProtocolException("Unroutable protocol scheme: " + target);
89          }
90          final boolean secure = URIScheme.HTTPS.same(target.getSchemeName());
91          final URIAuthority authority;
92          if (secure && request != null && !sameNamedEndpoint(request.getAuthority(), host)) {
93              authority = request.getAuthority();
94          } else {
95              authority = null;
96          }
97          final InetAddress inetAddress = determineLocalAddress(target, context);
98  
99          if (proxy == null) {
100             return new HttpRoute(target, authority, inetAddress, secure);
101         }
102         return new HttpRoute(target, authority, inetAddress, proxy, secure);
103     }
104 
105     @Override
106     public final HttpRoute determineRoute(final HttpHost host, final HttpContext context) throws HttpException {
107         return determineRoute(host, null, context);
108     }
109 
110     /**
111      * This implementation returns null.
112      *
113      * @throws HttpException may be thrown if overridden
114      */
115     protected HttpHost determineProxy(
116             final HttpHost target,
117             final HttpContext context) throws HttpException {
118         return null;
119     }
120 
121     /**
122      * This implementation returns null.
123      *
124      * @throws HttpException may be thrown if overridden
125      */
126     protected InetAddress determineLocalAddress(
127             final HttpHost firstHop,
128             final HttpContext context) throws HttpException {
129         return null;
130     }
131 
132 }