This project has retired. For details please refer to its Attic page.
RepositoryURL xref
View Javadoc
1   package org.apache.archiva.model;
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  /**
23   * RepositoryURL - Mutable (and protocol forgiving) URL object.
24   *
25   *
26   */
27  public class RepositoryURL
28  {
29      private String url;
30  
31      private String protocol;
32  
33      private String host;
34  
35      private String port;
36  
37      private String username;
38  
39      private String password;
40  
41      private String path;
42  
43      public RepositoryURL( String url )
44      {
45          this.url = url;
46  
47          // .\ Parse the URL \.____________________________________________
48  
49          int pos;
50  
51          pos = url.indexOf( ":/" );
52          if ( pos == ( -1 ) )
53          {
54              throw new IllegalArgumentException( "Invalid URL, unable to parse protocol:// from " + url );
55          }
56  
57          protocol = url.substring( 0, pos );
58  
59          // Determine the post protocol position.
60          int postProtocolPos = protocol.length() + 1;
61          while ( url.charAt( postProtocolPos ) == '/' )
62          {
63              postProtocolPos++;
64          }
65          
66          // Handle special case with file protocol (which has no host, port, username, or password)
67          if ( "file".equals( protocol ) )
68          {
69              path = "/" + url.substring( postProtocolPos );
70  
71              return;
72          }
73  
74          // attempt to find the start of the 'path'
75          pos = url.indexOf( '/', postProtocolPos );
76  
77          // no path specified - ex "http://localhost"
78          if ( pos == ( -1 ) )
79          {
80              // set pos to end of string. (needed for 'middle section')
81              pos = url.length();
82              // default path
83              path = "/";
84          }
85          else
86          {
87              // get actual path.
88              path = url.substring( pos );
89          }
90  
91          // get the middle section ( username : password @ hostname : port )
92          String middle = url.substring( postProtocolPos, pos );
93  
94          pos = middle.indexOf( '@' );
95  
96          // we have an authentication section.
97          if ( pos > 0 )
98          {
99              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 }