1 package org.apache.maven.scm.provider.bazaar.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
23 import org.codehaus.plexus.util.StringUtils;
24
25 import java.io.File;
26
27
28
29
30
31 public class BazaarScmProviderRepository
32 extends ScmProviderRepositoryWithHost
33 {
34
35 private static final String FILE = "file://";
36
37 private static final String SFTP = "sftp://";
38
39 private static final String FTP = "ftp://";
40
41 private static final String AFTP = "aftp://";
42
43 private static final String HTTP = "http://";
44
45 private static final String HTTPS = "https://";
46
47 private static final String BZR = "bzr://";
48
49 private static final String BZR_SSH = "bzr+ssh://";
50
51
52 private static final String SSH = "ssh://";
53
54 private static final String UNKNOWN = "";
55
56 private final String path;
57
58 private final String protocol;
59
60 private final String orgUrl;
61
62 public BazaarScmProviderRepository( String url )
63 {
64 orgUrl = url;
65 protocol = getProtocol( url );
66 path = parseUrl( url );
67 }
68
69 public String getURI()
70 {
71 if ( FILE.equals( protocol ) )
72 {
73 return orgUrl;
74 }
75 else
76 {
77 return protocol + ( needsAuthentication() ? addUser() + addPassword() + addAt() : "" ) + addHost()
78 + addPort() + addPath();
79 }
80 }
81
82
83
84
85 public String validateURI()
86 {
87
88 String msg = null;
89
90 if ( UNKNOWN.equals( protocol ) )
91 {
92 msg = "Unknown protocol (URL should start with something like 'sftp://' or 'file://'";
93 }
94 else if ( needsAuthentication() )
95 {
96 if ( getUser() == null )
97 {
98 msg = "Username is missing for protocol " + protocol;
99 }
100 else if ( getPassword() == null )
101 {
102 msg = "Password is missing for protocol " + protocol;
103 }
104 else if ( getHost() == null )
105 {
106 msg = "Host (eg. www.myhost.com) is missing for protocol " + protocol;
107 }
108 }
109
110 else if ( getPort() != 0 && getHost() == null )
111 {
112 msg = "Got port information without any host for protocol " + protocol;
113 }
114
115 if ( msg != null )
116 {
117 msg = "Something could be wrong about the repository URL: " + orgUrl + "\nReason: " + msg
118 + "\nCheck http://maven.apache.org/scm for usage and hints.";
119 }
120 return msg;
121 }
122
123 private String getProtocol( String url )
124 {
125 String prot = UNKNOWN;
126 if ( url.startsWith( FILE ) )
127 {
128 prot = FILE;
129 }
130 else if ( url.startsWith( FTP ) )
131 {
132 prot = FTP;
133 }
134 else if ( url.startsWith( SFTP ) )
135 {
136 prot = SFTP;
137 }
138 else if ( url.startsWith( AFTP ) )
139 {
140 prot = AFTP;
141 }
142 else if ( url.startsWith( HTTP ) )
143 {
144 prot = HTTP;
145 }
146 else if ( url.startsWith( HTTPS ) )
147 {
148 prot = HTTPS;
149 }
150 else if ( url.startsWith( BZR ) )
151 {
152 prot = BZR;
153 }
154 else if ( url.startsWith( BZR_SSH ) )
155 {
156 prot = BZR_SSH;
157 }
158 else if ( url.startsWith( SSH ) )
159 {
160 prot = SSH;
161 }
162
163 return prot;
164 }
165
166 private String parseUrl( String url )
167 {
168 if ( UNKNOWN.equals( protocol ) )
169 {
170 return url;
171 }
172
173
174 url = url.substring( protocol.length() );
175
176 url = parseUsernameAndPassword( url );
177
178 url = parseHostAndPort( url );
179
180 url = parsePath( url );
181
182 return url;
183 }
184
185 private String parseHostAndPort( String url )
186 {
187 if ( !FILE.equals( protocol ) )
188 {
189 int indexSlash = url.indexOf( '/' );
190
191 String hostPort = url;
192 if ( indexSlash > 0 )
193 {
194 hostPort = url.substring( 0, indexSlash );
195 }
196
197 int indexColon = hostPort.indexOf( ':' );
198 if ( indexColon > 0 )
199 {
200 setHost( hostPort.substring( 0, indexColon ) );
201 url = StringUtils.replace( url, getHost(), "" );
202 setPort( Integer.parseInt( hostPort.substring( indexColon + 1 ) ) );
203 url = StringUtils.replace( url, ":" + getPort(), "" );
204 }
205 else
206 {
207 setHost( hostPort );
208 url = StringUtils.replace( url, getHost(), "" );
209 }
210 }
211
212 return url;
213 }
214
215 private String parseUsernameAndPassword( String url )
216 {
217 if ( needsAuthentication() )
218 {
219 String[] split = url.split( "@" );
220 if ( split.length == 2 )
221 {
222 url = split[1];
223 split = split[0].split( ":" );
224 if ( split.length == 2 )
225 {
226 setUser( split[0] );
227 setPassword( split[1] );
228 }
229 else
230 {
231 setUser( split[0] );
232 }
233 }
234 }
235 return url;
236 }
237
238 private String parsePath( String url )
239 {
240 if ( FILE.equals( protocol ) )
241 {
242
243 url = StringUtils.replace( url, "/", File.separator );
244
245
246 File tmpFile = new File( url );
247 String url2 = url.substring( File.pathSeparator.length() );
248 File tmpFile2 = new File( url2 );
249 if ( !tmpFile.exists() && !tmpFile2.exists() )
250 {
251
252 }
253
254 url = tmpFile2.exists() ? url2 : url;
255
256
257 url = StringUtils.replace( url, File.separator, "/" );
258 }
259
260 return url;
261 }
262
263 private String addUser()
264 {
265 return ( getUser() == null ) ? "" : getUser();
266 }
267
268 private String addPassword()
269 {
270 return ( getPassword() == null ) ? "" : ":" + getPassword();
271 }
272
273 private String addAt()
274 {
275 return needsAuthentication() ? "@" : "";
276 }
277
278 private String addHost()
279 {
280 return ( getHost() == null ) ? "" : getHost();
281 }
282
283 private String addPort()
284 {
285 return ( getPort() == 0 ) ? "" : ":" + getPort();
286 }
287
288 private String addPath()
289 {
290 return path;
291 }
292
293 private boolean needsAuthentication()
294 {
295 return SFTP.equals( protocol ) || FTP.equals( protocol ) || HTTPS.equals( protocol ) || AFTP.equals( protocol )
296 || BZR.equals( protocol ) || BZR_SSH.equals( protocol ) || SSH.equals( protocol );
297 }
298
299
300 public String toString()
301 {
302 return "Bazaar Repository Interpreted from: " + orgUrl + ":\nProtocol: " + protocol + "\nHost: "
303 + getHost() + "\nPort: " + getPort() + "\nUsername: " + getUser() + "\nPassword: " + getPassword()
304 + "\nPath: " + path;
305 }
306 }