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