1 /* 2 * Copyright (C) 2012 The Android Open Source Project 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 com.squareup.okhttp.internal.Util; 19 import java.net.Proxy; 20 import java.net.UnknownHostException; 21 import java.util.List; 22 import javax.net.ssl.HostnameVerifier; 23 import javax.net.ssl.SSLSocketFactory; 24 25 import static com.squareup.okhttp.internal.Util.equal; 26 27 /** 28 * A specification for a connection to an origin server. For simple connections, 29 * this is the server's hostname and port. If an explicit proxy is requested (or 30 * {@link Proxy#NO_PROXY no proxy} is explicitly requested), this also includes 31 * that proxy information. For secure connections the address also includes the 32 * SSL socket factory and hostname verifier. 33 * 34 * <p>HTTP requests that share the same {@code Address} may also share the same 35 * {@link Connection}. 36 */ 37 public final class Address { 38 final Proxy proxy; 39 final String uriHost; 40 final int uriPort; 41 final SSLSocketFactory sslSocketFactory; 42 final HostnameVerifier hostnameVerifier; 43 final OkAuthenticator authenticator; 44 final List<String> transports; 45 46 public Address(String uriHost, int uriPort, SSLSocketFactory sslSocketFactory, 47 HostnameVerifier hostnameVerifier, OkAuthenticator authenticator, Proxy proxy, 48 List<String> transports) throws UnknownHostException { 49 if (uriHost == null) throw new NullPointerException("uriHost == null"); 50 if (uriPort <= 0) throw new IllegalArgumentException("uriPort <= 0: " + uriPort); 51 if (authenticator == null) throw new IllegalArgumentException("authenticator == null"); 52 if (transports == null) throw new IllegalArgumentException("transports == null"); 53 this.proxy = proxy; 54 this.uriHost = uriHost; 55 this.uriPort = uriPort; 56 this.sslSocketFactory = sslSocketFactory; 57 this.hostnameVerifier = hostnameVerifier; 58 this.authenticator = authenticator; 59 this.transports = Util.immutableList(transports); 60 } 61 62 /** Returns the hostname of the origin server. */ 63 public String getUriHost() { 64 return uriHost; 65 } 66 67 /** 68 * Returns the port of the origin server; typically 80 or 443. Unlike 69 * may {@code getPort()} accessors, this method never returns -1. 70 */ 71 public int getUriPort() { 72 return uriPort; 73 } 74 75 /** 76 * Returns the SSL socket factory, or null if this is not an HTTPS 77 * address. 78 */ 79 public SSLSocketFactory getSslSocketFactory() { 80 return sslSocketFactory; 81 } 82 83 /** 84 * Returns the hostname verifier, or null if this is not an HTTPS 85 * address. 86 */ 87 public HostnameVerifier getHostnameVerifier() { 88 return hostnameVerifier; 89 } 90 91 92 /** 93 * Returns the client's authenticator. This method never returns null. 94 */ 95 public OkAuthenticator getAuthenticator() { 96 return authenticator; 97 } 98 99 /** 100 * Returns the client's transports. This method always returns a non-null list 101 * that contains "http/1.1", possibly among other transports. 102 */ 103 public List<String> getTransports() { 104 return transports; 105 } 106 107 /** 108 * Returns this address's explicitly-specified HTTP proxy, or null to 109 * delegate to the HTTP client's proxy selector. 110 */ 111 public Proxy getProxy() { 112 return proxy; 113 } 114 115 @Override public boolean equals(Object other) { 116 if (other instanceof Address) { 117 Address that = (Address) other; 118 return equal(this.proxy, that.proxy) 119 && this.uriHost.equals(that.uriHost) 120 && this.uriPort == that.uriPort 121 && equal(this.sslSocketFactory, that.sslSocketFactory) 122 && equal(this.hostnameVerifier, that.hostnameVerifier) 123 && equal(this.authenticator, that.authenticator) 124 && equal(this.transports, that.transports); 125 } 126 return false; 127 } 128 129 @Override public int hashCode() { 130 int result = 17; 131 result = 31 * result + uriHost.hashCode(); 132 result = 31 * result + uriPort; 133 result = 31 * result + (sslSocketFactory != null ? sslSocketFactory.hashCode() : 0); 134 result = 31 * result + (hostnameVerifier != null ? hostnameVerifier.hashCode() : 0); 135 result = 31 * result + (authenticator != null ? authenticator.hashCode() : 0); 136 result = 31 * result + (proxy != null ? proxy.hashCode() : 0); 137 result = 31 * result + transports.hashCode(); 138 return result; 139 } 140 } 141