Coverage Report - org.mule.transport.AbstractMuleMessageFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractMuleMessageFactory
0%
0/25
0%
0/10
0
 
 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  0
         super();
 22  0
         muleContext = context;
 23  0
     }
 24  
 
 25  
     public MuleMessage create(Object transportMessage, String encoding) throws Exception
 26  
     {
 27  0
         return create(transportMessage, null, encoding);
 28  
     }
 29  
     
 30  
     public MuleMessage create(Object transportMessage, MuleMessage previousMessage, String encoding) 
 31  
         throws Exception
 32  
     {
 33  0
         if (transportMessage == null)
 34  
         {
 35  0
             return new DefaultMuleMessage(NullPayload.getInstance(), muleContext);
 36  
         }
 37  
 
 38  0
         if (!isTransportMessageTypeSupported(transportMessage))
 39  
         {
 40  0
             throw new MessageTypeNotSupportedException(transportMessage, getClass());
 41  
         }
 42  
 
 43  0
         Object payload = extractPayload(transportMessage, encoding);
 44  
         DefaultMuleMessage message;
 45  0
         if (previousMessage != null)
 46  
         {
 47  0
             message = new DefaultMuleMessage(payload, previousMessage, muleContext);
 48  
         }
 49  
         else
 50  
         {
 51  0
             message = new DefaultMuleMessage(payload, muleContext);
 52  
         }
 53  
 
 54  0
         message.setEncoding(encoding);
 55  0
         addProperties(message, transportMessage);
 56  0
         addAttachments(message, transportMessage);
 57  0
         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  0
     }
 68  
 
 69  
     protected void addAttachments(DefaultMuleMessage message, Object transportMessage) throws Exception
 70  
     {
 71  
         // Template method
 72  0
     }
 73  
 
 74  
     private boolean isTransportMessageTypeSupported(Object transportMessage)
 75  
     {
 76  0
         Class<?> transportMessageType = transportMessage.getClass();
 77  0
         boolean match = false;
 78  0
         for (Class<?> type : getSupportedTransportMessageTypes())
 79  
         {
 80  0
             if (type.isAssignableFrom(transportMessageType))
 81  
             {
 82  0
                 match = true;
 83  0
                 break;
 84  
             }
 85  
         }
 86  0
         return match;
 87  
     }
 88  
 }