• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/routing/BasicRouteDirector.java $
3  * $Revision: 620255 $
4  * $Date: 2008-02-10 02:23:55 -0800 (Sun, 10 Feb 2008) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 
32 package org.apache.http.conn.routing;
33 
34 
35 
36 /**
37  * Basic implementation of an {@link HttpRouteDirector HttpRouteDirector}.
38  * This implementation is stateless and therefore thread-safe.
39  *
40  * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
41  *
42  *
43  * <!-- empty lines to avoid svn diff problems -->
44  * @version $Revision: 620255 $
45  *
46  * @since 4.0
47  */
48 public class BasicRouteDirector implements HttpRouteDirector {
49 
50     // public default constructor
51 
52 
53     /**
54      * Provides the next step.
55      *
56      * @param plan      the planned route
57      * @param fact      the currently established route, or
58      *                  <code>null</code> if nothing is established
59      *
60      * @return  one of the constants defined in this class, indicating
61      *          either the next step to perform, or success, or failure.
62      *          0 is for success, a negative value for failure.
63      */
nextStep(RouteInfo plan, RouteInfo fact)64     public int nextStep(RouteInfo plan, RouteInfo fact) {
65         if (plan == null) {
66             throw new IllegalArgumentException
67                 ("Planned route may not be null.");
68         }
69 
70         int step = UNREACHABLE;
71 
72         if ((fact == null) || (fact.getHopCount() < 1))
73             step = firstStep(plan);
74         else if (plan.getHopCount() > 1)
75             step = proxiedStep(plan, fact);
76         else
77             step = directStep(plan, fact);
78 
79         return step;
80 
81     } // nextStep
82 
83 
84     /**
85      * Determines the first step to establish a route.
86      *
87      * @param plan      the planned route
88      *
89      * @return  the first step
90      */
firstStep(RouteInfo plan)91     protected int firstStep(RouteInfo plan) {
92 
93         return (plan.getHopCount() > 1) ?
94             CONNECT_PROXY : CONNECT_TARGET;
95     }
96 
97 
98     /**
99      * Determines the next step to establish a direct connection.
100      *
101      * @param plan      the planned route
102      * @param fact      the currently established route
103      *
104      * @return  one of the constants defined in this class, indicating
105      *          either the next step to perform, or success, or failure
106      */
directStep(RouteInfo plan, RouteInfo fact)107     protected int directStep(RouteInfo plan, RouteInfo fact) {
108 
109         if (fact.getHopCount() > 1)
110             return UNREACHABLE;
111         if (!plan.getTargetHost().equals(fact.getTargetHost()))
112             return UNREACHABLE;
113         // If the security is too low, we could now suggest to layer
114         // a secure protocol on the direct connection. Layering on direct
115         // connections has not been supported in HttpClient 3.x, we don't
116         // consider it here until there is a real-life use case for it.
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         // Local address has to match only if the plan specifies one.
124         if ((plan.getLocalAddress() != null) &&
125             !plan.getLocalAddress().equals(fact.getLocalAddress())
126             )
127             return UNREACHABLE;
128 
129         return COMPLETE;
130     }
131 
132 
133     /**
134      * Determines the next step to establish a connection via proxy.
135      *
136      * @param plan      the planned route
137      * @param fact      the currently established route
138      *
139      * @return  one of the constants defined in this class, indicating
140      *          either the next step to perform, or success, or failure
141      */
proxiedStep(RouteInfo plan, RouteInfo fact)142     protected int proxiedStep(RouteInfo plan, RouteInfo fact) {
143 
144         if (fact.getHopCount() <= 1)
145             return UNREACHABLE;
146         if (!plan.getTargetHost().equals(fact.getTargetHost()))
147             return UNREACHABLE;
148         final int phc = plan.getHopCount();
149         final int fhc = fact.getHopCount();
150         if (phc < fhc)
151             return UNREACHABLE;
152 
153         for (int i=0; i<fhc-1; i++) {
154             if (!plan.getHopTarget(i).equals(fact.getHopTarget(i)))
155                 return UNREACHABLE;
156         }
157         // now we know that the target matches and proxies so far are the same
158         if (phc > fhc)
159             return TUNNEL_PROXY; // need to extend the proxy chain
160 
161         // proxy chain and target are the same, check tunnelling and layering
162         if ((fact.isTunnelled() && !plan.isTunnelled()) ||
163             (fact.isLayered()   && !plan.isLayered()))
164             return UNREACHABLE;
165 
166         if (plan.isTunnelled() && !fact.isTunnelled())
167             return TUNNEL_TARGET;
168         if (plan.isLayered() && !fact.isLayered())
169             return LAYER_PROTOCOL;
170 
171         // tunnel and layering are the same, remains to check the security
172         // Should we tolerate if security is better than planned?
173         // (plan.isSecure() && !fact.isSecure())
174         if (plan.isSecure() != fact.isSecure())
175             return UNREACHABLE;
176 
177         return COMPLETE;
178     }
179 
180 
181 } // class BasicRouteDirector
182