001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.scm.provider.hg.repository; 020 021import java.io.File; 022import java.util.Objects; 023 024import org.apache.commons.lang3.StringUtils; 025import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost; 026 027/** 028 * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a> 029 * 030 */ 031public class HgScmProviderRepository extends ScmProviderRepositoryWithHost { 032 // Known and tested protocols 033 private static final String FILE = ""; 034 035 private static final String SFTP = "sftp://"; 036 037 private static final String FTP = "ftp://"; 038 039 private static final String AFTP = "aftp://"; 040 041 private static final String HTTP = "http://"; 042 043 private static final String HTTPS = "https://"; 044 045 private final String path; 046 047 private final String protocol; 048 049 private final String orgUrl; 050 051 public HgScmProviderRepository(String url) { 052 orgUrl = url; 053 protocol = getProtocol(url); 054 path = parseUrl(url); 055 } 056 057 public String getURI() { 058 return protocol + addAuthority() + addHost() + addPort() + addPath(); 059 } 060 061 /** 062 * @return A message if the repository as an invalid URI, null if the URI seems fine. 063 */ 064 public String validateURI() { 065 066 String msg = null; 067 068 if (needsAuthentication()) { 069 if (getUser() == null) { 070 msg = "Username is missing for protocol " + protocol; 071 } else if (getPassword() == null) { 072 msg = "Password is missing for protocol " + protocol; 073 } else if (getHost() == null) { 074 msg = "Host (eg. www.myhost.com) is missing for protocol " + protocol; 075 } 076 } else if (getPort() != 0 && getHost() == null) { 077 msg = "Got port information without any host for protocol " + protocol; 078 } 079 080 if (msg != null) { 081 msg = "Something could be wrong about the repository URL: " + orgUrl + "\nReason: " + msg 082 + "\nCheck http://maven.apache.org/scm for usage and hints."; 083 } 084 return msg; 085 } 086 087 private String getProtocol(String url) { 088 // Assume we have a file unless we find a URL based syntax 089 String prot = FILE; 090 if (url.startsWith(SFTP)) { 091 prot = SFTP; 092 } else if (url.startsWith(HTTP)) { 093 prot = HTTP; 094 } else if (url.startsWith(HTTPS)) { 095 prot = HTTPS; 096 } 097 098 return prot; 099 } 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}