View Javadoc

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