1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.ftp; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.MuleEvent; |
15 | |
import org.mule.api.MuleMessage; |
16 | |
import org.mule.api.endpoint.EndpointURI; |
17 | |
import org.mule.api.endpoint.OutboundEndpoint; |
18 | |
import org.mule.api.retry.RetryContext; |
19 | |
import org.mule.transport.AbstractMessageDispatcher; |
20 | |
import org.mule.transport.NullPayload; |
21 | |
|
22 | |
import java.io.InputStream; |
23 | |
import java.io.OutputStream; |
24 | |
|
25 | |
import org.apache.commons.io.IOUtils; |
26 | |
import org.apache.commons.net.ftp.FTPClient; |
27 | |
|
28 | |
public class FtpMessageDispatcher extends AbstractMessageDispatcher |
29 | |
{ |
30 | |
protected final FtpConnector connector; |
31 | |
|
32 | |
public FtpMessageDispatcher(OutboundEndpoint endpoint) |
33 | |
{ |
34 | 0 | super(endpoint); |
35 | 0 | this.connector = (FtpConnector) endpoint.getConnector(); |
36 | 0 | } |
37 | |
|
38 | |
protected void doDispose() |
39 | |
{ |
40 | |
|
41 | 0 | } |
42 | |
|
43 | |
protected void doDispatch(MuleEvent event) throws Exception |
44 | |
{ |
45 | 0 | Object data = event.getMessage().getPayload(); |
46 | 0 | OutputStream out = connector.getOutputStream((OutboundEndpoint) endpoint, event); |
47 | |
|
48 | |
try |
49 | |
{ |
50 | 0 | if (data instanceof InputStream) |
51 | |
{ |
52 | 0 | InputStream is = ((InputStream) data); |
53 | 0 | IOUtils.copy(is, out); |
54 | 0 | is.close(); |
55 | 0 | } |
56 | |
else |
57 | |
{ |
58 | |
byte[] dataBytes; |
59 | 0 | if (data instanceof byte[]) |
60 | |
{ |
61 | 0 | dataBytes = (byte[]) data; |
62 | |
} |
63 | |
else |
64 | |
{ |
65 | 0 | dataBytes = data.toString().getBytes(event.getEncoding()); |
66 | |
} |
67 | 0 | IOUtils.write(dataBytes, out); |
68 | |
} |
69 | |
} |
70 | |
finally |
71 | |
{ |
72 | 0 | out.close(); |
73 | 0 | } |
74 | 0 | } |
75 | |
|
76 | |
protected MuleMessage doSend(MuleEvent event) throws Exception |
77 | |
{ |
78 | 0 | doDispatch(event); |
79 | 0 | return new DefaultMuleMessage(NullPayload.getInstance(), connector.getMuleContext()); |
80 | |
} |
81 | |
|
82 | |
protected void doConnect() throws Exception |
83 | |
{ |
84 | |
|
85 | 0 | } |
86 | |
|
87 | |
protected void doDisconnect() throws Exception |
88 | |
{ |
89 | |
try |
90 | |
{ |
91 | 0 | EndpointURI uri = endpoint.getEndpointURI(); |
92 | 0 | FTPClient client = connector.getFtp(uri); |
93 | 0 | connector.destroyFtp(uri, client); |
94 | |
} |
95 | 0 | catch (Exception e) |
96 | |
{ |
97 | |
|
98 | 0 | } |
99 | 0 | } |
100 | |
|
101 | |
@Override |
102 | |
public RetryContext validateConnection(RetryContext retryContext) |
103 | |
{ |
104 | 0 | FTPClient client = null; |
105 | |
try |
106 | |
{ |
107 | 0 | client = connector.createFtpClient(endpoint); |
108 | 0 | client.sendNoOp(); |
109 | 0 | client.logout(); |
110 | 0 | client.disconnect(); |
111 | |
|
112 | 0 | retryContext.setOk(); |
113 | |
} |
114 | 0 | catch (Exception ex) |
115 | |
{ |
116 | 0 | retryContext.setFailed(ex); |
117 | |
} |
118 | |
finally |
119 | |
{ |
120 | 0 | try |
121 | |
{ |
122 | 0 | if (client != null) |
123 | |
{ |
124 | 0 | connector.releaseFtp(endpoint.getEndpointURI(), client); |
125 | |
} |
126 | |
} |
127 | 0 | catch (Exception e) |
128 | |
{ |
129 | 0 | if (logger.isDebugEnabled()) |
130 | |
{ |
131 | 0 | logger.debug("Failed to release ftp client " + client, e); |
132 | |
} |
133 | |
|
134 | 0 | } |
135 | 0 | } |
136 | |
|
137 | 0 | return retryContext; |
138 | |
} |
139 | |
} |