jdk/src/share/classes/java/net/Proxy.java
author chegar
Fri, 16 Sep 2011 12:09:04 -0700
changeset 10596 39b3a979e600
parent 5506 202f599c92aa
child 19069 1d9cb0d080e3
permissions -rw-r--r--
7090158: Networking Libraries don't build with javac -Werror Summary: Minor changes to networking java files to remove warnings Reviewed-by: chegar, weijun, hawtin Contributed-by: kurchi.subhra.hazra@oracle.com, sasha_bu@hotmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This class represents a proxy setting, typically a type (http, socks) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * a socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A <code>Proxy</code> is an immutable object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @see     java.net.ProxySelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author Yingxian Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Jean-Christophe Collet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class Proxy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Represents the proxy type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public enum Type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
         * Represents a direct connection, or the absence of a proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        DIRECT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
         * Represents proxy for high level protocols such as HTTP or FTP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        HTTP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
         * Represents a SOCKS (V4 or V5) proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        SOCKS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private Type type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private SocketAddress sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * A proxy setting that represents a <code>DIRECT</code> connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * basically telling the protocol handler not to use any proxying.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Used, for instance, to create sockets bypassing any other global
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * proxy settings (like SOCKS):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * <code>Socket s = new Socket(Proxy.NO_PROXY);</code><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public final static Proxy NO_PROXY = new Proxy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // Creates the proxy that represents a <code>DIRECT</code> connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private Proxy() {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    76
        type = Type.DIRECT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        sa = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Creates an entry representing a PROXY connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Certain combinations are illegal. For instance, for types Http, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Socks, a SocketAddress <b>must</b> be provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Use the <code>Proxy.NO_PROXY</code> constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * for representing a direct connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param type the <code>Type</code> of the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param sa the <code>SocketAddress</code> for that proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @throws IllegalArgumentException when the type and the address are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * incompatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public Proxy(Type type, SocketAddress sa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if ((type == Type.DIRECT) || !(sa instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new IllegalArgumentException("type " + type + " is not compatible with address " + sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.sa = sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Returns the proxy type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return a Type representing the proxy type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public Type type() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Returns the socket address of the proxy, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <code>null</code> if its a direct connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @return a <code>SocketAddress</code> representing the socket end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *         point of the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public SocketAddress address() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Constructs a string representation of this Proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * This String is constructed by calling toString() on its type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * and concatenating " @ " and the toString() result from its address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * if its type is not <code>DIRECT</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @return  a string representation of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (type() == Type.DIRECT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return "DIRECT";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return type() + " @ " + address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Compares this object against the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * The result is <code>true</code> if and only if the argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * not <code>null</code> and it represents the same proxy as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Two instances of <code>Proxy</code> represent the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * address if both the SocketAddresses and type are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param   obj   the object to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return  <code>true</code> if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @see java.net.InetSocketAddress#equals(java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public final boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (obj == null || !(obj instanceof Proxy))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        Proxy p = (Proxy) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (p.type() == type()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (address() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                return (p.address() == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                return address().equals(p.address());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Returns a hashcode for this Proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return  a hash code value for this Proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public final int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (address() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return type().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return type().hashCode() + address().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}