1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.sftp;
12
13 import org.mule.api.MuleMessage;
14 import org.mule.api.construct.FlowConstruct;
15 import org.mule.api.endpoint.InboundEndpoint;
16 import org.mule.api.lifecycle.CreateException;
17 import org.mule.transport.AbstractPollingMessageReceiver;
18 import org.mule.transport.sftp.notification.SftpNotifier;
19
20 import java.io.InputStream;
21 import java.util.Arrays;
22
23
24
25
26
27
28 public class SftpMessageReceiver extends AbstractPollingMessageReceiver
29 {
30
31 private SftpReceiverRequesterUtil sftpRRUtil = null;
32
33 public SftpMessageReceiver(SftpConnector connector,
34 FlowConstruct flow,
35 InboundEndpoint endpoint,
36 long frequency) throws CreateException
37 {
38 super(connector, flow, endpoint);
39
40 this.setFrequency(frequency);
41
42 sftpRRUtil = new SftpReceiverRequesterUtil(endpoint);
43 }
44
45 public SftpMessageReceiver(SftpConnector connector, FlowConstruct flow, InboundEndpoint endpoint)
46 throws CreateException
47 {
48 super(connector, flow, endpoint);
49 sftpRRUtil = new SftpReceiverRequesterUtil(endpoint);
50 }
51
52 public void poll() throws Exception
53 {
54 if (logger.isDebugEnabled())
55 {
56 logger.debug("Pooling. Called at endpoint " + endpoint.getEndpointURI());
57 }
58 try
59 {
60 String[] files = sftpRRUtil.getAvailableFiles(false);
61
62 if (files.length == 0)
63 {
64 if (logger.isDebugEnabled())
65 {
66 logger.debug("Pooling. No matching files found at endpoint " + endpoint.getEndpointURI());
67 }
68 }
69 else
70 {
71 if (logger.isDebugEnabled())
72 {
73 logger.debug("Pooling. " + files.length + " files found at " + endpoint.getEndpointURI()
74 + ":" + Arrays.toString(files));
75 }
76 for (String file : files)
77 {
78 if (getLifecycleState().isStopping())
79 {
80 break;
81 }
82 routeFile(file);
83 }
84 if (logger.isDebugEnabled())
85 {
86 logger.debug("Pooling. Routed all " + files.length + " files found at "
87 + endpoint.getEndpointURI());
88 }
89 }
90 }
91 catch (Exception e)
92 {
93 logger.error("Error in poll", e);
94 throw e;
95 }
96 }
97
98 @Override
99 protected boolean pollOnPrimaryInstanceOnly()
100 {
101 return true;
102 }
103
104 protected void routeFile(String path) throws Exception
105 {
106
107
108 SftpNotifier notifier = new SftpNotifier((SftpConnector) connector, createNullMuleMessage(),
109 endpoint, flowConstruct.getName());
110
111 InputStream inputStream = sftpRRUtil.retrieveFile(path, notifier);
112
113 if (logger.isDebugEnabled())
114 {
115 logger.debug("Routing file: " + path);
116 }
117
118 MuleMessage message = createMuleMessage(inputStream);
119
120 message.setOutboundProperty(SftpConnector.PROPERTY_FILENAME, path);
121 message.setOutboundProperty(SftpConnector.PROPERTY_ORIGINAL_FILENAME, path);
122
123
124 notifier.setMessage(message);
125 routeMessage(message);
126
127 if (logger.isDebugEnabled())
128 {
129 logger.debug("Routed file: " + path);
130 }
131 }
132
133
134
135
136 @Override
137 protected MuleMessage handleUnacceptedFilter(MuleMessage message) {
138 logger.debug("the filter said no, now trying to close the payload stream");
139 try {
140 final SftpInputStream payload = (SftpInputStream) message.getPayload();
141 payload.close();
142 }
143 catch (Exception e) {
144 logger.debug("unable to close payload stream", e);
145 }
146 return super.handleUnacceptedFilter(message);
147 }
148
149 public void doConnect() throws Exception
150 {
151
152 }
153
154 public void doDisconnect() throws Exception
155 {
156
157 }
158
159 protected void doDispose()
160 {
161
162 }
163 }