2
votes

I am attempting to use an sftp outbound gateway to download a file from a remote sftp server. Here is the gateway configuration:

<int-sftp:outbound-gateway id="downloadGateway"                 
                command="get" 
                command-options="-P"
                expression="payload.remoteDirectory + payload.filename"
                session-factory="cachingSessionFactory"
                local-directory="downloads"
                auto-create-directory="true"
                use-temporary-file-name="true"
                mode="REPLACE"                  
                local-filename-generator-expression="#remoteFileName + '.' + @currentDate.getDateStr()"
                />

In cases when a local file with the same name already exists, I'd like to replace it with the newly downloaded file. This is why I set the mode to "REPLACE". Instead, I get an exception:

Caused by: org.springframework.messaging.MessagingException: Local file downloads\myFile.xml.2015-04-14 already exists
at org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway.get(AbstractRemoteFileOutboundGateway.java:700)
at org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway$2.doInSession(AbstractRemoteFileOutboundGateway.java:436)
at org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway$2.doInSession(AbstractRemoteFileOutboundGateway.java:432)
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:334)
at org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway.doGet(AbstractRemoteFileOutboundGateway.java:432)
at org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway.handleRequestMessage(AbstractRemoteFileOutboundGateway.java:394)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:99)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
at org.springframework.integration.handler.MessageHandlerChain$1.send(MessageHandlerChain.java:123)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:95)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:248)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:171)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:119)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:105)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
at org.springframework.integration.handler.MessageHandlerChain$1.send(MessageHandlerChain.java:123)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:95)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:248)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:171)
at org.springframework.integration.splitter.AbstractMessageSplitter.produceOutput(AbstractMessageSplitter.java:129)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:119)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:105)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
at org.springframework.integration.handler.MessageHandlerChain$1.send(MessageHandlerChain.java:123)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:95)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:248)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:171)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:119)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:105)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
at org.springframework.integration.handler.MessageHandlerChain.handleMessageInternal(MessageHandlerChain.java:104)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
... 7 more

Am I correct t assume that mode attribute is meant for remote files when the gateway is used with put commands? Is there any way for me to achieve the behavior I am looking for with sftp outbound gateway?

2

2 Answers

0
votes

Interesting; yours is the second request for this, this week.

Currently, there's no work-around aside from removing the file yourself (or fetching the files to a temporary directory and then using a file outbound adapter to move them - which does offer the replace mode).

I opened a JIRA Issue to add this feature the other day.

Actually, the presence of the mode attribute on the gateway is a bug, it's currently ignored.

0
votes

Its an old thread but in case you are looking for your valid values for mode, here they are(Ref: org.springframework.integration.file.support.FileExistsMode):

   /**
     * Append data to any pre-existing files; close after each append.
     */
    APPEND,

    /**
     * Append data to any pre-existing files; do not flush/close after
     * appending.
     * @since 4.3
     */
    APPEND_NO_FLUSH,

    /**
     * Raise an exception in case the file to be written already exists.
     */
    FAIL,

    /**
     * If the file already exists, do nothing.
     */
    IGNORE,

    /**
     * If the file already exists, replace it.
     */
    REPLACE;