View Javadoc

1   /*
2    * $Id: MessagePropertiesTransformer.java 7976 2007-08-21 14:26:13Z 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  
11  package org.mule.transformers.simple;
12  
13  import org.mule.transformers.AbstractEventAwareTransformer;
14  import org.mule.umo.UMOEventContext;
15  import org.mule.umo.UMOMessage;
16  import org.mule.umo.transformer.TransformerException;
17  
18  import java.text.MessageFormat;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  /**
27   * A configurable message transformer that allows users to add, overwrite and delete
28   * properties on the current message. Users can set a {@link List} of
29   * 'deleteProperties' names to remove from the message and can also set a {@link Map}
30   * of 'addProperties' that will be added to the message and possibly overwrite
31   * existing properties with the same name.
32   * <p>
33   * If {@link #overwrite} is set to <code>false</code>, and a property exists on
34   * the message (even if the value is <code>null</code>, it will be left intact.
35   * The transformer then acts as a more gentle 'enricher'. The default setting is
36   * <code>true</code>.
37   */
38  public class MessagePropertiesTransformer extends AbstractEventAwareTransformer
39  {
40      private List deleteProperties = null;
41      private Map addProperties = null;
42      private boolean overwrite = true;
43  
44      public MessagePropertiesTransformer()
45      {
46          registerSourceType(Object.class);
47          setReturnClass(Object.class);
48      }
49  
50      // @Override
51      public Object clone() throws CloneNotSupportedException
52      {
53          MessagePropertiesTransformer clone = (MessagePropertiesTransformer) super.clone();
54  
55          if (deleteProperties != null)
56          {
57              clone.setDeleteProperties(new ArrayList(deleteProperties));
58          }
59  
60          if (addProperties != null)
61          {
62              clone.setAddProperties(new HashMap(addProperties));
63          }
64  
65          return clone;
66      }
67  
68      public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException
69      {
70          final UMOMessage message = context.getMessage();
71  
72          if (deleteProperties != null && deleteProperties.size() > 0)
73          {
74              for (Iterator iterator = deleteProperties.iterator(); iterator.hasNext();)
75              {
76                  message.removeProperty(iterator.next().toString());
77              }
78          }
79  
80          if (addProperties != null && addProperties.size() > 0)
81          {
82              final Set propertyNames = message.getPropertyNames();
83              for (Iterator iterator = addProperties.entrySet().iterator(); iterator.hasNext();)
84              {
85                  Map.Entry entry = (Map.Entry) iterator.next();
86                  if (entry.getKey() == null)
87                  {
88                      logger.error("Setting Null property keys is not supported, this entry is being ignored");
89                  }
90                  else
91                  {
92                      final String key = entry.getKey().toString();
93  
94                      final Object value = entry.getValue();
95                      if (overwrite)
96                      {
97                          if (logger.isDebugEnabled())
98                          {
99                              if (!propertyNames.contains(key))
100                             {
101                                 logger.debug("Overwriting message property " + key);
102                             }
103                         }
104                         message.setProperty(key, value);
105                     }
106                     else
107                     {
108                         if (propertyNames.contains(key))
109                         {
110                             if (logger.isDebugEnabled())
111                             {
112                                 logger.debug(MessageFormat.format(
113                                         "Message already contains the property and overwrite is false, skipping: key={0}, value={1}",
114                                         new Object[] {key, value}));
115                             }
116                         }
117                     }
118                 }
119             }
120         }
121 
122         return src;
123     }
124 
125     public List getDeleteProperties()
126     {
127         return deleteProperties;
128     }
129 
130     public void setDeleteProperties(List deleteProperties)
131     {
132         this.deleteProperties = deleteProperties;
133     }
134 
135     public Map getAddProperties()
136     {
137         return addProperties;
138     }
139 
140     public void setAddProperties(Map addProperties)
141     {
142         this.addProperties = addProperties;
143     }
144 
145     public boolean isOverwrite()
146     {
147         return overwrite;
148     }
149 
150     public void setOverwrite(final boolean overwrite)
151     {
152         this.overwrite = overwrite;
153     }
154 }