View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.repository;
20  
21  import java.util.Objects;
22  
23  /**
24   * A proxy to use for connections to a repository.
25   */
26  public final class Proxy {
27  
28      /**
29       * Type denoting a proxy for HTTP transfers.
30       */
31      public static final String TYPE_HTTP = "http";
32  
33      /**
34       * Type denoting a proxy for HTTPS transfers.
35       */
36      public static final String TYPE_HTTPS = "https";
37  
38      private final String type;
39  
40      private final String host;
41  
42      private final int port;
43  
44      private final Authentication auth;
45  
46      /**
47       * Creates a new proxy with the specified properties and no authentication.
48       *
49       * @param type The type of the proxy, e.g. "http", may be {@code null}.
50       * @param host The host of the proxy, may be {@code null}.
51       * @param port The port of the proxy.
52       */
53      public Proxy(String type, String host, int port) {
54          this(type, host, port, null);
55      }
56  
57      /**
58       * Creates a new proxy with the specified properties.
59       *
60       * @param type The type of the proxy, e.g. "http", may be {@code null}.
61       * @param host The host of the proxy, may be {@code null}.
62       * @param port The port of the proxy.
63       * @param auth The authentication to use for the proxy connection, may be {@code null}.
64       */
65      public Proxy(String type, String host, int port, Authentication auth) {
66          this.type = (type != null) ? type : "";
67          this.host = (host != null) ? host : "";
68          this.port = port;
69          this.auth = auth;
70      }
71  
72      /**
73       * Gets the type of this proxy.
74       *
75       * @return The type of this proxy, never {@code null}.
76       */
77      public String getType() {
78          return type;
79      }
80  
81      /**
82       * Gets the host for this proxy.
83       *
84       * @return The host for this proxy, never {@code null}.
85       */
86      public String getHost() {
87          return host;
88      }
89  
90      /**
91       * Gets the port number for this proxy.
92       *
93       * @return The port number for this proxy.
94       */
95      public int getPort() {
96          return port;
97      }
98  
99      /**
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 }