View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleContext;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.transport.MessageTypeNotSupportedException;
13  import org.mule.api.transport.MuleMessageFactory;
14  
15  public abstract class AbstractMuleMessageFactory implements MuleMessageFactory
16  {
17      protected MuleContext muleContext;
18  
19      public AbstractMuleMessageFactory(MuleContext context)
20      {
21          super();
22          muleContext = context;
23      }
24  
25      public MuleMessage create(Object transportMessage, String encoding) throws Exception
26      {
27          return create(transportMessage, null, encoding);
28      }
29      
30      public MuleMessage create(Object transportMessage, MuleMessage previousMessage, String encoding) 
31          throws Exception
32      {
33          if (transportMessage == null)
34          {
35              return new DefaultMuleMessage(NullPayload.getInstance(), muleContext);
36          }
37  
38          if (!isTransportMessageTypeSupported(transportMessage))
39          {
40              throw new MessageTypeNotSupportedException(transportMessage, getClass());
41          }
42  
43          Object payload = extractPayload(transportMessage, encoding);
44          DefaultMuleMessage message;
45          if (previousMessage != null)
46          {
47              message = new DefaultMuleMessage(payload, previousMessage, muleContext);
48          }
49          else
50          {
51              message = new DefaultMuleMessage(payload, muleContext);
52          }
53  
54          message.setEncoding(encoding);
55          addProperties(message, transportMessage);
56          addAttachments(message, transportMessage);
57          return message;
58      }
59  
60      protected abstract Class<?>[] getSupportedTransportMessageTypes();
61  
62      protected abstract Object extractPayload(Object transportMessage, String encoding) throws Exception;
63  
64      protected void addProperties(DefaultMuleMessage message, Object transportMessage) throws Exception
65      {
66          // Template method
67      }
68  
69      protected void addAttachments(DefaultMuleMessage message, Object transportMessage) throws Exception
70      {
71          // Template method
72      }
73  
74      private boolean isTransportMessageTypeSupported(Object transportMessage)
75      {
76          Class<?> transportMessageType = transportMessage.getClass();
77          boolean match = false;
78          for (Class<?> type : getSupportedTransportMessageTypes())
79          {
80              if (type.isAssignableFrom(transportMessageType))
81              {
82                  match = true;
83                  break;
84              }
85          }
86          return match;
87      }
88  }