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 org.apache.hc.client5.http.RouteInfo;
31  import org.apache.hc.client5.http.routing.HttpRouteDirector;
32  import org.apache.hc.core5.annotation.Contract;
33  import org.apache.hc.core5.annotation.ThreadingBehavior;
34  import org.apache.hc.core5.util.Args;
35  
36  /**
37   * Basic {@link HttpRouteDirector} implementation.
38   *
39   * @since 4.0
40   */
41  @Contract(threading = ThreadingBehavior.STATELESS)
42  public class BasicRouteDirector implements HttpRouteDirector {
43  
44      /**
45       * Default instance of {@link BasicRouteDirector}.
46       *
47       * @since 5.2
48       */
49      public static final BasicRouteDirector INSTANCE = new BasicRouteDirector();
50  
51      /**
52       * Provides the next step.
53       *
54       * @param plan      the planned route
55       * @param fact      the currently established route, or
56       *                  {@code null} if nothing is established
57       *
58       * @return  one of the constants defined in this class, indicating
59       *          either the next step to perform, or success, or failure.
60       *          0 is for success, a negative value for failure.
61       */
62      @Override
63      public int nextStep(final RouteInfo plan, final RouteInfo fact) {
64          Args.notNull(plan, "Planned route");
65  
66          int step = UNREACHABLE;
67  
68          if (fact == null || fact.getHopCount() < 1) {
69              step = firstStep(plan);
70          } else if (plan.getHopCount() > 1) {
71              step = proxiedStep(plan, fact);
72          } else {
73              step = directStep(plan, fact);
74          }
75  
76          return step;
77  
78      } // nextStep
79  
80  
81      /**
82       * Determines the first step to establish a route.
83       *
84       * @param plan      the planned route
85       *
86       * @return  the first step
87       */
88      protected int firstStep(final RouteInfo plan) {
89  
90          return (plan.getHopCount() > 1) ?
91              CONNECT_PROXY : CONNECT_TARGET;
92      }
93  
94  
95      /**
96       * Determines the next step to establish a direct connection.
97       *
98       * @param plan      the planned route
99       * @param fact      the currently established route
100      *
101      * @return  one of the constants defined in this class, indicating
102      *          either the next step to perform, or success, or failure
103      */
104     protected int directStep(final RouteInfo plan, final RouteInfo fact) {
105 
106         if (fact.getHopCount() > 1) {
107             return UNREACHABLE;
108         }
109         if (!plan.getTargetHost().equals(fact.getTargetHost()))
110          {
111             return UNREACHABLE;
112         // If the security is too low, we could now suggest to layer
113         // a secure protocol on the direct connection. Layering on direct
114         // connections has not been supported in HttpClient 3.x, we don't
115         // consider it here until there is a real-life use case for it.
116         }
117 
118         // Should we tolerate if security is better than planned?
119         // (plan.isSecure() && !fact.isSecure())
120         if (plan.isSecure() != fact.isSecure()) {
121             return UNREACHABLE;
122         }
123 
124         // Local address has to match only if the plan specifies one.
125         if (plan.getLocalAddress() != null &&
126                 !plan.getLocalAddress().equals(fact.getLocalAddress())) {
127             return UNREACHABLE;
128         }
129 
130         return COMPLETE;
131     }
132 
133 
134     /**
135      * Determines the next step to establish a connection via proxy.
136      *
137      * @param plan      the planned route
138      * @param fact      the currently established route
139      *
140      * @return  one of the constants defined in this class, indicating
141      *          either the next step to perform, or success, or failure
142      */
143     protected int proxiedStep(final RouteInfo plan, final RouteInfo fact) {
144 
145         if (fact.getHopCount() <= 1) {
146             return UNREACHABLE;
147         }
148         if (!plan.getTargetHost().equals(fact.getTargetHost())) {
149             return UNREACHABLE;
150         }
151         final int phc = plan.getHopCount();
152         final int fhc = fact.getHopCount();
153         if (phc < fhc) {
154             return UNREACHABLE;
155         }
156 
157         for (int i = 0; i < fhc - 1; i++) {
158             if (!plan.getHopTarget(i).equals(fact.getHopTarget(i))) {
159                 return UNREACHABLE;
160             }
161         }
162         // now we know that the target matches and proxies so far are the same
163         if (phc > fhc)
164          {
165             return TUNNEL_PROXY; // need to extend the proxy chain
166         }
167 
168         // proxy chain and target are the same, check tunnelling and layering
169         if (fact.isTunnelled() && !plan.isTunnelled() ||
170                 fact.isLayered() && !plan.isLayered()) {
171             return UNREACHABLE;
172         }
173 
174         if (plan.isTunnelled() && !fact.isTunnelled()) {
175             return TUNNEL_TARGET;
176         }
177         if (plan.isLayered() && !fact.isLayered()) {
178             return LAYER_PROTOCOL;
179         }
180 
181         // tunnel and layering are the same, remains to check the security
182         // Should we tolerate if security is better than planned?
183         // (plan.isSecure() && !fact.isSecure())
184         if (plan.isSecure() != fact.isSecure()) {
185             return UNREACHABLE;
186         }
187 
188         return COMPLETE;
189     }
190 
191 }