0
votes

Im trying to figure out how to use a sftp endpoint in a flow where the properties are dynamic depending on some incoming variable. So the flow is basically like this:

<flow name="dynamicSftpEndpoint">
    <sftp:inbound-endpoint name="inbound" connector-ref="SFTP"
                           host="#[sftpHost]" port="#[sftpPort]"
                           user="#[sftpUser]" password="#[sftpPassword]"
                           archiveDir="#[sftpInboundArchive]"
                           responseTimeout="${sftp.responseTimeout}"
                           path="#[sftpInboundPath]">
        <file:filename-regex-filter pattern="#[sftpInboundPattern]" caseSensitive="false"/>
    </sftp:inbound-endpoint>
    <set-variable variableName="filename" value="#[message.inboundProperties.originalFilename]"/>
    <log:info message="File '#[filename]' received from endpoint #[market]"/>
</flow>

But I get an exception like

Caused by: org.mule.api.endpoint.MalformedEndpointException: The endpoint "sftp://#[sftpUser]:#[sftpPassword]@#[sftpHost]:#[sftpPort]/#[sftpInboundPath]" is malformed and cannot be parsed.  If this is the name of a global endpoint, check the name is correct, that the endpoint exists, and that you are using the correct configuration (eg the "ref" attribute).  Note that names on inbound and outbound endpoints cannot be used to send or receive messages; use a named global endpoint instead.. Only Outbound endpoints can be dynamic

I noticed the last sentence there that only outbound endpoint can be dynamic. But does anyone have an idea on a workaround for dynamic inbound endpoints as well?

Thanks

3

3 Answers

0
votes

@Christian Karlsson , You can use Mule requester module if you want to get any resource with dyanamic endpoints. You can find more details at https://github.com/mulesoft/mule-module-requester.

Hope this helps.

0
votes

The Mule SFTP connector does not support dynamic endpoint.

You could however use a script to read a file over sftp mid flow. Something like:

<scripting:transformer>
            <scripting:script engine="Groovy">
                <scripting:text>
                    def endpointBuilder = muleContext.endpointFactory.getEndpointBuilder( 
                    "sftp://${sftp.username}@${sftp.host}:${sftp.port}/${sftp.path}?identityFile=${app.home}/${sftp.keyPath}&amp;passphrase=${sftp.passphrase}&amp;connector=sftp-csv") 
                    endpointBuilder.addMessageProcessor(new org.mule.routing.MessageFilter(new org.mule.transport.file.filters.FilenameWildcardFilter(sessionVars.expectedFilename))) 
                    def inboundEndpoint = endpointBuilder.buildInboundEndpoint() 
                    inboundEndpoint.request(30000L) 
                </scripting:text>
            </scripting:script>
        </scripting:transformer>
0
votes

Inbound endpoints can't be dynamic with in the flow, alternatively;

  • define different flows with all possible inbound endpoints and use choice filter to redirect to specific flow
  • use java to dynamically create endpoints using EndpointBuilder interface code sample:

// sftp server connector config String uri = "sftp://" + userDetails + "@" + serverDetails + "/tmp/sftpFolder"; EndpointBuilder endpointBuilder = eventContext.getMuleContext().getEndpointFactory().getEndpointBuilder(uri); InboundEndpoint inboundEndpoint = endpointBuilder.buildInboundEndpoint(); SftpConnector connector = (SftpConnector) inboundEndpoint.getConnector(); connector.connect(); // sftp client connection config SftpClient client = new SftpClient(host); client.setPort(port); client.login(user, password); //client.mkdir("dirFromSftp"); client.changeWorkingDirectory("dirFromSftp"); String[] listFiles = client.listFiles(); for(String file: listFiles) { System.out.println("File : " + file); }