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.InetAddress;
31
32 import org.apache.http.HttpException;
33 import org.apache.http.HttpHost;
34 import org.apache.http.HttpRequest;
35 import org.apache.http.ProtocolException;
36 import org.apache.http.annotation.Contract;
37 import org.apache.http.annotation.ThreadingBehavior;
38 import org.apache.http.client.config.RequestConfig;
39 import org.apache.http.client.protocol.HttpClientContext;
40 import org.apache.http.conn.SchemePortResolver;
41 import org.apache.http.conn.UnsupportedSchemeException;
42 import org.apache.http.conn.routing.HttpRoute;
43 import org.apache.http.conn.routing.HttpRoutePlanner;
44 import org.apache.http.protocol.HttpContext;
45 import org.apache.http.util.Args;
46
47 /**
48  * Default implementation of an {@link HttpRoutePlanner}. It will not make use of
49  * any Java system properties, nor of system or browser proxy settings.
50  *
51  * @since 4.3
52  */

53 @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
54 public class DefaultRoutePlanner implements HttpRoutePlanner {
55
56     private final SchemePortResolver schemePortResolver;
57
58     public DefaultRoutePlanner(final SchemePortResolver schemePortResolver) {
59         super();
60         this.schemePortResolver = schemePortResolver != null ? schemePortResolver :
61             DefaultSchemePortResolver.INSTANCE;
62     }
63
64     @Override
65     public HttpRoute determineRoute(
66             final HttpHost host,
67             final HttpRequest request,
68             final HttpContext context) throws HttpException {
69         Args.notNull(request, "Request");
70         if (host == null) {
71             throw new ProtocolException("Target host is not specified");
72         }
73         final HttpClientContext clientContext = HttpClientContext.adapt(context);
74         final RequestConfig config = clientContext.getRequestConfig();
75         final InetAddress local = config.getLocalAddress();
76         HttpHost proxy = config.getProxy();
77         if (proxy == null) {
78             proxy = determineProxy(host, request, context);
79         }
80
81         final HttpHost target;
82         if (host.getPort() <= 0) {
83             try {
84                 target = new HttpHost(
85                         host.getHostName(),
86                         this.schemePortResolver.resolve(host),
87                         host.getSchemeName());
88             } catch (final UnsupportedSchemeException ex) {
89                 throw new HttpException(ex.getMessage());
90             }
91         } else {
92             target = host;
93         }
94         final boolean secure = target.getSchemeName().equalsIgnoreCase("https");
95         return proxy == null
96                         ? new HttpRoute(target, local, secure)
97                         : new HttpRoute(target, local, proxy, secure);
98     }
99
100     /**
101      * This implementation returns null.
102      *
103      * @throws HttpException may be thrown if overridden
104      */

105     protected HttpHost determineProxy(
106             final HttpHost target,
107             final HttpRequest request,
108             final HttpContext context) throws HttpException {
109         return null;
110     }
111
112 }
113