Home | History | Annotate | Download | only in okhttp
      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 {@linkplain 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  * </ul>
     32  * Each route is a specific selection of these options.
     33  */
     34 public final class Route {
     35   final Address address;
     36   final Proxy proxy;
     37   final InetSocketAddress inetSocketAddress;
     38 
     39   public Route(Address address, Proxy proxy, InetSocketAddress inetSocketAddress) {
     40     if (address == null) {
     41       throw new NullPointerException("address == null");
     42     }
     43     if (proxy == null) {
     44       throw new NullPointerException("proxy == null");
     45     }
     46     if (inetSocketAddress == null) {
     47       throw new NullPointerException("inetSocketAddress == null");
     48     }
     49     this.address = address;
     50     this.proxy = proxy;
     51     this.inetSocketAddress = inetSocketAddress;
     52   }
     53 
     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    * when it is null. When the address's proxy is null, the proxy selector is
     63    * used.
     64    */
     65   public Proxy getProxy() {
     66     return proxy;
     67   }
     68 
     69   public InetSocketAddress getSocketAddress() {
     70     return inetSocketAddress;
     71   }
     72 
     73   /**
     74    * Returns true if this route tunnels HTTPS through an HTTP proxy. See <a
     75    * href="http://www.ietf.org/rfc/rfc2817.txt">RFC 2817, Section 5.2</a>.
     76    */
     77   public boolean requiresTunnel() {
     78     return address.sslSocketFactory != null && proxy.type() == Proxy.Type.HTTP;
     79   }
     80 
     81   @Override public boolean equals(Object obj) {
     82     if (obj instanceof Route) {
     83       Route other = (Route) obj;
     84       return address.equals(other.address)
     85           && proxy.equals(other.proxy)
     86           && inetSocketAddress.equals(other.inetSocketAddress);
     87     }
     88     return false;
     89   }
     90 
     91   @Override public int hashCode() {
     92     int result = 17;
     93     result = 31 * result + address.hashCode();
     94     result = 31 * result + proxy.hashCode();
     95     result = 31 * result + inetSocketAddress.hashCode();
     96     return result;
     97   }
     98 }
     99