简体   繁体   中英

Java. Validate SOAP message against a WSDL

I need to validate a SOAP message against a WSDL ? (in the same way that an XML file can be validated against a XSD schema).

I am not calling any webservice; I just have a SOAP message and a WSDL , and I need to verify that the SOAP message is correct. without calling the webservice or whatsoever afterwards.

I need to make this validate within a Java program. Do you know of a small Java library to do this?

ps: I am aware that several JAX-WS libraries can validate the request/response when you call a webservice. But again, I am not calling any webservice; I have a simple SOAP message, and a WSDL , and I a need a function that validates the SOAP message against the WSDL .

ps: I am also aware that there tools that can do this, such as SOAPUI and XMLSpy . Again, I need to do this validation within my Java program.

ps: I am aware that I could extract the body part of the SOAP message, and validate it against the XSD . However, I'd like to validate the entire SOAP message against the WSDL .

You might look at the source code for the WsdlValidator class in the open-source soapUI project.

https://github.com/SmartBear/soapui

http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/support/wsdl/WsdlValidator.html

If you are creating a webservice client using a code generator based on Apache CXF or Apache Axis 2 , Chances are high that your webservice code should work just fine as long as both the webservice provider and your client are in the same version of SOAP/WS* standards .Based on the API you choose to invoke the webservice you can see whether the logging capability of that API can be used to print out the SOAP request generated .

Another approach could be to use a tool like SOAPUI. You could try to setup a mock webservice using SOAP UI based on the WSDL you have and then call test your webservice client by calling the mock service . See SOAP UI Link here http://www.soapui.org/Getting-Started/web-service-sample-project/1-Web-Service-Mocking.html

Try this

  1. Use the WSDL and create a Jax-RPC proxy
  2. Create a webservice JAX RPC handler and register it to the webservice reference Please refer this link to see howto create webservice handlers
  3. In the handler, try the below code in the public boolean handleRequest(MessageContext mc) add these lines

     mc.setProperty("USE_TEST_FILE", <your response in .xml file>); return false; 
  4. In the public boolean handleResponse(MessageContext mc)

     String testFileName = (String) mc.getProperty("USE_TEST_FILE"); InputStream instream = <read file as an input stream> SOAPMessageContext smc = (SOAPMessageContext) mc; SOAPMessage message = smc.getMessage(); SOAPPart soapPart = message.getSOAPPart(); soapPart.setContent(new StreamSource(instream)); smc.setMessage(message); return false; 

now run your proxy.. the proxy will fail if the message is not valid per WSDL

You can create a stub using the WSDL you have and then make the query to the stub using your XML request. If this goes properly your request is correct.

You can import the WSDL in your project and then make a call to the stub service using your XML. This way you can make changes to the validations without any change in the code.

As SOAP message is xml, you can validate whole message against xsd. For example I found this schema for SOAP message. You could:

  1. Extract schema from your wsdl,
  2. Validate message against schema from wsdl and soap schema.

Should you want to validate against specific WSDL, you can prepare you schema by hand: just place root element of your message instead of this part:

<xs:sequence>
<xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
</xs:sequence>

in SOAP schema.

Validate against the SOAP envelope schema , then extract body and headers and validate against specific schema. Should be simple enough.

I think that you may be looking for an answer which is way too complex for what you really need. All you really need to do is have the appropriate DTD/XSD documents registered so that the normal JAXP classes can reference them.

This code, from Sun/Oracle , implements a basic EntityResolver . You need to build this class such that it will provide the DocumentBuilder with the appropriate InputSource s for the schema defined in the documents you are trying to validate.

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

public class MyResolver implements EntityResolver {
    public InputSource resolveEntity (String publicId, String systemId) {
        if (systemId.equals("http://www.myhost.com/today")) {
            // return a special input source
            MyReader reader = new MyReader();
            return new InputSource(reader);
        } else {
            // use the default behaviour
            return null;
        }
    }
}

Using this code, you can pull in your custom EntityResolver and have it used during the parsing/validation of the document.

final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware = true;
dbf.setValidating = true;
final DocumentBuilder db = dbf.newDocumentBuilder();
final MyResolver r = MyResolver.newInstance();
db.setEntityResolver(r);
final Document docFromFile = db.parse("path/to/my/document.xml");
// or
final Document docFromStream = db.parse(new FileInputStream("blah"));

请查看Apache AXIS2 API。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM