Coverage Report - org.apache.maven.wagon.providers.ssh.ganymed.AbstractGanymedWagon
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractGanymedWagon
0% 
0% 
7
 
 1  
 package org.apache.maven.wagon.providers.ssh.ganymed;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2006 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import ch.ethz.ssh2.Connection;
 20  
 import ch.ethz.ssh2.HTTPProxyData;
 21  
 import ch.ethz.ssh2.ProxyData;
 22  
 import ch.ethz.ssh2.Session;
 23  
 import ch.ethz.ssh2.StreamGobbler;
 24  
 import org.apache.maven.wagon.CommandExecutionException;
 25  
 import org.apache.maven.wagon.Streams;
 26  
 import org.apache.maven.wagon.authentication.AuthenticationException;
 27  
 import org.apache.maven.wagon.providers.ssh.AbstractSshWagon;
 28  
 import org.apache.maven.wagon.providers.ssh.CommandExecutorStreamProcessor;
 29  
 import org.codehaus.plexus.util.IOUtil;
 30  
 
 31  
 import java.io.BufferedReader;
 32  
 import java.io.File;
 33  
 import java.io.IOException;
 34  
 import java.io.InputStream;
 35  
 import java.io.InputStreamReader;
 36  
 
 37  0
 public abstract class AbstractGanymedWagon
 38  
     extends AbstractSshWagon
 39  
 {
 40  
     protected Connection connection;
 41  
 
 42  
     public void openConnection()
 43  
         throws AuthenticationException
 44  
     {
 45  0
         super.openConnection();
 46  
 
 47  0
         String host = getRepository().getHost();
 48  0
         int port = getPort();
 49  
 
 50  0
         File privateKey = getPrivateKey();
 51  
 
 52  
 /* TODO!
 53  
         if ( !interactive )
 54  
         {
 55  
             interactiveUserInfo = new NullInteractiveUserInfo();
 56  
             uIKeyboardInteractive = null;
 57  
         }
 58  
 */
 59  
 
 60  0
         connection = new Connection( host, port );
 61  
 
 62  0
         if ( proxyInfo != null && proxyInfo.getHost() != null )
 63  
         {
 64  0
             ProxyData proxy = new HTTPProxyData( proxyInfo.getHost(), proxyInfo.getPort(), proxyInfo.getUserName(),
 65  
                                                  proxyInfo.getPassword() );
 66  
 
 67  0
             connection.setProxyData( proxy );
 68  
         }
 69  
 
 70  
         /* TODO! need to create a custom ServerHostKeyVerifier, and then pass that to connect later on.
 71  
           Note: the verifier will also need to add to ~/.ssh/known_hosts if it happens to be updated
 72  
 
 73  
         if ( knownHostsProvider != null )
 74  
         {
 75  
             try
 76  
             {
 77  
                 String contents = knownHostsProvider.getContents();
 78  
                 if ( contents != null )
 79  
                 {
 80  
                     sch.setKnownHosts( new StringInputStream( contents ) );
 81  
                 }
 82  
             }
 83  
             catch ( JSchException e )
 84  
             {
 85  
                 fireSessionError( e );
 86  
                 // continue without known_hosts
 87  
             }
 88  
         }
 89  
         */
 90  
 
 91  
         try
 92  
         {
 93  
             // TODO: connection timeout?
 94  0
             connection.connect();
 95  
         }
 96  0
         catch ( IOException e )
 97  
         {
 98  0
             fireSessionError( e );
 99  0
             throw new AuthenticationException( "Cannot connect. Reason: " + e.getMessage(), e );
 100  0
         }
 101  
 
 102  
         try
 103  
         {
 104  
             boolean authenticated;
 105  
 
 106  0
             if ( privateKey != null && privateKey.exists() )
 107  
             {
 108  0
                 authenticated = connection.authenticateWithPublicKey( authenticationInfo.getUserName(), privateKey,
 109  
                                                                       authenticationInfo.getPassphrase() );
 110  
             }
 111  
             else
 112  
             {
 113  0
                 authenticated = connection.authenticateWithPassword( authenticationInfo.getUserName(),
 114  
                                                                      authenticationInfo.getPassword() );
 115  
             }
 116  
             // TODO! keyboard interactive
 117  
 
 118  0
             if ( !authenticated )
 119  
             {
 120  0
                 throw new AuthenticationException( "Authentication failed." );
 121  
             }
 122  
         }
 123  0
         catch ( IOException e )
 124  
         {
 125  0
             closeConnection();
 126  0
             fireSessionError( e );
 127  0
             throw new AuthenticationException( "Cannot authenticate. Reason: " + e.getMessage(), e );
 128  0
         }
 129  0
     }
 130  
 
 131  
     // TODO! factor out into a separate class?
 132  
     public Streams executeCommand( String command, boolean ignoreFailures )
 133  
         throws CommandExecutionException
 134  
     {
 135  0
         fireTransferDebug( "Executing command: " + command );
 136  
 
 137  
         Session session;
 138  
         try
 139  
         {
 140  0
             session = connection.openSession();
 141  
         }
 142  0
         catch ( IOException e )
 143  
         {
 144  0
             throw new CommandExecutionException( "Cannot open session. Reason: " + e.getMessage(), e );
 145  0
         }
 146  
 
 147  
         try
 148  
         {
 149  0
             session.execCommand( command );
 150  
         }
 151  0
         catch ( IOException e )
 152  
         {
 153  0
             throw new CommandExecutionException( "Cannot execute remote command: " + command, e );
 154  0
         }
 155  
 
 156  0
         InputStream stdout = new StreamGobbler( session.getStdout() );
 157  0
         InputStream stderr = new StreamGobbler( session.getStderr() );
 158  
 
 159  0
         BufferedReader stdoutReader = new BufferedReader( new InputStreamReader( stdout ) );
 160  0
         BufferedReader stderrReader = new BufferedReader( new InputStreamReader( stderr ) );
 161  
 
 162  
         try
 163  
         {
 164  0
             Streams streams = CommandExecutorStreamProcessor.processStreams( stderrReader, stdoutReader );
 165  
 
 166  0
             if ( streams.getErr().length() > 0 )
 167  
             {
 168  0
                 int exitCode = session.getExitStatus().intValue();
 169  0
                 throw new CommandExecutionException( "Exit code: " + exitCode + " - " + streams.getErr() );
 170  
             }
 171  
 
 172  0
             return streams;
 173  
         }
 174  0
         catch ( IOException e )
 175  
         {
 176  0
             throw new CommandExecutionException( "Cannot read streams after remote command: " + command, e );
 177  
         }
 178  
         finally
 179  
         {
 180  0
             IOUtil.close( stdoutReader );
 181  0
             IOUtil.close( stderrReader );
 182  
         }
 183  
     }
 184  
 
 185  
     public void closeConnection()
 186  
     {
 187  0
         if ( connection != null )
 188  
         {
 189  0
             connection.close();
 190  0
             connection = null;
 191  
         }
 192  0
     }
 193  
 }