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