View Javadoc

1   package org.sonatype.aether.repository;
2   
3   /*******************************************************************************
4    * Copyright (c) 2010-2011 Sonatype, Inc.
5    * All rights reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    *   http://www.eclipse.org/legal/epl-v10.html
9    *******************************************************************************/
10  
11  /**
12   * A proxy to use for connections to a repository. <em>Note:</em> Instances of this class are immutable and the exposed
13   * mutators return new objects rather than changing the current instance.
14   * 
15   * @author Benjamin Bentmann
16   */
17  public final class Proxy
18  {
19  
20      /**
21       * Type denoting a proxy for HTTP transfers.
22       */
23      public static final String TYPE_HTTP = "http";
24  
25      /**
26       * Type denoting a proxy for HTTPS transfers.
27       */
28      public static final String TYPE_HTTPS = "https";
29  
30      private final String type;
31  
32      private final String host;
33  
34      private final int port;
35  
36      private final Authentication auth;
37  
38      /**
39       * Creates a new proxy with the specified properties.
40       * 
41       * @param type The type of the proxy, e.g. "http", may be {@code null}.
42       * @param host The host of the proxy, may be {@code null}.
43       * @param port The port of the proxy.
44       * @param auth The authentication to use for the proxy connection, may be {@code null}.
45       */
46      public Proxy( String type, String host, int port, Authentication auth )
47      {
48          this.type = ( type != null ) ? type : "";
49          this.host = ( host != null ) ? host : "";
50          this.port = port;
51          this.auth = auth;
52      }
53  
54      /**
55       * Gets the type of this proxy.
56       * 
57       * @return The type of this proxy, never {@code null}.
58       */
59      public String getType()
60      {
61          return type;
62      }
63  
64      /**
65       * Sets the type of the proxy.
66       * 
67       * @param type The type of the proxy, e.g. "http", may be {@code null}.
68       * @return The new proxy, never {@code null}.
69       */
70      public Proxy setType( String type )
71      {
72          if ( this.type.equals( type ) || ( type == null && this.type.length() <= 0 ) )
73          {
74              return this;
75          }
76          return new Proxy( type, host, port, auth );
77      }
78  
79      /**
80       * Gets the host for this proxy.
81       * 
82       * @return The host for this proxy, never {@code null}.
83       */
84      public String getHost()
85      {
86          return host;
87      }
88  
89      /**
90       * Sets the host of the proxy.
91       * 
92       * @param host The host of the proxy, may be {@code null}.
93       * @return The new proxy, never {@code null}.
94       */
95      public Proxy setHost( String host )
96      {
97          if ( this.host.equals( host ) || ( host == null && this.host.length() <= 0 ) )
98          {
99              return this;
100         }
101         return new Proxy( type, host, port, auth );
102     }
103 
104     /**
105      * Gets the port number for this proxy.
106      * 
107      * @return The port number for this proxy.
108      */
109     public int getPort()
110     {
111         return port;
112     }
113 
114     /**
115      * Sets the port number for the proxy.
116      * 
117      * @param port The port number for the proxy.
118      * @return The new proxy, never {@code null}.
119      */
120     public Proxy setPort( int port )
121     {
122         if ( this.port == port )
123         {
124             return this;
125         }
126         return new Proxy( type, host, port, auth );
127     }
128 
129     /**
130      * Gets the authentication to use for the proxy connection.
131      * 
132      * @return The authentication to use or {@code null} if none.
133      */
134     public Authentication getAuthentication()
135     {
136         return auth;
137     }
138 
139     /**
140      * Sets the authentication to use for the proxy connection.
141      * 
142      * @param auth The authentication to use, may be {@code null}.
143      * @return The new proxy, never {@code null}.
144      */
145     public Proxy setAuthentication( Authentication auth )
146     {
147         if ( eq( this.auth, auth ) )
148         {
149             return this;
150         }
151         return new Proxy( type, host, port, auth );
152     }
153 
154     @Override
155     public String toString()
156     {
157         return getHost() + ':' + getPort();
158     }
159 
160     @Override
161     public boolean equals( Object obj )
162     {
163         if ( this == obj )
164         {
165             return true;
166         }
167         if ( obj == null || !getClass().equals( obj.getClass() ) )
168         {
169             return false;
170         }
171 
172         Proxy that = (Proxy) obj;
173 
174         return eq( type, that.type ) && eq( host, that.host ) && port == that.port && eq( auth, that.auth );
175     }
176 
177     private static <T> boolean eq( T s1, T s2 )
178     {
179         return s1 != null ? s1.equals( s2 ) : s2 == null;
180     }
181 
182     @Override
183     public int hashCode()
184     {
185         int hash = 17;
186         hash = hash * 31 + hash( host );
187         hash = hash * 31 + hash( type );
188         hash = hash * 31 + port;
189         hash = hash * 31 + hash( auth );
190         return hash;
191     }
192 
193     private static int hash( Object obj )
194     {
195         return obj != null ? obj.hashCode() : 0;
196     }
197 
198 }