View Javadoc

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