/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/repository/HgScmProviderRepository.java

https://github.com/intelchen/maven-scm · Java · 266 lines · 189 code · 45 blank · 32 comment · 51 complexity · 4c2a81261d8b07c7b8b45eebc40333d8 MD5 · raw file

  1. package org.apache.maven.scm.provider.hg.repository;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
  21. import org.codehaus.plexus.util.StringUtils;
  22. import java.io.File;
  23. /**
  24. * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
  25. *
  26. */
  27. public class HgScmProviderRepository
  28. extends ScmProviderRepositoryWithHost
  29. {
  30. //Known and tested protocols
  31. private static final String FILE = "";
  32. private static final String SFTP = "sftp://";
  33. private static final String FTP = "ftp://";
  34. private static final String AFTP = "aftp://";
  35. private static final String HTTP = "http://";
  36. private static final String HTTPS = "https://";
  37. private final String path;
  38. private final String protocol;
  39. private final String orgUrl;
  40. public HgScmProviderRepository( String url )
  41. {
  42. orgUrl = url;
  43. protocol = getProtocol( url );
  44. path = parseUrl( url );
  45. }
  46. public String getURI()
  47. {
  48. return protocol + addAuthority() + addHost() + addPort() + addPath();
  49. }
  50. /**
  51. * @return A message if the repository as an invalid URI, null if the URI seems fine.
  52. */
  53. public String validateURI()
  54. {
  55. String msg = null;
  56. if ( needsAuthentication() )
  57. {
  58. if ( getUser() == null )
  59. {
  60. msg = "Username is missing for protocol " + protocol;
  61. }
  62. else if ( getPassword() == null )
  63. {
  64. msg = "Password is missing for protocol " + protocol;
  65. }
  66. else if ( getHost() == null )
  67. {
  68. msg = "Host (eg. www.myhost.com) is missing for protocol " + protocol;
  69. }
  70. }
  71. else if ( getPort() != 0 && getHost() == null )
  72. {
  73. msg = "Got port information without any host for protocol " + protocol;
  74. }
  75. if ( msg != null )
  76. {
  77. msg =
  78. "Something could be wrong about the repository URL: " + orgUrl + "\nReason: " + msg
  79. + "\nCheck http://maven.apache.org/scm for usage and hints.";
  80. }
  81. return msg;
  82. }
  83. private String getProtocol( String url )
  84. {
  85. // Assume we have a file unless we find a URL based syntax
  86. String prot = FILE;
  87. if ( url.startsWith( SFTP ) )
  88. {
  89. prot = SFTP;
  90. }
  91. else if ( url.startsWith( HTTP ) )
  92. {
  93. prot = HTTP;
  94. }
  95. else if ( url.startsWith( HTTPS ) )
  96. {
  97. prot = HTTPS;
  98. }
  99. return prot;
  100. }
  101. private String parseUrl( String url )
  102. {
  103. if ( protocol == FILE )
  104. {
  105. return url;
  106. }
  107. //Strip protocol
  108. url = url.substring( protocol.length() );
  109. url = parseUsernameAndPassword( url );
  110. url = parseHostAndPort( url );
  111. url = parsePath( url );
  112. return url; //is now only the path
  113. }
  114. private String parseHostAndPort( String url )
  115. {
  116. if ( protocol != FILE )
  117. {
  118. int indexSlash = url.indexOf( '/' );
  119. String hostPort = url;
  120. if ( indexSlash > 0 )
  121. {
  122. hostPort = url.substring( 0, indexSlash );
  123. }
  124. int indexColon = hostPort.indexOf( ':' );
  125. if ( indexColon > 0 )
  126. {
  127. setHost( hostPort.substring( 0, indexColon ) );
  128. url = StringUtils.replace( url, getHost(), "" );
  129. setPort( Integer.parseInt( hostPort.substring( indexColon + 1 ) ) );
  130. url = StringUtils.replace( url, ":" + getPort(), "" );
  131. }
  132. else
  133. {
  134. setHost( hostPort );
  135. url = StringUtils.replace( url, getHost(), "" );
  136. }
  137. }
  138. return url;
  139. }
  140. private String parseUsernameAndPassword( String url )
  141. {
  142. if ( canAuthenticate() )
  143. {
  144. String[] split = url.split( "@" );
  145. if ( split.length == 2 )
  146. {
  147. url = split[1]; //Strip away 'username:password@' from url
  148. split = split[0].split( ":" );
  149. if ( split.length == 2 )
  150. { //both username and password
  151. setUser( split[0] );
  152. setPassword( split[1] );
  153. }
  154. else
  155. { //only username
  156. setUser( split[0] );
  157. }
  158. }
  159. }
  160. return url;
  161. }
  162. private String parsePath( String url )
  163. {
  164. if ( protocol == FILE )
  165. {
  166. //Use OS dependent path separator
  167. url = StringUtils.replace( url, "/", File.separator );
  168. //Test first path separator (*nix systems use them to denote root)
  169. File tmpFile = new File( url ); //most likly a *nix system
  170. String url2 = url.substring( File.pathSeparator.length() );
  171. File tmpFile2 = new File( url2 ); //most likly a windows system
  172. if ( !tmpFile.exists() && !tmpFile2.exists() )
  173. {
  174. // This is trouble - Trouble is reported in validateURI()
  175. }
  176. url = tmpFile2.exists() ? url2 : url;
  177. }
  178. return url;
  179. }
  180. private String addUser()
  181. {
  182. return ( getUser() == null ) ? "" : getUser();
  183. }
  184. private String addPassword()
  185. {
  186. return ( getPassword() == null ) ? "" : ":" + getPassword();
  187. }
  188. private String addHost()
  189. {
  190. return ( getHost() == null ) ? "" : getHost();
  191. }
  192. private String addPort()
  193. {
  194. return ( getPort() == 0 ) ? "" : ":" + getPort();
  195. }
  196. private String addPath()
  197. {
  198. return path;
  199. }
  200. private boolean needsAuthentication()
  201. {
  202. return protocol == SFTP || protocol == FTP || protocol == HTTPS || protocol == AFTP;
  203. }
  204. private String addAuthority()
  205. {
  206. return ( (canAuthenticate() && (getUser() != null))
  207. ? addUser() + addPassword() + "@"
  208. : "" );
  209. }
  210. private boolean canAuthenticate()
  211. {
  212. return needsAuthentication() || protocol == HTTP;
  213. }
  214. /** {@inheritDoc} */
  215. public String toString()
  216. {
  217. return "Hg Repository Interpreted from: " + orgUrl + ":\nProtocol: " + protocol + "\nHost: " + getHost()
  218. + "\nPort: " + getPort() + "\nUsername: " + getUser() + "\nPassword: " + getPassword() + "\nPath: "
  219. + path;
  220. }
  221. }