This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.model;
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
022/**
023 * RepositoryURL - Mutable (and protocol forgiving) URL object.
024 *
025 *
026 */
027public class RepositoryURL
028{
029    private String url;
030
031    private String protocol;
032
033    private String host;
034
035    private String port;
036
037    private String username;
038
039    private String password;
040
041    private String path;
042
043    public RepositoryURL( String url )
044    {
045        this.url = url;
046
047        // .\ Parse the URL \.____________________________________________
048
049        int pos;
050
051        pos = url.indexOf( ":/" );
052        if ( pos == ( -1 ) )
053        {
054            throw new IllegalArgumentException( "Invalid URL, unable to parse protocol:// from " + url );
055        }
056
057        protocol = url.substring( 0, pos );
058
059        // Determine the post protocol position.
060        int postProtocolPos = protocol.length() + 1;
061        while ( url.charAt( postProtocolPos ) == '/' )
062        {
063            postProtocolPos++;
064        }
065        
066        // Handle special case with file protocol (which has no host, port, username, or password)
067        if ( "file".equals( protocol ) )
068        {
069            path = "/" + url.substring( postProtocolPos );
070
071            return;
072        }
073
074        // attempt to find the start of the 'path'
075        pos = url.indexOf( '/', postProtocolPos );
076
077        // no path specified - ex "http://localhost"
078        if ( pos == ( -1 ) )
079        {
080            // set pos to end of string. (needed for 'middle section')
081            pos = url.length();
082            // default path
083            path = "/";
084        }
085        else
086        {
087            // get actual path.
088            path = url.substring( pos );
089        }
090
091        // get the middle section ( username : password @ hostname : port )
092        String middle = url.substring( postProtocolPos, pos );
093
094        pos = middle.indexOf( '@' );
095
096        // we have an authentication section.
097        if ( pos > 0 )
098        {
099            String authentication = middle.substring( 0, pos );
100            middle = middle.substring( pos + 1 ); // lop off authentication for host:port search
101
102            pos = authentication.indexOf( ':' );
103
104            // we have a password.
105            if ( pos > 0 )
106            {
107                username = authentication.substring( 0, pos );
108                password = authentication.substring( pos + 1 );
109            }
110            else
111            {
112                username = authentication;
113            }
114        }
115
116        pos = middle.indexOf( ':' );
117
118        // we have a defined port
119        if ( pos > 0 )
120        {
121            host = middle.substring( 0, pos );
122            port = middle.substring( pos + 1 );
123        }
124        else
125        {
126            host = middle;
127        }
128    }
129
130    @Override
131    public String toString()
132    {
133        return url;
134    }
135
136    public String getUsername()
137    {
138        return username;
139    }
140
141    public String getPassword()
142    {
143        return password;
144    }
145
146    public String getHost()
147    {
148        return host;
149    }
150
151    public String getPath()
152    {
153        return path;
154    }
155
156    public String getPort()
157    {
158        return port;
159    }
160
161    public String getProtocol()
162    {
163        return protocol;
164    }
165
166    public String getUrl()
167    {
168        return url;
169    }
170}