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  public class HgScmProviderRepository extends ScmProviderRepositoryWithHost {
31      // Known and tested protocols
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       * @return a message if the repository as an invalid URI, null if the URI seems fine
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          // Assume we have a file unless we find a URL based syntax
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         // Strip protocol
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; // is now only the path
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]; // Strip away 'username:password@' from url
144                 split = split[0].split(":");
145                 if (split.length == 2) { // both username and password
146                     setUser(split[0]);
147                     setPassword(split[1]);
148                 } else { // only username
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             // Use OS dependent path separator
159             url = StringUtils.replace(url, "/", File.separator);
160 
161             // Test first path separator (*nix systems use them to denote root)
162             File tmpFile = new File(url); // most likly a *nix system
163             String url2 = url.substring(File.pathSeparator.length());
164             File tmpFile2 = new File(url2); // most likly a windows system
165             if (!tmpFile.exists() && !tmpFile2.exists()) {
166                 // This is trouble - Trouble is reported in validateURI()
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      * {@inheritDoc}
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 }