Coverage Report - org.mule.endpoint.outbound.OutboundEndpointMimeTypeCheckingMessageProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
OutboundEndpointMimeTypeCheckingMessageProcessor
0%
0/18
0%
0/6
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.endpoint.outbound;
 8  
 
 9  
 import org.mule.api.MessagingException;
 10  
 import org.mule.api.MuleEvent;
 11  
 import org.mule.api.MuleMessage;
 12  
 import org.mule.api.config.MuleProperties;
 13  
 import org.mule.api.endpoint.OutboundEndpoint;
 14  
 import org.mule.api.processor.MessageProcessor;
 15  
 import org.mule.api.transport.PropertyScope;
 16  
 import org.mule.config.i18n.CoreMessages;
 17  
 import org.mule.util.ObjectUtils;
 18  
 
 19  
 import javax.activation.MimeType;
 20  
 import javax.activation.MimeTypeParseException;
 21  
 
 22  
 
 23  
 /**
 24  
  * Verify that the outbound mime type is acceptable by this endpoint.
 25  
  */
 26  
 public class OutboundEndpointMimeTypeCheckingMessageProcessor implements MessageProcessor
 27  
 {
 28  
     private OutboundEndpoint endpoint;
 29  
 
 30  
     public OutboundEndpointMimeTypeCheckingMessageProcessor(OutboundEndpoint endpoint)
 31  0
     {
 32  0
         this.endpoint = endpoint;
 33  0
     }
 34  
 
 35  
     public MuleEvent process(MuleEvent event) throws MessagingException
 36  
     {
 37  0
         String endpointMimeType = endpoint.getMimeType();
 38  0
         if (endpointMimeType != null)
 39  
         {
 40  0
             MuleMessage message = event.getMessage();
 41  0
             String contentType = message.getProperty(MuleProperties.CONTENT_TYPE_PROPERTY, PropertyScope.OUTBOUND);
 42  0
             if (contentType == null)
 43  
             {
 44  0
                 message.setProperty(MuleProperties.CONTENT_TYPE_PROPERTY, endpointMimeType, PropertyScope.OUTBOUND);
 45  
             }
 46  
             else
 47  
             {
 48  
                 try
 49  
                 {
 50  0
                     MimeType mt = new MimeType(contentType);
 51  0
                     String messageMimeType = mt.getPrimaryType() + "/" + mt.getSubType();
 52  0
                     if (!messageMimeType.equals(endpointMimeType))
 53  
                     {
 54  0
                         throw new MessagingException(
 55  
                             CoreMessages.unexpectedMIMEType(messageMimeType, endpointMimeType), event);
 56  
                     }
 57  
                 }
 58  0
                 catch (MimeTypeParseException ex)
 59  
                 {
 60  0
                     throw new MessagingException(CoreMessages.illegalMIMEType(contentType), event, ex);
 61  0
                 }
 62  
             }
 63  
         }
 64  
 
 65  0
         return event;
 66  
     }
 67  
     
 68  
     @Override
 69  
     public String toString()
 70  
     {
 71  0
         return ObjectUtils.toString(this);
 72  
     }
 73  
 }