View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.sftp;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.endpoint.InboundEndpoint;
11  import org.mule.transport.AbstractMessageRequester;
12  import org.mule.transport.sftp.notification.SftpNotifier;
13  
14  import java.io.InputStream;
15  
16  /**
17   * <code>SftpMessageRequester</code> polls files on request (e.g. from a
18   * quartz-inbound-endpoint) from an sftp service on request using jsch. This
19   * requester produces an InputStream payload, which can be materialized in a
20   * MessageDispatcher or Component.
21   */
22  public class SftpMessageRequester extends AbstractMessageRequester
23  {
24  
25      private SftpReceiverRequesterUtil sftpRRUtil = null;
26  
27      public SftpMessageRequester(InboundEndpoint endpoint)
28      {
29          super(endpoint);
30  
31          sftpRRUtil = new SftpReceiverRequesterUtil(endpoint);
32  
33      }
34  
35      @Override
36      protected MuleMessage doRequest(long timeout) throws Exception
37      {
38          String[] files = sftpRRUtil.getAvailableFiles(true);
39  
40          if (files.length == 0) return null;
41  
42          String path = files[0];
43          // TODO. ML FIX. Can't we figure out the current service (for logging/audit
44          // purpose)???
45          SftpNotifier notifier = new SftpNotifier((SftpConnector) connector, createNullMuleMessage(),
46              endpoint, endpoint.getName());
47  
48          InputStream inputStream = sftpRRUtil.retrieveFile(path, notifier);
49  
50          logger.debug("Routing file: " + path);
51  
52          MuleMessage message = createMuleMessage(inputStream);
53          message.setOutboundProperty(SftpConnector.PROPERTY_ORIGINAL_FILENAME, path);
54  
55          // Now we can update the notifier with the message
56          notifier.setMessage(message);
57          return message;
58      }
59  
60  }