Mule4 — SoapKit Router. Validation of XML Against XSD
SoapKit router is a new addition from Mulesoft. The SoapKit router is for SOAP communication, which uses the WSDL file for endpoint definitions.
Join the DZone community and get the full member experience.
Join For FreeSoapKit router is a new addition from Mulesoft. The SoapKit router is for SOAP communication, which uses the WSDL file for endpoint definitions.
Create a Soap-Based Project in Mule 4
Step 1: Create a project
Step 2: Add the .wsdl file under src\main\resources\api\personService.WSDL location and right-click on the WSDL file and go to Mule -> Generate Flows from WSDL to generate flows based on the WSDL definitions.
Step 3: Before generating flow, it will confirm for Service, Port, and destination file (where the flow will generate).
Step 4: Before start implementing the flow, add a .xsd file under src\main\resources\xsd\personService.XSD location.
Step 5: Add flow to validate input soap message against XSD schema.
Step 6: Deploy the API and call the endpoint as like below. For valid payload, it will respond based on your implementation.
Step 7: For invalid input content, it will throw an error like this. Error payload will depend on its implementation.
Step 8: Code snippet. Here is the code repository for this, or you can find it below.
<mule xmlns:xml-module="http://www.mulesoft.org/schema/mule/xml-module" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:apikit-soap="http://www.mulesoft.org/schema/mule/apikit-soap" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.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/apikit-soap http://www.mulesoft.org/schema/mule/apikit-soap/current/mule-apikit-soap.xsd http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/xml-module http://www.mulesoft.org/schema/mule/xml-module/current/mule-xml-module.xsd">
<flow name="soapkit-https-listener-router">
<http:listener config-ref="api-httpListenerConfig" path="/PersonServiceImplService/PersonServiceImplPort" outputMimeType="application/xml" allowedMethods="POST">
<http:response statusCode="#[vars.httpStatus default 200]">
<http:headers><![CDATA[#[(vars.outboundHeaders default {}) ++
{'x-correlation-id': correlationId}]
]]></http:headers>
</http:response>
<http:error-response statusCode="#[vars.httpStatus default 500]">
<http:body><![CDATA[#[payload]]]></http:body>
<http:headers><![CDATA[#[(vars.outboundHeaders default {}) ++
{'x-correlation-id': correlationId}]
]]></http:headers>
</http:error-response>
</http:listener>
<apikit-soap:router doc:name="SOAP Kit Router" doc:id="6c1f2c12-5b5c-4e02-bf26-60b1b4939755"
config-ref="soap-config" outputMimeType="application/xml">
<ee:repeatable-file-store-stream bufferUnit="MB" />
<apikit-soap:message ><![CDATA[#[%dw 2.0
output application/xml
---
payload]]]></apikit-soap:message>
<apikit-soap:attributes><![CDATA[#[%dw 2.0
output application/java
---
{
headers: attributes.headers,
method: attributes.method,
queryString: attributes.queryString
}]]]></apikit-soap:attributes>
</apikit-soap:router>
<error-handler >
<on-error-propagate enableNotifications="true" logException="true" doc:name="On Error Propagate" doc:id="fcbf42b2-819d-4971-a9be-810f537c2095" type="XML-MODULE:SCHEMA_NOT_HONOURED">
<ee:transform doc:name="prepare Error Resaponse Message" doc:id="0c54d276-aaff-4286-9673-99680355f2f1" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/xml
ns soapenv http://schemas.xmlsoap.org/soap/envelope/
---
soapenv#Envelope : {
soapenv#Body : {
TYPE : error.errorType.identifier,
MESSAGE : error.description
}
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</on-error-propagate>
</error-handler>
</flow>
<flow name="requestedPersonInfo:\soap-config">
<flow-ref doc:name="validate-schema" name="validate-schema" doc:id="4760d834-fb49-4cbe-bc52-08a5b5b1fd52" />
<ee:transform doc:name="Prepare Response Message" doc:id="e85fceed-6d73-411d-bcd9-82ed62ca13d4">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/xml
ns ank http://ankuran.online/
---
ank#requestedPersonInfoResponse: {
return:{
id: uuid(),
message: "Hello " ++ payload.body.requestedPersonInfo.arg0.firstName ++ " " ++ payload.body.requestedPersonInfo.arg0.middleName ++ " " ++ payload.body.requestedPersonInfo.arg0.lastName ++ ", your datails are saved successfully."
}
}
]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<sub-flow name="validate-schema" doc:id="6acf23a0-1117-49ea-b5ee-bb11897466f6" >
<ee:transform doc:name="prepare input Message" doc:id="6ef6ff7e-fe7e-4387-b18c-213b6c6b5752">
<ee:message>
<ee:set-payload><![CDATA[payload
]]></ee:set-payload>
</ee:message>
<ee:variables >
<ee:set-variable variableName="requestedBody" ><![CDATA[%dw 2.0
output application/xml
---
payload.body]]></ee:set-variable>
</ee:variables>
</ee:transform>
<logger level="INFO" doc:name="Input Message" doc:id="61810594-b3ac-45f7-859e-81ddc3aef01b" message="Input Message : #[vars.requestedBody]"/>
<xml-module:validate-schema doc:name="Validate schema" doc:id="06ae8cf9-4cdb-4f54-8215-b91411253fc3" config-ref="XML_Config" schemas="xsd\personService.xsd">
<xml-module:content ><![CDATA[#[vars.requestedBody]]]></xml-module:content>
</xml-module:validate-schema>
</sub-flow>
</mule>
Opinions expressed by DZone contributors are their own.
Comments