camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From deepak_a <angesh...@gmail.com>
Subject Re: Problem with sFTP PUT (solved + Potential code enhancement?)
Date Thu, 03 Jul 2014 14:42:49 GMT
Hi,

After going through the code - its now clear on why the PUT action failed.
As explained in my previous post - by sFTP server provider does not allow
command like CD to be run after logging in.

Following is my routes.xml

	    <from uri="file:src/data?move=.done"/>
	                            <setHeader headerName="CamelFileName">

                                <simple>DTS4.UP.C5T62.S60300</simple>
                        </setHeader>         
        
        <to
uri="sftp://TestUser@54.79.63.220/put?privateKeyFile=C:\Downloads\AWS\AP\TestUser&amp;binary=true&amp;disconnect=true&amp;stepwise=false&amp;maximumReconnectAttempts=0"/>


After looking into the code I noticed the following.

1. after SSH session is established
2. WriteFile method of GenericFileProducer is invoked. If the target
directory is specified (in my case it is /put). then method buildDirectory
of class SftpOperations is invoked

            if (directory != null) {
                if (!operations.buildDirectory(directory, absolute)) {
                    log.debug("Cannot build directory [{}] (could be because
of denied permissions)", directory);
                }
            }


3. <extract from method: buildDirectory>

        try {
            // maybe the full directory already exists
            try {
                channel.cd(directory);
                success = true;
            } catch (SftpException e) {
                // ignore, we could not change directory so try to create it
instead
            }

            if (!success) {
                LOG.debug("Trying to build remote directory: {}",
directory);

                try {
                    channel.mkdir(directory);
                    success = true;
                } catch (SftpException e) {
                    // we are here if the server side doesn't create
intermediate folders
                    // so create the folder one by one
                    success = buildDirectoryChunks(directory);
                }
            }
        } catch (IOException e) {
            throw new GenericFileOperationFailedException("Cannot build
directory: " + directory, e);
        } catch (SftpException e) {
            throw new GenericFileOperationFailedException("Cannot build
directory: " + directory, e);
        } finally {
            // change back to original directory
            if (originalDirectory != null) {
                changeCurrentDirectory(originalDirectory);
            }
        }


The strange (weird) thing I understood is - we check if the full directory
already exist by doing a channel.cd(directory)
If that fails - then we ignore (because we are going to create it!!!!)
Then we make an attempt to create directory (channel.mkdir(directory)).
In the finally block we again try to change back to the original directory.

In my case - all these 3 action will fail - because my sFTP server provider
does not allow any of these actions to be taken.


The only option I have is to build my own sFTP component - as explained in
http://camel.apache.org/writing-components.html


I now have 3 classes
LombardSftpComponent.java
LombardSftpEndpoint.java
LombardSftpOperations.java

and I set up my route end point as

<to
uri="LombardSftp://jean@172.20.30.187:22/dtcc?password=123&amp;binary=true&amp;delay=60000&amp;stepwise=false"/>


Note: I am still curious to see how other users tackled scenarios where sFTP
server did not provide access to change directory (unless I have missed
something vital)


regards




--
View this message in context: http://camel.465427.n5.nabble.com/Problem-with-sFTP-PUT-tp5752794p5753292.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Mime
View raw message