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