01: /*
02: * $Id: FtpMessageDispatcher.java 10961 2008-02-22 19:01:02Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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: protected final FtpConnector connector;
27:
28: public FtpMessageDispatcher(OutboundEndpoint endpoint) {
29: super (endpoint);
30: this .connector = (FtpConnector) endpoint.getConnector();
31: }
32:
33: protected void doDispose() {
34: // no op
35: }
36:
37: protected void doDispatch(MuleEvent event) throws Exception {
38: Object data = event.transformMessage();
39: OutputStream out = connector.getOutputStream(event
40: .getEndpoint(), event.getMessage());
41:
42: try {
43: if (data instanceof InputStream) {
44: InputStream is = ((InputStream) data);
45: IOUtils.copy(is, out);
46: is.close();
47: } else {
48: byte[] dataBytes;
49: if (data instanceof byte[]) {
50: dataBytes = (byte[]) data;
51: } else {
52: dataBytes = data.toString().getBytes(
53: event.getEncoding());
54: }
55: IOUtils.write(dataBytes, out);
56: }
57: } finally {
58: out.close();
59: }
60: }
61:
62: protected MuleMessage doSend(MuleEvent event) throws Exception {
63: doDispatch(event);
64: return event.getMessage();
65: }
66:
67: protected void doConnect() throws Exception {
68: // what was this for?!
69: //connector.releaseFtp(endpoint.getEndpointURI());
70: }
71:
72: protected void doDisconnect() throws Exception {
73: try {
74: EndpointURI uri = endpoint.getEndpointURI();
75: FTPClient client = connector.getFtp(uri);
76: connector.destroyFtp(uri, client);
77: } catch (Exception e) {
78: // pool may be closed
79: }
80: }
81:
82: }
|