Coverage Report - org.mule.transport.ftp.FtpMessageRequester
 
Classes in this File Line Coverage Branch Coverage Complexity
FtpMessageRequester
83%
34/41
45%
10/22
4
 
 1  
 /*
 2  
  * $Id: FtpMessageRequester.java 12377 2008-07-17 15:59:53Z tcarlson $
 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.DefaultMuleMessage;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.endpoint.EndpointURI;
 16  
 import org.mule.api.endpoint.InboundEndpoint;
 17  
 import org.mule.transport.AbstractMessageRequester;
 18  
 import org.mule.transport.file.FileConnector;
 19  
 
 20  
 import java.io.FilenameFilter;
 21  
 import java.io.IOException;
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 
 25  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 26  
 import org.apache.commons.net.ftp.FTPClient;
 27  
 import org.apache.commons.net.ftp.FTPFile;
 28  
 import org.apache.commons.net.ftp.FTPReply;
 29  
 
 30  
 public class FtpMessageRequester extends AbstractMessageRequester
 31  
 {
 32  
     protected final FtpConnector connector;
 33  
 
 34  
     public FtpMessageRequester(InboundEndpoint endpoint)
 35  
     {
 36  2
         super(endpoint);
 37  2
         this.connector = (FtpConnector) endpoint.getConnector();
 38  2
     }
 39  
 
 40  
     protected void doDispose()
 41  
     {
 42  
         // no op
 43  2
     }
 44  
 
 45  
     protected void doConnect() throws Exception
 46  
     {
 47  
         // what was this for?!
 48  
         //connector.releaseFtp(endpoint.getEndpointURI());
 49  2
     }
 50  
 
 51  
     protected void doDisconnect() throws Exception
 52  
     {
 53  
         try
 54  
         {
 55  2
             EndpointURI uri = endpoint.getEndpointURI();
 56  2
             FTPClient client = connector.getFtp(uri);
 57  0
             connector.destroyFtp(uri, client);
 58  
         }
 59  2
         catch (Exception e)
 60  
         {
 61  
             // pool may be closed
 62  0
         }
 63  2
     }
 64  
 
 65  
     /**
 66  
      * Make a specific request to the underlying transport
 67  
      *
 68  
      * @param timeout the maximum time the operation should block before returning.
 69  
      *            The call should return immediately if there is data available. If
 70  
      *            no data becomes available before the timeout elapses, null will be
 71  
      *            returned
 72  
      * @return the result of the request wrapped in a MuleMessage object. Null will be
 73  
      *         returned if no data was avaialable
 74  
      * @throws Exception if the call to the underlying protocal cuases an exception
 75  
      */
 76  
     protected MuleMessage doRequest(long timeout) throws Exception
 77  
     {
 78  2
         FTPClient client = null;
 79  
         try
 80  
         {
 81  2
             client = connector.createFtpClient(endpoint);
 82  
 
 83  2
             FilenameFilter filenameFilter = null;
 84  2
             if (endpoint.getFilter() instanceof FilenameFilter)
 85  
             {
 86  0
                 filenameFilter = (FilenameFilter) endpoint.getFilter();
 87  
             }
 88  
 
 89  2
             FTPFile[] files = client.listFiles();
 90  2
             if (!FTPReply.isPositiveCompletion(client.getReplyCode()))
 91  
             {
 92  0
                 throw new IOException("Ftp error: " + client.getReplyCode());
 93  
             }
 94  2
             if (files == null || files.length == 0)
 95  
             {
 96  0
                 return null;
 97  
             }
 98  2
             List fileList = new ArrayList();
 99  2
             FTPFile file = null;
 100  2
             for (int i = 0; i < files.length; i++)
 101  
             {
 102  2
                 file = files[i];
 103  2
                 if (file.isFile())
 104  
                 {
 105  2
                     if (filenameFilter == null || filenameFilter.accept(null, file.getName()))
 106  
                     {
 107  2
                         if (connector.validateFile(file))
 108  
                         {
 109  2
                             fileList.add(file);
 110  
                             // only read the first one
 111  2
                             break;
 112  
                         }
 113  
                     }
 114  
                 }
 115  
             }
 116  2
             if (fileList.size() == 0)
 117  
             {
 118  0
                 return null;
 119  
             }
 120  
 
 121  2
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 122  2
             if (!client.retrieveFile(file.getName(), baos))
 123  
             {
 124  0
                 throw new IOException("Ftp error: " + client.getReplyCode());
 125  
             }
 126  
 
 127  2
             MuleMessage reply = new DefaultMuleMessage(connector.getMessageAdapter(baos.toByteArray()));
 128  2
             reply.setProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, file.getName());
 129  2
             reply.setProperty(FileConnector.PROPERTY_FILE_SIZE, new Long(file.getSize()));
 130  2
             return reply;
 131  
         }
 132  
         finally
 133  
         {
 134  2
             logger.debug("leaving doRequest()");
 135  2
             connector.releaseFtp(endpoint.getEndpointURI(), client);
 136  
         }
 137  
     }
 138  
 }