1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.ftp; |
12 | |
|
13 | |
import org.mule.impl.MuleMessage; |
14 | |
import org.mule.providers.AbstractMessageDispatcher; |
15 | |
import org.mule.umo.UMOEvent; |
16 | |
import org.mule.umo.UMOMessage; |
17 | |
import org.mule.umo.endpoint.UMOEndpointURI; |
18 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
19 | |
import org.mule.umo.provider.UMOStreamMessageAdapter; |
20 | |
|
21 | |
import java.io.FilenameFilter; |
22 | |
import java.io.IOException; |
23 | |
import java.io.OutputStream; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.apache.commons.io.IOUtils; |
28 | |
import org.apache.commons.io.output.ByteArrayOutputStream; |
29 | |
import org.apache.commons.net.ftp.FTPClient; |
30 | |
import org.apache.commons.net.ftp.FTPFile; |
31 | |
import org.apache.commons.net.ftp.FTPReply; |
32 | |
|
33 | |
public class FtpMessageDispatcher extends AbstractMessageDispatcher |
34 | |
{ |
35 | |
protected final FtpConnector connector; |
36 | |
|
37 | |
public FtpMessageDispatcher(UMOImmutableEndpoint endpoint) |
38 | |
{ |
39 | 0 | super(endpoint); |
40 | 0 | this.connector = (FtpConnector) endpoint.getConnector(); |
41 | 0 | } |
42 | |
|
43 | |
protected void doDispose() |
44 | |
{ |
45 | |
|
46 | 0 | } |
47 | |
|
48 | |
protected void doDispatch(UMOEvent event) throws Exception |
49 | |
{ |
50 | 0 | Object data = event.getTransformedMessage(); |
51 | 0 | OutputStream out = connector.getOutputStream(event.getEndpoint(), event.getMessage()); |
52 | |
|
53 | |
try |
54 | |
{ |
55 | 0 | if (data instanceof UMOStreamMessageAdapter) |
56 | |
{ |
57 | 0 | IOUtils.copy(((UMOStreamMessageAdapter) data).getInputStream(), out); |
58 | 0 | ((UMOStreamMessageAdapter) data).getOutputStream().close(); |
59 | |
} |
60 | |
else |
61 | |
{ |
62 | |
byte[] dataBytes; |
63 | 0 | if (data instanceof byte[]) |
64 | |
{ |
65 | 0 | dataBytes = (byte[]) data; |
66 | |
} |
67 | |
else |
68 | |
{ |
69 | 0 | dataBytes = data.toString().getBytes(); |
70 | |
} |
71 | 0 | IOUtils.write(dataBytes, out); |
72 | |
} |
73 | |
} |
74 | |
finally |
75 | |
{ |
76 | 0 | out.close(); |
77 | 0 | } |
78 | 0 | } |
79 | |
|
80 | |
protected UMOMessage doSend(UMOEvent event) throws Exception |
81 | |
{ |
82 | 0 | doDispatch(event); |
83 | 0 | return event.getMessage(); |
84 | |
} |
85 | |
|
86 | |
protected void doConnect() throws Exception |
87 | |
{ |
88 | |
|
89 | |
|
90 | 0 | } |
91 | |
|
92 | |
protected void doDisconnect() throws Exception |
93 | |
{ |
94 | |
try |
95 | |
{ |
96 | 0 | UMOEndpointURI uri = endpoint.getEndpointURI(); |
97 | 0 | FTPClient client = connector.getFtp(uri); |
98 | 0 | connector.destroyFtp(uri, client); |
99 | |
} |
100 | 0 | catch (Exception e) |
101 | |
{ |
102 | |
|
103 | 0 | } |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
protected UMOMessage doReceive(long timeout) throws Exception |
118 | |
{ |
119 | 0 | FTPClient client = connector.getFtp(endpoint.getEndpointURI()); |
120 | |
try |
121 | |
{ |
122 | 0 | logger.debug("entering doReceive()"); |
123 | 0 | connector.enterActiveOrPassiveMode(client, endpoint); |
124 | 0 | connector.setupFileType(client, endpoint); |
125 | 0 | if (!client.changeWorkingDirectory(endpoint.getEndpointURI().getPath())) |
126 | |
{ |
127 | 0 | throw new IOException("Ftp error: " + client.getReplyCode()); |
128 | |
} |
129 | |
|
130 | 0 | FilenameFilter filenameFilter = null; |
131 | 0 | if (endpoint.getFilter() instanceof FilenameFilter) |
132 | |
{ |
133 | 0 | filenameFilter = (FilenameFilter) endpoint.getFilter(); |
134 | |
} |
135 | |
|
136 | 0 | FTPFile[] files = client.listFiles(); |
137 | 0 | if (!FTPReply.isPositiveCompletion(client.getReplyCode())) |
138 | |
{ |
139 | 0 | throw new IOException("Ftp error: " + client.getReplyCode()); |
140 | |
} |
141 | 0 | if (files == null || files.length == 0) |
142 | |
{ |
143 | 0 | return null; |
144 | |
} |
145 | 0 | List fileList = new ArrayList(); |
146 | 0 | for (int i = 0; i < files.length; i++) |
147 | |
{ |
148 | 0 | if (files[i].isFile()) |
149 | |
{ |
150 | 0 | if (filenameFilter == null || filenameFilter.accept(null, files[i].getName())) |
151 | |
{ |
152 | 0 | fileList.add(files[i]); |
153 | |
|
154 | 0 | break; |
155 | |
} |
156 | |
} |
157 | |
} |
158 | 0 | if (fileList.size() == 0) |
159 | |
{ |
160 | 0 | return null; |
161 | |
} |
162 | |
|
163 | 0 | FTPFile file = (FTPFile) fileList.get(0); |
164 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
165 | 0 | if (!client.retrieveFile(file.getName(), baos)) |
166 | |
{ |
167 | 0 | throw new IOException("Ftp error: " + client.getReplyCode()); |
168 | |
} |
169 | 0 | return new MuleMessage(connector.getMessageAdapter(baos.toByteArray())); |
170 | |
|
171 | |
} |
172 | |
finally |
173 | |
{ |
174 | 0 | logger.debug("leaving doReceive()"); |
175 | 0 | connector.releaseFtp(endpoint.getEndpointURI(), client); |
176 | |
} |
177 | |
} |
178 | |
|
179 | |
} |