Mule ESB CXF异常处理

时间:2012-11-08 09:25:09

标签: exception cxf mule fuseesb jbossfuse

我在http出站流中使用jaxws-client在mule通信中使用以下XML(POJO是出站Jaxws客户端的输出)。

为了确保在maven中使用wsdl2java插件生成WS代码,生成的代码具有来自WS的异常类,该类是:

com.nsn.ossbss.charge_once.wsdl.entity.tis.wsdl._1.TisException

当我们在WS引发此异常后获得SOAPFault时,确保http响应为500

com.nsn.ossbss.charge_once.wsdl.entity.tis.wsdl._1.TisException

INFO  2012-11-08 11:08:08,008 [[cxf].TCP_C_L.receiver.02] org.mule.transport.http.HttpClientMessageDispatcher: Received a redirect, but followRedirects=false. Response code: 500 Server Error
ERROR 2012-11-08 11:08:13,008 [[cxf].TCP_C_L.receiver.02] org.mule.exception.CatchMessagingExceptionStrategy: 
********************************************************************************
Message               : error. Failed to route event via endpoint: org.mule.module.cxf.CxfOutboundMessageProcessor. Message payload is of type: PostMethod
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. error (com.nsn.ossbss.charge_once.wsdl.entity.tis.wsdl._1.TisException)
  sun.reflect.NativeConstructorAccessorImpl:-2 (null)
2. error. Failed to route event via endpoint: org.mule.module.cxf.CxfOutboundMessageProcessor. Message payload is of type: PostMethod (org.mule.api.transport.DispatchException)
  org.mule.module.cxf.CxfOutboundMessageProcessor:148 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transport/DispatchException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
com.nsn.ossbss.charge_once.wsdl.entity.tis.wsdl._1.TisException: error
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

INFO  2012-11-08 11:08:13,009 [[cxf].TCP_C_L.receiver.02] org.mule.api.processor.LoggerMessageProcessor: org.apache.commons.httpclient.methods.PostMethod@d02649
INFO  2012-11-08 11:08:13,010 [[cxf].TCP_C_L.receiver.02] org.mule.api.processor.LoggerMessageProcessor: org.apache.commons.httpclient.methods.PostMethod@d02649

我在使用以下方式处理激活时遇到上述错误:

<catch-exception-strategy>
            <logger message="#[payload]" level="INFO" doc:name="Logger" />
            <transformer ref="ByteArrayToString" />
            <append-string-transformer message="&#xD;&#xA;" />
            <logger message="#[payload]" level="INFO" doc:name="Logger" />
        </catch-exception-strategy>

肯定我需要处理与apache camel相同的异常,我需要指定mule异常并且mule不应该在控制台中引发上述异常,因为它将由XML配置处理。

来自jaxWs-client outbound的传入消息的类型是

org.apache.commons.httpclient.methods.PostMethod@d02649

我如何设法让Mule ESB理解WS异常,我试过这个

并且它也没有用。

你能告诉我。

在Fuse ESB中我做到了这样:

<onException>
<exception>com.nsn.ossbss.charge_once.wsdl.entity.tis.wsdl._1.TisException</exception>  
            <handled>
                <constant>true</constant>
            </handled>
            <to uri="log:test" />
            <setBody>

                <simple>${exception.getFaultInfo}</simple>

            </setBody>
            <to uri="log:exception" />
            <convertBodyTo      type="com.nsn.ossbss.charge_once.wsdl.entity.tis.xsd._1.FaultMessage" />
            <marshal ref="decExc" />
            <convertBodyTo type="String" />
            <bean ref="nulladd" />
        </onException>

