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