View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
29   *
30   */
31  public class HgScmProviderRepository extends ScmProviderRepositoryWithHost {
32      // Known and tested protocols
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       * @return A message if the repository as an invalid URI, null if the URI seems fine.
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          // Assume we have a file unless we find a URL based syntax
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         // Strip protocol
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; // is now only the path
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]; // Strip away 'username:password@' from url
145                 split = split[0].split(":");
146                 if (split.length == 2) { // both username and password
147                     setUser(split[0]);
148                     setPassword(split[1]);
149                 } else { // only username
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             // Use OS dependent path separator
160             url = StringUtils.replace(url, "/", File.separator);
161 
162             // Test first path separator (*nix systems use them to denote root)
163             File tmpFile = new File(url); // most likly a *nix system
164             String url2 = url.substring(File.pathSeparator.length());
165             File tmpFile2 = new File(url2); // most likly a windows system
166             if (!tmpFile.exists() && !tmpFile2.exists()) {
167                 // This is trouble - Trouble is reported in validateURI()
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     /** {@inheritDoc} */
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 }