1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/*
2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/routing/BasicRouteDirector.java $
3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 620255 $
4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-02-10 02:23:55 -0800 (Sun, 10 Feb 2008) $
5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one
8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * or more contributor license agreements.  See the NOTICE file
9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed with this work for additional information
10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * regarding copyright ownership.  The ASF licenses this file
11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * to you under the Apache License, Version 2.0 (the
12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "License"); you may not use this file except in compliance
13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * with the License.  You may obtain a copy of the License at
14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *   http://www.apache.org/licenses/LICENSE-2.0
16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing,
18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * software distributed under the License is distributed on an
19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * KIND, either express or implied.  See the License for the
21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * specific language governing permissions and limitations
22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * under the License.
23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many
26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation.  For more
27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see
28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>.
29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.conn.routing;
33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/**
37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Basic implementation of an {@link HttpRouteDirector HttpRouteDirector}.
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This implementation is stateless and therefore thread-safe.
39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <!-- empty lines to avoid svn diff problems -->
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @version $Revision: 620255 $
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 4.0
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class BasicRouteDirector implements HttpRouteDirector {
49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // public default constructor
51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Provides the next step.
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param plan      the planned route
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param fact      the currently established route, or
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  <code>null</code> if nothing is established
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  one of the constants defined in this class, indicating
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          either the next step to perform, or success, or failure.
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          0 is for success, a negative value for failure.
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int nextStep(RouteInfo plan, RouteInfo fact) {
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (plan == null) {
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Planned route may not be null.");
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int step = UNREACHABLE;
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if ((fact == null) || (fact.getHopCount() < 1))
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            step = firstStep(plan);
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        else if (plan.getHopCount() > 1)
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            step = proxiedStep(plan, fact);
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        else
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            step = directStep(plan, fact);
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return step;
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    } // nextStep
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Determines the first step to establish a route.
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param plan      the planned route
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  the first step
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected int firstStep(RouteInfo plan) {
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return (plan.getHopCount() > 1) ?
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            CONNECT_PROXY : CONNECT_TARGET;
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Determines the next step to establish a direct connection.
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param plan      the planned route
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param fact      the currently established route
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  one of the constants defined in this class, indicating
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          either the next step to perform, or success, or failure
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected int directStep(RouteInfo plan, RouteInfo fact) {
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (fact.getHopCount() > 1)
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return UNREACHABLE;
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (!plan.getTargetHost().equals(fact.getTargetHost()))
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return UNREACHABLE;
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // If the security is too low, we could now suggest to layer
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // a secure protocol on the direct connection. Layering on direct
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // connections has not been supported in HttpClient 3.x, we don't
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // consider it here until there is a real-life use case for it.
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // Should we tolerate if security is better than planned?
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // (plan.isSecure() && !fact.isSecure())
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (plan.isSecure() != fact.isSecure())
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return UNREACHABLE;
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // Local address has to match only if the plan specifies one.
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if ((plan.getLocalAddress() != null) &&
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            !plan.getLocalAddress().equals(fact.getLocalAddress())
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            )
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return UNREACHABLE;
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return COMPLETE;
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Determines the next step to establish a connection via proxy.
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param plan      the planned route
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param fact      the currently established route
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  one of the constants defined in this class, indicating
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          either the next step to perform, or success, or failure
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected int proxiedStep(RouteInfo plan, RouteInfo fact) {
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (fact.getHopCount() <= 1)
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return UNREACHABLE;
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (!plan.getTargetHost().equals(fact.getTargetHost()))
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return UNREACHABLE;
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        final int phc = plan.getHopCount();
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        final int fhc = fact.getHopCount();
150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (phc < fhc)
151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return UNREACHABLE;
152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        for (int i=0; i<fhc-1; i++) {
154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (!plan.getHopTarget(i).equals(fact.getHopTarget(i)))
155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                return UNREACHABLE;
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // now we know that the target matches and proxies so far are the same
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (phc > fhc)
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return TUNNEL_PROXY; // need to extend the proxy chain
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // proxy chain and target are the same, check tunnelling and layering
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if ((fact.isTunnelled() && !plan.isTunnelled()) ||
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            (fact.isLayered()   && !plan.isLayered()))
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return UNREACHABLE;
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (plan.isTunnelled() && !fact.isTunnelled())
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return TUNNEL_TARGET;
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (plan.isLayered() && !fact.isLayered())
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return LAYER_PROTOCOL;
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // tunnel and layering are the same, remains to check the security
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // Should we tolerate if security is better than planned?
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // (plan.isSecure() && !fact.isSecure())
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (plan.isSecure() != fact.isSecure())
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return UNREACHABLE;
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return COMPLETE;
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project} // class BasicRouteDirector
182