/** * Creates a new tracker for the given route. * Only target and origin are taken from the route, * everything else remains to be tracked. * * @param route the route to track */ public RouteTracker(HttpRoute route) { this(route.getTargetHost(), route.getLocalAddress()); }
/** * Determines the first step to establish a route. * * @param plan the planned route * * @return the first step */ protected int firstStep(RouteInfo plan) { return (plan.getHopCount() > 1) ? CONNECT_PROXY : CONNECT_TARGET; }
/** * Obtains the tracked route. * If a route has been tracked, it is {@link #isConnected connected}. * If not connected, nothing has been tracked so far. * * @return the tracked route, or * <code>null</code> if nothing has been tracked so far */ public final HttpRoute toRoute() { return !this.connected ? null : new HttpRoute(this.targetHost, this.localAddress, this.proxyChain, this.secure, this.tunnelled, this.layered); }
if (fact.getHopCount() <= 1) return UNREACHABLE; if (!plan.getTargetHost().equals(fact.getTargetHost())) return UNREACHABLE; final int phc = plan.getHopCount(); final int fhc = fact.getHopCount(); if (phc < fhc) return UNREACHABLE; if (!plan.getHopTarget(i).equals(fact.getHopTarget(i))) return UNREACHABLE; if ((fact.isTunnelled() && !plan.isTunnelled()) || (fact.isLayered() && !plan.isLayered())) return UNREACHABLE; if (plan.isTunnelled() && !fact.isTunnelled()) return TUNNEL_TARGET; if (plan.isLayered() && !fact.isLayered()) return LAYER_PROTOCOL; if (plan.isSecure() != fact.isSecure()) return UNREACHABLE;
throws HttpException, IOException { HttpRouteDirector rowdy = new BasicRouteDirector(); int step; do { HttpRoute fact = managedConn.getRoute(); step = rowdy.nextStep(route, fact); final int hop = fact.getHopCount()-1; // the hop to establish boolean secure = createTunnelToProxy(route, hop, context); this.log.debug("Tunnel to proxy created."); managedConn.tunnelProxy(route.getHopTarget(hop), secure, this.params); } break;
/** * Provides the next step. * * @param plan the planned route * @param fact the currently established route, or * <code>null</code> if nothing is established * * @return one of the constants defined in this class, indicating * either the next step to perform, or success, or failure. * 0 is for success, a negative value for failure. */ public int nextStep(RouteInfo plan, RouteInfo fact) { if (plan == null) { throw new IllegalArgumentException ("Planned route may not be null."); } int step = UNREACHABLE; if ((fact == null) || (fact.getHopCount() < 1)) step = firstStep(plan); else if (plan.getHopCount() > 1) step = proxiedStep(plan, fact); else step = directStep(plan, fact); return step; } // nextStep
public final HttpHost getHopTarget(int hop) { if (hop < 0) throw new IllegalArgumentException ("Hop index must not be negative: " + hop); final int hopcount = getHopCount(); if (hop >= hopcount) throw new IllegalArgumentException ("Hop index " + hop + " exceeds route length " + hopcount); HttpHost result = null; if (hop < hopcount-1) result = this.proxyChain[hop]; else result = this.targetHost; return result; }
@Override public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException { HttpRoute route = delegate.determineRoute(target, request, context); routes.add(route); return route; } }
public final HttpHost getHopTarget(int hop) { if (hop < 0) throw new IllegalArgumentException ("Hop index must not be negative: " + hop); final int hopcount = getHopCount(); if (hop >= hopcount) { throw new IllegalArgumentException ("Hop index " + hop + " exceeds tracked route length " + hopcount +"."); } HttpHost result = null; if (hop < hopcount-1) result = this.proxyChain[hop]; else result = this.targetHost; return result; }
/** * Obtains the {@link ConnRoutePNames#FORCED_ROUTE FORCED_ROUTE} * parameter value. * {@link #NO_ROUTE} will be mapped to <code>null</code>, * to allow unsetting in a hierarchy. * * @param params the parameters in which to look up * * @return the forced route set in the argument parameters, or * <code>null</code> if not set */ public static HttpRoute getForcedRoute(HttpParams params) { if (params == null) { throw new IllegalArgumentException("Parameters must not be null."); } HttpRoute route = (HttpRoute) params.getParameter(FORCED_ROUTE); if ((route != null) && NO_ROUTE.equals(route)) { // value is explicitly unset route = null; } return route; }
public HttpRoute getRoute() { assertAttached(); return (poolEntry.tracker == null) ? null : poolEntry.tracker.toRoute(); }
/** * Generates a hash code for this route. * * @return the hash code */ @Override public final int hashCode() { int hc = this.targetHost.hashCode(); if (this.localAddress != null) hc ^= localAddress.hashCode(); if (this.proxyChain != null) { hc ^= proxyChain.length; for (HttpHost aProxyChain : proxyChain) hc ^= aProxyChain.hashCode(); } if (this.secure) hc ^= 0x11111111; hc ^= this.tunnelled.hashCode(); hc ^= this.layered.hashCode(); return hc; }
/** * Creates a new route with all attributes specified explicitly. * * @param target the host to which to route * @param local the local address to route from, or * <code>null</code> for the default * @param proxies the proxy chain to use, or * <code>null</code> for a direct route * @param secure <code>true</code> if the route is (to be) secure, * <code>false</code> otherwise * @param tunnelled the tunnel type of this route * @param layered the layering type of this route */ public HttpRoute(HttpHost target, InetAddress local, HttpHost[] proxies, boolean secure, TunnelType tunnelled, LayerType layered) { this(local, target, toChain(proxies), secure, tunnelled, layered); }
private HttpContext getContext( URI uri ) { HttpClientContext httpClientContext = HttpClientContext.create(); //used by httpclient version >= 4.3 httpClientContext .setAttribute( HttpClientContext.HTTP_ROUTE, new HttpRoute( new HttpHost( uri.getHost(), uri.getPort() ) ) ); //used by httpclient version 4.2 httpClientContext .setAttribute( HttpClientContext.HTTP_TARGET_HOST, new HttpHost( uri.getHost(), uri.getPort() ) ); return httpClientContext; }
StringBuilder cab = new StringBuilder(50 + getHopCount()*30);
StringBuilder cab = new StringBuilder(50 + getHopCount()*30);
/** * Indicates creation of an entry for this pool. * The entry will <i>not</i> be added to the list of free entries, * it is only recognized as belonging to this pool now. It can then * be passed to {@link #freeEntry freeEntry}. * * @param entry the entry that was created for this pool */ public void createdEntry(BasicPoolEntry entry) { if (!route.equals(entry.getPlannedRoute())) { throw new IllegalArgumentException ("Entry not planned for this pool." + "\npool: " + route + "\nplan: " + entry.getPlannedRoute()); } numEntries++; }
/** * Generates a hash code for this tracked route. * Route trackers are modifiable and should therefore not be used * as lookup keys. Use {@link #toRoute toRoute} to obtain an * unmodifiable representation of the tracked route. * * @return the hash code */ @Override public final int hashCode() { int hc = this.targetHost.hashCode(); if (this.localAddress != null) hc ^= localAddress.hashCode(); if (this.proxyChain != null) { hc ^= proxyChain.length; for (int i=0; i<proxyChain.length; i++) hc ^= proxyChain[i].hashCode(); } if (this.connected) hc ^= 0x11111111; if (this.secure) hc ^= 0x22222222; hc ^= this.tunnelled.hashCode(); hc ^= this.layered.hashCode(); return hc; }
/** * Creates a new route with at most one proxy. * * @param target the host to which to route * @param local the local address to route from, or * <code>null</code> for the default * @param proxy the proxy to use, or * <code>null</code> for a direct route * @param secure <code>true</code> if the route is (to be) secure, * <code>false</code> otherwise * @param tunnelled <code>true</code> if the route is (to be) tunnelled * via the proxy, * <code>false</code> otherwise * @param layered <code>true</code> if the route includes a * layered protocol, * <code>false</code> otherwise */ public HttpRoute(HttpHost target, InetAddress local, HttpHost proxy, boolean secure, TunnelType tunnelled, LayerType layered) { this(local, target, toChain(proxy), secure, tunnelled, layered); }
/** * Creates a new route through a proxy. * When using this constructor, the <code>proxy</code> MUST be given. * For convenience, it is assumed that a secure connection will be * layered over a tunnel through the proxy. * * @param target the host to which to route * @param local the local address to route from, or * <code>null</code> for the default * @param proxy the proxy to use * @param secure <code>true</code> if the route is (to be) secure, * <code>false</code> otherwise */ public HttpRoute(HttpHost target, InetAddress local, HttpHost proxy, boolean secure) { this(local, target, toChain(proxy), secure, secure ? TunnelType.TUNNELLED : TunnelType.PLAIN, secure ? LayerType.LAYERED : LayerType.PLAIN); if (proxy == null) { throw new IllegalArgumentException ("Proxy host may not be null."); } }