001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.impl.conn; 029 030import java.net.InetAddress; 031 032import org.apache.http.HttpException; 033import org.apache.http.HttpHost; 034import org.apache.http.HttpRequest; 035import org.apache.http.ProtocolException; 036import org.apache.http.annotation.Contract; 037import org.apache.http.annotation.ThreadingBehavior; 038import org.apache.http.client.config.RequestConfig; 039import org.apache.http.client.protocol.HttpClientContext; 040import org.apache.http.conn.SchemePortResolver; 041import org.apache.http.conn.UnsupportedSchemeException; 042import org.apache.http.conn.routing.HttpRoute; 043import org.apache.http.conn.routing.HttpRoutePlanner; 044import org.apache.http.protocol.HttpContext; 045import org.apache.http.util.Args; 046 047/** 048 * Default implementation of an {@link HttpRoutePlanner}. It will not make use of 049 * any Java system properties, nor of system or browser proxy settings. 050 * 051 * @since 4.3 052 */ 053@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL) 054public class DefaultRoutePlanner implements HttpRoutePlanner { 055 056 private final SchemePortResolver schemePortResolver; 057 058 public DefaultRoutePlanner(final SchemePortResolver schemePortResolver) { 059 super(); 060 this.schemePortResolver = schemePortResolver != null ? schemePortResolver : 061 DefaultSchemePortResolver.INSTANCE; 062 } 063 064 @Override 065 public HttpRoute determineRoute( 066 final HttpHost host, 067 final HttpRequest request, 068 final HttpContext context) throws HttpException { 069 Args.notNull(request, "Request"); 070 if (host == null) { 071 throw new ProtocolException("Target host is not specified"); 072 } 073 final HttpClientContext clientContext = HttpClientContext.adapt(context); 074 final RequestConfig config = clientContext.getRequestConfig(); 075 final InetAddress local = config.getLocalAddress(); 076 HttpHost proxy = config.getProxy(); 077 if (proxy == null) { 078 proxy = determineProxy(host, request, context); 079 } 080 081 final HttpHost target; 082 if (host.getPort() <= 0) { 083 try { 084 target = new HttpHost( 085 host.getHostName(), 086 this.schemePortResolver.resolve(host), 087 host.getSchemeName()); 088 } catch (final UnsupportedSchemeException ex) { 089 throw new HttpException(ex.getMessage()); 090 } 091 } else { 092 target = host; 093 } 094 final boolean secure = target.getSchemeName().equalsIgnoreCase("https"); 095 if (proxy == null) { 096 return new HttpRoute(target, local, secure); 097 } else { 098 return new HttpRoute(target, local, proxy, secure); 099 } 100 } 101 102 /** 103 * This implementation returns null. 104 * 105 * @throws HttpException may be thrown if overridden 106 */ 107 protected HttpHost determineProxy( 108 final HttpHost target, 109 final HttpRequest request, 110 final HttpContext context) throws HttpException { 111 return null; 112 } 113 114}