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  
31  import java.net.InetAddress;
32  
33  import org.apache.http.HttpException;
34  import org.apache.http.HttpHost;
35  import org.apache.http.HttpRequest;
36  import org.apache.http.annotation.Contract;
37  import org.apache.http.annotation.ThreadingBehavior;
38  import org.apache.http.conn.params.ConnRouteParams;
39  import org.apache.http.conn.routing.HttpRoute;
40  import org.apache.http.conn.routing.HttpRoutePlanner;
41  import org.apache.http.conn.scheme.Scheme;
42  import org.apache.http.conn.scheme.SchemeRegistry;
43  import org.apache.http.protocol.HttpContext;
44  import org.apache.http.util.Args;
45  import org.apache.http.util.Asserts;
46  
47  /**
48   * Default implementation of an {@link HttpRoutePlanner}. This implementation
49   * is based on {@link org.apache.http.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 org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li>
57   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li>
58   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#FORCED_ROUTE}</li>
59   * </ul>
60   *
61   * @since 4.0
62   *
63   * @deprecated (4.3) use {@link DefaultRoutePlanner}
64   */
65  @Contract(threading = ThreadingBehavior.SAFE)
66  @Deprecated
67  public class DefaultHttpRoutePlanner implements HttpRoutePlanner {
68  
69      /** The scheme registry. */
70      protected final SchemeRegistry schemeRegistry; // class is @Contract(threading = ThreadingBehavior.SAFE)
71  
72      /**
73       * Creates a new default route planner.
74       *
75       * @param schreg    the scheme registry
76       */
77      public DefaultHttpRoutePlanner(final SchemeRegistry schreg) {
78          Args.notNull(schreg, "Scheme registry");
79          schemeRegistry = schreg;
80      }
81  
82      @Override
83      public HttpRoute determineRoute(final HttpHost target,
84                                      final HttpRequest request,
85                                      final HttpContext context)
86          throws HttpException {
87  
88          Args.notNull(request, "HTTP request");
89  
90          // If we have a forced route, we can do without a target.
91          HttpRoute route =
92              ConnRouteParams.getForcedRoute(request.getParams());
93          if (route != null) {
94              return route;
95          }
96  
97          // If we get here, there is no forced route.
98          // So we need a target to compute a route.
99  
100         Asserts.notNull(target, "Target host");
101 
102         final InetAddress local =
103             ConnRouteParams.getLocalAddress(request.getParams());
104         final HttpHost proxy =
105             ConnRouteParams.getDefaultProxy(request.getParams());
106 
107         final Scheme schm;
108         try {
109             schm = this.schemeRegistry.getScheme(target.getSchemeName());
110         } catch (final IllegalStateException ex) {
111             throw new HttpException(ex.getMessage());
112         }
113         // as it is typically used for TLS/SSL, we assume that
114         // a layered scheme implies a secure connection
115         final boolean secure = schm.isLayered();
116 
117         if (proxy == null) {
118             route = new HttpRoute(target, local, secure);
119         } else {
120             route = new HttpRoute(target, local, proxy, secure);
121         }
122         return route;
123     }
124 
125 }