View Javadoc

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.protocol.HttpContext;
43  
44  /**
45   * {@link org.apache.http.conn.routing.HttpRoutePlanner} implementation
46   * based on {@link ProxySelector}. By default, this class will pick up
47   * the proxy settings of the JVM, either from system properties
48   * or from the browser running the application.
49   *
50   * @since 4.3
51   */
52  @Immutable
53  public class SystemDefaultRoutePlanner extends DefaultRoutePlanner {
54  
55      private final ProxySelector proxySelector;
56  
57      public SystemDefaultRoutePlanner(
58              final SchemePortResolver schemePortResolver,
59              final ProxySelector proxySelector) {
60          super(schemePortResolver);
61          this.proxySelector = proxySelector != null ? proxySelector : ProxySelector.getDefault();
62      }
63  
64      public SystemDefaultRoutePlanner(final ProxySelector proxySelector) {
65          this(null, proxySelector);
66      }
67  
68      @Override
69      protected HttpHost determineProxy(
70              final HttpHost    target,
71              final HttpRequest request,
72              final HttpContext context) throws HttpException {
73          final URI targetURI;
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);
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(final List<Proxy> proxies) {
106         Proxy result = null;
107         // check the list for one we can use
108         for (int i=0; (result == null) && (i < proxies.size()); i++) {
109             final Proxy p = proxies.get(i);
110             switch (p.type()) {
111 
112             case DIRECT:
113             case HTTP:
114                 result = p;
115                 break;
116 
117             case SOCKS:
118                 // SOCKS hosts are not handled on the route level.
119                 // The socket may make use of the SOCKS host though.
120                 break;
121             }
122         }
123         if (result == null) {
124             //@@@ log as warning or info that only a socks proxy is available?
125             // result can only be null if all proxies are socks proxies
126             // socks proxies are not handled on the route planning level
127             result = Proxy.NO_PROXY;
128         }
129         return result;
130     }
131 
132 }