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.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      {
32          this.endpoint = endpoint;
33      }
34  
35      public MuleEvent process(MuleEvent event) throws MessagingException
36      {
37          String endpointMimeType = endpoint.getMimeType();
38          if (endpointMimeType != null)
39          {
40              MuleMessage message = event.getMessage();
41              String contentType = message.getProperty(MuleProperties.CONTENT_TYPE_PROPERTY, PropertyScope.OUTBOUND);
42              if (contentType == null)
43              {
44                  message.setProperty(MuleProperties.CONTENT_TYPE_PROPERTY, endpointMimeType, PropertyScope.OUTBOUND);
45              }
46              else
47              {
48                  try
49                  {
50                      MimeType mt = new MimeType(contentType);
51                      String messageMimeType = mt.getPrimaryType() + "/" + mt.getSubType();
52                      if (!messageMimeType.equals(endpointMimeType))
53                      {
54                          throw new MessagingException(
55                              CoreMessages.unexpectedMIMEType(messageMimeType, endpointMimeType), event);
56                      }
57                  }
58                  catch (MimeTypeParseException ex)
59                  {
60                      throw new MessagingException(CoreMessages.illegalMIMEType(contentType), event, ex);
61                  }
62              }
63          }
64  
65          return event;
66      }
67      
68      @Override
69      public String toString()
70      {
71          return ObjectUtils.toString(this);
72      }
73  }