Home | History | Annotate | Download | only in conn
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/DefaultHttpRoutePlanner.java $
      3  * $Revision: 658785 $
      4  * $Date: 2008-05-21 10:47:40 -0700 (Wed, 21 May 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.impl.conn;
     33 
     34 
     35 import java.net.InetAddress;
     36 
     37 import org.apache.http.HttpException;
     38 import org.apache.http.HttpHost;
     39 import org.apache.http.HttpRequest;
     40 import org.apache.http.protocol.HttpContext;
     41 
     42 import org.apache.http.conn.routing.HttpRoute;
     43 import org.apache.http.conn.routing.HttpRoutePlanner;
     44 import org.apache.http.conn.scheme.Scheme;
     45 import org.apache.http.conn.scheme.SchemeRegistry;
     46 
     47 import org.apache.http.conn.params.ConnRouteParams;
     48 
     49 
     50 /**
     51  * Default implementation of an {@link HttpRoutePlanner}.
     52  * This implementation is based on
     53  * {@link org.apache.http.conn.params.ConnRoutePNames parameters}.
     54  * It will not make use of any Java system properties,
     55  * nor of system or browser proxy settings.
     56  */
     57 public class DefaultHttpRoutePlanner implements HttpRoutePlanner {
     58 
     59     /** The scheme registry. */
     60     protected SchemeRegistry schemeRegistry;
     61 
     62 
     63     /**
     64      * Creates a new default route planner.
     65      *
     66      * @param schreg    the scheme registry
     67      */
     68     public DefaultHttpRoutePlanner(SchemeRegistry schreg) {
     69         if (schreg == null) {
     70             throw new IllegalArgumentException
     71                 ("SchemeRegistry must not be null.");
     72         }
     73         schemeRegistry = schreg;
     74     }
     75 
     76 
     77     // non-javadoc, see interface HttpRoutePlanner
     78     public HttpRoute determineRoute(HttpHost target,
     79                                     HttpRequest request,
     80                                     HttpContext context)
     81         throws HttpException {
     82 
     83         if (request == null) {
     84             throw new IllegalStateException
     85                 ("Request must not be null.");
     86         }
     87 
     88         // If we have a forced route, we can do without a target.
     89         HttpRoute route =
     90             ConnRouteParams.getForcedRoute(request.getParams());
     91         if (route != null)
     92             return route;
     93 
     94         // If we get here, there is no forced route.
     95         // So we need a target to compute a route.
     96 
     97         if (target == null) {
     98             throw new IllegalStateException
     99                 ("Target host must not be null.");
    100         }
    101 
    102         final InetAddress local =
    103             ConnRouteParams.getLocalAddress(request.getParams());
    104         final HttpHost proxy =
    105             ConnRouteParams.getDefaultProxy(request.getParams());
    106 
    107         final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
    108         // as it is typically used for TLS/SSL, we assume that
    109         // a layered scheme implies a secure connection
    110         final boolean secure = schm.isLayered();
    111 
    112         if (proxy == null) {
    113             route = new HttpRoute(target, local, secure);
    114         } else {
    115             route = new HttpRoute(target, local, proxy, secure);
    116         }
    117         return route;
    118     }
    119 
    120 
    121 }
    122