1 package org.apache.maven.scm.provider.hg.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 HgScmProviderRepository
32 extends ScmProviderRepositoryWithHost
33 {
34
35 private static final String 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 final String path;
48
49 private final String protocol;
50
51 private final String orgUrl;
52
53 public HgScmProviderRepository( String url )
54 {
55 orgUrl = url;
56 protocol = getProtocol( url );
57 path = parseUrl( url );
58 }
59
60 public String getURI()
61 {
62 return protocol + addAuthority() + addHost() + addPort() + addPath();
63 }
64
65
66
67
68 public String validateURI()
69 {
70
71 String msg = null;
72
73 if ( needsAuthentication() )
74 {
75 if ( getUser() == null )
76 {
77 msg = "Username is missing for protocol " + protocol;
78 }
79 else if ( getPassword() == null )
80 {
81 msg = "Password is missing for protocol " + protocol;
82 }
83 else if ( getHost() == null )
84 {
85 msg = "Host (eg. www.myhost.com) is missing for protocol " + protocol;
86 }
87 }
88
89 else if ( getPort() != 0 && getHost() == null )
90 {
91 msg = "Got port information without any host for protocol " + protocol;
92 }
93
94 if ( msg != null )
95 {
96 msg =
97 "Something could be wrong about the repository URL: " + orgUrl + "\nReason: " + msg
98 + "\nCheck http://maven.apache.org/scm for usage and hints.";
99 }
100 return msg;
101 }
102
103 private String getProtocol( String url )
104 {
105
106 String prot = FILE;
107 if ( url.startsWith( SFTP ) )
108 {
109 prot = SFTP;
110 }
111 else if ( url.startsWith( HTTP ) )
112 {
113 prot = HTTP;
114 }
115 else if ( url.startsWith( HTTPS ) )
116 {
117 prot = HTTPS;
118 }
119
120 return prot;
121 }
122
123 private String parseUrl( String url )
124 {
125 if ( protocol == FILE )
126 {
127 return url;
128 }
129
130
131 url = url.substring( protocol.length() );
132
133 url = parseUsernameAndPassword( url );
134
135 url = parseHostAndPort( url );
136
137 url = parsePath( url );
138
139 return url;
140 }
141
142 private String parseHostAndPort( String url )
143 {
144 if ( protocol != FILE )
145 {
146 int indexSlash = url.indexOf( '/' );
147
148 String hostPort = url;
149 if ( indexSlash > 0 )
150 {
151 hostPort = url.substring( 0, indexSlash );
152 }
153
154 int indexColon = hostPort.indexOf( ':' );
155 if ( indexColon > 0 )
156 {
157 setHost( hostPort.substring( 0, indexColon ) );
158 url = StringUtils.replace( url, getHost(), "" );
159 setPort( Integer.parseInt( hostPort.substring( indexColon + 1 ) ) );
160 url = StringUtils.replace( url, ":" + getPort(), "" );
161 }
162 else
163 {
164 setHost( hostPort );
165 url = StringUtils.replace( url, getHost(), "" );
166 }
167 }
168
169 return url;
170 }
171
172 private String parseUsernameAndPassword( String url )
173 {
174 if ( canAuthenticate() )
175 {
176 String[] split = url.split( "@" );
177 if ( split.length == 2 )
178 {
179 url = split[1];
180 split = split[0].split( ":" );
181 if ( split.length == 2 )
182 {
183 setUser( split[0] );
184 setPassword( split[1] );
185 }
186 else
187 {
188 setUser( split[0] );
189 }
190 }
191 }
192 return url;
193 }
194
195 private String parsePath( String url )
196 {
197 if ( protocol == FILE )
198 {
199
200 url = StringUtils.replace( url, "/", File.separator );
201
202
203 File tmpFile = new File( url );
204 String url2 = url.substring( File.pathSeparator.length() );
205 File tmpFile2 = new File( url2 );
206 if ( !tmpFile.exists() && !tmpFile2.exists() )
207 {
208
209 }
210
211 url = tmpFile2.exists() ? url2 : url;
212 }
213
214 return url;
215 }
216
217 private String addUser()
218 {
219 return ( getUser() == null ) ? "" : getUser();
220 }
221
222 private String addPassword()
223 {
224 return ( getPassword() == null ) ? "" : ":" + getPassword();
225 }
226
227 private String addHost()
228 {
229 return ( getHost() == null ) ? "" : getHost();
230 }
231
232 private String addPort()
233 {
234 return ( getPort() == 0 ) ? "" : ":" + getPort();
235 }
236
237 private String addPath()
238 {
239 return path;
240 }
241
242 private boolean needsAuthentication()
243 {
244 return protocol == SFTP || protocol == FTP || protocol == HTTPS || protocol == AFTP;
245 }
246
247 private String addAuthority()
248 {
249 return ( ( canAuthenticate() && ( getUser() != null ) ) ? addUser() + addPassword() + "@" : "" );
250 }
251
252
253 private boolean canAuthenticate()
254 {
255 return needsAuthentication() || protocol == HTTP;
256 }
257
258 public String toString()
259 {
260 return "Hg Repository Interpreted from: " + orgUrl + ":\nProtocol: " + protocol + "\nHost: " + getHost()
261 + "\nPort: " + getPort() + "\nUsername: " + getUser() + "\nPassword: " + getPassword() + "\nPath: "
262 + path;
263 }
264 }