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  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     58  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     59  *     for further details.
     60  */
     61 @Deprecated
     62 public class DefaultHttpRoutePlanner implements HttpRoutePlanner {
     63 
     64     /** The scheme registry. */
     65     protected SchemeRegistry schemeRegistry;
     66 
     67 
     68     /**
     69      * Creates a new default route planner.
     70      *
     71      * @param schreg    the scheme registry
     72      */
     73     public DefaultHttpRoutePlanner(SchemeRegistry schreg) {
     74         if (schreg == null) {
     75             throw new IllegalArgumentException
     76                 ("SchemeRegistry must not be null.");
     77         }
     78         schemeRegistry = schreg;
     79     }
     80 
     81 
     82     // non-javadoc, see interface HttpRoutePlanner
     83     public HttpRoute determineRoute(HttpHost target,
     84                                     HttpRequest request,
     85                                     HttpContext context)
     86         throws HttpException {
     87 
     88         if (request == null) {
     89             throw new IllegalStateException
     90                 ("Request must not be null.");
     91         }
     92 
     93         // If we have a forced route, we can do without a target.
     94         HttpRoute route =
     95             ConnRouteParams.getForcedRoute(request.getParams());
     96         if (route != null)
     97             return route;
     98 
     99         // If we get here, there is no forced route.
    100         // So we need a target to compute a route.
    101 
    102         if (target == null) {
    103             throw new IllegalStateException
    104                 ("Target host must not be null.");
    105         }
    106 
    107         final InetAddress local =
    108             ConnRouteParams.getLocalAddress(request.getParams());
    109         final HttpHost proxy =
    110             ConnRouteParams.getDefaultProxy(request.getParams());
    111 
    112         final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
    113         // as it is typically used for TLS/SSL, we assume that
    114         // a layered scheme implies a secure connection
    115         final boolean secure = schm.isLayered();
    116 
    117         if (proxy == null) {
    118             route = new HttpRoute(target, local, secure);
    119         } else {
    120             route = new HttpRoute(target, local, proxy, secure);
    121         }
    122         return route;
    123     }
    124 
    125 
    126 }
    127