Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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 */
28 package ch.boye.httpclientandroidlib.conn.routing;
30 import ch.boye.httpclientandroidlib.annotation.Immutable;
32 /**
33 * Basic implementation of an {@link HttpRouteDirector HttpRouteDirector}.
34 * This implementation is stateless and therefore thread-safe.
35 *
36 * @since 4.0
37 */
38 @Immutable
39 public class BasicRouteDirector implements HttpRouteDirector {
41 /**
42 * Provides the next step.
43 *
44 * @param plan the planned route
45 * @param fact the currently established route, or
46 * <code>null</code> 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 public int nextStep(RouteInfo plan, RouteInfo fact) {
53 if (plan == null) {
54 throw new IllegalArgumentException
55 ("Planned route may not be null.");
56 }
58 int step = UNREACHABLE;
60 if ((fact == null) || (fact.getHopCount() < 1))
61 step = firstStep(plan);
62 else if (plan.getHopCount() > 1)
63 step = proxiedStep(plan, fact);
64 else
65 step = directStep(plan, fact);
67 return step;
69 } // nextStep
72 /**
73 * Determines the first step to establish a route.
74 *
75 * @param plan the planned route
76 *
77 * @return the first step
78 */
79 protected int firstStep(RouteInfo plan) {
81 return (plan.getHopCount() > 1) ?
82 CONNECT_PROXY : CONNECT_TARGET;
83 }
86 /**
87 * Determines the next step to establish a direct connection.
88 *
89 * @param plan the planned route
90 * @param fact the currently established route
91 *
92 * @return one of the constants defined in this class, indicating
93 * either the next step to perform, or success, or failure
94 */
95 protected int directStep(RouteInfo plan, RouteInfo fact) {
97 if (fact.getHopCount() > 1)
98 return UNREACHABLE;
99 if (!plan.getTargetHost().equals(fact.getTargetHost()))
100 return UNREACHABLE;
101 // If the security is too low, we could now suggest to layer
102 // a secure protocol on the direct connection. Layering on direct
103 // connections has not been supported in HttpClient 3.x, we don't
104 // consider it here until there is a real-life use case for it.
106 // Should we tolerate if security is better than planned?
107 // (plan.isSecure() && !fact.isSecure())
108 if (plan.isSecure() != fact.isSecure())
109 return UNREACHABLE;
111 // Local address has to match only if the plan specifies one.
112 if ((plan.getLocalAddress() != null) &&
113 !plan.getLocalAddress().equals(fact.getLocalAddress())
114 )
115 return UNREACHABLE;
117 return COMPLETE;
118 }
121 /**
122 * Determines the next step to establish a connection via proxy.
123 *
124 * @param plan the planned route
125 * @param fact the currently established route
126 *
127 * @return one of the constants defined in this class, indicating
128 * either the next step to perform, or success, or failure
129 */
130 protected int proxiedStep(RouteInfo plan, RouteInfo fact) {
132 if (fact.getHopCount() <= 1)
133 return UNREACHABLE;
134 if (!plan.getTargetHost().equals(fact.getTargetHost()))
135 return UNREACHABLE;
136 final int phc = plan.getHopCount();
137 final int fhc = fact.getHopCount();
138 if (phc < fhc)
139 return UNREACHABLE;
141 for (int i=0; i<fhc-1; i++) {
142 if (!plan.getHopTarget(i).equals(fact.getHopTarget(i)))
143 return UNREACHABLE;
144 }
145 // now we know that the target matches and proxies so far are the same
146 if (phc > fhc)
147 return TUNNEL_PROXY; // need to extend the proxy chain
149 // proxy chain and target are the same, check tunnelling and layering
150 if ((fact.isTunnelled() && !plan.isTunnelled()) ||
151 (fact.isLayered() && !plan.isLayered()))
152 return UNREACHABLE;
154 if (plan.isTunnelled() && !fact.isTunnelled())
155 return TUNNEL_TARGET;
156 if (plan.isLayered() && !fact.isLayered())
157 return LAYER_PROTOCOL;
159 // tunnel and layering are the same, remains to check the security
160 // Should we tolerate if security is better than planned?
161 // (plan.isSecure() && !fact.isSecure())
162 if (plan.isSecure() != fact.isSecure())
163 return UNREACHABLE;
165 return COMPLETE;
166 }
168 }