001package org.apache.maven.wagon.repository;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.wagon.PathUtils;
023import org.apache.maven.wagon.WagonConstants;
024import org.codehaus.plexus.util.StringUtils;
025
026import java.io.Serializable;
027import java.util.Properties;
028
029/**
030 * This class is an abstraction of the location from/to resources
031 * can be transfered.
032 *
033 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
034 *
035 * @todo [BP] some things are specific to certain wagons (eg key stuff in authInfo, permissions)
036 */
037public class Repository
038    implements Serializable
039{
040    private static final long serialVersionUID = 1312227676322136247L;
041
042    private String id;
043
044    private String name;
045
046    private String host;
047
048    private int port = WagonConstants.UNKNOWN_PORT;
049
050    private String basedir;
051
052    private String protocol;
053
054    private String url;
055
056    private RepositoryPermissions permissions;
057
058    /**
059     * Properties influencing wagon behaviour
060     * which are very specific to particular wagon.
061     */
062    private Properties parameters = new Properties();
063
064    // Username/password are sometimes encoded in the URL
065    private String username = null;
066
067    private String password = null;
068
069    /**
070     * @deprecated use {@link #Repository(String, String)}
071     */
072    public Repository()
073    {
074
075    }
076
077    public Repository( String id, String url )
078    {
079        if ( id == null )
080        {
081            throw new NullPointerException( "id can not be null for Repository with url=" + url );
082        }
083
084        setId( id );
085
086        if ( url == null )
087        {
088            throw new NullPointerException( "url can not be null for Repository with id=" + id );
089        }
090
091        setUrl( url );
092    }
093
094    public String getId()
095    {
096        return id;
097    }
098
099    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        StringBuffer sb = new StringBuffer();
180
181        sb.append( protocol );
182
183        sb.append( "://" );
184
185        sb.append( host );
186
187        if ( port != WagonConstants.UNKNOWN_PORT )
188        {
189            sb.append( ":" );
190
191            sb.append( port );
192        }
193
194        sb.append( basedir );
195
196        return sb.toString();
197    }
198
199    public String getHost()
200    {
201        if ( host == null )
202        {
203            return "localhost";
204        }
205        return host;
206    }
207
208    public String getName()
209    {
210        if ( name == null )
211        {
212            return getId();
213        }
214        return name;
215    }
216
217    public String toString()
218    {
219        StringBuffer sb = new StringBuffer();
220
221        sb.append( "Repository[" );
222
223        if ( StringUtils.isNotEmpty( getName() ) )
224        {
225            sb.append( getName() ).append( "|" );
226        }
227
228        sb.append( getUrl() );
229        sb.append( "]" );
230
231        return sb.toString();
232    }
233
234    public String getProtocol()
235    {
236        return protocol;
237    }
238
239    public RepositoryPermissions getPermissions()
240    {
241        return permissions;
242    }
243
244    public void setPermissions( RepositoryPermissions permissions )
245    {
246        this.permissions = permissions;
247    }
248
249    public String getParameter( String key )
250    {
251        return parameters.getProperty( key );
252    }
253
254    public void setParameters( Properties parameters )
255    {
256        this.parameters = parameters;
257    }
258
259    public int hashCode()
260    {
261        final int prime = 31;
262        int result = 1;
263        result = prime * result + ( ( id == null ) ? 0 : id.hashCode() );
264        return result;
265    }
266
267    public boolean equals( Object obj )
268    {
269        if ( this == obj )
270        {
271            return true;
272        }
273        if ( obj == null )
274        {
275            return false;
276        }
277        if ( getClass() != obj.getClass() )
278        {
279            return false;
280        }
281        final Repository other = (Repository) obj;
282        if ( id == null )
283        {
284            if ( other.id != null )
285            {
286                return false;
287            }
288        }
289        else if ( !id.equals( other.id ) )
290        {
291            return false;
292        }
293        return true;
294    }
295
296    public String getUsername()
297    {
298        return username;
299    }
300
301    public String getPassword()
302    {
303        return password;
304    }
305
306    public void setProtocol( String protocol )
307    {
308        this.protocol = protocol;
309    }
310
311}