1 /* 2 * Copyright (C) 2013 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.squareup.okhttp; 17 18 import java.net.InetSocketAddress; 19 import java.net.Proxy; 20 21 /** 22 * The concrete route used by a connection to reach an abstract origin server. 23 * When creating a connection the client has many options: 24 * <ul> 25 * <li><strong>HTTP proxy:</strong> a proxy server may be explicitly 26 * configured for the client. Otherwise the {@link java.net.ProxySelector 27 * proxy selector} is used. It may return multiple proxies to attempt. 28 * <li><strong>IP address:</strong> whether connecting directly to an origin 29 * server or a proxy, opening a socket requires an IP address. The DNS 30 * server may return multiple IP addresses to attempt. 31 * <li><strong>Modern TLS:</strong> whether to include advanced TLS options 32 * when attempting a HTTPS connection. 33 * </ul> 34 * Each route is a specific selection of these options. 35 */ 36 public class Route { 37 final Address address; 38 final Proxy proxy; 39 final InetSocketAddress inetSocketAddress; 40 final boolean modernTls; 41 42 public Route(Address address, Proxy proxy, InetSocketAddress inetSocketAddress, 43 boolean modernTls) { 44 if (address == null) throw new NullPointerException("address == null"); 45 if (proxy == null) throw new NullPointerException("proxy == null"); 46 if (inetSocketAddress == null) throw new NullPointerException("inetSocketAddress == null"); 47 this.address = address; 48 this.proxy = proxy; 49 this.inetSocketAddress = inetSocketAddress; 50 this.modernTls = modernTls; 51 } 52 53 /** Returns the {@link Address} of this route. */ 54 public Address getAddress() { 55 return address; 56 } 57 58 /** 59 * Returns the {@link Proxy} of this route. 60 * 61 * <strong>Warning:</strong> This may disagree with {@link Address#getProxy} 62 * is null. When the address's proxy is null, the proxy selector will be used. 63 */ 64 public Proxy getProxy() { 65 return proxy; 66 } 67 68 /** Returns the {@link InetSocketAddress} of this route. */ 69 public InetSocketAddress getSocketAddress() { 70 return inetSocketAddress; 71 } 72 73 /** Returns true if this route uses modern TLS. */ 74 public boolean isModernTls() { 75 return modernTls; 76 } 77 78 @Override public boolean equals(Object obj) { 79 if (obj instanceof Route) { 80 Route other = (Route) obj; 81 return (address.equals(other.address) 82 && proxy.equals(other.proxy) 83 && inetSocketAddress.equals(other.inetSocketAddress) 84 && modernTls == other.modernTls); 85 } 86 return false; 87 } 88 89 @Override public int hashCode() { 90 int result = 17; 91 result = 31 * result + address.hashCode(); 92 result = 31 * result + proxy.hashCode(); 93 result = 31 * result + inetSocketAddress.hashCode(); 94 result = result + (modernTls ? (31 * result) : 0); 95 return result; 96 } 97 } 98