View Javadoc

1   /*
2    * $Id: FtpMessageDispatcher.java 10961 2008-02-22 19:01:02Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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          // no op
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          // what was this for?!
82          //connector.releaseFtp(endpoint.getEndpointURI());
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              // pool may be closed
96          }
97      }
98  
99  }