Java Reference
In-Depth Information
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
return doc;
}
The createSoapMessage method serves to accept the content of the CreditCard object as
a DOM document and add it as the only child payload of the SOAP message you construct.
Once you have a complete SOAP message, you pass that to the invoke method, which will set
up the mechanism to actually call the service.
You build QName s that match the port and the service as defined in the specified WSDL, and
then create a service, adding the known port to it. You specify SOAP 1.1 as the protocol the
service will be bound to. Finally, the dispatch is created with a type parameter of SOAP mes-
sage; you will pass it your constructed SOAP message as the request, and it will return a value
of type SOAPMessage as the response that will contain the authorization XML.
The marshal and unmarshal methods are mirror images of one another, with the marshal hand-
ling Java to XML and the unmarshal handling XML to Java. So, in addition to validating out-
going requests, you can use the same technique to validate response payloads in the unmar-
shal method. However, you have to be careful doing so because if your response is actually a
SOAP fault instead of a poorly formed authorization, you will get JAXB errors because it will
find a child it was not expecting in the SOAP body.
If you invoke the service with data that is not valid given the schema constraints, you get a
SAXParseException that has the following message:
org.xml.sax.SAXParseException: cvc-pattern-valid:
Value '4' is not facet-valid with respect to pattern '\d{16}' for type
'CardNumber'.
You've tried to invoke the service with a credit card number of 4 digits, but the constraint dic-
tates that you must pass 16 digits for it to be valid.
This is a portable and flexible way to work with your objects for validation against a schema.
It is relatively straightforward to modify this solution to make it a more general case to handle
any kind of request and response. That is, here you have hardcoded the schema location, pack-
age name, class names, and QName s for port and service into the methods. These could eas-
ily be passed into the method to create a schema validation handler that would work for any
schema, object, and service.
Search WWH ::




Custom Search