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