Coverage Report - org.mule.transport.ftp.FtpMuleMessageFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
FtpMuleMessageFactory
0%
0/26
0%
0/8
0
 
 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  0
         super(context);
 31  0
     }
 32  
 
 33  
     @Override
 34  
     protected Object extractPayload(Object transportMessage, String encoding) throws Exception
 35  
     {
 36  0
         FTPFile file = (FTPFile) transportMessage;
 37  0
         if (streaming)
 38  
         {
 39  0
             InputStream stream = ftpClient.retrieveFileStream(file.getName());
 40  0
             if (stream == null)
 41  
             {
 42  0
                 throw new IOException(MessageFormat.format("Failed to retrieve file {0}. Ftp error: {1}",
 43  
                     file.getName(), ftpClient.getReplyCode()));
 44  
             }
 45  0
             return stream;
 46  
         }
 47  
         else
 48  
         {
 49  0
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 50  0
             if (!ftpClient.retrieveFile(file.getName(), baos))
 51  
             {
 52  0
                 throw new IOException(MessageFormat.format("Failed to retrieve file {0}. Ftp error: {1}",
 53  
                     file.getName(), ftpClient.getReplyCode()));
 54  
             }
 55  0
             byte[] bytes = baos.toByteArray();
 56  0
             if (bytes.length > 0)
 57  
             {
 58  0
                 return bytes;
 59  
             }
 60  
             else
 61  
             {
 62  0
                 throw new IOException("File " + file.getName() + " is empty (zero bytes)");
 63  
             }
 64  
         }
 65  
     }
 66  
 
 67  
     @Override
 68  
     protected Class<?>[] getSupportedTransportMessageTypes()
 69  
     {
 70  0
         return new Class[]{FTPFile.class};
 71  
     }
 72  
 
 73  
     @Override
 74  
     protected void addProperties(DefaultMuleMessage message, Object transportMessage) throws Exception
 75  
     {
 76  0
         super.addProperties(message, transportMessage);
 77  
         
 78  0
         FTPFile file = (FTPFile) transportMessage;
 79  0
         message.setOutboundProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, file.getName());
 80  0
         message.setOutboundProperty(FileConnector.PROPERTY_FILE_SIZE, file.getSize());
 81  0
         message.setOutboundProperty(FileConnector.PROPERTY_FILE_TIMESTAMP, file.getTimestamp());
 82  0
     }
 83  
 
 84  
     public void setFtpClient(FTPClient ftpClient)
 85  
     {
 86  0
         this.ftpClient = ftpClient;
 87  0
     }
 88  
 
 89  
     public void setStreaming(boolean streaming)
 90  
     {
 91  0
         this.streaming = streaming;
 92  0
     }
 93  
 
 94  
 }