1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.ftp; |
12 | |
|
13 | |
import org.mule.api.MuleEvent; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.endpoint.EndpointURI; |
16 | |
import org.mule.api.endpoint.OutboundEndpoint; |
17 | |
import org.mule.transport.AbstractMessageDispatcher; |
18 | |
|
19 | |
import java.io.InputStream; |
20 | |
import java.io.OutputStream; |
21 | |
|
22 | |
import org.apache.commons.io.IOUtils; |
23 | |
import org.apache.commons.net.ftp.FTPClient; |
24 | |
|
25 | |
public class FtpMessageDispatcher extends AbstractMessageDispatcher |
26 | |
{ |
27 | |
protected final FtpConnector connector; |
28 | |
|
29 | |
public FtpMessageDispatcher(OutboundEndpoint endpoint) |
30 | |
{ |
31 | 6 | super(endpoint); |
32 | 6 | this.connector = (FtpConnector) endpoint.getConnector(); |
33 | 6 | } |
34 | |
|
35 | |
protected void doDispose() |
36 | |
{ |
37 | |
|
38 | 6 | } |
39 | |
|
40 | |
protected void doDispatch(MuleEvent event) throws Exception |
41 | |
{ |
42 | 8 | Object data = event.transformMessage(); |
43 | 8 | OutputStream out = connector.getOutputStream(event.getEndpoint(), event.getMessage()); |
44 | |
|
45 | |
try |
46 | |
{ |
47 | 8 | if (data instanceof InputStream) |
48 | |
{ |
49 | 0 | InputStream is = ((InputStream) data); |
50 | 0 | IOUtils.copy(is, out); |
51 | 0 | is.close(); |
52 | 0 | } |
53 | |
else |
54 | |
{ |
55 | |
byte[] dataBytes; |
56 | 8 | if (data instanceof byte[]) |
57 | |
{ |
58 | 6 | dataBytes = (byte[]) data; |
59 | |
} |
60 | |
else |
61 | |
{ |
62 | 2 | dataBytes = data.toString().getBytes(event.getEncoding()); |
63 | |
} |
64 | 8 | IOUtils.write(dataBytes, out); |
65 | |
} |
66 | |
} |
67 | |
finally |
68 | |
{ |
69 | 8 | out.close(); |
70 | 8 | } |
71 | 8 | } |
72 | |
|
73 | |
protected MuleMessage doSend(MuleEvent event) throws Exception |
74 | |
{ |
75 | 0 | doDispatch(event); |
76 | 0 | return event.getMessage(); |
77 | |
} |
78 | |
|
79 | |
protected void doConnect() throws Exception |
80 | |
{ |
81 | |
|
82 | |
|
83 | 6 | } |
84 | |
|
85 | |
protected void doDisconnect() throws Exception |
86 | |
{ |
87 | |
try |
88 | |
{ |
89 | 6 | EndpointURI uri = endpoint.getEndpointURI(); |
90 | 6 | FTPClient client = connector.getFtp(uri); |
91 | 0 | connector.destroyFtp(uri, client); |
92 | |
} |
93 | 6 | catch (Exception e) |
94 | |
{ |
95 | |
|
96 | 0 | } |
97 | 6 | } |
98 | |
|
99 | |
} |