/maven-scm-providers/maven-scm-provider-bazaar/src/main/java/org/apache/maven/scm/provider/bazaar/repository/BazaarScmProviderRepository.java

https://github.com/intelchen/maven-scm · Java · 311 lines · 229 code · 49 blank · 33 comment · 55 complexity · 93648b384dad46d20bff02dd203c3e29 MD5 · raw file

  1. package org.apache.maven.scm.provider.bazaar.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:torbjorn@smorgrav.org">Torbj�rn Eikli Sm�rgrav</a>
  25. *
  26. */
  27. public class BazaarScmProviderRepository
  28. extends ScmProviderRepositoryWithHost
  29. {
  30. //Known and tested protocols
  31. private static final String FILE = "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 static final String BZR = "bzr://";
  38. private static final String BZR_SSH = "bzr+ssh://";
  39. /** this is basically an abbreviation of {@value #BZR_SSH} */
  40. private static final String SSH = "ssh://";
  41. private static final String UNKNOWN = "";
  42. private final String path;
  43. private final String protocol;
  44. private final String orgUrl;
  45. public BazaarScmProviderRepository( String url )
  46. {
  47. orgUrl = url;
  48. protocol = getProtocol( url );
  49. path = parseUrl( url );
  50. }
  51. public String getURI()
  52. {
  53. if ( FILE.equals( protocol ) )
  54. {
  55. return orgUrl;
  56. }
  57. else
  58. {
  59. return protocol + ( needsAuthentication() ? addUser() + addPassword() + addAt() : "" ) + addHost()
  60. + addPort() + addPath();
  61. }
  62. }
  63. /**
  64. * @return A message if the repository as an invalid URI, null if the URI seems fine.
  65. */
  66. public String validateURI()
  67. {
  68. String msg = null;
  69. if ( UNKNOWN.equals( protocol ) )
  70. {
  71. msg = "Unknown protocol (URL should start with something like 'sftp://' or 'file://'";
  72. }
  73. else if ( needsAuthentication() )
  74. {
  75. if ( getUser() == null )
  76. {
  77. msg = "Username is missing for protocol " + protocol;
  78. }
  79. else if ( getPassword() == null )
  80. {
  81. msg = "Password is missing for protocol " + protocol;
  82. }
  83. else if ( getHost() == null )
  84. {
  85. msg = "Host (eg. www.myhost.com) is missing for protocol " + protocol;
  86. }
  87. }
  88. else if ( getPort() != 0 && getHost() == null )
  89. {
  90. msg = "Got port information without any host for protocol " + protocol;
  91. }
  92. if ( msg != null )
  93. {
  94. msg = "Something could be wrong about the repository URL: " + orgUrl + "\nReason: " + msg
  95. + "\nCheck http://maven.apache.org/scm for usage and hints.";
  96. }
  97. return msg;
  98. }
  99. private String getProtocol( String url )
  100. {
  101. String prot = UNKNOWN;
  102. if ( url.startsWith( FILE ) )
  103. {
  104. prot = FILE;
  105. }
  106. else if ( url.startsWith( FTP ) )
  107. {
  108. prot = FTP;
  109. }
  110. else if ( url.startsWith( SFTP ) )
  111. {
  112. prot = SFTP;
  113. }
  114. else if ( url.startsWith( AFTP ) )
  115. {
  116. prot = AFTP;
  117. }
  118. else if ( url.startsWith( HTTP ) )
  119. {
  120. prot = HTTP;
  121. }
  122. else if ( url.startsWith( HTTPS ) )
  123. {
  124. prot = HTTPS;
  125. }
  126. else if ( url.startsWith( BZR ) )
  127. {
  128. prot = BZR;
  129. }
  130. else if ( url.startsWith( BZR_SSH ) )
  131. {
  132. prot = BZR_SSH;
  133. }
  134. else if ( url.startsWith( SSH ) )
  135. {
  136. prot = SSH;
  137. }
  138. return prot;
  139. }
  140. private String parseUrl( String url )
  141. {
  142. if ( UNKNOWN.equals( protocol ) )
  143. {
  144. return url;
  145. }
  146. //Strip protocol
  147. url = url.substring( protocol.length() );
  148. url = parseUsernameAndPassword( url );
  149. url = parseHostAndPort( url );
  150. url = parsePath( url );
  151. return url; //is now only the path
  152. }
  153. private String parseHostAndPort( String url )
  154. {
  155. if ( !FILE.equals( protocol ) )
  156. {
  157. int indexSlash = url.indexOf( '/' );
  158. String hostPort = url;
  159. if ( indexSlash > 0 )
  160. {
  161. hostPort = url.substring( 0, indexSlash );
  162. }
  163. int indexColon = hostPort.indexOf( ':' );
  164. if ( indexColon > 0 )
  165. {
  166. setHost( hostPort.substring( 0, indexColon ) );
  167. url = StringUtils.replace( url, getHost(), "" );
  168. setPort( Integer.parseInt( hostPort.substring( indexColon + 1 ) ) );
  169. url = StringUtils.replace( url, ":" + getPort(), "" );
  170. }
  171. else
  172. {
  173. setHost( hostPort );
  174. url = StringUtils.replace( url, getHost(), "" );
  175. }
  176. }
  177. return url;
  178. }
  179. private String parseUsernameAndPassword( String url )
  180. {
  181. if ( needsAuthentication() )
  182. {
  183. String[] split = url.split( "@" );
  184. if ( split.length == 2 )
  185. {
  186. url = split[1]; //Strip away 'username:password@' from url
  187. split = split[0].split( ":" );
  188. if ( split.length == 2 )
  189. { //both username and password
  190. setUser( split[0] );
  191. setPassword( split[1] );
  192. }
  193. else
  194. { //only username
  195. setUser( split[0] );
  196. }
  197. }
  198. }
  199. return url;
  200. }
  201. private String parsePath( String url )
  202. {
  203. if ( FILE.equals( protocol ) )
  204. {
  205. //Use OS dependent path separator
  206. url = StringUtils.replace( url, "/", File.separator );
  207. //Test first path separator (*nix systems use them to denote root)
  208. File tmpFile = new File( url ); //most likly a *nix system
  209. String url2 = url.substring( File.pathSeparator.length() );
  210. File tmpFile2 = new File( url2 ); //most likly a windows system
  211. if ( !tmpFile.exists() && !tmpFile2.exists() )
  212. {
  213. // This is trouble - Trouble is reported in validateURI()
  214. }
  215. url = tmpFile2.exists() ? url2 : url;
  216. //Use URL path separator
  217. url = StringUtils.replace( url, File.separator, "/" );
  218. }
  219. return url;
  220. }
  221. private String addUser()
  222. {
  223. return ( getUser() == null ) ? "" : getUser();
  224. }
  225. private String addPassword()
  226. {
  227. return ( getPassword() == null ) ? "" : ":" + getPassword();
  228. }
  229. private String addAt()
  230. {
  231. return needsAuthentication() ? "@" : "";
  232. }
  233. private String addHost()
  234. {
  235. return ( getHost() == null ) ? "" : getHost();
  236. }
  237. private String addPort()
  238. {
  239. return ( getPort() == 0 ) ? "" : ":" + getPort();
  240. }
  241. private String addPath()
  242. {
  243. return path;
  244. }
  245. private boolean needsAuthentication()
  246. {
  247. return SFTP.equals( protocol ) ||
  248. FTP.equals( protocol ) ||
  249. HTTPS.equals( protocol ) ||
  250. AFTP.equals( protocol ) ||
  251. BZR.equals( protocol ) ||
  252. BZR_SSH.equals( protocol ) ||
  253. SSH.equals( protocol );
  254. }
  255. /** {@inheritDoc} */
  256. public String toString()
  257. {
  258. return "Bazaar Repository Interpreted from: " + orgUrl + ":\nProtocol: " + protocol + "\nHost: "
  259. + getHost() + "\nPort: " + getPort() + "\nUsername: " + getUser() + "\nPassword: " + getPassword()
  260. + "\nPath: " + path;
  261. }
  262. }