View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2010, 2012 Sonatype, Inc.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *    Sonatype, Inc. - initial API and implementation
10   *******************************************************************************/
11  package org.eclipse.aether.repository;
12  
13  /**
14   * A proxy to use for connections to a repository.
15   */
16  public final class Proxy
17  {
18  
19      /**
20       * Type denoting a proxy for HTTP transfers.
21       */
22      public static final String TYPE_HTTP = "http";
23  
24      /**
25       * Type denoting a proxy for HTTPS transfers.
26       */
27      public static final String TYPE_HTTPS = "https";
28  
29      private final String type;
30  
31      private final String host;
32  
33      private final int port;
34  
35      private final Authentication auth;
36  
37      /**
38       * Creates a new proxy with the specified properties and no authentication.
39       * 
40       * @param type The type of the proxy, e.g. "http", may be {@code null}.
41       * @param host The host of the proxy, may be {@code null}.
42       * @param port The port of the proxy.
43       */
44      public Proxy( String type, String host, int port )
45      {
46          this( type, host, port, null );
47      }
48  
49      /**
50       * Creates a new proxy with the specified properties.
51       * 
52       * @param type The type of the proxy, e.g. "http", may be {@code null}.
53       * @param host The host of the proxy, may be {@code null}.
54       * @param port The port of the proxy.
55       * @param auth The authentication to use for the proxy connection, may be {@code null}.
56       */
57      public Proxy( String type, String host, int port, Authentication auth )
58      {
59          this.type = ( type != null ) ? type : "";
60          this.host = ( host != null ) ? host : "";
61          this.port = port;
62          this.auth = auth;
63      }
64  
65      /**
66       * Gets the type of this proxy.
67       * 
68       * @return The type of this proxy, never {@code null}.
69       */
70      public String getType()
71      {
72          return type;
73      }
74  
75      /**
76       * Gets the host for this proxy.
77       * 
78       * @return The host for this proxy, never {@code null}.
79       */
80      public String getHost()
81      {
82          return host;
83      }
84  
85      /**
86       * Gets the port number for this proxy.
87       * 
88       * @return The port number for this proxy.
89       */
90      public int getPort()
91      {
92          return port;
93      }
94  
95      /**
96       * Gets the authentication to use for the proxy connection.
97       * 
98       * @return The authentication to use or {@code null} if none.
99       */
100     public Authentication getAuthentication()
101     {
102         return auth;
103     }
104 
105     @Override
106     public String toString()
107     {
108         return getHost() + ':' + getPort();
109     }
110 
111     @Override
112     public boolean equals( Object obj )
113     {
114         if ( this == obj )
115         {
116             return true;
117         }
118         if ( obj == null || !getClass().equals( obj.getClass() ) )
119         {
120             return false;
121         }
122 
123         Proxy that = (Proxy) obj;
124 
125         return eq( type, that.type ) && eq( host, that.host ) && port == that.port && eq( auth, that.auth );
126     }
127 
128     private static <T> boolean eq( T s1, T s2 )
129     {
130         return s1 != null ? s1.equals( s2 ) : s2 == null;
131     }
132 
133     @Override
134     public int hashCode()
135     {
136         int hash = 17;
137         hash = hash * 31 + hash( host );
138         hash = hash * 31 + hash( type );
139         hash = hash * 31 + port;
140         hash = hash * 31 + hash( auth );
141         return hash;
142     }
143 
144     private static int hash( Object obj )
145     {
146         return obj != null ? obj.hashCode() : 0;
147     }
148 
149 }