View Javadoc
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.file;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.DefaultMuleException;
11  import org.mule.api.MuleEvent;
12  import org.mule.api.MuleException;
13  import org.mule.api.MuleMessage;
14  import org.mule.api.endpoint.OutboundEndpoint;
15  import org.mule.api.transport.OutputHandler;
16  import org.mule.transformer.types.DataTypeFactory;
17  import org.mule.transport.AbstractMessageDispatcher;
18  import org.mule.transport.NullPayload;
19  import org.mule.transport.file.i18n.FileMessages;
20  import org.mule.util.FileUtils;
21  import org.mule.util.IOUtils;
22  import org.mule.util.StringUtils;
23  
24  import java.io.File;
25  import java.io.FileFilter;
26  import java.io.FileOutputStream;
27  import java.io.FilenameFilter;
28  import java.io.InputStream;
29  
30  /**
31   * <code>FileMessageDispatcher</code> is used to read/write files to the filesystem
32   */
33  public class FileMessageDispatcher extends AbstractMessageDispatcher
34  {
35      private final FileConnector connector;
36  
37      public FileMessageDispatcher(OutboundEndpoint endpoint)
38      {
39          super(endpoint);
40          this.connector = (FileConnector) endpoint.getConnector();
41  
42          if (endpoint.getProperty("outputAppend") != null)
43          {
44              throw new IllegalArgumentException("Configuring 'outputAppend' on a file endpoint is no longer supported. You may configure it on a file connector instead.");
45          }
46      }
47  
48      @Override
49      protected void doDispatch(MuleEvent event) throws Exception
50      {
51          Object data = event.getMessage().getPayload();
52          // Wrap the transformed message before passing it to the filename parser
53          MuleMessage message = new DefaultMuleMessage(data, event.getMessage(), event.getMuleContext());
54  
55          FileOutputStream fos = (FileOutputStream) connector.getOutputStream((OutboundEndpoint) endpoint, event);
56          try
57          {
58              if (event.getMessage().getOutboundProperty(FileConnector.PROPERTY_FILENAME) == null)
59              {
60                  event.getMessage().setOutboundProperty(FileConnector.PROPERTY_FILENAME,
61                                                         message.getOutboundProperty(FileConnector.PROPERTY_FILENAME,
62                                                                                     StringUtils.EMPTY));
63              }
64  
65              if (data instanceof byte[])
66              {
67                  fos.write((byte[]) data);
68              }
69              else if (data instanceof String)
70              {
71                  fos.write(data.toString().getBytes(event.getEncoding()));
72              }
73              else if (data instanceof OutputHandler)
74              {
75                  ((OutputHandler) data).write(event, fos);
76              }
77              else
78              {
79                  InputStream is = (InputStream) event.transformMessage(DataTypeFactory.create(InputStream.class));
80                  IOUtils.copyLarge(is, fos);
81                  is.close();
82              }
83          }
84          finally
85          {
86              logger.debug("Closing file");
87              fos.close();
88          }
89      }
90  
91      /**
92       * There is no associated session for a file connector
93       *
94       * @throws MuleException
95       */
96      public Object getDelegateSession() throws MuleException
97      {
98          return null;
99      }
100 
101     protected static File getNextFile(String dir, Object filter) throws MuleException
102     {
103         File[] files;
104         File file = FileUtils.newFile(dir);
105         File result = null;
106         try
107         {
108             if (file.exists())
109             {
110                 if (file.isFile())
111                 {
112                     result = file;
113                 }
114                 else if (file.isDirectory())
115                 {
116                     if (filter != null)
117                     {
118                         if (filter instanceof FileFilter)
119                         {
120                             files = file.listFiles((FileFilter) filter);
121                         }
122                         else if (filter instanceof FilenameFilter)
123                         {
124                             files = file.listFiles((FilenameFilter) filter);
125                         }
126                         else
127                         {
128                             throw new DefaultMuleException(FileMessages.invalidFilter(filter));
129                         }
130                     }
131                     else
132                     {
133                         files = file.listFiles();
134                     }
135                     if (files.length > 0)
136                     {
137                         result = files[0];
138                     }
139                 }
140             }
141             return result;
142         }
143         catch (Exception e)
144         {
145             throw new DefaultMuleException(FileMessages.errorWhileListingFiles(), e);
146         }
147     }
148 
149     @Override
150     protected MuleMessage doSend(MuleEvent event) throws Exception
151     {
152         doDispatch(event);
153         return new DefaultMuleMessage(NullPayload.getInstance(), connector.getMuleContext());
154     }
155 
156     @Override
157     protected void doDispose()
158     {
159         // no op
160     }
161 
162     @Override
163     protected void doConnect() throws Exception
164     {
165         // no op
166     }
167 
168     @Override
169     protected void doDisconnect() throws Exception
170     {
171         // no op
172     }
173 }