Coverage Report - org.mule.transport.file.FileMessageRequester
 
Classes in this File Line Coverage Branch Coverage Complexity
FileMessageRequester
0%
0/82
0%
0/42
0
 
 1  
 /*
 2  
  * $Id: FileMessageRequester.java 19223 2010-08-26 15:57:20Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.file;
 12  
 
 13  
 import org.mule.DefaultMuleMessage;
 14  
 import org.mule.api.DefaultMuleException;
 15  
 import org.mule.api.MuleException;
 16  
 import org.mule.api.MuleMessage;
 17  
 import org.mule.api.endpoint.InboundEndpoint;
 18  
 import org.mule.api.lifecycle.CreateException;
 19  
 import org.mule.api.routing.filter.Filter;
 20  
 import org.mule.transport.AbstractMessageRequester;
 21  
 import org.mule.transport.file.i18n.FileMessages;
 22  
 import org.mule.util.FileUtils;
 23  
 
 24  
 import java.io.File;
 25  
 import java.io.FileFilter;
 26  
 import java.io.FileNotFoundException;
 27  
 import java.io.FilenameFilter;
 28  
 import java.io.IOException;
 29  
 
 30  
 /**
 31  
  * <code>FileMessageRequester</code> is used to read/write files to the filesystem
 32  
  */
 33  
 public class FileMessageRequester extends AbstractMessageRequester
 34  
 {
 35  
     private final FileConnector connector;
 36  
 
 37  0
     private FilenameFilter filenameFilter = null;
 38  0
     private FileFilter fileFilter = null;
 39  
     
 40  
     public FileMessageRequester(InboundEndpoint endpoint) throws MuleException
 41  
     {
 42  0
         super(endpoint);
 43  0
         this.connector = (FileConnector) endpoint.getConnector();
 44  
         
 45  0
         Filter filter = endpoint.getFilter();
 46  0
         if (filter instanceof FilenameFilter)
 47  
         {
 48  0
             filenameFilter = (FilenameFilter) filter;
 49  
         }
 50  0
         else if (filter instanceof FileFilter)
 51  
         {
 52  0
             fileFilter = (FileFilter) filter;
 53  
         }
 54  0
         else if (filter != null)
 55  
         {
 56  0
             throw new CreateException(FileMessages.invalidFileFilter(endpoint.getEndpointURI()), this);
 57  
         }
 58  0
     }
 59  
 
 60  
     /**
 61  
      * There is no associated session for a file connector
 62  
      * 
 63  
      * @throws org.mule.api.MuleException
 64  
      */
 65  
     public Object getDelegateSession() throws MuleException
 66  
     {
 67  0
         return null;
 68  
     }
 69  
 
 70  
     /**
 71  
      * Will attempt to do a receive from a directory, if the endpointUri resolves to
 72  
      * a file name the file will be returned, otherwise the first file in the
 73  
      * directory according to the filename filter configured on the connector.
 74  
      * 
 75  
      * @param timeout this is ignored when doing a receive on this dispatcher
 76  
      * @return a message containing file contents or null if there was notthing to
 77  
      *         receive
 78  
      * @throws Exception
 79  
      */
 80  
     @Override
 81  
     protected MuleMessage doRequest(long timeout) throws Exception
 82  
     {
 83  0
         File file = FileUtils.newFile(endpoint.getEndpointURI().getAddress());
 84  0
         File result = null;
 85  
 
 86  0
         if (file.exists())
 87  
         {
 88  0
             if (file.isFile())
 89  
             {
 90  0
                 result = file;
 91  
             }
 92  0
             else if (file.isDirectory())
 93  
             {
 94  0
                 if (fileFilter != null)
 95  
                 {
 96  0
                     result = FileMessageDispatcher.getNextFile(
 97  
                         endpoint.getEndpointURI().getAddress(), fileFilter);
 98  
                 }
 99  
                 else
 100  
                 {
 101  0
                     result = FileMessageDispatcher.getNextFile(
 102  
                         endpoint.getEndpointURI().getAddress(), filenameFilter);
 103  
                 }
 104  
             }
 105  
             
 106  0
             if (result != null)
 107  
             {
 108  0
                 boolean checkFileAge = connector.getCheckFileAge();
 109  0
                 if (checkFileAge)
 110  
                 {
 111  0
                     long fileAge = connector.getFileAge();
 112  0
                     long lastMod = result.lastModified();
 113  0
                     long now = System.currentTimeMillis();
 114  0
                     long thisFileAge = now - lastMod;
 115  0
                     if (thisFileAge < fileAge)
 116  
                     {
 117  0
                         if (logger.isDebugEnabled())
 118  
                         {
 119  0
                             logger.debug("The file has not aged enough yet, will return nothing for: "
 120  
                                          + result.getCanonicalPath());
 121  
                         }
 122  0
                         return null;
 123  
                     }
 124  
                 }
 125  
 
 126  
                 // Don't we need to try to obtain a file lock as we do with receiver
 127  0
                 String sourceFileOriginalName = result.getName();
 128  
 
 129  
                 // set up destination file
 130  0
                 File destinationFile = null;
 131  0
                 String movDir = getMoveDirectory();
 132  0
                 if (movDir != null)
 133  
                 {
 134  0
                     String destinationFileName = sourceFileOriginalName;
 135  0
                     String moveToPattern = getMoveToPattern();
 136  0
                     if (moveToPattern != null)
 137  
                     {
 138  
                         // This isn't nice but is needed as MuleMessage is required to
 139  
                         // resolve the destination file name
 140  0
                         DefaultMuleMessage parserMesssage = new DefaultMuleMessage(null,
 141  
                             connector.getMuleContext());
 142  0
                         parserMesssage.setOutboundProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, sourceFileOriginalName);
 143  
 
 144  0
                         destinationFileName =
 145  
                             connector.getFilenameParser().getFilename(parserMesssage, moveToPattern);
 146  
                     }
 147  
                     // don't use new File() directly, see MULE-1112
 148  0
                     destinationFile = FileUtils.newFile(movDir, destinationFileName);
 149  
                 }
 150  
 
 151  0
                 MuleMessage returnMessage = null;
 152  0
                 String encoding = endpoint.getEncoding();
 153  
                 try
 154  
                 {
 155  0
                     if (connector.isStreaming())
 156  
                     {
 157  0
                         ReceiverFileInputStream receiverStream = new ReceiverFileInputStream(result,
 158  
                             connector.isAutoDelete(), destinationFile);
 159  0
                         returnMessage = createMuleMessage(receiverStream, encoding);
 160  0
                     }
 161  
                     else
 162  
                     {
 163  0
                         returnMessage = createMuleMessage(result, encoding);
 164  
                     }
 165  
                 }
 166  0
                 catch (FileNotFoundException e)
 167  
                 {
 168  
                     // we can ignore since we did manage to acquire a lock, but just
 169  
                     // in case
 170  0
                     logger.error("File being read disappeared!", e);
 171  0
                     return null;
 172  0
                 }
 173  0
                 returnMessage.setOutboundProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, sourceFileOriginalName);
 174  
 
 175  0
                 if (!connector.isStreaming())
 176  
                 {
 177  0
                     moveOrDelete(result, destinationFile);
 178  
                 }
 179  
 
 180  
                 // If we are streaming no need to move/delete now, that will be
 181  
                 // done when stream is closed
 182  0
                 return returnMessage;
 183  
             }
 184  
         }
 185  0
         return null;
 186  
     }
 187  
 
 188  
     private void moveOrDelete(final File sourceFile, File destinationFile) throws DefaultMuleException
 189  
     {
 190  
 
 191  0
         if (destinationFile != null)
 192  
         {
 193  
             // move sourceFile to new destination
 194  
             try
 195  
             {
 196  0
                 FileUtils.moveFile(sourceFile, destinationFile);
 197  
             }
 198  0
             catch (IOException e)
 199  
             {
 200  0
                 throw new DefaultMuleException(FileMessages.failedToMoveFile(sourceFile.getAbsolutePath(),
 201  
                     destinationFile.getAbsolutePath()));
 202  0
             }
 203  
         }
 204  0
         if (connector.isAutoDelete())
 205  
         {
 206  
             // no moveTo directory
 207  0
             if (destinationFile == null)
 208  
             {
 209  0
 System.out.println("\n\n***** FileMessageRequester moveOrDelete\n\n");
 210  
                 // delete source
 211  0
                 if (!sourceFile.delete())
 212  
                 {
 213  0
                     throw new DefaultMuleException(FileMessages.failedToDeleteFile(sourceFile));
 214  
                 }
 215  
             }
 216  
             else
 217  
             {
 218  
                 // nothing to do here since moveFile() should have deleted
 219  
                 // the source file for us
 220  
             }
 221  
         }
 222  
 
 223  0
     }
 224  
 
 225  
     @Override
 226  
     protected void doDispose()
 227  
     {
 228  
         // no op
 229  0
     }
 230  
 
 231  
     @Override
 232  
     protected void doConnect() throws Exception
 233  
     {
 234  
         // no op
 235  0
     }
 236  
 
 237  
     @Override
 238  
     protected void doDisconnect() throws Exception
 239  
     {
 240  
         // no op
 241  0
     }
 242  
 
 243  
     protected String getMoveDirectory()
 244  
     {
 245  0
         String moveDirectory = (String) endpoint.getProperty(FileConnector.PROPERTY_MOVE_TO_DIRECTORY);
 246  0
         if (moveDirectory == null)
 247  
         {
 248  0
             moveDirectory = connector.getMoveToDirectory();
 249  
         }
 250  0
         return moveDirectory;
 251  
     }
 252  
 
 253  
     protected String getMoveToPattern()
 254  
     {
 255  0
         String pattern = (String) endpoint.getProperty(FileConnector.PROPERTY_MOVE_TO_PATTERN);
 256  0
         if (pattern == null)
 257  
         {
 258  0
             pattern = connector.getMoveToPattern();
 259  
         }
 260  0
         return pattern;
 261  
     }
 262  
 
 263  
 }