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