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