1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.provider.hg.repository;
20
21 import java.io.File;
22 import java.util.Objects;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
26
27
28
29
30 public class HgScmProviderRepository extends ScmProviderRepositoryWithHost {
31
32 private static final String FILE = "";
33
34 private static final String SFTP = "sftp://";
35
36 private static final String FTP = "ftp://";
37
38 private static final String AFTP = "aftp://";
39
40 private static final String HTTP = "http://";
41
42 private static final String HTTPS = "https://";
43
44 private final String path;
45
46 private final String protocol;
47
48 private final String orgUrl;
49
50 public HgScmProviderRepository(String url) {
51 orgUrl = url;
52 protocol = getProtocol(url);
53 path = parseUrl(url);
54 }
55
56 public String getURI() {
57 return protocol + addAuthority() + addHost() + addPort() + addPath();
58 }
59
60
61
62
63 public String validateURI() {
64
65 String msg = null;
66
67 if (needsAuthentication()) {
68 if (getUser() == null) {
69 msg = "Username is missing for protocol " + protocol;
70 } else if (getPassword() == null) {
71 msg = "Password is missing for protocol " + protocol;
72 } else if (getHost() == null) {
73 msg = "Host (eg. www.myhost.com) is missing for protocol " + protocol;
74 }
75 } else if (getPort() != 0 && getHost() == null) {
76 msg = "Got port information without any host for protocol " + protocol;
77 }
78
79 if (msg != null) {
80 msg = "Something could be wrong about the repository URL: " + orgUrl + "\nReason: " + msg
81 + "\nCheck http://maven.apache.org/scm for usage and hints.";
82 }
83 return msg;
84 }
85
86 private String getProtocol(String url) {
87
88 String prot = FILE;
89 if (url.startsWith(SFTP)) {
90 prot = SFTP;
91 } else if (url.startsWith(HTTP)) {
92 prot = HTTP;
93 } else if (url.startsWith(HTTPS)) {
94 prot = HTTPS;
95 }
96
97 return prot;
98 }
99
100 private String parseUrl(String url) {
101 if (Objects.equals(protocol, FILE)) {
102 return url;
103 }
104
105
106 url = url.substring(protocol.length());
107
108 url = parseUsernameAndPassword(url);
109
110 url = parseHostAndPort(url);
111
112 url = parsePath(url);
113
114 return url;
115 }
116
117 private String parseHostAndPort(String url) {
118 if (!Objects.equals(protocol, FILE)) {
119 int indexSlash = url.indexOf('/');
120
121 String hostPort = url;
122 if (indexSlash > 0) {
123 hostPort = url.substring(0, indexSlash);
124 url = url.substring(indexSlash);
125 }
126
127 int indexColon = hostPort.indexOf(':');
128 if (indexColon > 0) {
129 setHost(hostPort.substring(0, indexColon));
130 setPort(Integer.parseInt(hostPort.substring(indexColon + 1)));
131 } else {
132 setHost(hostPort);
133 }
134 }
135
136 return url;
137 }
138
139 private String parseUsernameAndPassword(String url) {
140 if (canAuthenticate()) {
141 String[] split = url.split("@");
142 if (split.length == 2) {
143 url = split[1];
144 split = split[0].split(":");
145 if (split.length == 2) {
146 setUser(split[0]);
147 setPassword(split[1]);
148 } else {
149 setUser(split[0]);
150 }
151 }
152 }
153 return url;
154 }
155
156 private String parsePath(String url) {
157 if (Objects.equals(protocol, FILE)) {
158
159 url = StringUtils.replace(url, "/", File.separator);
160
161
162 File tmpFile = new File(url);
163 String url2 = url.substring(File.pathSeparator.length());
164 File tmpFile2 = new File(url2);
165 if (!tmpFile.exists() && !tmpFile2.exists()) {
166
167 }
168
169 url = tmpFile2.exists() ? url2 : url;
170 }
171
172 return url;
173 }
174
175 private String addUser() {
176 return (getUser() == null) ? "" : getUser();
177 }
178
179 private String addPassword() {
180 return (getPassword() == null) ? "" : ":" + getPassword();
181 }
182
183 private String addHost() {
184 return (getHost() == null) ? "" : getHost();
185 }
186
187 private String addPort() {
188 return (getPort() == 0) ? "" : ":" + getPort();
189 }
190
191 private String addPath() {
192 return path;
193 }
194
195 private boolean needsAuthentication() {
196 return Objects.equals(protocol, SFTP)
197 || Objects.equals(protocol, FTP)
198 || Objects.equals(protocol, HTTPS)
199 || Objects.equals(protocol, AFTP);
200 }
201
202 private String addAuthority() {
203 return ((canAuthenticate() && (getUser() != null)) ? addUser() + addPassword() + "@" : "");
204 }
205
206 private boolean canAuthenticate() {
207 return needsAuthentication() || Objects.equals(protocol, HTTP);
208 }
209
210
211
212 public String toString() {
213 return "Hg Repository Interpreted from: " + orgUrl + ":\nProtocol: " + protocol + "\nHost: " + getHost()
214 + "\nPort: " + getPort() + "\nUsername: " + getUser() + "\nPassword: " + getPassword() + "\nPath: "
215 + path;
216 }
217 }