View Javadoc

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