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.nio.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.client.protocol.ClientContext;
36  import org.apache.http.conn.params.ConnRouteParams;
37  import org.apache.http.conn.routing.HttpRoute;
38  import org.apache.http.conn.routing.HttpRoutePlanner;
39  import org.apache.http.nio.conn.scheme.AsyncScheme;
40  import org.apache.http.nio.conn.scheme.AsyncSchemeRegistry;
41  import org.apache.http.nio.conn.scheme.LayeringStrategy;
42  import org.apache.http.protocol.HttpContext;
43  
44  @Deprecated
45  public class DefaultHttpAsyncRoutePlanner implements HttpRoutePlanner {
46  
47      private final AsyncSchemeRegistry schemeRegistry;
48  
49      public DefaultHttpAsyncRoutePlanner(final AsyncSchemeRegistry schemeRegistry) {
50          super();
51          this.schemeRegistry = schemeRegistry;
52      }
53  
54      private AsyncSchemeRegistry getSchemeRegistry(final HttpContext context) {
55          AsyncSchemeRegistry reg = (AsyncSchemeRegistry) context.getAttribute(
56                  ClientContext.SCHEME_REGISTRY);
57          if (reg == null) {
58              reg = this.schemeRegistry;
59          }
60          return reg;
61      }
62  
63      @Override
64      public HttpRoute determineRoute(
65              final HttpHost target,
66              final HttpRequest request,
67              final HttpContext context) throws HttpException {
68          if (request == null) {
69              throw new IllegalStateException("Request may not be null");
70          }
71          HttpRoute route = ConnRouteParams.getForcedRoute(request.getParams());
72          if (route != null) {
73              return route;
74          }
75          if (target == null) {
76              throw new IllegalStateException("Target host may be null");
77          }
78          final InetAddress local = ConnRouteParams.getLocalAddress(request.getParams());
79          final HttpHost proxy = ConnRouteParams.getDefaultProxy(request.getParams());
80          final AsyncScheme scheme;
81          try {
82              final AsyncSchemeRegistry registry = getSchemeRegistry(context);
83              scheme = registry.getScheme(target);
84          } catch (final IllegalStateException ex) {
85              throw new HttpException(ex.getMessage());
86          }
87          final LayeringStrategy layeringStrategy = scheme.getLayeringStrategy();
88          final boolean secure = layeringStrategy != null && layeringStrategy.isSecure();
89          if (proxy == null) {
90              route = new HttpRoute(target, local, secure);
91          } else {
92              route = new HttpRoute(target, local, proxy, secure);
93          }
94          return route;
95      }
96  
97  }