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  /**
23   * A proxy to use for connections to a repository.
24   */
25  public final class Proxy
26  {
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      {
55          this( type, host, port, null );
56      }
57  
58      /**
59       * Creates a new proxy with the specified properties.
60       * 
61       * @param type The type of the proxy, e.g. "http", may be {@code null}.
62       * @param host The host of the proxy, may be {@code null}.
63       * @param port The port of the proxy.
64       * @param auth The authentication to use for the proxy connection, may be {@code null}.
65       */
66      public Proxy( String type, String host, int port, Authentication auth )
67      {
68          this.type = ( type != null ) ? type : "";
69          this.host = ( host != null ) ? host : "";
70          this.port = port;
71          this.auth = auth;
72      }
73  
74      /**
75       * Gets the type of this proxy.
76       * 
77       * @return The type of this proxy, never {@code null}.
78       */
79      public String getType()
80      {
81          return type;
82      }
83  
84      /**
85       * Gets the host for this proxy.
86       * 
87       * @return The host for this proxy, never {@code null}.
88       */
89      public String getHost()
90      {
91          return host;
92      }
93  
94      /**
95       * Gets the port number for this proxy.
96       * 
97       * @return The port number for this proxy.
98       */
99      public int getPort()
100     {
101         return port;
102     }
103 
104     /**
105      * Gets the authentication to use for the proxy connection.
106      * 
107      * @return The authentication to use or {@code null} if none.
108      */
109     public Authentication getAuthentication()
110     {
111         return auth;
112     }
113 
114     @Override
115     public String toString()
116     {
117         return getHost() + ':' + getPort();
118     }
119 
120     @Override
121     public boolean equals( Object obj )
122     {
123         if ( this == obj )
124         {
125             return true;
126         }
127         if ( obj == null || !getClass().equals( obj.getClass() ) )
128         {
129             return false;
130         }
131 
132         Proxy that = (Proxy) obj;
133 
134         return eq( type, that.type ) && eq( host, that.host ) && port == that.port && eq( auth, that.auth );
135     }
136 
137     private static <T> boolean eq( T s1, T s2 )
138     {
139         return s1 != null ? s1.equals( s2 ) : s2 == null;
140     }
141 
142     @Override
143     public int hashCode()
144     {
145         int hash = 17;
146         hash = hash * 31 + hash( host );
147         hash = hash * 31 + hash( type );
148         hash = hash * 31 + port;
149         hash = hash * 31 + hash( auth );
150         return hash;
151     }
152 
153     private static int hash( Object obj )
154     {
155         return obj != null ? obj.hashCode() : 0;
156     }
157 
158 }