View Javadoc

1   /*
2    * $Id: InboundAttachmentsAnnotationComponent.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  package org.mule.api.annotations.param;
11  
12  import org.mule.util.StringDataSource;
13  
14  import java.io.IOException;
15  import java.util.ArrayList;
16  import java.util.List;
17  import java.util.Map;
18  
19  import javax.activation.DataHandler;
20  
21  /**
22   * Tests various cases for how attachments can be injected into a component invocation
23   */
24  public class InboundAttachmentsAnnotationComponent
25  {
26      public DataHandler processAttachment(@InboundAttachments("foo") DataHandler foo)
27      {
28          return foo;
29      }
30  
31      public String processAttachmentOptional(@InboundAttachments("faz?") DataHandler faz)
32      {
33          if(faz==null)
34          {
35              return "faz not set";
36          }
37          return null;
38      }
39  
40      //This tests automatic conversion of DataHandler to its content
41      public String processAttachmentWithType(@InboundAttachments("foo") String foo)
42      {
43          return foo;
44      }
45  
46      public Map<String, DataHandler> processAttachments(@InboundAttachments("foo, bar") Map<String, DataHandler> attachments)
47      {
48          return attachments;
49      }
50      
51      public Map<String, DataHandler> processAttachmentsAll(@InboundAttachments("*") Map<String, DataHandler> attachments)
52      {
53          return attachments;
54      }
55  
56      public Map<String, DataHandler> processAttachmentsWildcard(@InboundAttachments("ba*") Map<String, DataHandler> attachments)
57      {
58          return attachments;
59      }
60      
61      public Map<String, DataHandler> processAttachmentsMultiWildcard(@InboundAttachments("ba*, f*") Map<String, DataHandler> attachments)
62      {
63          return attachments;
64      }
65      
66      public Map<String, DataHandler> processSingleMapAttachment(@InboundAttachments("foo") Map<String, DataHandler> attachments)
67      {
68          return attachments;
69      }
70      
71      public Map<String, DataHandler> processAttachmentsOptional(@InboundAttachments("foo, bar, baz?") Map<String, DataHandler> attachments)
72      {
73          return attachments;
74      }
75  
76      public Map<String, DataHandler> processAttachmentsAllOptional(@InboundAttachments("foo?, bar?, baz?") Map<String, DataHandler> attachments)
77      {
78          return attachments;
79      }
80      
81      public Map<String, DataHandler> processUnmodifiableAttachments(@InboundAttachments("foo, bar") Map<String, DataHandler> attachments)
82      {
83          //Should throw UnsupportedOperationException
84          //TODO auto wrap method on Map
85          attachments.put("car", new DataHandler(new StringDataSource("carValue")));
86          return attachments;
87      }
88  
89      public List processAttachmentsList(@InboundAttachments("foo, bar, baz") List<DataHandler> attachments)
90      {
91          return readToList(attachments);
92      }
93  
94      public List processAttachmentsListAll(@InboundAttachments("*") List<DataHandler> attachments)
95      {
96          return readToList(attachments);
97      }
98  
99      public List processSingleAttachmentList(@InboundAttachments("foo") List<DataHandler> attachments)
100     {
101         return readToList(attachments);
102     }
103 
104     public List processAttachmentsListOptional(@InboundAttachments("foo, bar, baz?") List<DataHandler> attachments)
105     {
106         return readToList(attachments);
107     }
108 
109     public List processAttachmentsListAllOptional(@InboundAttachments("foo?, bar?, baz?") List<DataHandler> attachments)
110     {
111         return readToList(attachments);
112     }
113 
114     public List processUnmodifiableAttachmentsList(@InboundAttachments("foo, bar") List<DataHandler> attachments)
115     {
116         //Should throw UnsupportedOperationException
117         attachments.add(new DataHandler(new StringDataSource("carValue")));
118         return readToList(attachments);
119     }
120 
121     public List processAttachmentsListWildcard(@InboundAttachments("ba*") List<DataHandler> attachments)
122     {
123         return readToList(attachments);
124     }
125 
126     public List processAttachmentsListMultiWildcard(@InboundAttachments("ba*, f*") List<DataHandler> attachments)
127     {
128         return readToList(attachments);
129     }
130 
131     private List readToList(List<DataHandler> list)
132     {
133         if(list.size()==0) return list;
134         List l = new ArrayList(list.size());
135         for (DataHandler dataHandler : list)
136         {
137             try
138             {
139                 l.add(dataHandler.getContent());
140             }
141             catch (IOException e)
142             {
143                 e.printStackTrace();
144             }
145         }
146         return l;
147     }
148 }