Coverage Report - org.apache.maven.wagon.providers.ssh.ganymed.ScpWagon
 
Classes in this File Line Coverage Branch Coverage Complexity
ScpWagon
0% 
0% 
9
 
 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.SCPClient;
 20  
 import org.apache.maven.wagon.CommandExecutionException;
 21  
 import org.apache.maven.wagon.PathUtils;
 22  
 import org.apache.maven.wagon.PermissionModeUtils;
 23  
 import org.apache.maven.wagon.ResourceDoesNotExistException;
 24  
 import org.apache.maven.wagon.TransferFailedException;
 25  
 import org.apache.maven.wagon.authorization.AuthorizationException;
 26  
 import org.apache.maven.wagon.events.TransferEvent;
 27  
 import org.apache.maven.wagon.repository.RepositoryPermissions;
 28  
 import org.apache.maven.wagon.resource.Resource;
 29  
 import org.codehaus.plexus.util.IOUtil;
 30  
 import org.codehaus.plexus.util.StringUtils;
 31  
 
 32  
 import java.io.File;
 33  
 import java.io.FileNotFoundException;
 34  
 import java.io.FileOutputStream;
 35  
 import java.io.IOException;
 36  
 import java.io.OutputStream;
 37  
 
 38  
 /**
 39  
  * A base class for deployers and fetchers using protocols from SSH2 family and
 40  
  * JSch library for underlying implementation
 41  
  * <p/>
 42  
  * This is responsible for authentification stage of the process.
 43  
  * <p/>
 44  
  * We will first try to use public keys for authentication and if that doesn't
 45  
  * work then we fall back to using the login and password
 46  
  *
 47  
  * @version $Id: ScpWagon.java 430944 2006-08-12 02:02:20Z carlos $
 48  
  * @todo [BP] add compression flag
 49  
  */
 50  0
 public class ScpWagon
 51  
     extends AbstractGanymedWagon
 52  
 {
 53  
     public void put( File source, String destination )
 54  
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
 55  
     {
 56  0
         String basedir = getRepository().getBasedir();
 57  
 
 58  0
         destination = StringUtils.replace( destination, "\\", "/" );
 59  0
         String dir = PathUtils.dirname( destination );
 60  0
         dir = StringUtils.replace( dir, "\\", "/" );
 61  
 
 62  0
         Resource resource = new Resource( destination );
 63  
 
 64  0
         firePutInitiated( resource, source );
 65  
 
 66  
         try
 67  
         {
 68  0
             String umaskCmd = null;
 69  0
             if ( getRepository().getPermissions() != null )
 70  
             {
 71  0
                 String dirPerms = getRepository().getPermissions().getDirectoryMode();
 72  
 
 73  0
                 if ( dirPerms != null )
 74  
                 {
 75  0
                     umaskCmd = "umask " + PermissionModeUtils.getUserMaskFor( dirPerms );
 76  
                 }
 77  
             }
 78  
 
 79  0
             String mkdirCmd = "mkdir -p " + getPath( basedir, dir );
 80  
 
 81  0
             if ( umaskCmd != null )
 82  
             {
 83  0
                 mkdirCmd = umaskCmd + "; " + mkdirCmd;
 84  
             }
 85  
 
 86  0
             executeCommand( mkdirCmd );
 87  
         }
 88  0
         catch ( CommandExecutionException e )
 89  
         {
 90  0
             throw new TransferFailedException( "Error performing commands for file transfer", e );
 91  0
         }
 92  
 
 93  0
         String path = getPath( basedir, destination );
 94  
 
 95  0
         RepositoryPermissions permissions = getRepository().getPermissions();
 96  
 
 97  0
         firePutStarted( resource, source );
 98  
 
 99  
         // TODO: should we just incorporate this code directly to be able to use the normal putTransfer and tracking?
 100  0
         SCPClient client = new SCPClient( connection );
 101  
         try
 102  
         {
 103  0
             int index = path.lastIndexOf( '/' );
 104  0
             client.put( source.getAbsolutePath(), path.substring( index + 1 ), path.substring( 0, index ),
 105  
                         getOctalMode( permissions ) );
 106  
         }
 107  0
         catch ( IOException e )
 108  
         {
 109  0
             throw new TransferFailedException( "Error transferring file. Reason: " + e.getMessage(), e );
 110  0
         }
 111  
 
 112  0
         postProcessListeners( resource, source, TransferEvent.REQUEST_PUT );
 113  
 
 114  0
         firePutCompleted( resource, source );
 115  
 
 116  
         try
 117  
         {
 118  0
             if ( permissions != null && permissions.getGroup() != null )
 119  
             {
 120  0
                 executeCommand( "chgrp -f " + permissions.getGroup() + " " + path );
 121  
             }
 122  
         }
 123  0
         catch ( CommandExecutionException e )
 124  
         {
 125  0
             throw new TransferFailedException( "Error performing commands for file transfer", e );
 126  0
         }
 127  0
     }
 128  
 
 129  
     public void get( String resourceName, File destination )
 130  
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
 131  
     {
 132  0
         Resource resource = new Resource( resourceName );
 133  
 
 134  0
         fireGetInitiated( resource, destination );
 135  
 
 136  0
         String basedir = getRepository().getBasedir();
 137  
 
 138  0
         String path = getPath( basedir, resourceName );
 139  
 
 140  0
         destination.getParentFile().mkdirs();
 141  
 
 142  
         OutputStream os;
 143  
         try
 144  
         {
 145  0
             os = new FileOutputStream( destination );
 146  
         }
 147  0
         catch ( FileNotFoundException e )
 148  
         {
 149  0
             throw new TransferFailedException( "Error writing output file. Reason: " + e.getMessage(), e );
 150  0
         }
 151  
 
 152  
         try
 153  
         {
 154  0
             fireGetStarted( resource, destination );
 155  
 
 156  
             // TODO: should we just incorporate this code directly to be able to use the normal putTransfer and tracking?
 157  0
             SCPClient client = new SCPClient( connection );
 158  0
             client.get( path, os );
 159  
 
 160  0
             postProcessListeners( resource, destination, TransferEvent.REQUEST_GET );
 161  
 
 162  0
             fireGetCompleted( resource, destination );
 163  
         }
 164  0
         catch ( IOException e )
 165  
         {
 166  0
             if ( e.getCause().getMessage().trim().endsWith( "No such file or directory" ) )
 167  
             {
 168  0
                 throw new ResourceDoesNotExistException( e.getMessage().trim() );
 169  
             }
 170  
             else
 171  
             {
 172  0
                 throw new TransferFailedException( "Error transferring file. Reason: " + e.getMessage(), e );
 173  
             }
 174  
         }
 175  
         finally
 176  
         {
 177  0
             IOUtil.close( os );
 178  0
         }
 179  0
     }
 180  
 }