Coverage Report - org.mule.transport.ftp.FtpMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
FtpMessageDispatcher
0%
0/44
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.MuleEvent;
 11  
 import org.mule.api.MuleMessage;
 12  
 import org.mule.api.endpoint.EndpointURI;
 13  
 import org.mule.api.endpoint.OutboundEndpoint;
 14  
 import org.mule.api.retry.RetryContext;
 15  
 import org.mule.transport.AbstractMessageDispatcher;
 16  
 import org.mule.transport.NullPayload;
 17  
 
 18  
 import java.io.InputStream;
 19  
 import java.io.OutputStream;
 20  
 
 21  
 import org.apache.commons.io.IOUtils;
 22  
 import org.apache.commons.net.ftp.FTPClient;
 23  
 
 24  
 public class FtpMessageDispatcher extends AbstractMessageDispatcher
 25  
 {
 26  
     protected final FtpConnector connector;
 27  
 
 28  
     public FtpMessageDispatcher(OutboundEndpoint endpoint)
 29  
     {
 30  0
         super(endpoint);
 31  0
         this.connector = (FtpConnector) endpoint.getConnector();
 32  0
     }
 33  
 
 34  
     protected void doDispose()
 35  
     {
 36  
         // no op
 37  0
     }
 38  
 
 39  
     protected void doDispatch(MuleEvent event) throws Exception
 40  
     {
 41  0
         Object data = event.getMessage().getPayload();
 42  0
         OutputStream out = connector.getOutputStream((OutboundEndpoint) endpoint, event);
 43  
 
 44  
         try
 45  
         {
 46  0
             if (data instanceof InputStream)
 47  
             {
 48  0
                 InputStream is = ((InputStream) data);
 49  0
                 IOUtils.copy(is, out);
 50  0
                 is.close();
 51  0
             }
 52  
             else
 53  
             {
 54  
                 byte[] dataBytes;
 55  0
                 if (data instanceof byte[])
 56  
                 {
 57  0
                     dataBytes = (byte[]) data;
 58  
                 }
 59  
                 else
 60  
                 {
 61  0
                     dataBytes = data.toString().getBytes(event.getEncoding());
 62  
                 }
 63  0
                 IOUtils.write(dataBytes, out);
 64  
             }
 65  
         }
 66  
         finally
 67  
         {
 68  0
             out.close();
 69  0
         }
 70  0
     }
 71  
 
 72  
     protected MuleMessage doSend(MuleEvent event) throws Exception
 73  
     {
 74  0
         doDispatch(event);
 75  0
         return new DefaultMuleMessage(NullPayload.getInstance(), connector.getMuleContext());
 76  
     }
 77  
 
 78  
     protected void doConnect() throws Exception
 79  
     {
 80  
         // template method
 81  0
     }
 82  
 
 83  
     protected void doDisconnect() throws Exception
 84  
     {
 85  
         try
 86  
         {
 87  0
             EndpointURI uri = endpoint.getEndpointURI();
 88  0
             FTPClient client = connector.getFtp(uri);
 89  0
             connector.destroyFtp(uri, client);
 90  
         }
 91  0
         catch (Exception e)
 92  
         {
 93  
             // pool may be closed
 94  0
         }
 95  0
     }
 96  
 
 97  
     @Override
 98  
     public RetryContext validateConnection(RetryContext retryContext)
 99  
     {
 100  0
         FTPClient client = null;
 101  
         try
 102  
         {
 103  0
             client = connector.createFtpClient(endpoint);
 104  0
             client.sendNoOp();
 105  0
             client.logout();
 106  0
             client.disconnect();
 107  
 
 108  0
             retryContext.setOk();
 109  
         }
 110  0
         catch (Exception ex)
 111  
         {
 112  0
             retryContext.setFailed(ex);
 113  
         }
 114  
         finally
 115  
         {
 116  0
             try
 117  
             {
 118  0
                 if (client != null)
 119  
                 {
 120  0
                     connector.releaseFtp(endpoint.getEndpointURI(), client);
 121  
                 }
 122  
             }
 123  0
             catch (Exception e)
 124  
             {
 125  0
                 if (logger.isDebugEnabled())
 126  
                 {
 127  0
                     logger.debug("Failed to release ftp client " + client, e);
 128  
                 }
 129  
 
 130  0
             }
 131  0
         }
 132  
 
 133  0
         return retryContext;
 134  
     }
 135  
 }