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 super(endpoint);
32 this.connector = (FtpConnector) endpoint.getConnector();
33 }
34
35 protected void doDispose()
36 {
37
38 }
39
40 protected void doDispatch(MuleEvent event) throws Exception
41 {
42 Object data = event.transformMessage();
43 OutputStream out = connector.getOutputStream(event.getEndpoint(), event.getMessage());
44
45 try
46 {
47 if (data instanceof InputStream)
48 {
49 InputStream is = ((InputStream) data);
50 IOUtils.copy(is, out);
51 is.close();
52 }
53 else
54 {
55 byte[] dataBytes;
56 if (data instanceof byte[])
57 {
58 dataBytes = (byte[]) data;
59 }
60 else
61 {
62 dataBytes = data.toString().getBytes(event.getEncoding());
63 }
64 IOUtils.write(dataBytes, out);
65 }
66 }
67 finally
68 {
69 out.close();
70 }
71 }
72
73 protected MuleMessage doSend(MuleEvent event) throws Exception
74 {
75 doDispatch(event);
76 return event.getMessage();
77 }
78
79 protected void doConnect() throws Exception
80 {
81
82
83 }
84
85 protected void doDisconnect() throws Exception
86 {
87 try
88 {
89 EndpointURI uri = endpoint.getEndpointURI();
90 FTPClient client = connector.getFtp(uri);
91 connector.destroyFtp(uri, client);
92 }
93 catch (Exception e)
94 {
95
96 }
97 }
98
99 }