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