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