001package org.eclipse.aether.repository;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022/**
023 * A proxy to use for connections to a repository.
024 */
025public final class Proxy
026{
027
028    /**
029     * Type denoting a proxy for HTTP transfers.
030     */
031    public static final String TYPE_HTTP = "http";
032
033    /**
034     * Type denoting a proxy for HTTPS transfers.
035     */
036    public static final String TYPE_HTTPS = "https";
037
038    private final String type;
039
040    private final String host;
041
042    private final int port;
043
044    private final Authentication auth;
045
046    /**
047     * Creates a new proxy with the specified properties and no authentication.
048     * 
049     * @param type The type of the proxy, e.g. "http", may be {@code null}.
050     * @param host The host of the proxy, may be {@code null}.
051     * @param port The port of the proxy.
052     */
053    public Proxy( String type, String host, int port )
054    {
055        this( type, host, port, null );
056    }
057
058    /**
059     * Creates a new proxy with the specified properties.
060     * 
061     * @param type The type of the proxy, e.g. "http", may be {@code null}.
062     * @param host The host of the proxy, may be {@code null}.
063     * @param port The port of the proxy.
064     * @param auth The authentication to use for the proxy connection, may be {@code null}.
065     */
066    public Proxy( String type, String host, int port, Authentication auth )
067    {
068        this.type = ( type != null ) ? type : "";
069        this.host = ( host != null ) ? host : "";
070        this.port = port;
071        this.auth = auth;
072    }
073
074    /**
075     * Gets the type of this proxy.
076     * 
077     * @return The type of this proxy, never {@code null}.
078     */
079    public String getType()
080    {
081        return type;
082    }
083
084    /**
085     * Gets the host for this proxy.
086     * 
087     * @return The host for this proxy, never {@code null}.
088     */
089    public String getHost()
090    {
091        return host;
092    }
093
094    /**
095     * Gets the port number for this proxy.
096     * 
097     * @return The port number for this proxy.
098     */
099    public int getPort()
100    {
101        return port;
102    }
103
104    /**
105     * Gets the authentication to use for the proxy connection.
106     * 
107     * @return The authentication to use or {@code null} if none.
108     */
109    public Authentication getAuthentication()
110    {
111        return auth;
112    }
113
114    @Override
115    public String toString()
116    {
117        return getHost() + ':' + getPort();
118    }
119
120    @Override
121    public boolean equals( Object obj )
122    {
123        if ( this == obj )
124        {
125            return true;
126        }
127        if ( obj == null || !getClass().equals( obj.getClass() ) )
128        {
129            return false;
130        }
131
132        Proxy that = (Proxy) obj;
133
134        return eq( type, that.type ) && eq( host, that.host ) && port == that.port && eq( auth, that.auth );
135    }
136
137    private static <T> boolean eq( T s1, T s2 )
138    {
139        return s1 != null ? s1.equals( s2 ) : s2 == null;
140    }
141
142    @Override
143    public int hashCode()
144    {
145        int hash = 17;
146        hash = hash * 31 + hash( host );
147        hash = hash * 31 + hash( type );
148        hash = hash * 31 + port;
149        hash = hash * 31 + hash( auth );
150        return hash;
151    }
152
153    private static int hash( Object obj )
154    {
155        return obj != null ? obj.hashCode() : 0;
156    }
157
158}