Wednesday, December 29, 2010

XSD validation and exception handling in OSB

In OSB ( Oracle Service Bus , aqualogic service bus ) it is relative easy to add schema validation to your proxy service and to make a custom exception handling for this validation. Just follow the next steps. First in this example I use these xml schema's for the request and response operation.
The request xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://whitehorses.nl/request"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://whitehorses.nl/request"
 elementFormDefault="qualified"
 attributeFormDefault="unqualified">
 <xs:element name="request">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="runid" type="xs:short"/>
    <xs:element name="message" type="xs:string" minOccurs="0"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>
And the response xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://whitehorses.nl/response"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://whitehorses.nl/response"
 elementFormDefault="qualified"
 attributeFormDefault="unqualified">
 <xs:element name="response">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="runid" type="xs:short"/>
    <xs:element name="message" type="xs:string" minOccurs="0"/>
    <xs:element name="errorid" type="xs:string" minOccurs="0"/>
    <xs:element name="error" type="xs:anyType" minOccurs="0"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>
When everything goes well I will only return the runid element else we get the full message with the errorcode. With these xsd's I made a simple WSDL which I can use in the proxy service.

First part of this blog entry is to make the happy flow when this works we can add the xsd validation.
Make a new proxy service and use this WSDL.
 


The next step is to make a simple business service with file transport

In my case I put the request xml in the c drive temp folder.

Go back to your proxy service where we create a new message flow.
Add a route-node to the flow with inside a new routing. This routing will call the file business service

Add an assign component to request action flow so I can retrieve the runid from the request and add this to the runid variable. This variable I can use for the response.
                              To make a return message I add an assign to the response action. Now I add the response template xml to the body variable


Add an insert to the response action after the assign. In this insert we will add the runid of the request to the runid of the response message.
The first part is finished, you service should work now. In the second part we will add the XSD validation.
To make a custom exception handling for the xsd we need to add a Pipeline Pair node.

In the request pipeline I will add a new stage

In this stage I use the validation component. Now we have to select an element in the body variable which OSB will validate against the XSD. Provide the XSD and use the raise error option.

Add an error handler to this stage

First we add an assign to the stage to retrieve the runid of the request message so I can use this for the response.

Add a second assign to the stage. In this assign I will add the response template xml to the body variable

Add the runid to the response xml

Add the error details to the response xml

And the error code

With the reply we can give back the response xml back to the client. Very important report no error because this is a handled exception

The second part is also finished, we only have to test this proxy service by adding a unknow element to the request and look at the response. Here is a picture of the result.

No comments:

Post a Comment