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