1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/DefaultHttpRoutePlanner.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,129 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl.conn; 1.32 + 1.33 + 1.34 +import java.net.InetAddress; 1.35 + 1.36 +import ch.boye.httpclientandroidlib.annotation.ThreadSafe; 1.37 + 1.38 +import ch.boye.httpclientandroidlib.HttpException; 1.39 +import ch.boye.httpclientandroidlib.HttpHost; 1.40 +import ch.boye.httpclientandroidlib.HttpRequest; 1.41 +import ch.boye.httpclientandroidlib.protocol.HttpContext; 1.42 + 1.43 +import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; 1.44 +import ch.boye.httpclientandroidlib.conn.routing.HttpRoutePlanner; 1.45 +import ch.boye.httpclientandroidlib.conn.scheme.Scheme; 1.46 +import ch.boye.httpclientandroidlib.conn.scheme.SchemeRegistry; 1.47 + 1.48 +import ch.boye.httpclientandroidlib.conn.params.ConnRouteParams; 1.49 + 1.50 +/** 1.51 + * Default implementation of an {@link HttpRoutePlanner}. This implementation 1.52 + * is based on {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames parameters}. 1.53 + * It will not make use of any Java system properties, nor of system or 1.54 + * browser proxy settings. 1.55 + * <p> 1.56 + * The following parameters can be used to customize the behavior of this 1.57 + * class: 1.58 + * <ul> 1.59 + * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li> 1.60 + * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li> 1.61 + * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#FORCED_ROUTE}</li> 1.62 + * </ul> 1.63 + * 1.64 + * @since 4.0 1.65 + */ 1.66 +@ThreadSafe 1.67 +public class DefaultHttpRoutePlanner implements HttpRoutePlanner { 1.68 + 1.69 + /** The scheme registry. */ 1.70 + protected final SchemeRegistry schemeRegistry; // class is @ThreadSafe 1.71 + 1.72 + /** 1.73 + * Creates a new default route planner. 1.74 + * 1.75 + * @param schreg the scheme registry 1.76 + */ 1.77 + public DefaultHttpRoutePlanner(SchemeRegistry schreg) { 1.78 + if (schreg == null) { 1.79 + throw new IllegalArgumentException 1.80 + ("SchemeRegistry must not be null."); 1.81 + } 1.82 + schemeRegistry = schreg; 1.83 + } 1.84 + 1.85 + public HttpRoute determineRoute(HttpHost target, 1.86 + HttpRequest request, 1.87 + HttpContext context) 1.88 + throws HttpException { 1.89 + 1.90 + if (request == null) { 1.91 + throw new IllegalStateException 1.92 + ("Request must not be null."); 1.93 + } 1.94 + 1.95 + // If we have a forced route, we can do without a target. 1.96 + HttpRoute route = 1.97 + ConnRouteParams.getForcedRoute(request.getParams()); 1.98 + if (route != null) 1.99 + return route; 1.100 + 1.101 + // If we get here, there is no forced route. 1.102 + // So we need a target to compute a route. 1.103 + 1.104 + if (target == null) { 1.105 + throw new IllegalStateException 1.106 + ("Target host must not be null."); 1.107 + } 1.108 + 1.109 + final InetAddress local = 1.110 + ConnRouteParams.getLocalAddress(request.getParams()); 1.111 + final HttpHost proxy = 1.112 + ConnRouteParams.getDefaultProxy(request.getParams()); 1.113 + 1.114 + final Scheme schm; 1.115 + try { 1.116 + schm = schemeRegistry.getScheme(target.getSchemeName()); 1.117 + } catch (IllegalStateException ex) { 1.118 + throw new HttpException(ex.getMessage()); 1.119 + } 1.120 + // as it is typically used for TLS/SSL, we assume that 1.121 + // a layered scheme implies a secure connection 1.122 + final boolean secure = schm.isLayered(); 1.123 + 1.124 + if (proxy == null) { 1.125 + route = new HttpRoute(target, local, secure); 1.126 + } else { 1.127 + route = new HttpRoute(target, local, proxy, secure); 1.128 + } 1.129 + return route; 1.130 + } 1.131 + 1.132 +}