1
votes

How to Handle this exception? Mule throw exception without executing exception flow in case I sent wrong encoding value (UTF-88) in Content-Type.

WARN 2017-12-22 10:19:55,733 [[2017-mule_api_registry-uni_auth_app].HttpConnectoConfig.worker.02] org.mule.module.http.internal.listener.DefaultHttpListener: Exception occurred parsing request: java.nio.charset.UnsupportedCharsetException: UTF-88 at java.nio.charset.Charset.forName(Charset.java:531) ~[?:1.8.0_111] at org.mule.transformer.types.SimpleDataType.setEncoding(SimpleDataType.java:89) ~[mule-core-3.9.0.jar:3.9.0] at org.mule.DefaultMuleMessage.updateDataTypeWithProperty(DefaultMuleMessage.java:551) ~[mule-core-3.9.0.jar:3.9.0] at org.mule.DefaultMuleMessage.setProperty(DefaultMuleMessage.java:502) ~[mule-core-3.9.0.jar:3.9.0] at org.mule.DefaultMuleMessage.setProperty(DefaultMuleMessage.java:494) ~[mule-core-3.9.0.jar:3.9.0] at org.mule.DefaultMuleMessage.addProperties(DefaultMuleMessage.java:1398) ~[mule-core-3.9.0.jar:3.9.0] at org.mule.DefaultMuleMessage.(DefaultMuleMessage.java:198) ~[mule-core-3.9.0.jar:3.9.0] at org.mule.DefaultMuleMessage.(DefaultMuleMessage.java:175) ~[mule-core-3.9.0.jar:3.9.0] at org.mule.module.http.internal.listener.HttpRequestToMuleEvent.transform(HttpRequestToMuleEvent.java:128) ~[mule-module-http-3.9.0.jar:3.9.0] at org.mule.module.http.internal.listener.DefaultHttpListener.createEvent(DefaultHttpListener.java:187) ~[mule-module-http-3.9.0.jar:3.9.0] at org.mule.module.http.internal.listener.DefaultHttpListener.access$000(DefaultHttpListener.java:48) ~[mule-module-http-3.9.0.jar:3.9.0] at org.mule.module.http.internal.listener.DefaultHttpListener$1.handleRequest(DefaultHttpListener.java:133) ~[mule-module-http-3.9.0.jar:3.9.0] at org.mule.module.http.internal.listener.grizzly.GrizzlyRequestDispatcherFilter.handleRead(GrizzlyRequestDispatcherFilter.java:100) ~[mule-module-http-3.9.0.jar:3.9.0] at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) ~[grizzly-framework-2.3.33.jar:2.3.33] at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284) ~[grizzly-framework-2.3.33.jar:2.3.33] at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201) ~[grizzly-framework-2.3.33.jar:2.3.33] at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133) ~[grizzly-framework-2.3.33.jar:2.3.33] at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) ~[grizzly-framework-2.3.33.jar:2.3.33] at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) ~[grizzly-framework-2.3.33.jar:2.3.33] at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539) ~[grizzly-framework-2.3.33.jar:2.3.33] at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) ~[grizzly-framework-2.3.33.jar:2.3.33] at org.mule.module.http.internal.listener.grizzly.ExecutorPerServerAddressIOStrategy.run0(ExecutorPerServerAddressIOStrategy.java:119) ~[mule-module-http-3.9.0.jar:3.9.0] at org.mule.module.http.internal.listener.grizzly.ExecutorPerServerAddressIOStrategy.access$100(ExecutorPerServerAddressIOStrategy.java:31) ~[mule-module-http-3.9.0.jar:3.9.0] at org.mule.module.http.internal.listener.grizzly.ExecutorPerServerAddressIOStrategy$WorkerThreadRunnable.run(ExecutorPerServerAddressIOStrategy.java:142) ~[mule-module-http-3.9.0.jar:3.9.0] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_111] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_111] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_111]

2

2 Answers

0
votes

1.Use a MEL expression in each element to test the Content-Type header:

#[message.inboundProperties['Content-Type'].contains('UTF-8')]

2.Use the element to act as the default statement of a switch: throw your exception from there.

eg :

 <choice>
     <when expression="#[message.inboundProperties['Content-Type'].contains('??????? your required content type ??????')]">
           <logger message="when number one invoked" level="WARN"/>
      </when>
      <otherwise>
           <logger message="otherwise invoked" level="WARN"/>
      </otherwise>
 </choice>
0
votes

In mule core, there is a class org.mule.transformer.types.SimpleDataType.java which is responsible for this exception. The method setEncoding checks the validity of the charset and for invalid charset, Charset.forname method throws UnsupportedCharsetException. To avoid this you can modify the code block like this-

public void setEncoding(String encoding)
{
    if (!StringUtils.isEmpty(encoding))
    {
        try {
            // Checks that the encoding is valid and supported
            Charset.forName(encoding);
        }catch(UnsupportedCharsetException e) {
            encoding = "UTF-8";
        }
    }

    this.encoding = encoding;
}

You have to set a default charset, otherwise you can not get the payload from the MuleMessage instance like-

message.getPayloadAsString();

In my case, I set it 'UTF-8'. You can set it based on your need.