michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.impl.conn; michael@0: michael@0: michael@0: import java.net.InetAddress; michael@0: import java.net.InetSocketAddress; michael@0: import java.net.Proxy; michael@0: import java.net.ProxySelector; michael@0: import java.net.URI; michael@0: import java.net.URISyntaxException; michael@0: import java.util.List; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; michael@0: import ch.boye.httpclientandroidlib.HttpException; michael@0: import ch.boye.httpclientandroidlib.HttpHost; michael@0: import ch.boye.httpclientandroidlib.HttpRequest; michael@0: import ch.boye.httpclientandroidlib.protocol.HttpContext; michael@0: michael@0: import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; michael@0: import ch.boye.httpclientandroidlib.conn.routing.HttpRoutePlanner; michael@0: import ch.boye.httpclientandroidlib.conn.scheme.Scheme; michael@0: import ch.boye.httpclientandroidlib.conn.scheme.SchemeRegistry; michael@0: michael@0: import ch.boye.httpclientandroidlib.conn.params.ConnRouteParams; michael@0: michael@0: michael@0: /** michael@0: * Default implementation of an {@link HttpRoutePlanner}. michael@0: * This implementation is based on {@link java.net.ProxySelector}. michael@0: * By default, it will pick up the proxy settings of the JVM, either michael@0: * from system properties or from the browser running the application. michael@0: * Additionally, it interprets some michael@0: * {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames parameters}, michael@0: * though not the {@link michael@0: * ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY}. michael@0: *

michael@0: * The following parameters can be used to customize the behavior of this michael@0: * class: michael@0: *

michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @NotThreadSafe // e.g [gs]etProxySelector() michael@0: public class ProxySelectorRoutePlanner implements HttpRoutePlanner { michael@0: michael@0: /** The scheme registry. */ michael@0: protected final SchemeRegistry schemeRegistry; // @ThreadSafe michael@0: michael@0: /** The proxy selector to use, or null for system default. */ michael@0: protected ProxySelector proxySelector; michael@0: michael@0: /** michael@0: * Creates a new proxy selector route planner. michael@0: * michael@0: * @param schreg the scheme registry michael@0: * @param prosel the proxy selector, or michael@0: * null for the system default michael@0: */ michael@0: public ProxySelectorRoutePlanner(SchemeRegistry schreg, michael@0: ProxySelector prosel) { michael@0: michael@0: if (schreg == null) { michael@0: throw new IllegalArgumentException michael@0: ("SchemeRegistry must not be null."); michael@0: } michael@0: schemeRegistry = schreg; michael@0: proxySelector = prosel; michael@0: } michael@0: michael@0: /** michael@0: * Obtains the proxy selector to use. michael@0: * michael@0: * @return the proxy selector, or null for the system default michael@0: */ michael@0: public ProxySelector getProxySelector() { michael@0: return this.proxySelector; michael@0: } michael@0: michael@0: /** michael@0: * Sets the proxy selector to use. michael@0: * michael@0: * @param prosel the proxy selector, or michael@0: * null to use the system default michael@0: */ michael@0: public void setProxySelector(ProxySelector prosel) { michael@0: this.proxySelector = prosel; michael@0: } michael@0: michael@0: public HttpRoute determineRoute(HttpHost target, michael@0: HttpRequest request, michael@0: HttpContext context) michael@0: throws HttpException { michael@0: michael@0: if (request == null) { michael@0: throw new IllegalStateException michael@0: ("Request must not be null."); michael@0: } michael@0: michael@0: // If we have a forced route, we can do without a target. michael@0: HttpRoute route = michael@0: ConnRouteParams.getForcedRoute(request.getParams()); michael@0: if (route != null) michael@0: return route; michael@0: michael@0: // If we get here, there is no forced route. michael@0: // So we need a target to compute a route. michael@0: michael@0: if (target == null) { michael@0: throw new IllegalStateException michael@0: ("Target host must not be null."); michael@0: } michael@0: michael@0: final InetAddress local = michael@0: ConnRouteParams.getLocalAddress(request.getParams()); michael@0: final HttpHost proxy = determineProxy(target, request, context); michael@0: michael@0: final Scheme schm = michael@0: this.schemeRegistry.getScheme(target.getSchemeName()); michael@0: // as it is typically used for TLS/SSL, we assume that michael@0: // a layered scheme implies a secure connection michael@0: final boolean secure = schm.isLayered(); michael@0: michael@0: if (proxy == null) { michael@0: route = new HttpRoute(target, local, secure); michael@0: } else { michael@0: route = new HttpRoute(target, local, proxy, secure); michael@0: } michael@0: return route; michael@0: } michael@0: michael@0: /** michael@0: * Determines a proxy for the given target. michael@0: * michael@0: * @param target the planned target, never null michael@0: * @param request the request to be sent, never null michael@0: * @param context the context, or null michael@0: * michael@0: * @return the proxy to use, or null for a direct route michael@0: * michael@0: * @throws HttpException michael@0: * in case of system proxy settings that cannot be handled michael@0: */ michael@0: protected HttpHost determineProxy(HttpHost target, michael@0: HttpRequest request, michael@0: HttpContext context) michael@0: throws HttpException { michael@0: michael@0: // the proxy selector can be 'unset', so we better deal with null here michael@0: ProxySelector psel = this.proxySelector; michael@0: if (psel == null) michael@0: psel = ProxySelector.getDefault(); michael@0: if (psel == null) michael@0: return null; michael@0: michael@0: URI targetURI = null; michael@0: try { michael@0: targetURI = new URI(target.toURI()); michael@0: } catch (URISyntaxException usx) { michael@0: throw new HttpException michael@0: ("Cannot convert host to URI: " + target, usx); michael@0: } michael@0: List proxies = psel.select(targetURI); michael@0: michael@0: Proxy p = chooseProxy(proxies, target, request, context); michael@0: michael@0: HttpHost result = null; michael@0: if (p.type() == Proxy.Type.HTTP) { michael@0: // convert the socket address to an HttpHost michael@0: if (!(p.address() instanceof InetSocketAddress)) { michael@0: throw new HttpException michael@0: ("Unable to handle non-Inet proxy address: "+p.address()); michael@0: } michael@0: final InetSocketAddress isa = (InetSocketAddress) p.address(); michael@0: // assume default scheme (http) michael@0: result = new HttpHost(getHost(isa), isa.getPort()); michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: /** michael@0: * Obtains a host from an {@link InetSocketAddress}. michael@0: * michael@0: * @param isa the socket address michael@0: * michael@0: * @return a host string, either as a symbolic name or michael@0: * as a literal IP address string michael@0: *
michael@0: * (TODO: determine format for IPv6 addresses, with or without [brackets]) michael@0: */ michael@0: protected String getHost(InetSocketAddress isa) { michael@0: michael@0: //@@@ Will this work with literal IPv6 addresses, or do we michael@0: //@@@ need to wrap these in [] for the string representation? michael@0: //@@@ Having it in this method at least allows for easy workarounds. michael@0: return isa.isUnresolved() ? michael@0: isa.getHostName() : isa.getAddress().getHostAddress(); michael@0: michael@0: } michael@0: michael@0: /** michael@0: * Chooses a proxy from a list of available proxies. michael@0: * The default implementation just picks the first non-SOCKS proxy michael@0: * from the list. If there are only SOCKS proxies, michael@0: * {@link Proxy#NO_PROXY Proxy.NO_PROXY} is returned. michael@0: * Derived classes may implement more advanced strategies, michael@0: * such as proxy rotation if there are multiple options. michael@0: * michael@0: * @param proxies the list of proxies to choose from, michael@0: * never null or empty michael@0: * @param target the planned target, never null michael@0: * @param request the request to be sent, never null michael@0: * @param context the context, or null michael@0: * michael@0: * @return a proxy type michael@0: */ michael@0: protected Proxy chooseProxy(List proxies, michael@0: HttpHost target, michael@0: HttpRequest request, michael@0: HttpContext context) { michael@0: michael@0: if ((proxies == null) || proxies.isEmpty()) { michael@0: throw new IllegalArgumentException michael@0: ("Proxy list must not be empty."); michael@0: } michael@0: michael@0: Proxy result = null; michael@0: michael@0: // check the list for one we can use michael@0: for (int i=0; (result == null) && (i < proxies.size()); i++) { michael@0: michael@0: Proxy p = proxies.get(i); michael@0: switch (p.type()) { michael@0: michael@0: case DIRECT: michael@0: case HTTP: michael@0: result = p; michael@0: break; michael@0: michael@0: case SOCKS: michael@0: // SOCKS hosts are not handled on the route level. michael@0: // The socket may make use of the SOCKS host though. michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (result == null) { michael@0: //@@@ log as warning or info that only a socks proxy is available? michael@0: // result can only be null if all proxies are socks proxies michael@0: // socks proxies are not handled on the route planning level michael@0: result = Proxy.NO_PROXY; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: } michael@0: