View Javadoc
1   package org.apache.maven.wagon.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 org.apache.maven.wagon.PathUtils;
23  import org.apache.maven.wagon.WagonConstants;
24  import org.codehaus.plexus.util.StringUtils;
25  
26  import java.io.Serializable;
27  import java.util.Properties;
28  
29  /**
30   * This class is an abstraction of the location from/to resources
31   * can be transfered.
32   *
33   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
34   *
35   * @todo [BP] some things are specific to certain wagons (eg key stuff in authInfo, permissions)
36   */
37  public class Repository
38      implements Serializable
39  {
40      private static final long serialVersionUID = 1312227676322136247L;
41  
42      private String id;
43  
44      private String name;
45  
46      private String host;
47  
48      private int port = WagonConstants.UNKNOWN_PORT;
49  
50      private String basedir;
51  
52      private String protocol;
53  
54      private String url;
55  
56      private RepositoryPermissions permissions;
57  
58      /**
59       * Properties influencing wagon behaviour
60       * which are very specific to particular wagon.
61       */
62      private Properties parameters = new Properties();
63  
64      // Username/password are sometimes encoded in the URL
65      private String username = null;
66  
67      private String password = null;
68  
69      /**
70       * @deprecated use {@link #Repository(String, String)}
71       */
72      public Repository()
73      {
74  
75      }
76  
77      public Repository( String id, String url )
78      {
79          if ( id == null )
80          {
81              throw new NullPointerException( "id cannot be null" );
82          }
83  
84          setId( id );
85  
86          if ( url == null )
87          {
88              throw new NullPointerException( "url cannot be null" );
89          }
90  
91          setUrl( url );
92      }
93  
94      public String getId()
95      {
96          return id;
97      }
98  
99      public void setId( String id )
100     {
101         this.id = id;
102     }
103 
104     /**
105      * Retrieve the base directory of the repository. This is derived from the full repository URL, and
106      * contains the entire path component.
107      *
108      * @return the base directory
109      */
110     public String getBasedir()
111     {
112         return basedir;
113     }
114 
115     public void setBasedir( String basedir )
116     {
117         this.basedir = basedir;
118     }
119 
120     public void setName( String name )
121     {
122         this.name = name;
123     }
124 
125     public int getPort()
126     {
127         return port;
128     }
129 
130     public void setPort( int port )
131     {
132         this.port = port;
133     }
134 
135     public void setUrl( String url )
136     {
137         this.url = url;
138 
139         // TODO [BP]: refactor out the PathUtils URL stuff into a class like java.net.URL, so you only parse once
140         //  can't use URL class as is because it won't recognise our protocols, though perhaps we could attempt to
141         //  register handlers for scp, etc?
142 
143         this.protocol = PathUtils.protocol( url );
144 
145         this.host = PathUtils.host( url );
146 
147         this.port = PathUtils.port( url );
148 
149         this.basedir = PathUtils.basedir( url );
150 
151         String username = PathUtils.user( url );
152         this.username = username;
153 
154         if ( username != null )
155         {
156             String password = PathUtils.password( url );
157 
158             if ( password != null )
159             {
160                 this.password = password;
161 
162                 username += ":" + password;
163             }
164 
165             username += "@";
166 
167             int index = url.indexOf( username );
168             this.url = url.substring( 0, index ) + url.substring( index + username.length() );
169         }
170     }
171 
172     public String getUrl()
173     {
174         if ( url != null )
175         {
176             return url;
177         }
178 
179         StringBuilder sb = new StringBuilder();
180 
181         sb.append( protocol );
182 
183         sb.append( "://" );
184 
185         if ( isIPv6Address() )
186         {
187             // If this is IPv6 then we have to surround it
188             // with brackets '[' and ']'
189             sb.append( "[" ).append( getHost() ).append( "]" );
190         }
191         else
192         {
193             sb.append( getHost() );
194         }
195 
196         if ( port != WagonConstants.UNKNOWN_PORT )
197         {
198             sb.append( ":" );
199 
200             sb.append( port );
201         }
202 
203         sb.append( basedir );
204 
205         return sb.toString();
206     }
207 
208     /**
209      * Checks whtther provided url contains IPv6 format in host portion
210      *
211      * @return true if provide host part is of IPv6 format
212      */
213     private boolean isIPv6Address()
214     {
215         return getHost().contains( ":" );
216     }
217 
218     public String getHost()
219     {
220         if ( host == null )
221         {
222             return "localhost";
223         }
224         return host;
225     }
226 
227     public String getName()
228     {
229         if ( name == null )
230         {
231             return getId();
232         }
233         return name;
234     }
235 
236     public String toString()
237     {
238         StringBuilder sb = new StringBuilder();
239 
240         sb.append( "Repository[" );
241 
242         if ( StringUtils.isNotEmpty( getName() ) )
243         {
244             sb.append( getName() ).append( "|" );
245         }
246 
247         sb.append( getUrl() );
248         sb.append( "]" );
249 
250         return sb.toString();
251     }
252 
253     public String getProtocol()
254     {
255         return protocol;
256     }
257 
258     public RepositoryPermissions getPermissions()
259     {
260         return permissions;
261     }
262 
263     public void setPermissions( RepositoryPermissions permissions )
264     {
265         this.permissions = permissions;
266     }
267 
268     public String getParameter( String key )
269     {
270         return parameters.getProperty( key );
271     }
272 
273     public void setParameters( Properties parameters )
274     {
275         this.parameters = parameters;
276     }
277 
278     public int hashCode()
279     {
280         final int prime = 31;
281         int result = 1;
282         result = prime * result + ( ( id == null ) ? 0 : id.hashCode() );
283         return result;
284     }
285 
286     public boolean equals( Object obj )
287     {
288         if ( this == obj )
289         {
290             return true;
291         }
292         if ( obj == null )
293         {
294             return false;
295         }
296         if ( getClass() != obj.getClass() )
297         {
298             return false;
299         }
300         final Repository other = (Repository) obj;
301         if ( id == null )
302         {
303             if ( other.id != null )
304             {
305                 return false;
306             }
307         }
308         else if ( !id.equals( other.id ) )
309         {
310             return false;
311         }
312         return true;
313     }
314 
315     public String getUsername()
316     {
317         return username;
318     }
319 
320     public String getPassword()
321     {
322         return password;
323     }
324 
325     public void setProtocol( String protocol )
326     {
327         this.protocol = protocol;
328     }
329 
330 }