Malinda Peiris
Malinda Peiris

Reputation: 614

In WSO2 ESB is validate XML before coming to the api

Is there a way to validate XML coming to the WSO2 ESB API since in order to validate.

I'm getting an error when the wrong XML comes to my API. This is the error I'm getting when it comes to the API I want to validate before that.

[2018-10-19 10:00:03,531] ERROR - LogMediator Could not build full log message: com.ctc.wstx.exc.WstxParsingException: Unexpected close tag ; expected .

Sending XML

<Request>
    <DeleteServiceRequest> 
       <ServiceLineId>12344455</ServiceLineId> 

</Request> 

Header of the API

<?xml version="1.0" encoding="UTF-8"?>
<api context="/test" name="testAPI" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>

Upvotes: 1

Views: 430

Answers (1)

ophychius
ophychius

Reputation: 2653

The problem is that to validate the message, you need to build it first. The message you receive is not well-formed xml, therefore the first time the API tries to build the message it will fail. However to validate the XML building of the message is also required and as such xml validation mediator will also fail.

So no, you cannot validate the XML when the message you receive is not correct XML. (technically it is not XML). Generally this is when you send back an error to the client. And since they make a technical error calling the API, you can just serve them with the actual error you are getting so that they know what to fix.

You can use the following properties to get information about your error, and then construct a fault message with this information to send back to the client. For example, the following fault sequence will log error details and send a simple error message back to the client.

      <faultSequence>
         <log level="custom">
            <property name="text" value="An unexpected error occured"/>
            <property expression="get-property('ERROR_MESSAGE')" name="message"/>
            <property expression="get-property('ERROR_DETAIL')" name="detail"/>
            <property expression="get-property('ERROR_CODE')" name="code"/>
            <property expression="get-property('ERROR_DETAIL')" name="detail"/>
         </log>
         <payloadFactory media-type="xml">
            <format>
               <ERROR xmlns="">
                  <MESSAGE>You broke it</MESSAGE>
                  <DETAIL>$1</DETAIL>
               </ERROR>
            </format>
            <args>
               <arg evaluator="xml" expression="get-property('ERROR_MESSAGE')"/>
            </args>
         </payloadFactory>
         <respond/>
      </faultSequence>

You might also want to set the http status code to a proper value before returning the message, for example:

<property name="HTTP_SC" value="500" scope="axis2"/>

Upvotes: 3

Related Questions