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