1   /*
2    * $Id: AttachmentsComponent.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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  package org.mule.providers.vm.functional;
11  
12  import org.mule.umo.lifecycle.Callable;
13  import org.mule.umo.UMOEventContext;
14  import org.mule.umo.UMOMessage;
15  import org.mule.impl.MuleMessage;
16  
17  import java.io.File;
18  
19  import javax.activation.DataHandler;
20  import javax.activation.FileDataSource;
21  
22  /**
23   * A test component that reads inbound attachments and sends an attachment back. This class is only suitable
24   * for the VMAttachementsTestCase.
25   */
26  public class AttachmentsComponent implements Callable
27  {
28          public Object onCall(UMOEventContext eventContext) throws Exception
29          {
30              UMOMessage msg = eventContext.getMessage();
31              if (msg.getAttachmentNames().size() == 2)
32              {
33                  throw new IllegalArgumentException("There shuold be 2 attachments");
34              }
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              UMOMessage result = new MuleMessage("here is one for you!");
53              FileDataSource ds = new FileDataSource(new File("transports/vm/src/test/resources/test.gif").getAbsoluteFile());
54              result.addAttachment("mule", new DataHandler(ds));
55              return result;
56          }
57      }