Avatar billede fredand Forsker
23. juli 2018 - 16:54 Der er 4 kommentarer

How to validate a instance of a class generated from JaxP?

Hello Guys!
I'm more familar with XmlBeans how ever to day I thougt I could solve a enhancement-request we got; add validation to a webservice we got

In this case we have a WSDL with the XSD inline,
We generate the interface classes with jaxws-maven-plugin that results a with a jar with classes and the wsdl inside. (Correct me if I'm wrong when I say that JaxWS uses JaxP?)
The webservice returns a object of a class from this jar.
I would like to handle the validation errors on the server side so I do not think it is suitable to just use @Schemavalidation on the webservice. 

I now guess I need to:
1) extract the xsd from the wsdl into a javax.xml.validation.Schema
2) covert the xmlObject into a Node
3) perform the validation

My main headache is to understand how to solve number 1, do you guys got any clues?
Of course do you see any better solution?
Best regards
Fredrik
Avatar billede arne_v Ekspert
23. juli 2018 - 18:34 #1
I am not sure that I understand what you are trying to do.

You have a web service client calling a web service.

The web service return an object of a class.

Do you want to validate the object server side before returning it or do you want to validate it client side after receiving it?

And why? If the WSDL is generated then it should automatically match the class.

Anyway re q#1 then a WSDL is a form of XML, so you can read a WSDL file into DOM document and pick the types out with XPath.
Avatar billede fredand Forsker
23. juli 2018 - 20:35 #2
Hello Arne,
I would like to validate it on the server side before returning it.

We have experience that our server returns null in some cases for a element, for eg "b", see below.
In the wsdl it looks something like this (bare with me if I type it wrong)
<xsd:element name="a" minOccurs="0" />
<xsd:element name="b" />
<xsd:element name="c" minOccurs="0" />

By mistake the developer did not know that the default value for minOccurs is 1.

Now we are going through a integration bus called "DataPower", if I remember the name correct. The "bus" got validation-mode "on", and things started to go wrong. The integration team said that they could switch the validation-mode off, but I think it is better to change the wsdl to:
...
<xsd:element name="b" minOccurs="0" />
...
and also validate it on the server and perhaps some testcases.

In some how it looks like the webservice has been able to return objects that is not valid before we went through the DataPower-gizmo.
Do you got any suggestions or thoughts, Arne?
Best regards
Fredrik
Avatar billede arne_v Ekspert
24. juli 2018 - 19:24 #3
I think it is tricky.

You can obviously do:

boolean isvalid(Object obj, String xsd)
{
    String serobj = something.serialize(obj);
    return isvalid(serobj, xsd);
}
boolean isvalid(String serobj, String xsd) {
    try {
        Document serdoc = specialparse(serobj, xsd);
        return true;
    } catch(ParseException ex) {
        return false;
    }
}

But the questions is whether that is a valid test.

It is only a valid test if something.serialize produce the same XML as whatever the server is really using.
Avatar billede arne_v Ekspert
25. juli 2018 - 04:27 #4
A different approach would be to install a servlet filter that intercepts the web service calls and check the response, basically parse the XML for the object with validation against schema.

Obviously the options in case of an error are very limited.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester



IT-JOB