1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
package org.mule.endpoint.inbound; |
13 | |
|
14 | |
import org.mule.api.MessagingException; |
15 | |
import org.mule.api.MuleEvent; |
16 | |
import org.mule.api.MuleMessage; |
17 | |
import org.mule.api.config.MuleProperties; |
18 | |
import org.mule.api.endpoint.InboundEndpoint; |
19 | |
import org.mule.api.processor.MessageProcessor; |
20 | |
import org.mule.api.transport.PropertyScope; |
21 | |
import org.mule.config.i18n.CoreMessages; |
22 | |
import org.mule.util.ObjectUtils; |
23 | |
|
24 | |
import javax.activation.MimeType; |
25 | |
import javax.activation.MimeTypeParseException; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class InboundEndpointMimeTypeCheckingMessageProcessor implements MessageProcessor |
31 | |
{ |
32 | |
private InboundEndpoint endpoint; |
33 | |
|
34 | |
public InboundEndpointMimeTypeCheckingMessageProcessor(InboundEndpoint endpoint) |
35 | 0 | { |
36 | 0 | this.endpoint = endpoint; |
37 | 0 | } |
38 | |
|
39 | |
public MuleEvent process(MuleEvent event) throws MessagingException |
40 | |
{ |
41 | 0 | String endpointMimeType = endpoint.getMimeType(); |
42 | 0 | if (endpointMimeType != null) |
43 | |
{ |
44 | 0 | MuleMessage message = event.getMessage(); |
45 | 0 | String contentType = message.getProperty(MuleProperties.CONTENT_TYPE_PROPERTY, PropertyScope.INBOUND); |
46 | 0 | if (contentType == null) |
47 | |
{ |
48 | 0 | contentType = message.getProperty(MuleProperties.CONTENT_TYPE_PROPERTY, PropertyScope.OUTBOUND); |
49 | |
} |
50 | 0 | if (contentType == null) |
51 | |
{ |
52 | 0 | message.setProperty(MuleProperties.CONTENT_TYPE_PROPERTY, endpointMimeType, PropertyScope.INBOUND); |
53 | |
} |
54 | |
else |
55 | |
{ |
56 | |
try |
57 | |
{ |
58 | 0 | MimeType mt = new MimeType(contentType); |
59 | 0 | String messageMimeType = mt.getPrimaryType() + "/" + mt.getSubType(); |
60 | 0 | if (!messageMimeType.equals(endpointMimeType)) |
61 | |
{ |
62 | 0 | throw new MessagingException( |
63 | |
CoreMessages.unexpectedMIMEType(messageMimeType, endpointMimeType), event); |
64 | |
} |
65 | |
} |
66 | 0 | catch (MimeTypeParseException ex) |
67 | |
{ |
68 | 0 | throw new MessagingException(CoreMessages.illegalMIMEType(contentType), event, ex); |
69 | 0 | } |
70 | |
} |
71 | |
} |
72 | |
|
73 | 0 | return event; |
74 | |
} |
75 | |
|
76 | |
@Override |
77 | |
public String toString() |
78 | |
{ |
79 | 0 | return ObjectUtils.toString(this); |
80 | |
} |
81 | |
} |