View Javadoc

1   package org.apache.continuum.scm;
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.commons.lang.StringUtils;
23  import org.apache.maven.scm.provider.ScmUrlUtils;
24  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
25  
26  /**
27   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
28   */
29  public class ContinuumScmUtils
30  {
31      public static final String GIT_SCM_PROVIDERTYPE = "git";
32  
33      // CONTINUUM-2628
34      public static ContinuumScmConfiguration setSCMCredentialsforSSH( ContinuumScmConfiguration config, String scmUrl,
35                                                                       String scmUsername, String scmPassword )
36      {
37          String sshScmUsername = "";
38          String sshScmPassword = "";
39          String providerType = ScmUrlUtils.getProvider( scmUrl );
40  
41          String scmSpecificUrl = scmUrl.substring( providerType.length() + 5 );
42  
43          if ( providerType.contains( GIT_SCM_PROVIDERTYPE ) && scmSpecificUrl.startsWith(
44              GitScmProviderRepository.PROTOCOL_SSH ) )
45          {
46              scmSpecificUrl = scmSpecificUrl.substring( GitScmProviderRepository.PROTOCOL_SSH.length() + 3 );
47  
48              // extract user information
49              int indexAt = scmSpecificUrl.indexOf( "@" );
50              if ( indexAt >= 0 )
51              {
52                  String userInfo = scmSpecificUrl.substring( 0, indexAt );
53                  sshScmUsername = userInfo;
54                  int indexPwdSep = userInfo.indexOf( ":" );
55                  // password is specified in the url
56                  if ( indexPwdSep < 0 )
57                  {
58                      sshScmUsername = userInfo.substring( indexPwdSep + 1 );
59                  }
60                  else
61                  {
62                      sshScmUsername = userInfo.substring( 0, indexPwdSep );
63                      sshScmPassword = userInfo.substring( indexPwdSep + 1 );
64                  }
65              }
66          }
67  
68          if ( StringUtils.isBlank( sshScmUsername ) )
69          {
70              config.setUsername( scmUsername );
71              config.setPassword( scmPassword );
72          }
73          else
74          {
75              config.setUsername( sshScmUsername );
76              if ( !StringUtils.isBlank( sshScmPassword ) )
77              {
78                  config.setPassword( sshScmPassword );
79              }
80          }
81  
82          return config;
83      }
84  }