Announcement Announcement Module
Collapse
No announcement yet.
SFTP outbound - class path resource cannot be resolved Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SFTP outbound - class path resource cannot be resolved

    Hello,

    I am completely new to spring framework. I am trying to play with sample code related to SFTP outbound. But I have run into an issue. I have tried few things but nothing seems to work. I would really appreciate any help.

    I am loading all jars, properties files etc., in classpath using shell script. Here is my code and exception log:


    Code:
    public class SftpOutboundTransferSample {
    
    	public void sendFile(File file) throws Exception {
    		ClassPathXmlApplicationContext ac = 
    			new ClassPathXmlApplicationContext("/SftpOutboundTransferSample-context.xml", SftpOutboundTransferSample.class);
    		
    		ac.start();
    		
    		if (file.exists()){
    			Message<File> message = MessageBuilder.withPayload(file).build();
    			MessageChannel inputChannel = ac.getBean("inputChannel", MessageChannel.class);
    			inputChannel.send(message);
    			Thread.sleep(2000);
    		}
    		
    		String fileName = file.getName();
    		
    		if (new File("remote-target-dir/" + fileName).exists()){
    			System.out.println("Successfully transfered '" + fileName + "' to a remote location.");
    		}
    		ac.stop();
    	}
    }
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:int="http://www.springframework.org/schema/integration"
    	xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp.xsd">
    	
    	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      		<property name="locations" value="classpath:batch.properties"/>
    	</bean>
    	
    	<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
    		<property name="host" value="loclahost"/>
    		<property name="privateKey" value="classpath:sftp_rsa"/>
    		<property name="privateKeyPassphrase" value="${sshPrivateKey.Passphrase}"/>
    		<property name="port" value="22"/>
    		<property name="user" value="localuser"/>
    	</bean>
    	
    	<int:channel id="inputChannel"/>
    	
    	<int-sftp:outbound-channel-adapter id="sftpOutboundAdapter"
    				session-factory="sftpSessionFactory"
    				channel="inputChannel"
    				remote-filename-generator-expression="payload.getName()"
    				remote-directory="/export/home/remoteuser/reports"/>
    </beans>
    Code:
    [2012-07-17 14:10:25.025 EDT] FATAL main SendReport - EXCEPTION caught, stack trace follows: 
    org.springframework.integration.MessageHandlingException: error occurred in message handler [org.springframework.integration.file.remote.handler.FileTransferringMessageHandler#0]
    	at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:79)
    	at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:114)
    	at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:101)
    	at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:61)
    	at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
    	at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:128)
    	at com.cibc.ecm.filenetbatch.report.SftpOutboundTransferSample.sendFile(SftpOutboundTransferSample.java:21)
    	at com.cibc.ecm.filenetbatch.report.SendReport.main(SendReport.java:81)
    Caused by: 
    java.lang.IllegalStateException: failed to create SFTP Session
    	at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:158)
    	at org.springframework.integration.file.remote.session.CachingSessionFactory.doGetSession(CachingSessionFactory.java:98)
    	at org.springframework.integration.file.remote.session.CachingSessionFactory.getSession(CachingSessionFactory.java:77)
    	at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:128)
    	at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
    	... 7 more
    Caused by: 
    java.io.FileNotFoundException: class path resource [sftp_rsa] cannot be resolved to URL because it does not exist
    	at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:179)
    	at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:48)
    	at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.initJschSession(DefaultSftpSessionFactory.java:174)
    	at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:152)
    	... 11 more
    Thanking in advance

  • #2
    Do you have sftp_rsa file in the classpath?

    Comment


    • #3
      Originally posted by Andrei Tsibets View Post
      Do you have sftp_rsa file in the classpath?
      Yes, I do. I was wondering how can I check from java code to make sure that it is being loaded in class path?

      Comment


      • #4
        Did you get the answer ?

        I have similar code working fine with Userid/password but when i switched to privateKey, it fails with following error message.

        org.springframework.integration.MessageHandlingExc eption: error occurred in message handler [org.springframework.integration.file.remote.handle r.FileTransferringMessageHandler#0]

        I made sure the ssh public key is given to receiver, added into known_host.
        I also have the private key in the class path, and it loads properly, as i used same key to my java object for testing purpose and it prints the key in log, meaning the private key is reachable and read.

        Any clue ?

        <bean id="mySftpSessionFactory" class="org.springframework.integration.sftp.sessio n.DefaultSftpSessionFactory"> <property name="host" value="${sftp.hostname}"/>
        <property name="privateKey" value="classpath:META-INF/keys/id_rsa" />
        <property name="user" value="${sftp.username}"/>
        <property name="port" value="${sftp.port}"/>
        <!-- <property name="password" value="${sftp.password}"/> -->
        </bean>

        This all works fine if i switch to password version.

        Comment


        • #5
          The public key goes into authorized_keys, not known_hosts.

          Most *nix boxes have other security requirements for ssh, such as .ssh being chmod 700 and authorized_keys being chmod 600.

          Best to try to ssh (or sftp) from the command line to debug these issues.

          Comment


          • #6
            Gary,

            You caught it right, i actually did cat into authorized_keys, i was doing something else right before posting here, and i wrote known_host in the flow.

            But, I went little further mean time in debugging, and here is what i have now.

            I have breakpoint in the
            class
            org.springframework.integration.file.remote.handle r.FileTransferringMessageHandler
            and method
            handleMessageInternal

            It fails at following line
            Session<F> session = this.sessionFactory.getSession();

            The error message
            com.jcraft.jsch.JSchException: invalid privatekey: C:\workspaces\repo\mybatch\target\classes\META-INF\keys\id_rsa

            This is the ssh (private)key for my local windows system created with the help of puttyGen.

            Its not liking the key, and so not creating session.

            what am I missing ?

            Thanks a lot with your time and answer on this forum.

            Comment


            • #7
              PuTTY uses a different format for public/private keys to openSSH.

              PuTTYgen has an option to convert it - Conversions - export OpenSSH key

              Comment


              • #8
                I produced a new key and replaced on receiver server and now its working, that was on local windows machine.

                I ran everything from my LINUX machine now, creating fresh ssh and transporting and installing the public key on receiver server, but it is failing now, with same error while i created it again and again.

                I am not sure what i can provide here that will help getting answer.

                Comment


                • #9
                  And you can sftp ok from the command line?

                  Comment


                  • #10
                    Originally posted by Gary Russell View Post
                    And you can sftp ok from the command line?
                    Yes, it works fine with command line scp.

                    Comment

                    Working...
                    X