View Javadoc

1   /*
2    * $Id: MessagePropertiesTransformer.java 11536 2008-04-08 18:00:40Z tcarlson $
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.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   * A configurable message transformer that allows users to add, overwrite and delete
27   * properties on the current message. Users can set a {@link List} of
28   * 'deleteProperties' names to remove from the message and can also set a {@link Map}
29   * of 'addProperties' that will be added to the message and possibly overwrite
30   * existing properties with the same name. <p/> If {@link #overwrite} is set to
31   * <code>false</code>, and a property exists on the message (even if the value is
32   * <code>null</code>, it will be left intact. The transformer then acts as a more
33   * gentle 'enricher'. The default setting is <code>true</code>.
34   */
35  public class MessagePropertiesTransformer extends AbstractMessageAwareTransformer
36  {
37      private List deleteProperties = null;
38      private Map addProperties = null;
39      /** the properties map containing rename mappings for message properties */
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      // @Override
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         /* perform renaming transformation */
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                         /* log transformation */
148                         if (logger.isDebugEnabled() && !propertyNames.contains(key))
149                         {
150                             logger.debug("renaming message property " + key + " to " + value);
151                         }
152 
153                         /*
154                          * store current value of the property. then remove key and
155                          * store value under new key
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      * @return the renameProperties
189      */
190     public Map getRenameProperties()
191     {
192         return this.renameProperties;
193     }
194 
195     /**
196      * @param renameProperties the renameProperties to set
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 }