Coverage Report - org.mule.transport.ftp.FtpMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
FtpMessageDispatcher
70%
19/27
75%
3/4
1.5
 
 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  6
         super(endpoint);
 32  6
         this.connector = (FtpConnector) endpoint.getConnector();
 33  6
     }
 34  
 
 35  
     protected void doDispose()
 36  
     {
 37  
         // no op
 38  6
     }
 39  
 
 40  
     protected void doDispatch(MuleEvent event) throws Exception
 41  
     {
 42  8
         Object data = event.transformMessage();
 43  8
         OutputStream out = connector.getOutputStream(event.getEndpoint(), event.getMessage());
 44  
 
 45  
         try
 46  
         {
 47  8
             if (data instanceof InputStream)
 48  
             {
 49  0
                 InputStream is = ((InputStream) data);
 50  0
                 IOUtils.copy(is, out);
 51  0
                 is.close();
 52  0
             }
 53  
             else
 54  
             {
 55  
                 byte[] dataBytes;
 56  8
                 if (data instanceof byte[])
 57  
                 {
 58  6
                     dataBytes = (byte[]) data;
 59  
                 }
 60  
                 else
 61  
                 {
 62  2
                     dataBytes = data.toString().getBytes(event.getEncoding());
 63  
                 }
 64  8
                 IOUtils.write(dataBytes, out);
 65  
             }
 66  
         }
 67  
         finally
 68  
         {
 69  8
             out.close();
 70  8
         }
 71  8
     }
 72  
 
 73  
     protected MuleMessage doSend(MuleEvent event) throws Exception
 74  
     {
 75  0
         doDispatch(event);
 76  0
         return event.getMessage();
 77  
     }
 78  
 
 79  
     protected void doConnect() throws Exception
 80  
     {
 81  
         // what was this for?!
 82  
         //connector.releaseFtp(endpoint.getEndpointURI());
 83  6
     }
 84  
 
 85  
     protected void doDisconnect() throws Exception
 86  
     {
 87  
         try
 88  
         {
 89  6
             EndpointURI uri = endpoint.getEndpointURI();
 90  6
             FTPClient client = connector.getFtp(uri);
 91  0
             connector.destroyFtp(uri, client);
 92  
         }
 93  6
         catch (Exception e)
 94  
         {
 95  
             // pool may be closed
 96  0
         }
 97  6
     }
 98  
 
 99  
 }