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