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
022import java.util.Objects;
023
024/**
025 * A proxy to use for connections to a repository.
026 */
027public final class Proxy
028{
029
030    /**
031     * Type denoting a proxy for HTTP transfers.
032     */
033    public static final String TYPE_HTTP = "http";
034
035    /**
036     * Type denoting a proxy for HTTPS transfers.
037     */
038    public static final String TYPE_HTTPS = "https";
039
040    private final String type;
041
042    private final String host;
043
044    private final int port;
045
046    private final Authentication auth;
047
048    /**
049     * Creates a new proxy with the specified properties and no authentication.
050     * 
051     * @param type The type of the proxy, e.g. "http", may be {@code null}.
052     * @param host The host of the proxy, may be {@code null}.
053     * @param port The port of the proxy.
054     */
055    public Proxy( String type, String host, int port )
056    {
057        this( type, host, port, null );
058    }
059
060    /**
061     * Creates a new proxy with the specified properties.
062     * 
063     * @param type The type of the proxy, e.g. "http", may be {@code null}.
064     * @param host The host of the proxy, may be {@code null}.
065     * @param port The port of the proxy.
066     * @param auth The authentication to use for the proxy connection, may be {@code null}.
067     */
068    public Proxy( String type, String host, int port, Authentication auth )
069    {
070        this.type = ( type != null ) ? type : "";
071        this.host = ( host != null ) ? host : "";
072        this.port = port;
073        this.auth = auth;
074    }
075
076    /**
077     * Gets the type of this proxy.
078     * 
079     * @return The type of this proxy, never {@code null}.
080     */
081    public String getType()
082    {
083        return type;
084    }
085
086    /**
087     * Gets the host for this proxy.
088     * 
089     * @return The host for this proxy, never {@code null}.
090     */
091    public String getHost()
092    {
093        return host;
094    }
095
096    /**
097     * Gets the port number for this proxy.
098     * 
099     * @return The port number for this proxy.
100     */
101    public int getPort()
102    {
103        return port;
104    }
105
106    /**
107     * Gets the authentication to use for the proxy connection.
108     * 
109     * @return The authentication to use or {@code null} if none.
110     */
111    public Authentication getAuthentication()
112    {
113        return auth;
114    }
115
116    @Override
117    public String toString()
118    {
119        return getHost() + ':' + getPort();
120    }
121
122    @Override
123    public boolean equals( Object obj )
124    {
125        if ( this == obj )
126        {
127            return true;
128        }
129        if ( obj == null || !getClass().equals( obj.getClass() ) )
130        {
131            return false;
132        }
133
134        Proxy that = (Proxy) obj;
135
136        return Objects.equals( type, that.type )
137                && Objects.equals( host, that.host ) && port == that.port
138                && Objects.equals( auth, that.auth );
139    }
140
141    @Override
142    public int hashCode()
143    {
144        int hash = 17;
145        hash = hash * 31 + hash( host );
146        hash = hash * 31 + hash( type );
147        hash = hash * 31 + port;
148        hash = hash * 31 + hash( auth );
149        return hash;
150    }
151
152    private static int hash( Object obj )
153    {
154        return obj != null ? obj.hashCode() : 0;
155    }
156
157}