View Javadoc

1   /*
2    * $Id: FileMuleMessageFactory.java 19191 2010-08-25 21:05:23Z tcarlson $
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.MuleContext;
15  import org.mule.api.MuleMessage;
16  import org.mule.transport.AbstractMuleMessageFactory;
17  
18  import java.io.File;
19  
20  /**
21   * <code>FileMuleMessageFactory</code> creates a new {@link MuleMessage} with a
22   * {@link File} or {@link ReceiverFileInputStream} payload. Users can obtain the
23   * filename and directory in the properties using
24   * <code>FileConnector.PROPERTY_FILENAME</code> and
25   * <code>FileConnector.PROPERTY_DIRECTORY</code>.
26   */
27  public class FileMuleMessageFactory extends AbstractMuleMessageFactory
28  {
29      public FileMuleMessageFactory(MuleContext context)
30      {
31          super(context);
32      }
33  
34      @Override
35      protected Class<?>[] getSupportedTransportMessageTypes()
36      {
37          return new Class[]{File.class, ReceiverFileInputStream.class};
38      }
39  
40      @Override
41      protected Object extractPayload(Object transportMessage, String encoding) throws Exception
42      {
43          return transportMessage;
44      }
45  
46      @Override
47      protected void addProperties(DefaultMuleMessage message, Object transportMessage) throws Exception
48      {
49          super.addProperties(message, transportMessage);
50          File file = convertToFile(transportMessage);
51          setPropertiesFromFile(message, file);
52      }
53  
54      protected File convertToFile(Object transportMessage)
55      {
56          File file = null;
57  
58          if (transportMessage instanceof File)
59          {
60              file = (File) transportMessage;
61          }
62          else if (transportMessage instanceof ReceiverFileInputStream)
63          {
64              file = ((ReceiverFileInputStream) transportMessage).getCurrentFile();
65          }
66  
67          return file;
68      }
69  
70      protected void setPropertiesFromFile(MuleMessage message, File file)
71      {
72          message.setOutboundProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, file.getName());
73          message.setOutboundProperty(FileConnector.PROPERTY_DIRECTORY, file.getParent());
74      }
75  }