简体   繁体   中英

Error occurred in the mediation of the class mediator WSO2 ESB

I used wso2 esb 5.0 for create proxy services. I created proxy service using class mediator. Below is the java class.

public class CalculatePaymentAmount extends AbstractMediator {

    public boolean mediate(MessageContext messageContext) {

        String noOfMonths = messageContext.getEnvelope().getBody().getFirstElement().
                getFirstChildWithName(new QName("noOfMonths")).getText();

        String InsuranceRate = messageContext.getEnvelope().getBody().getFirstElement().
                getFirstChildWithName(new QName("InsuranceRate")).getText();

        DecimalFormat decimalFormat = new DecimalFormat("#.##");

        double totalAmount = Double.parseDouble(noOfMonths) * Double.parseDouble(InsuranceRate);

        messageContext.setProperty("noOfMonths", noOfMonths);
        messageContext.setProperty("paymentAmount", decimalFormat.format(totalAmount));

        return true;
    }

    public String getType() {
        return null;
    }

    public void setTraceState(int traceState) {
        traceState = 0;
    }

    public int getTraceState() {
        return 0;
    }

}

I created the proxy service using class mediator. Below define the proxy code.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="PaymentAmountProxy"
       startOnLoad="true"
       statistics="disable"
       trace="disable"
       transports="http,https">
   <target>
      <inSequence>
         <log/>
         <class name="com.mediator.java.CalculatePaymentAmount"/>
         <property expression="get-property('default','noOfMonths')"
                   name="getNoOfMonths"
                   scope="default"
                   type="STRING"/>
         <property expression="get-property('default','paymentAmount')"
                   name="getPaymentAmount"
                   scope="default"
                   type="STRING"/>
         <log>
            <property expression="get-property('default','getNoOfMonths')"
                      name="No.Of Months:"/>
            <property expression="get-property('default','getPaymentAmount')"
                      name="Paymrent Amount:"/>
         </log>
      </inSequence>
   </target>
   <description/>
</proxy>

This one working fine and gave expected response. But when I use this class mediator with other mediators, it getting errors when I invoked the proxy service. Below mentioned the proxy service that I used with class mediator.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="LatestLicenseRenewalSystem"
       startOnLoad="true"
       statistics="disable"
       trace="disable"
       transports="http,https">
   <target>
      <inSequence>
         <log/>
         <property expression="get-property('transport','VehicleNo')"
                   name="vehicleNo"
                   scope="default"
                   type="STRING"/>
         <log>
            <property expression="get-property('default','vehicleNo')" name="VehicleNo"/>
         </log>
         <payloadFactory media-type="xml">
            <format>
               <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                                 xmlns:wsa="http://www.w3.org/2005/08/addressing"
                                 xmlns:sam="http://sample.esb.org">
                  <soapenv:Header/>
                  <soapenv:Body>
                     <sam:getPolicyID>
                        <sam:vehicleNumber>$1</sam:vehicleNumber>
                     </sam:getPolicyID>
                  </soapenv:Body>
               </soapenv:Envelope>
            </format>
            <args>
               <arg evaluator="xml" expression="get-property('default','vehicleNo')"/>
            </args>
         </payloadFactory>
         <log level="full"/>
         <header name="Action" scope="default" value="urn:getCertificateID"/>
         <call>
            <endpoint>
               <address format="soap12"
                        uri="http://172.17.0.1:9763/services/EmissionTestService.EmissionTestServiceHttpSoap12Endpoint/">
                  <enableAddressing/>
               </address>
            </endpoint>
         </call>
         <log level="full"/>
         <property xmlns:ns="http://sample.esb.org"
                   expression="//ns:getCertificateIDResponse/ns:return"
                   name="certificateID"
                   scope="default"
                   type="STRING"/>
         <log>
            <property expression="get-property('default','certificateID')"
                      name="CertificateID"/>
         </log>
         <class name="com.mediator.java.CalculatePaymentAmount"/>
         <property expression="get-property('default','noOfMonths')"
                   name="getNoOfMonths"
                   scope="default"
                   type="STRING"/>
         <property expression="get-property('default','paymentAmount')"
                   name="getPaymentAmount"
                   scope="default"
                   type="STRING"/>
         <log>
            <property expression="get-property('default','getNoOfMonths')"
                      name="No.Of Months:"/>
            <property expression="get-property('default','getPaymentAmount')"
                      name="Paymrent Amount:"/>
         </log>
         <respond/>
      </inSequence>
   </target>
   <description/>
</proxy>

Below error is occurred when invoking the above proxy service.

LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: urn:getCertificateIDResponse, SOAPAction: urn:getCertificateIDResponse, ReplyTo: http://www.w3.org/2005/08/addressing/anonymous, MessageID: urn:uuid:6f4557eb-b8ff-4c19-bbe8-4c7e929d8386, Direction: request, MESSAGE = Executing default 'fault' sequence, ERROR_CODE = 0, ERROR_MESSAGE = Error occured in the mediation of the class mediator, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Action>urn:getCertificateIDResponse</wsa:Action><wsa:RelatesTo>urn:uuid:6f4557eb-b8ff-4c19-bbe8-4c7e929d8386</wsa:RelatesTo></soapenv:Header><soapenv:Body><ns:getCertificateIDResponse xmlns:ns="http://sample.esb.org"><ns:return>-1250719063</ns:return></ns:getCertificateIDResponse></soapenv:Body></soapenv:Envelope> 

Can anyone help me to solve this. Any help or workarounds are really appreciated.

Your class mediator is accessing the elements 'noOfMonths' and 'InsuranceRate' from the message context. But according to the error log, the message context is having a different soap envelope which does not have the above elements.

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Action>urn:getCertificateIDResponse</wsa:Action><wsa:RelatesTo>urn:uuid:6f4557eb-b8ff-4c19-bbe8-4c7e929d8386</wsa:RelatesTo></soapenv:Header><soapenv:Body><ns:getCertificateIDResponse xmlns:ns="http://sample.esb.org"><ns:return>-1250719063</ns:return></ns:getCertificateIDResponse></soapenv:Body></soapenv:Envelope> 

This must me the response received from the call operation before the class mediator.

You have to either isolate the class mediator from the call operation and use different proxy services or move the class mediator above the payload factory.

For solve this issue, use payloadFactory mediator before the class mediator and set the parameters for the payload. I mentioned below the code.

<payloadFactory media-type="xml">
            <format>
               <paymentDetails xmlns="">
                  <noOfMonths>$1</noOfMonths>
                  <InsuranceRate>$2</InsuranceRate>
               </paymentDetails>
            </format>
            <args>
               <arg evaluator="xml" expression="get-property('default','noOfMonths')"/>
               <arg evaluator="xml" expression="get-property('default','InsuranceRate')"/>
            </args>
         </payloadFactory>
         <class name="com.mediator.java.CalculatePaymentAmount"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM