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