001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.conn.routing; 029 030import org.apache.http.annotation.Contract; 031import org.apache.http.annotation.ThreadingBehavior; 032import org.apache.http.util.Args; 033 034/** 035 * Basic {@link HttpRouteDirector} implementation. 036 * 037 * @since 4.0 038 */ 039@Contract(threading = ThreadingBehavior.IMMUTABLE) 040public class BasicRouteDirector implements HttpRouteDirector { 041 042 /** 043 * Provides the next step. 044 * 045 * @param plan the planned route 046 * @param fact the currently established route, or 047 * {@code null} if nothing is established 048 * 049 * @return one of the constants defined in this class, indicating 050 * either the next step to perform, or success, or failure. 051 * 0 is for success, a negative value for failure. 052 */ 053 @Override 054 public int nextStep(final RouteInfo plan, final RouteInfo fact) { 055 Args.notNull(plan, "Planned route"); 056 057 int step = UNREACHABLE; 058 059 if ((fact == null) || (fact.getHopCount() < 1)) { 060 step = firstStep(plan); 061 } else if (plan.getHopCount() > 1) { 062 step = proxiedStep(plan, fact); 063 } else { 064 step = directStep(plan, fact); 065 } 066 067 return step; 068 069 } // nextStep 070 071 072 /** 073 * Determines the first step to establish a route. 074 * 075 * @param plan the planned route 076 * 077 * @return the first step 078 */ 079 protected int firstStep(final RouteInfo plan) { 080 081 return (plan.getHopCount() > 1) ? 082 CONNECT_PROXY : CONNECT_TARGET; 083 } 084 085 086 /** 087 * Determines the next step to establish a direct connection. 088 * 089 * @param plan the planned route 090 * @param fact the currently established route 091 * 092 * @return one of the constants defined in this class, indicating 093 * either the next step to perform, or success, or failure 094 */ 095 protected int directStep(final RouteInfo plan, final RouteInfo fact) { 096 097 if (fact.getHopCount() > 1) { 098 return UNREACHABLE; 099 } 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 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}