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.vm.functional;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleEventContext;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.lifecycle.Callable;
13  
14  import java.io.File;
15  
16  import javax.activation.DataHandler;
17  import javax.activation.FileDataSource;
18  
19  /**
20   * A test service that reads inbound attachments and sends an attachment back. This
21   * class is only suitable for the VMAttachementsTestCase.
22   */
23  public class AttachmentsComponent implements Callable
24  {
25      public Object onCall(MuleEventContext eventContext) throws Exception
26      {
27          MuleMessage msg = eventContext.getMessage();
28          if (msg.getInboundAttachmentNames().size() == 2)
29          {
30              throw new IllegalArgumentException("There shuold be 2 attachments");
31          }
32  
33          DataHandler dh = msg.getInboundAttachment("test-attachment");
34          if (dh == null)
35          {
36              throw new IllegalArgumentException("test-attachment is not on the message");
37          }
38          if (!dh.getContentType().startsWith("text/xml"))
39          {
40              throw new IllegalArgumentException("content type is not text/xml");
41          }
42  
43          if (!"Mmm... attachments!".equals(msg.getPayloadAsString()))
44          {
45              throw new IllegalArgumentException("payload is incorrect");
46          }
47          // Lets return an image
48          MuleMessage result = new DefaultMuleMessage("here is one for you!", eventContext.getMuleContext());
49          FileDataSource ds = new FileDataSource(
50              new File("transports/vm/src/test/resources/test.gif").getAbsoluteFile());
51          result.addOutboundAttachment("mule", new DataHandler(ds));
52          return result;
53      }
54  }