1 /*
2  * ====================================================================
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  * ====================================================================
20  *
21  * This software consists of voluntary contributions made by many
22  * individuals on behalf of the Apache Software Foundation.  For more
23  * information on the Apache Software Foundation, please see
24  * <http://www.apache.org/>.
25  *
26  */
27 
28 package ch.boye.httpclientandroidlib.impl.conn;
29 
30 
31 import java.net.InetAddress;
32 
33 import ch.boye.httpclientandroidlib.HttpException;
34 import ch.boye.httpclientandroidlib.HttpHost;
35 import ch.boye.httpclientandroidlib.HttpRequest;
36 import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
37 import ch.boye.httpclientandroidlib.conn.params.ConnRouteParams;
38 import ch.boye.httpclientandroidlib.conn.routing.HttpRoute;
39 import ch.boye.httpclientandroidlib.conn.routing.HttpRoutePlanner;
40 import ch.boye.httpclientandroidlib.conn.scheme.Scheme;
41 import ch.boye.httpclientandroidlib.conn.scheme.SchemeRegistry;
42 import ch.boye.httpclientandroidlib.protocol.HttpContext;
43 import ch.boye.httpclientandroidlib.util.Args;
44 import ch.boye.httpclientandroidlib.util.Asserts;
45 
46 /**
47  * Default implementation of an {@link HttpRoutePlanner}. This implementation
48  * is based on {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames parameters}.
49  * It will not make use of any Java system properties, nor of system or
50  * browser proxy settings.
51  * <p>
52  * The following parameters can be used to customize the behavior of this
53  * class:
54  * <ul>
55  *  <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li>
56  *  <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li>
57  *  <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#FORCED_ROUTE}</li>
58  * </ul>
59  *
60  * @since 4.0
61  *
62  * @deprecated (4.3) use {@link DefaultRoutePlanner}
63  */
64 @ThreadSafe
65 @Deprecated
66 public class DefaultHttpRoutePlanner implements HttpRoutePlanner {
67 
68     /** The scheme registry. */
69     protected final SchemeRegistry schemeRegistry; // class is @ThreadSafe
70 
71     /**
72      * Creates a new default route planner.
73      *
74      * @param schreg    the scheme registry
75      */
DefaultHttpRoutePlanner(final SchemeRegistry schreg)76     public DefaultHttpRoutePlanner(final SchemeRegistry schreg) {
77         Args.notNull(schreg, "Scheme registry");
78         schemeRegistry = schreg;
79     }
80 
determineRoute(final HttpHost target, final HttpRequest request, final HttpContext context)81     public HttpRoute determineRoute(final HttpHost target,
82                                     final HttpRequest request,
83                                     final HttpContext context)
84         throws HttpException {
85 
86         Args.notNull(request, "HTTP request");
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 
95         // If we get here, there is no forced route.
96         // So we need a target to compute a route.
97 
98         Asserts.notNull(target, "Target host");
99 
100         final InetAddress local =
101             ConnRouteParams.getLocalAddress(request.getParams());
102         final HttpHost proxy =
103             ConnRouteParams.getDefaultProxy(request.getParams());
104 
105         final Scheme schm;
106         try {
107             schm = this.schemeRegistry.getScheme(target.getSchemeName());
108         } catch (final IllegalStateException ex) {
109             throw new HttpException(ex.getMessage());
110         }
111         // as it is typically used for TLS/SSL, we assume that
112         // a layered scheme implies a secure connection
113         final boolean secure = schm.isLayered();
114 
115         if (proxy == null) {
116             route = new HttpRoute(target, local, secure);
117         } else {
118             route = new HttpRoute(target, local, proxy, secure);
119         }
120         return route;
121     }
122 
123 }
124