View Javadoc

1   /*
2    * $Id: OutboundEndpointPropertyMessageProcessor.java 22156 2011-06-08 21:36:30Z 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.endpoint.outbound;
12  
13  import org.mule.OptimizedRequestContext;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.config.MuleProperties;
18  import org.mule.api.endpoint.OutboundEndpoint;
19  import org.mule.api.processor.MessageProcessor;
20  import org.mule.util.ObjectUtils;
21  
22  import java.util.Iterator;
23  
24  /**
25   * Sets the outbound endpoint uri on as a property of the message using the following key:
26   * {@link MuleProperties#MULE_ENDPOINT_PROPERTY}.
27   */
28  public class OutboundEndpointPropertyMessageProcessor implements MessageProcessor
29  {
30  
31      private String[] ignoredPropertyOverrides = new String[]{MuleProperties.MULE_METHOD_PROPERTY};
32  
33      private OutboundEndpoint endpoint;
34  
35      public OutboundEndpointPropertyMessageProcessor(OutboundEndpoint endpoint)
36      {
37          this.endpoint = endpoint;
38      }
39  
40      public MuleEvent process(MuleEvent event) throws MuleException
41      {
42          event.getMessage().setOutboundProperty(MuleProperties.MULE_ENDPOINT_PROPERTY,
43              endpoint.getEndpointURI().toString());
44  
45          if (endpoint.getProperties() != null)
46          {
47              for (Iterator<?> iterator = endpoint.getProperties().keySet().iterator(); iterator.hasNext();)
48              {
49                  String prop = (String) iterator.next();
50                  Object value = endpoint.getProperties().get(prop);
51                  // don't overwrite property on the message
52                  if (!ignoreProperty(event.getMessage(), prop))
53                  {
54                      // inbound endpoint properties are in the invocation scope
55                      event.getMessage().setInvocationProperty(prop, value);
56                  }
57              }
58          }
59          event = OptimizedRequestContext.unsafeSetEvent(event);
60          return event;
61      }
62  
63      protected boolean ignoreProperty(MuleMessage message, String key)
64      {
65          if (key == null)
66          {
67              return true;
68          }
69  
70          for (int i = 0; i < ignoredPropertyOverrides.length; i++)
71          {
72              if (key.equals(ignoredPropertyOverrides[i]))
73              {
74                  return false;
75              }
76          }
77  
78          return null != message.getOutboundProperty(key);
79      }
80  
81      @Override
82      public String toString()
83      {
84          return ObjectUtils.toString(this);
85      }
86  }