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