我怎样才能在Mule ESB中做同样的事情。

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:file="http://www.mulesoft.org/schema/mule/file"
    xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:tcp="http://www.mulesoft.org/schema/mule/tcp"
    xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="EE-3.3.0"
    xmlns:script="http://www.mulesoft.org/schema/mule/scripting"
    xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd 
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd 
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd 
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/3.2/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/tcp http://www.mulesoft.org/schema/mule/tcp/current/mule-tcp.xsd 
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd ">
    <spring:beans>
        <spring:bean name="samlCallbackHandler"
            class="com.mulesoft.mule.example.security.SAMLCallbackHandler" id="samlCallbackHandler" />
    </spring:beans>




    <sub-flow name="Jaxws" doc:name="Jaxws">
        <cxf:jaxws-client operation="ExecuteCommand"
            serviceClass="com.nsn.ossbss.charge_once.wsdl.entity.tis.wsdl._1.TisInterface"
            doc:name="UsernameToken SOAP client">
            <!-- <cxf:ws-security> <cxf:ws-config> <cxf:property key="action" value="UsernameToken 
                Timestamp" /> <cxf:property key="user" value="joe" /> <cxf:property key="passwordCallbackClass" 
                value="com.mulesoft.mule.example.security.PasswordCallback" /> <cxf:property 
                key="mustUnderstand" value="false" /> </cxf:ws-config> </cxf:ws-security> -->
        </cxf:jaxws-client>
    </sub-flow>

    <sub-flow name="usernameToken1" doc:name="usernameToken1">
        <!-- <cxf:jaxws-client operation="ExecuteCommand" serviceClass="com.nsn.ossbss.charge_once.wsdl.entity.tis.wsdl._1.TisInterface" 
            doc:name="UsernameToken SOAP client"> <cxf:ws-security> <cxf:ws-config> <cxf:property 
            key="action" value="UsernameToken Timestamp" /> <cxf:property key="user" 
            value="joe" /> <cxf:property key="passwordCallbackClass" value="com.mulesoft.mule.example.security.PasswordCallback" 
            /> <cxf:property key="mustUnderstand" value="false" /> </cxf:ws-config> </cxf:ws-security> 
            </cxf:jaxws-client> -->
        <http:outbound-endpoint exchange-pattern="request-response"
            host="localhost" port="8899" path="cxf/services"
            doc:name="Invoke NSNusernameToken Web Service">
            <cxf:jaxws-client operation="ExecuteCommand"
                serviceClass="com.nsn.ossbss.charge_once.wsdl.entity.tis.wsdl._1.TisInterface"
                doc:name="UsernameToken SOAP client">
                <cxf:ws-security>
                    <cxf:ws-config>
                        <cxf:property key="action" value="UsernameToken Timestamp" />
                        <cxf:property key="user" value="joe" />
                        <cxf:property key="passwordCallbackClass"
                            value="com.mulesoft.mule.example.security.PasswordCallback" />
                        <cxf:property key="mustUnderstand" value="false" />
                    </cxf:ws-config>
                </cxf:ws-security>
                <cxf:inInterceptors />
                <cxf:outInterceptors />



            </cxf:jaxws-client>




        </http:outbound-endpoint>





        <logger message="#[payload]" level="INFO" doc:name="Logger" />

        <mulexml:jaxb-object-to-xml-transformer
            jaxbContext-ref="xmltoobj" />
        <object-to-string-transformer />
        <append-string-transformer message="&#xD;&#xA;" />

        <logger message="#[payload]" level="INFO" doc:name="Logger" />

    </sub-flow>
    <object-to-string-transformer name="ObjectToString" />
    <byte-array-to-string-transformer
        name="ByteArrayToString" />



    <tcp:connector name="TCP_C_L" validateConnections="false"
        receiveBufferSize="102400" sendBufferSize="102400" doc:name="TCP">
        <tcp:xml-protocol />
    </tcp:connector>



    <mulexml:jaxb-context name="myJaxbContext"
        packageNames="com.nsn.ossbss.charge_once.wsdl.entity.tis.xsd._1" />





    <mulexml:jaxb-context name="xmltoobj"
        packageNames="com.nsn.ossbss.charge_once.wsdl.entity.tis.xsd._1" />


    <flow name="NSNServiceClient" doc:name="NSN IN Unified Client Service">
        <tcp:inbound-endpoint host="localhost" port="51000"
            connector-ref="TCP_C_L" />
        <transformer ref="ByteArrayToString" />

        <logger message="#[payload]" level="INFO" doc:name="Logger" />
        <mulexml:jaxb-xml-to-object-transformer
            jaxbContext-ref="myJaxbContext" />

        <flow-ref name="usernameToken1" doc:name="usernameToken1" />

        <logger message="#[payload]" level="INFO" doc:name="Logger" />

        <!-- <default-exception-strategy> <choice> 
        <when evaluator="groovy" expression='payload.getException().getClass().getName().equals("com.nsn.ossbss.charge_once.wsdl.entity.tis.wsdl._1.TisException")'> 
            <logger message="#[payload]" level="INFO" doc:name="Logger" /> <object-to-string-transformer/> 
            <append-string-transformer message="&#xD;&#xA;" /> <vm:outbound-endpoint 
            path="error.notification"/> </when> </choice> </default-exception-strategy> -->



        <catch-exception-strategy>

            <logger message="#[payload]" level="INFO" doc:name="Logger" />

            <transformer ref="ByteArrayToString" />


            <append-string-transformer message="&#xD;&#xA;" />
            <logger message="#[payload]" level="INFO" doc:name="Logger" />




        </catch-exception-strategy>




        <!-- <auto-transformer returnClass="com.nsn.ossbss.charge_once.wsdl.entity.tis.wsdl._1.TisException"/> -->






        <!-- <custom-exception-strategy class="com.nsn.ossbss.charge_once.wsdl.entity.tis.wsdl._1.TisException"> 
            <logger message="#[payload]" level="INFO" doc:name="Logger" /> </custom-exception-strategy> -->

    </flow>




    <flow name="SecurityClients" doc:name="SecurityClients">
        <http:inbound-endpoint exchange-pattern="request-response"
            host="localhost" port="63080" path="client" doc:name="HTTP Inbound Endpoint" />
        <set-payload
            value="#[message.inboundProperties['http.query.params']['name']]"
            doc:name="Set payload with 'name' query param" />
        <set-variable variableName="clientType"
            value="#[message.inboundProperties['http.query.params']['clientType']]"
            doc:name="Set clientType" />
        <choice doc:name="Choice">
            <when expression="#[clientType == 'unsecure']">
                <processor-chain>
                    <flow-ref name="unsecure" doc:name="Invoke unsecure sub-flow" />
                </processor-chain>
            </when>
            <!--<when expression="#[clientType == 'usernameToken']"> <processor-chain> 
                <flow-ref name="usernameToken1" doc:name="usernameToken1" /> </processor-chain> 
                </when> -->
            <when expression="#[clientType == 'usernameTokenSigned']">
                <processor-chain>
                    <flow-ref name="usernameTokenSigned" doc:name="Invoke usernameToken Signed sub-flow" />
                </processor-chain>
            </when>
            <when expression="#[clientType == 'usernameTokenEncrypted']">
                <processor-chain>
                    <flow-ref name="usernameTokenEncrypted" doc:name="Invoke usernameToken Encrypted sub-flow" />
                </processor-chain>
            </when>
            <when expression="#[clientType == 'samlToken']">
                <processor-chain>
                    <flow-ref name="samlToken" doc:name="Invoke samlToken sub-flow" />
                </processor-chain>
            </when>
            <when expression="#[clientType == 'samlTokenSigned']">
                <processor-chain>
                    <flow-ref name="samlTokenSigned" doc:name="Invoke samlToken Signed sub-flow" />
                </processor-chain>
            </when>
            <otherwise>
                <processor-chain>
                    <set-payload value="Client type is not supported"
                        doc:name="Client type is not supported" />
                </processor-chain>
            </otherwise>
        </choice>
        <set-property propertyName="Content-Type" value="text/plain"
            doc:name="Set response Content-Type" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="There has been an Error processing the request"
                doc:name="Set Payload" />
            <set-property propertyName="Content-Type" value="text/plain"
                doc:name="Set response Content-Type" />
        </catch-exception-strategy>



    </flow>
    <sub-flow name="unsecure" doc:name="unsecure">
        <cxf:jaxws-client operation="greet"
            serviceClass="com.mulesoft.mule.example.security.Greeter" doc:name="Unsecure SOAP client"
            doc:description="Unsecure SOAP client" />
        <http:outbound-endpoint exchange-pattern="request-response"
            host="localhost" port="63081" path="services/unsecure" doc:name="Invoke unsecure Web Service" />
    </sub-flow>
    <!-- <sub-flow name="usernameToken" doc:name="usernameToken"> <cxf:jaxws-client 
        operation="greet" serviceClass="com.mulesoft.mule.example.security.Greeter" 
        doc:name="UsernameToken SOAP client"> <cxf:ws-security> <cxf:ws-config> <cxf:property 
        key="action" value="UsernameToken Timestamp"/> <cxf:property key="user" value="joe"/> 
        <cxf:property key="passwordCallbackClass" value="com.mulesoft.mule.example.security.PasswordCallback"/> 
        </cxf:ws-config> </cxf:ws-security> </cxf:jaxws-client> <http:outbound-endpoint 
        exchange-pattern="request-response" host="localhost" port="63081" path="services/username" 
        doc:name="Invoke usernameToken Web Service"/> </sub-flow> -->
    <sub-flow name="usernameTokenSigned" doc:name="usernameTokenSigned">
        <cxf:jaxws-client operation="greet"
            serviceClass="com.mulesoft.mule.example.security.Greeter" doc:name="UsernameToken Signed SOAP client">
            <cxf:ws-security>
                <cxf:ws-config>
                    <cxf:property key="action" value="UsernameToken Signature Timestamp" />
                    <cxf:property key="signaturePropFile" value="wssecurity.properties" />
                    <cxf:property key="user" value="joe" />
                    <cxf:property key="passwordCallbackClass"
                        value="com.mulesoft.mule.example.security.PasswordCallback" />
                </cxf:ws-config>
            </cxf:ws-security>
        </cxf:jaxws-client>
        <http:outbound-endpoint exchange-pattern="request-response"
            host="localhost" port="63081" path="services/signed"
            doc:name="Invoke usernameToken Signed Web Service" />
    </sub-flow>
    <sub-flow name="usernameTokenEncrypted" doc:name="usernameTokenEncrypted">
        <cxf:jaxws-client operation="greet"
            serviceClass="com.mulesoft.mule.example.security.Greeter" doc:name="UsernameToken Encrypted SOAP client">
            <cxf:ws-security>
                <cxf:ws-config>
                    <cxf:property key="action" value="UsernameToken Timestamp Encrypt" />
                    <cxf:property key="encryptionPropFile" value="wssecurity.properties" />
                    <cxf:property key="user" value="joe" />
                    <cxf:property key="encryptionUser" value="joe" />
                    <cxf:property key="passwordCallbackClass"
                        value="com.mulesoft.mule.example.security.PasswordCallback" />
                </cxf:ws-config>
            </cxf:ws-security>
        </cxf:jaxws-client>
        <http:outbound-endpoint exchange-pattern="request-response"
            host="localhost" port="63081" path="services/encrypted"
            doc:name="Invoke usernameToken Encrypted Web Service" />
    </sub-flow>
    <sub-flow name="samlToken" doc:name="samlToken">
        <cxf:jaxws-client operation="greet"
            serviceClass="com.mulesoft.mule.example.security.Greeter" doc:name="SAMLToken SOAP client">
            <cxf:ws-security>
                <cxf:ws-config>
                    <cxf:property key="action" value="SAMLTokenUnsigned Timestamp" />
                    <cxf:property key="samlPropFile" value="saml.properties" />
                    <cxf:property key="samlCallbackClass"
                        value="com.mulesoft.mule.example.security.SAMLCallbackHandler" />
                </cxf:ws-config>
            </cxf:ws-security>
        </cxf:jaxws-client>
        <http:outbound-endpoint exchange-pattern="request-response"
            host="localhost" port="63081" path="services/saml" doc:name="Invoke SAMLToken Web Service" />
    </sub-flow>
    <sub-flow name="samlTokenSigned" doc:name="samlTokenSigned">
        <cxf:jaxws-client operation="greet"
            serviceClass="com.mulesoft.mule.example.security.Greeter" doc:name="SAMLToken Signed SOAP client">
            <cxf:ws-security>
                <cxf:ws-config>
                    <cxf:property key="action" value="SAMLTokenSigned" />
                    <cxf:property key="samlPropFile" value="saml.properties" />
                    <cxf:property key="signatureKeyIdentifier" value="DirectReference" />
                    <cxf:property key="user" value="joe" />
                    <cxf:property key="passwordCallbackClass"
                        value="com.mulesoft.mule.example.security.PasswordCallback" />
                    <cxf:property key="samlCallbackClass"
                        value="com.mulesoft.mule.example.security.SAMLCallbackHandler" />
                </cxf:ws-config>
            </cxf:ws-security>
        </cxf:jaxws-client>
        <http:outbound-endpoint exchange-pattern="request-response"
            host="localhost" port="63081" path="services/signedsaml"
            doc:name="Invoke SAMLToken Signed Web Service" />
    </sub-flow>

</mule>

第一个子流usernameToken1正在调用我在apache camel上部署的Web服务,我刚刚在上面提到的异常类的问题中提到过。

请告诉我你的反馈,

1 个答案:

答案 0 :(得分:2)

您遇到的问题是,当Payload到达异常策略块时,消息已被jaxws-client使用,您无法再获取它。

您可以做的是让jaxws客户端位于出站端点之外(之前)。在出站端点之后,您将获得Soap消息。您可以仅通过检查http状态来检查它是否是例外。如果状态是错误,您可以将消息转换为字符串并将其存储在变量中。一旦消息在返回的路上到达客户端,它将解释Soap消息并抛出异常策略块将处理它的异常。在异常内部,您可以读取存储Soap Fault的变量,并随意执行任何操作。

我知道这是一个非常麻烦的解决方案,但它是我能想到的唯一一个。我之前说的问题是,HTTP从服务接收回流,然后在解组并抛出异常之前,jaxws客户端会使用该流。保留Soap Fault的唯一方法是每次Mule序列化输入,这可能非常昂贵。

HTH, EVA