View Javadoc

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