View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.ftp;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleContext;
11  import org.mule.transport.AbstractMuleMessageFactory;
12  import org.mule.transport.file.FileConnector;
13  
14  import java.io.ByteArrayOutputStream;
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.text.MessageFormat;
18  
19  import org.apache.commons.net.ftp.FTPClient;
20  import org.apache.commons.net.ftp.FTPFile;
21  
22  public class FtpMuleMessageFactory extends AbstractMuleMessageFactory
23  {
24  
25      private FTPClient ftpClient;
26      private boolean streaming;
27  
28      public FtpMuleMessageFactory(MuleContext context)
29      {
30          super(context);
31      }
32  
33      @Override
34      protected Object extractPayload(Object transportMessage, String encoding) throws Exception
35      {
36          FTPFile file = (FTPFile) transportMessage;
37          if (streaming)
38          {
39              InputStream stream = ftpClient.retrieveFileStream(file.getName());
40              if (stream == null)
41              {
42                  throw new IOException(MessageFormat.format("Failed to retrieve file {0}. Ftp error: {1}",
43                      file.getName(), ftpClient.getReplyCode()));
44              }
45              return stream;
46          }
47          else
48          {
49              ByteArrayOutputStream baos = new ByteArrayOutputStream();
50              if (!ftpClient.retrieveFile(file.getName(), baos))
51              {
52                  throw new IOException(MessageFormat.format("Failed to retrieve file {0}. Ftp error: {1}",
53                      file.getName(), ftpClient.getReplyCode()));
54              }
55              byte[] bytes = baos.toByteArray();
56              if (bytes.length > 0)
57              {
58                  return bytes;
59              }
60              else
61              {
62                  throw new IOException("File " + file.getName() + " is empty (zero bytes)");
63              }
64          }
65      }
66  
67      @Override
68      protected Class<?>[] getSupportedTransportMessageTypes()
69      {
70          return new Class[]{FTPFile.class};
71      }
72  
73      @Override
74      protected void addProperties(DefaultMuleMessage message, Object transportMessage) throws Exception
75      {
76          super.addProperties(message, transportMessage);
77          
78          FTPFile file = (FTPFile) transportMessage;
79          message.setOutboundProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, file.getName());
80          message.setOutboundProperty(FileConnector.PROPERTY_FILE_SIZE, file.getSize());
81          message.setOutboundProperty(FileConnector.PROPERTY_FILE_TIMESTAMP, file.getTimestamp());
82      }
83  
84      public void setFtpClient(FTPClient ftpClient)
85      {
86          this.ftpClient = ftpClient;
87      }
88  
89      public void setStreaming(boolean streaming)
90      {
91          this.streaming = streaming;
92      }
93  
94  }