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:
michael@0: import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
michael@0:
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: * Default implementation of an {@link HttpRoutePlanner}. This implementation
michael@0: * is based on {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames parameters}.
michael@0: * It will not make use of any Java system properties, nor of system or
michael@0: * browser proxy settings.
michael@0: *
michael@0: * The following parameters can be used to customize the behavior of this
michael@0: * class:
michael@0: *
michael@0: * - {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#DEFAULT_PROXY}
michael@0: * - {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#LOCAL_ADDRESS}
michael@0: * - {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#FORCED_ROUTE}
michael@0: *
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: @ThreadSafe
michael@0: public class DefaultHttpRoutePlanner implements HttpRoutePlanner {
michael@0:
michael@0: /** The scheme registry. */
michael@0: protected final SchemeRegistry schemeRegistry; // class is @ThreadSafe
michael@0:
michael@0: /**
michael@0: * Creates a new default route planner.
michael@0: *
michael@0: * @param schreg the scheme registry
michael@0: */
michael@0: public DefaultHttpRoutePlanner(SchemeRegistry schreg) {
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: }
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 =
michael@0: ConnRouteParams.getDefaultProxy(request.getParams());
michael@0:
michael@0: final Scheme schm;
michael@0: try {
michael@0: schm = schemeRegistry.getScheme(target.getSchemeName());
michael@0: } catch (IllegalStateException ex) {
michael@0: throw new HttpException(ex.getMessage());
michael@0: }
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: }