Search This Blog

Breaking

Friday, 5 February 2021

How to connect winscp with apache commons library

 A true SDET is, who automates everything which can be automated comes in his way. :)  

Recently, I came across a scenario where the Team is using extensively WinSCP for the movement of files from local machine to remote or vice versa for testing purposes. I gone with each step and identified that these steps (in a process to automate everything) which we are doing for ages manually :) can be automated. s this involves, WinSCP, so my journey started with the google search, how we can connect WinSCP with java. I googled some posts and found, that we can use Apache common VFS for this purpose. There are other ways too, but I am a big fan of Apache utilities from the past.




So I added the following jar in my POC project:

<dependency>

    <groupId>org.apache.commons</groupId>

    <artifactId>commons-vfs2</artifactId>

    <version>2.4</version>

</dependency>

<dependency>

    <groupId>com.jcraft</groupId>

    <artifactId>jsch</artifactId>

    <version>0.1.55</version>

</dependency>

And started working towards it.

My use case was :

1. Navigate to a directory

2. Find the latest file with an extension of. Dat

3. Copy it to your local directory

----------------------------------------------------------

4. Edit this .dat file

5. Move this updated file to the remote machine again

6. Hit a job

7. After the job completes, do the validations in DB as per the new entries inserted.


Note: This article is till step 3 only.


        String hostName = "10.164.123.29";

        String username = "developers";

        String password = "E2LQA2@ws";

        String localFilePath = "C:\\FakePath\\TestFile.DAT";

        String remoteFilePath = "/ABS/CDE/AllFiles/ADD/ArchiveFiles/";

You need to fill in your hostname, username and password details along with localFilePath where you will copy that file, and remoteFilePath from where you will copy it.

   existAndDownload(hostName, username, password,localFilePath, remoteFilePath);



import java.io.File;

import java.net.URLEncoder;

import java.util.Arrays;

import java.util.Collections;

import java.util.Vector;

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelSftp;

import org.apache.commons.vfs2.FileObject;

import org.apache.commons.vfs2.FileSystemException;

import org.apache.commons.vfs2.FileSystemOptions;

import org.apache.commons.vfs2.Selectors;

import org.apache.commons.vfs2.impl.StandardFileSystemManager;

import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;

 // This method first check the file present in the directory and then copy it to your local machine

public static void existAndDownload(String hostName, String username, String password,String localFilePath, String remoteFilePath) {

        StandardFileSystemManager manager = new StandardFileSystemManager();

        FileObject remoteFile = null;

        String remoteFilePathForlatestFile = null;


        try {

            manager.init();

            // Create local file object. Change location if necessary for new downloadFilePath

            FileObject localFile = manager.resolveFile(localFilePath);

            // List all the files in that directory.Try to give the directory path

            FileObject remoteFileObject = manager.resolveFile(createConnectionString(hostName, username, URLEncoder.encode(password,"UTF-8"), remoteFilePath), createDefaultOptions());

            FileObject[] children = remoteFileObject.getChildren();

            Arrays.sort(children, Collections.reverseOrder());

            for ( int i = 0; i < children.length; i++ ){

                System.out.println( children[ i ].getName().getBaseName() );

                if(children[ i ].getName().getBaseName().startsWith("2021")){

                    System.out.println(children[ i ].getName().getBaseName());

                    remoteFilePathForlatestFile =  remoteFilePath  + children[ i ].getName().getBaseName() ;

                    break;

                }

            }

            remoteFile = manager.resolveFile(createConnectionString(hostName, username, URLEncoder.encode(password,"UTF-8"),remoteFilePathForlatestFile ), createDefaultOptions());

            // Copy local file to sftp server

            localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);

            System.out.println("File download success");

        } catch (Exception e) {

            throw new RuntimeException(e);

        } finally {

            manager.close();

        }

    }


This is the first part of that activity. Later, I need to edit this file and place in the same location.

I will share with you, once I will complete that activity.


Issues Encountered: If you are using a password with special characters in it, then you need to Unicode it before using it. Otherwise, you will get an error like this;

Could not find a file with URI "file path" because it is a relative path, and no base URI was provided.

Fix: URLEncoder.encode(password,"UTF-8")


Till then, Happy Learning.

No comments:

Post a Comment