|
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.annotation.ThreadSafe; |
|
34 |
|
35 import ch.boye.httpclientandroidlib.HttpException; |
|
36 import ch.boye.httpclientandroidlib.HttpHost; |
|
37 import ch.boye.httpclientandroidlib.HttpRequest; |
|
38 import ch.boye.httpclientandroidlib.protocol.HttpContext; |
|
39 |
|
40 import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; |
|
41 import ch.boye.httpclientandroidlib.conn.routing.HttpRoutePlanner; |
|
42 import ch.boye.httpclientandroidlib.conn.scheme.Scheme; |
|
43 import ch.boye.httpclientandroidlib.conn.scheme.SchemeRegistry; |
|
44 |
|
45 import ch.boye.httpclientandroidlib.conn.params.ConnRouteParams; |
|
46 |
|
47 /** |
|
48 * Default implementation of an {@link HttpRoutePlanner}. This implementation |
|
49 * is based on {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames parameters}. |
|
50 * It will not make use of any Java system properties, nor of system or |
|
51 * browser proxy settings. |
|
52 * <p> |
|
53 * The following parameters can be used to customize the behavior of this |
|
54 * class: |
|
55 * <ul> |
|
56 * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li> |
|
57 * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li> |
|
58 * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#FORCED_ROUTE}</li> |
|
59 * </ul> |
|
60 * |
|
61 * @since 4.0 |
|
62 */ |
|
63 @ThreadSafe |
|
64 public class DefaultHttpRoutePlanner implements HttpRoutePlanner { |
|
65 |
|
66 /** The scheme registry. */ |
|
67 protected final SchemeRegistry schemeRegistry; // class is @ThreadSafe |
|
68 |
|
69 /** |
|
70 * Creates a new default route planner. |
|
71 * |
|
72 * @param schreg the scheme registry |
|
73 */ |
|
74 public DefaultHttpRoutePlanner(SchemeRegistry schreg) { |
|
75 if (schreg == null) { |
|
76 throw new IllegalArgumentException |
|
77 ("SchemeRegistry must not be null."); |
|
78 } |
|
79 schemeRegistry = schreg; |
|
80 } |
|
81 |
|
82 public HttpRoute determineRoute(HttpHost target, |
|
83 HttpRequest request, |
|
84 HttpContext context) |
|
85 throws HttpException { |
|
86 |
|
87 if (request == null) { |
|
88 throw new IllegalStateException |
|
89 ("Request must not be null."); |
|
90 } |
|
91 |
|
92 // If we have a forced route, we can do without a target. |
|
93 HttpRoute route = |
|
94 ConnRouteParams.getForcedRoute(request.getParams()); |
|
95 if (route != null) |
|
96 return route; |
|
97 |
|
98 // If we get here, there is no forced route. |
|
99 // So we need a target to compute a route. |
|
100 |
|
101 if (target == null) { |
|
102 throw new IllegalStateException |
|
103 ("Target host must not be null."); |
|
104 } |
|
105 |
|
106 final InetAddress local = |
|
107 ConnRouteParams.getLocalAddress(request.getParams()); |
|
108 final HttpHost proxy = |
|
109 ConnRouteParams.getDefaultProxy(request.getParams()); |
|
110 |
|
111 final Scheme schm; |
|
112 try { |
|
113 schm = schemeRegistry.getScheme(target.getSchemeName()); |
|
114 } catch (IllegalStateException ex) { |
|
115 throw new HttpException(ex.getMessage()); |
|
116 } |
|
117 // as it is typically used for TLS/SSL, we assume that |
|
118 // a layered scheme implies a secure connection |
|
119 final boolean secure = schm.isLayered(); |
|
120 |
|
121 if (proxy == null) { |
|
122 route = new HttpRoute(target, local, secure); |
|
123 } else { |
|
124 route = new HttpRoute(target, local, proxy, secure); |
|
125 } |
|
126 return route; |
|
127 } |
|
128 |
|
129 } |