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