Forum Discussion

LazyWriter's avatar
LazyWriter
New Contributor
9 years ago

How to validate the SOAP Response against WSDL?

Hi everybody,

 

I built a web service based on the responses messages created by SOAPUI. It runs OK but I got a request to validate every SOAP Request and Response recently. My webservice can get the request and generate response. wsdl file is also available. So I have 3 String variables to store the Request, Response and the WSDL.

 

Below is the method I used for validate requet messages against the wsdl file:

 

private boolean validateRequestMessage(String message){
    	// Get operation name from the message
        RequestObject reqObj = new RequestObject();
        reqObj.setRequest(message);
        String opName = reqObj.getOperation();
        // Wsdl file's location
    	String wsdlUrl = responseDir + "/" + "mywsdl.wsdl";
    	try{
    		WsdlProject project = new WsdlProject();
    		// Import the WSDL file
    		WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, wsdlUrl);
    		WsdlInterface wsdl = wsdls[0];
    		WsdlRequest request = null;
    		
    		String name = "";
    		for(Operation operation : wsdl.getOperationList()){
    			WsdlOperation op = (WsdlOperation) operation;
    			
    			switch(op.getName()){
    			case "OP1":
    				name = "op1-request";
   					break;
    			case "OP2":
    				name = "op2-request";
    				break;
    			case "OP3":
    				name = "op3-request";
    				break;
    			case "OP4":
    				name = "op4-request";
    				break;
    			default:
    				name = "";
    				break;
    			}
    			if(opName.equals(name)){
        			request = op.addNewRequest(opName);
        			WsdlValidator validator = new WsdlValidator((WsdlContext) (operation.getInterface()).getDefinitionContext());
        			
        			WsdlResponseMessageExchange wsdlResponseMessageExchange = new WsdlResponseMessageExchange(request);
        			wsdlResponseMessageExchange.setRequestContent(message);
        			
        			ValidationError[] result = validator.assertRequest(wsdlResponseMessageExchange, false);
        			
        			if(result.length>0){
        				for(int i=0;i<result.length;i++){
        					System.out.println("WSDL Validation Error:" + result[i].toString());
        				}
    					return false;
        			}
        			return true;
    			}
    		}
    		
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return false;
    }

It works with the request validation, but does not work for the response validation. Because the WsdlResponseMessageExchange object only have setRequestContext() method but don't have semilar method to set reponse with String parameter. Is there any other way to make assertResponse() work with response message?

 

Thanks,

Alex

 

 

 

 

    • LazyWriter's avatar
      LazyWriter
      New Contributor

      Unfortunately I have to use pure java. It's a part of web service which gets the SOAP request and then sends SOAP response back. The reponse messages are created by templates so I need a String parameter for this method. All I need to do is how to make a WsdlResponse object to have the response context.