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