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