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 ;-)

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

    becomes

    1
    2
    
    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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 @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.

tags:

About Pascal Alma

Pascal started as an Oracle Developer in 1997 and developed numerous applications with Oracle Designer/Developer and PL/SQL. Since 2001 Pascal becomes more and more active with the development of software at the Java/J2EE platform. Nowadays Pascal is a senior JEE Developer/ Architect and has a lot of experience with several open source initiatives/ frameworks especially within the Enterprise Integration area. Besides these technical skills Pascal is a big Scrum enthusiastic.

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.