Using SFTP Transport in Mule2

The SFTP Transport allows files to be read and written to and from directories over SFTP. It is not a standard transport that is distributed with Mule itself but can be added as an extra project. The project can be found here. However, this isn’t updated for Mule2.2 yet.
Here is how to make it work in Mule2.2:

  • See the installation guide of the project itself
    Make sure you have Maven installed and met the other prerequisites stated at the page.
  • Download the source code from here.

  • Make the code work with Mule 2.2.x
  • To do this open the following files in your favorite editor:

    • /resources/META-INF/mule-sftp.xslt
    • /resources/META-INF/spring.handlers
    • /resources/META-INF/spring.schemas

    In all three files replace ‘2.0’ in the namespaces with ‘2.2’

  • Modify one statement
  • in the class ‘SftpMessageDispatcher’ at line 65 I had to remove the second parameter ‘true’ to make it compile. Not sure what it did but it seems to work without it 😉

    String filename = (String) event.getProperty(SftpConnector.PROPERTY_FILENAME, true);
    

    becomes

    String filename = (String) event.getProperty(SftpConnector.PROPERTY_FILENAME);
    
  • Move the properties file
  • In the new version the properties file is expected in another place, so move the file ‘/resources/META-INF/services/org/mule/providers/sftp.properties’ to ‘/resources/META-INF/services/org/mule/transport/sftp.properties’.

That’s it! You can now build the project (without the supplied tests however) by running mvn deploy -DskipTest=true

To use this transport in your Mule project you need to follow the steps like I described here to add a transport to your Mule application.
Here is my example mule-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:spring="http://www.springframework.org/schema/beans"
       xmlns:test="http://www.mulesource.org/schema/mule/test/2.2"
       xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2"
       xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2"
       xmlns:xm="http://www.mulesource.org/schema/mule/xml/2.2"
       xmlns:mule-ss="http://www.mulesource.org/schema/mule/spring-security/2.2"
       xmlns:ss="http://www.springframework.org/schema/security"
       xmlns:sftp="http://www.mulesource.org/schema/mule/sftp/2.2"
       xsi:schemaLocation="
       http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
       http://www.mulesource.org/schema/mule/test/2.2 http://www.mulesource.org/schema/mule/test/2.2/mule-test.xsd
       http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd
       http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.mulesource.org/schema/mule/xml/2.2 http://www.mulesource.org/schema/mule/xml/2.2/mule-xml.xsd
       http://www.mulesource.org/schema/mule/http/2.2 http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd
       http://www.mulesource.org/schema/mule/spring-security/2.2 http://www.mulesource.org/schema/mule/spring-security/2.2/mule-spring-security.xsd
       http://www.mulesource.org/schema/mule/sftp/2.2 http://www.mulesource.org/schema/mule/sftp/2.2/mule-sftp.xsd
       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd">

    <sftp:connector name="sftpConnector" pollingFrequency="500"
          		autoDelete="false">
    </sftp:connector>
    <vm:connector name="internalVM" queueEvents="true"/>

    <model name="main">
        <service name="testComponent">
            <inbound>
                <sftp:inbound-endpoint connector-ref="sftpConnector" port="22"
                      host="mule-server" path="bin/" user="pascal" password="pascal"/>
            </inbound>
            <outbound>
                <pass-through-router>
                    <vm:outbound-endpoint path="test.download" connector-ref="internalVM" />
                </pass-through-router>
            </outbound>
        </service>
    </model>
</mule>

and here is the method in my Java test class:

 @Test
    public void testService() throws Exception {

        client = getMuleClient();

        MuleMessage m = client.request("vm://test.download", 10000);

        if (m != null) {
            String filename = (String) m.getProperty("originalFilename");

            System.out.println("============================================");
            System.out.println("filename = " + filename);

            Object o = m.getPayload();

            System.out.println("o  = " + o);
            System.out.println("===========================================");

        }
    }

So, although I didn’t get the unit test to work, the SFTP transport is usable in Mule if you need it to.

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in Mule and tagged . Bookmark the permalink.

2 Responses to Using SFTP Transport in Mule2

  1. shane says:

    Are you saying you were unable to get this to work?

  2. Pascal Alma says:

    Hi Shane,

    What I said was that I did got it working, except for the unit tests that are supplied in the transport. But by building the Maven component with -Dtest.skip=true it builds fine and works like a charm in my situation.

Comments are closed.