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 org.apache.http.impl.conn;
29
30 import java.net.InetSocketAddress;
31 import java.net.Proxy;
32 import java.net.ProxySelector;
33 import java.net.URI;
34 import java.net.URISyntaxException;
35 import java.util.List;
36
37 import org.apache.http.HttpException;
38 import org.apache.http.HttpHost;
39 import org.apache.http.HttpRequest;
40 import org.apache.http.annotation.Immutable;
41 import org.apache.http.conn.SchemePortResolver;
42 import org.apache.http.conn.routing.HttpRoutePlanner;
43 import org.apache.http.protocol.HttpContext;
44
45 /**
46 * {@link HttpRoutePlanner} implementation based on {@link ProxySelector}.
47 * By default, it will pick up the proxy settings of the JVM, either
48 * from system properties or from the browser running the application.
49 * Additionally, it interprets some
50 * {@link org.apache.http.conn.params.ConnRoutePNames parameters},
51 * though not the {@link
52 * org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY}.
53 *
54 * @since 4.3
55 */
56 @Immutable
57 public class SystemDefaultRoutePlanner extends DefaultRoutePlanner {
58
59 private final ProxySelector proxySelector;
60
61 public SystemDefaultRoutePlanner(
62 final SchemePortResolver schemePortResolver,
63 final ProxySelector proxySelector) {
64 super(schemePortResolver);
65 this.proxySelector = proxySelector != null ? proxySelector : ProxySelector.getDefault();
66 }
67
68 @Override
69 protected HttpHost determineProxy(
70 final HttpHost target,
71 final HttpRequest request,
72 final HttpContext context) throws HttpException {
73 URI targetURI = null;
74 try {
75 targetURI = new URI(target.toURI());
76 } catch (final URISyntaxException ex) {
77 throw new HttpException("Cannot convert host to URI: " + target, ex);
78 }
79 final List<Proxy> proxies = this.proxySelector.select(targetURI);
80 final Proxy p = chooseProxy(proxies, target, request, context);
81 HttpHost result = null;
82 if (p.type() == Proxy.Type.HTTP) {
83 // convert the socket address to an HttpHost
84 if (!(p.address() instanceof InetSocketAddress)) {
85 throw new HttpException("Unable to handle non-Inet proxy address: " + p.address());
86 }
87 final InetSocketAddress isa = (InetSocketAddress) p.address();
88 // assume default scheme (http)
89 result = new HttpHost(getHost(isa), isa.getPort());
90 }
91
92 return result;
93 }
94
95 private String getHost(final InetSocketAddress isa) {
96
97 //@@@ Will this work with literal IPv6 addresses, or do we
98 //@@@ need to wrap these in [] for the string representation?
99 //@@@ Having it in this method at least allows for easy workarounds.
100 return isa.isUnresolved() ?
101 isa.getHostName() : isa.getAddress().getHostAddress();
102
103 }
104
105 private Proxy chooseProxy(
106 final List<Proxy> proxies,
107 final HttpHost target,
108 final HttpRequest request,
109 final HttpContext context) {
110 Proxy result = null;
111 // check the list for one we can use
112 for (int i=0; (result == null) && (i < proxies.size()); i++) {
113 final Proxy p = proxies.get(i);
114 switch (p.type()) {
115
116 case DIRECT:
117 case HTTP:
118 result = p;
119 break;
120
121 case SOCKS:
122 // SOCKS hosts are not handled on the route level.
123 // The socket may make use of the SOCKS host though.
124 break;
125 }
126 }
127 if (result == null) {
128 //@@@ log as warning or info that only a socks proxy is available?
129 // result can only be null if all proxies are socks proxies
130 // socks proxies are not handled on the route planning level
131 result = Proxy.NO_PROXY;
132 }
133 return result;
134 }
135
136 }