Proxy class in Java

java.net.Proxy

This class represents a proxy setting, typically a type (http, socks) and a socket address. A Proxy is an immutable object.

Declaration

public class Proxy {

    public enum Type {

        DIRECT,

        HTTP,

        SOCKS
    };

    private Type type;
    private SocketAddress sa;

    public static final Proxy NO_PROXY = new Proxy();

    private Proxy() {
        type = Type.DIRECT;
        sa = null;
    }

    public Proxy(Type type, SocketAddress sa) {
        if ((type == Type.DIRECT) || !(sa instanceof InetSocketAddress))
            throw new IllegalArgumentException("type " + type + " is not compatible with address " + sa);
        this.type = type;
        this.sa = sa;
    }

    public Type type() {
        return type;
    }

    public SocketAddress address() {
        return sa;
    }

    public String toString() {
        if (type() == Type.DIRECT)
            return "DIRECT";
        return type() + " @ " + address();
    }

    public final boolean equals(Object obj) {
        if (obj == null || !(obj instanceof Proxy))
            return false;
        Proxy p = (Proxy) obj;
        if (p.type() == type()) {
            if (address() == null) {
                return (p.address() == null);
            } else
                return address().equals(p.address());
        }
        return false;
    }

    public final int hashCode() {
        if (address() == null)
            return type().hashCode();
        return type().hashCode() + address().hashCode();
    }
}


Some methods of Proxy class

Proxy(Type, SocketAddress)This method creates an entry representing a PROXY connection. 

address()This method returns the socket address of the proxy, or null if it's a direct connection.

Proxy ConstantsSome of the Proxy constants like Proxy.NO_PROXY, Type.DIRECT, etc.

equals(Object)This method compares this object against the specified object.

hashCode()This method returns a hashcode for this Proxy.

toString()This method constructs a string representation of this Proxy. This String is constructed by calling toString() on its type and concatenating " @ " and the toString() results from its address if its type is not DIRECT.

type()This method returns the proxy type.

No comments:

Post a Comment