JAX-WS SOAP handler client side
In the previous article, We have created a web service and attached a handler to retrieve the client MAC address in header block, for every incoming SOAP message.
In this article, we will create a web service client which can access the web service published in previous article.
Most important thing here is to define a SOAP handler to the client so that every outgoing message from client will have a client’s MAC address added in the header of a SOAP message.
Step 1
Create a maven project
Step 2
Run wsimport command
wsimport command generates all the required files by taking WSDL generated by the published service in the previous article.
wsimport -keep http://localhost:8888/ws/soapHandler?wsdl -d C:\Users\RAJ\Desktop\output
-keep : option to keep the generated files
-d : option to sepcify the folder where the generated files will be stored
Now check the Output in the folder
Command has generated 6 source files and 6 class files
We need to concentrate only on HelloWorldServiceImplService.java for client development
Step 3
Copy all these generated source files into project
HelloWorldServiceImplService.java looks like below
- import java.net.MalformedURLException;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import javax.xml.ws.Service;
- import javax.xml.ws.WebEndpoint;
- import javax.xml.ws.WebServiceClient;
- import javax.xml.ws.WebServiceException;
- import javax.xml.ws.WebServiceFeature;
- /**
- * This class was generated by the JAX-WS RI.
- * JAX-WS RI 2.2.9-b130926.1035
- * Generated source version: 2.2
- *
- */
- @WebServiceClient(name = "HelloWorldServiceImplService", targetNamespace = "http://ws.kb.com/",
- wsdlLocation = "http://localhost:8888/ws/soapHandler?wsdl")
- public class HelloWorldServiceImplService
- extends Service
- {
- ………………………………………………………
- }
import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.WebEndpoint; import javax.xml.ws.WebServiceClient; import javax.xml.ws.WebServiceException; import javax.xml.ws.WebServiceFeature; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.2.9-b130926.1035 * Generated source version: 2.2 * */ @WebServiceClient(name = "HelloWorldServiceImplService", targetNamespace = "http://ws.kb.com/", wsdlLocation = "http://localhost:8888/ws/soapHandler?wsdl") public class HelloWorldServiceImplService extends Service { ……………………………………………………… }
Step 4
Create a SOAP Handler
- package com.kb;
- import java.io.IOException;
- import java.util.Set;
- import javax.xml.namespace.QName;
- import javax.xml.soap.SOAPConstants;
- import javax.xml.soap.SOAPEnvelope;
- import javax.xml.soap.SOAPException;
- import javax.xml.soap.SOAPHeader;
- import javax.xml.soap.SOAPHeaderElement;
- import javax.xml.soap.SOAPMessage;
- import javax.xml.ws.handler.MessageContext;
- import javax.xml.ws.handler.soap.SOAPHandler;
- import javax.xml.ws.handler.soap.SOAPMessageContext;
- public class MACAddressInjectHandler implements SOAPHandler<SOAPMessageContext>{
- public boolean handleMessage(SOAPMessageContext context) {
- System.out.println("Client : handleMessage() Begin");
- Boolean outBoundProperty = (Boolean)
- context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
- //If its an outgoing message from client, then outBoundProperty will be true
- if(outBoundProperty){
- try{
- SOAPMessage soapMsg = context.getMessage();
- SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
- SOAPHeader soapHeader = soapEnv.getHeader();
- //if no header, add the header
- if (soapHeader == null){
- soapHeader = soapEnv.addHeader();
- }
- String macAddress = getMACAddress();
- //add a soap header called "macAddress"
- QName qname = new QName("http://ws.kb.com/", "macAddress");
- SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname);
- soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
- //Add MAC address to SOAP header
- soapHeaderElement.addTextNode(macAddress);
- soapMsg.saveChanges();
- //Output the message to console
- soapMsg.writeTo(System.out);
- }catch(SOAPException e){
- System.err.println(e);
- }catch(IOException e){
- System.err.println(e);
- }
- }
- //Returning true makes other handler chain to continue the execution
- return true;
- }
- public boolean handleFault(SOAPMessageContext context) {
- System.out.println("Client : handleFault() Begin");
- return true;
- }
- public void close(MessageContext context) {
- System.out.println("Client : close() Begin");
- }
- public Set<QName> getHeaders() {
- System.out.println("Client : getHeaders() Begin");
- return null;
- }
- private String getMACAddress(){
- //Write a java code to get client machine MAC address and return it dynamically
- //Returning valid MAC address
- return "E0-DB-55-A4-10-Z4";
- //Returning invalid MAC address
- //return "E0-DB-55-A4-10-Q1";
- }
- }
package com.kb; import java.io.IOException; import java.util.Set; import javax.xml.namespace.QName; import javax.xml.soap.SOAPConstants; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPHeader; import javax.xml.soap.SOAPHeaderElement; import javax.xml.soap.SOAPMessage; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; import javax.xml.ws.handler.soap.SOAPMessageContext; public class MACAddressInjectHandler implements SOAPHandler<SOAPMessageContext>{ public boolean handleMessage(SOAPMessageContext context) { System.out.println("Client : handleMessage() Begin"); Boolean outBoundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); //If its an outgoing message from client, then outBoundProperty will be true if(outBoundProperty){ try{ SOAPMessage soapMsg = context.getMessage(); SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope(); SOAPHeader soapHeader = soapEnv.getHeader(); //if no header, add the header if (soapHeader == null){ soapHeader = soapEnv.addHeader(); } String macAddress = getMACAddress(); //add a soap header called "macAddress" QName qname = new QName("http://ws.kb.com/", "macAddress"); SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname); soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT); //Add MAC address to SOAP header soapHeaderElement.addTextNode(macAddress); soapMsg.saveChanges(); //Output the message to console soapMsg.writeTo(System.out); }catch(SOAPException e){ System.err.println(e); }catch(IOException e){ System.err.println(e); } } //Returning true makes other handler chain to continue the execution return true; } public boolean handleFault(SOAPMessageContext context) { System.out.println("Client : handleFault() Begin"); return true; } public void close(MessageContext context) { System.out.println("Client : close() Begin"); } public Set<QName> getHeaders() { System.out.println("Client : getHeaders() Begin"); return null; } private String getMACAddress(){ //Write a java code to get client machine MAC address and return it dynamically //Returning valid MAC address return "E0-DB-55-A4-10-Z4"; //Returning invalid MAC address //return "E0-DB-55-A4-10-Q1"; } }
In this handler , we are just adding MAC address to the SOAP header
Step 5
Create a SOAP handler xml file
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <javaee:handler-chain>
- <javaee:handler>
- <javaee:handler-
- class>com.kb.handler.MACAddressInjectHandler</javaee:handler-class>
- </javaee:handler>
- </javaee:handler-chain>
- </javaee:handler-chains>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <javaee:handler-chain> <javaee:handler> <javaee:handler- class>com.kb.handler.MACAddressInjectHandler</javaee:handler-class> </javaee:handler> </javaee:handler-chain> </javaee:handler-chains>
We have created the handler-chain.xml file to add our SOAP handler MACAddressInjectHandler in the handler list.
Step 6
Attach a SOAP handler to web service client
- package com.kb.ws;
- import java.net.MalformedURLException;
- import java.net.URL;
- import javax.jws.HandlerChain;
- import javax.xml.namespace.QName;
- import javax.xml.ws.Service;
- import javax.xml.ws.WebEndpoint;
- import javax.xml.ws.WebServiceClient;
- import javax.xml.ws.WebServiceException;
- import javax.xml.ws.WebServiceFeature;
- /**
- * This class was generated by the JAX-WS RI.
- * JAX-WS RI 2.2.9-b130926.1035
- * Generated source version: 2.2
- *
- */
- @WebServiceClient(name = "HelloWorldServiceImplService", targetNamespace = "http://ws.kb.com/", wsdlLocation = "http://localhost:8888/ws/soapHandler?wsdl")
- @HandlerChain(file="../handler/handler-chain.xml")
- public class HelloWorldServiceImplService
- extends Service
- {
- ………………………………………………………
- }
package com.kb.ws; import java.net.MalformedURLException; import java.net.URL; import javax.jws.HandlerChain; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.WebEndpoint; import javax.xml.ws.WebServiceClient; import javax.xml.ws.WebServiceException; import javax.xml.ws.WebServiceFeature; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.2.9-b130926.1035 * Generated source version: 2.2 * */ @WebServiceClient(name = "HelloWorldServiceImplService", targetNamespace = "http://ws.kb.com/", wsdlLocation = "http://localhost:8888/ws/soapHandler?wsdl") @HandlerChain(file="../handler/handler-chain.xml") public class HelloWorldServiceImplService extends Service { ……………………………………………………… }
In this web service client, we are giving information about the handlers using @HandlerChain annotation.
In this annotation,we have specified the handler-chain.xml file name developed in previous step.
Step 7
Create a client to access the published web service
- package com.kb.client;
- import com.kb.ws.HelloWorldService;
- import com.kb.ws.HelloWorldServiceImplService;
- public class HelloWorldClient {
- public static void main(String[] args) {
- HelloWorldServiceImplService service = new HelloWorldServiceImplService();
- HelloWorldService serviceInterface = service.getHelloWorldServiceImplPort();
- String response = serviceInterface.sayHelloWorld();
- System.out.println(response);
- }
- }
package com.kb.client; import com.kb.ws.HelloWorldService; import com.kb.ws.HelloWorldServiceImplService; public class HelloWorldClient { public static void main(String[] args) { HelloWorldServiceImplService service = new HelloWorldServiceImplService(); HelloWorldService serviceInterface = service.getHelloWorldServiceImplPort(); String response = serviceInterface.sayHelloWorld(); System.out.println(response); } }
We have created a client class to invoke the web service.