1
2
3
4
5
6
7
8
9
10
11 package org.mule.transformer.simple;
12
13 import org.mule.api.MuleMessage;
14 import org.mule.api.transformer.TransformerException;
15 import org.mule.transformer.AbstractMessageAwareTransformer;
16
17 import java.text.MessageFormat;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25
26
27
28
29
30
31
32
33
34
35 public class MessagePropertiesTransformer extends AbstractMessageAwareTransformer
36 {
37 private List deleteProperties = null;
38 private Map addProperties = null;
39
40 private Map renameProperties;
41 private boolean overwrite = true;
42
43 public MessagePropertiesTransformer()
44 {
45 registerSourceType(Object.class);
46 setReturnClass(Object.class);
47 }
48
49
50 public Object clone() throws CloneNotSupportedException
51 {
52 MessagePropertiesTransformer clone = (MessagePropertiesTransformer)super.clone();
53
54 if (deleteProperties != null)
55 {
56 clone.setDeleteProperties(new ArrayList(deleteProperties));
57 }
58
59 if (addProperties != null)
60 {
61 clone.setAddProperties(new HashMap(addProperties));
62 }
63
64 if (renameProperties != null)
65 {
66 clone.setRenameProperties(new HashMap(renameProperties));
67 }
68 return clone;
69 }
70
71 public Object transform(MuleMessage message, String outputEncoding) throws TransformerException
72 {
73 if (deleteProperties != null && deleteProperties.size() > 0)
74 {
75 for (Iterator iterator = deleteProperties.iterator(); iterator.hasNext();)
76 {
77 message.removeProperty(iterator.next().toString());
78 }
79 }
80
81 if (addProperties != null && addProperties.size() > 0)
82 {
83 final Set propertyNames = message.getPropertyNames();
84 for (Iterator iterator = addProperties.entrySet().iterator(); iterator.hasNext();)
85 {
86 Map.Entry entry = (Map.Entry)iterator.next();
87 if (entry.getKey() == null)
88 {
89 logger.error("Setting Null property keys is not supported, this entry is being ignored");
90 }
91 else
92 {
93 final String key = entry.getKey().toString();
94
95 final Object value = entry.getValue();
96 if (overwrite)
97 {
98 if (logger.isDebugEnabled())
99 {
100 if (!propertyNames.contains(key))
101 {
102 logger.debug("Overwriting message property " + key);
103 }
104 }
105 message.setProperty(key, value);
106 }
107 else
108 {
109 if (propertyNames.contains(key))
110 {
111 if (logger.isDebugEnabled())
112 {
113 logger.debug(MessageFormat.format(
114 "Message already contains the property and overwrite is false, skipping: key={0}, value={1}",
115 new Object[]{key, value}));
116 }
117 }
118 }
119 }
120 }
121 }
122
123
124 if (this.renameProperties != null && this.renameProperties.size() > 0)
125 {
126 final Set propertyNames = message.getPropertyNames();
127 for (Iterator iterator = this.renameProperties.entrySet().iterator(); iterator.hasNext();)
128 {
129 Map.Entry entry = (Map.Entry)iterator.next();
130
131 if (entry.getKey() == null)
132 {
133 logger.error("Setting Null property keys is not supported, this entry is being ignored");
134 }
135 else
136 {
137 final String key = entry.getKey().toString();
138 final String value = (String)entry.getValue();
139
140 if (value == null)
141 {
142 logger.error("Setting Null property values for renameProperties is not supported, this entry is being ignored");
143 }
144 else
145 {
146
147
148 if (logger.isDebugEnabled() && !propertyNames.contains(key))
149 {
150 logger.debug("renaming message property " + key + " to " + value);
151 }
152
153
154
155
156
157 Object propValue = message.getProperty(key);
158 message.removeProperty(key);
159 message.setProperty(value, propValue);
160 }
161 }
162 }
163 }
164 return message;
165 }
166
167 public List getDeleteProperties()
168 {
169 return deleteProperties;
170 }
171
172 public void setDeleteProperties(List deleteProperties)
173 {
174 this.deleteProperties = deleteProperties;
175 }
176
177 public Map getAddProperties()
178 {
179 return addProperties;
180 }
181
182 public void setAddProperties(Map addProperties)
183 {
184 this.addProperties = addProperties;
185 }
186
187
188
189
190 public Map getRenameProperties()
191 {
192 return this.renameProperties;
193 }
194
195
196
197
198 public void setRenameProperties(Map renameProperties)
199 {
200 this.renameProperties = renameProperties;
201 }
202
203 public boolean isOverwrite()
204 {
205 return overwrite;
206 }
207
208 public void setOverwrite(final boolean overwrite)
209 {
210 this.overwrite = overwrite;
211 }
212 }