View Javadoc

1   /*
2    * $Id: DefaultMuleMessageDTO.java 19191 2010-08-25 21:05:23Z tcarlson $
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  package org.mule.message;
11  
12  import org.mule.DefaultMuleMessage;
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.transport.PropertyScope;
16  
17  import java.io.Serializable;
18  
19  /**
20   * A data transfer object representation of a {@link org.mule.api.MuleMessage}. THis object is used when ecoding Mule messages
21   * over the wire using XML, JSON or other serialization.
22   */
23  public class DefaultMuleMessageDTO extends BaseMessageDTO
24  {
25      private String replyTo;
26  
27      public DefaultMuleMessageDTO()
28      {
29          super();
30      }
31  
32      public DefaultMuleMessageDTO(Serializable message)
33      {
34          super(message);
35      }
36  
37      public DefaultMuleMessageDTO(MuleMessage message)
38      {
39          super(message.getPayload());
40          encodePropertiesForScope(PropertyScope.INBOUND, message);
41          encodePropertiesForScope(PropertyScope.OUTBOUND, message);
42          encodePropertiesForScope(PropertyScope.INVOCATION, message);
43          encodePropertiesForScope(PropertyScope.SESSION, message);
44          if(message.getReplyTo()!=null)
45          {
46              setReplyTo(message.getReplyTo().toString());
47          }
48      }
49  
50      protected void encodePropertiesForScope(PropertyScope scope, MuleMessage message)
51      {
52          for (String key : message.getPropertyNames(scope))
53          {
54              setProperty(String.format("%s#%s", scope.getScopeName(), key), message.getProperty(key, scope));
55          }
56      }
57  
58      public String getReplyTo()
59      {
60          return replyTo;
61      }
62  
63      public void setReplyTo(String replyTo)
64      {
65          this.replyTo = replyTo;
66      }
67  
68      public Object getData()
69      {
70          return getPayload();
71      }
72  
73      public void setData(Object data)
74      {
75          this.setPayload(data);
76      }
77  
78      public void addPropertiesTo(MuleMessage message)
79      {
80          for (String s : properties.keySet())
81          {
82              int i = s.indexOf("#");
83              String prefix = s.substring(0, i);
84              if (prefix.equals(PropertyScope.OUTBOUND.getScopeName()))
85              {
86                  message.setOutboundProperty(s.substring(i + 1), getProperty(s));
87              }
88              else if (prefix.equals(PropertyScope.SESSION.getScopeName()))
89              {
90                  message.setProperty(s.substring(i + 1), getProperty(s), PropertyScope.SESSION);
91              }
92              else
93              {
94                  message.setInvocationProperty(s.substring(i + 1), getProperty(s));
95              }
96          }
97          message.setReplyTo(getReplyTo());
98      }
99      
100     public MuleMessage toMuleMessage(MuleContext context)
101     {
102         MuleMessage message = new DefaultMuleMessage(getPayload(), context);
103         addPropertiesTo(message);
104         return message;
105     }
106 }