View Javadoc

1   /*
2    * $Id: OutboundEndpointMimeTypeCheckingMessageProcessor.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.endpoint.outbound;
12  
13  import org.mule.api.MessagingException;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.config.MuleProperties;
17  import org.mule.api.endpoint.OutboundEndpoint;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.api.transport.PropertyScope;
20  import org.mule.config.i18n.CoreMessages;
21  import org.mule.util.ObjectUtils;
22  
23  import javax.activation.MimeType;
24  import javax.activation.MimeTypeParseException;
25  
26  
27  /**
28   * Verify that the outbound mime type is acceptable by this endpoint.
29   */
30  public class OutboundEndpointMimeTypeCheckingMessageProcessor implements MessageProcessor
31  {
32      private OutboundEndpoint endpoint;
33  
34      public OutboundEndpointMimeTypeCheckingMessageProcessor(OutboundEndpoint endpoint)
35      {
36          this.endpoint = endpoint;
37      }
38  
39      public MuleEvent process(MuleEvent event) throws MessagingException
40      {
41          String endpointMimeType = endpoint.getMimeType();
42          if (endpointMimeType != null)
43          {
44              MuleMessage message = event.getMessage();
45              String contentType = message.getProperty(MuleProperties.CONTENT_TYPE_PROPERTY, PropertyScope.OUTBOUND);
46              if (contentType == null)
47              {
48                  message.setProperty(MuleProperties.CONTENT_TYPE_PROPERTY, endpointMimeType, PropertyScope.OUTBOUND);
49              }
50              else
51              {
52                  try
53                  {
54                      MimeType mt = new MimeType(contentType);
55                      String messageMimeType = mt.getPrimaryType() + "/" + mt.getSubType();
56                      if (!messageMimeType.equals(endpointMimeType))
57                      {
58                          throw new MessagingException(
59                              CoreMessages.unexpectedMIMEType(messageMimeType, endpointMimeType), event);
60                      }
61                  }
62                  catch (MimeTypeParseException ex)
63                  {
64                      throw new MessagingException(CoreMessages.illegalMIMEType(contentType), event, ex);
65                  }
66              }
67          }
68  
69          return event;
70      }
71      
72      @Override
73      public String toString()
74      {
75          return ObjectUtils.toString(this);
76      }
77  }