JAX-WS : SOAP handler in client side

This is part 2 of JAX-WS SOAP handler. In previous article – JAX-WS : SOAP handler in server side, you created a web service and attach a handler to retrieve the client MAC address in header block, for every incoming SOAP message.

SOAP handler in client side

In this article, you will develop a web service client to access the published service in previous article, and attach a handler to inject client’s MAC address into header block, for every outgoing SOAP message that’s send by client side.

Directory structure of this example

1. Web Service Client

Uses wsimport command to parse the published service WSDL file (http://localhost:8888/ws/server?wsdl) and generate all required files to access the service.


C:\>wsimport -keep -verbose http://localhost:8888/ws/server?wsdl
parsing WSDL...

generating code...
com\mkyong\ws\GetServerName.java
com\mkyong\ws\GetServerNameResponse.java
com\mkyong\ws\ObjectFactory.java
com\mkyong\ws\ServerInfo.java
com\mkyong\ws\ServerInfoService.java
com\mkyong\ws\package-info.java

Six files are generated automatically, you may only need to concern on the ServerInfoService.java.

File : ServerInfoService.java


@WebServiceClient(name = "ServerInfoService", 
	targetNamespace = "http://ws.mkyong.com/", 
	wsdlLocation = "http://localhost:8888/ws/server?wsdl")
public class ServerInfoService extends Service
{
	//......
}

A client to access the published service.
File : WsClient.java


package com.mkyong.client;

import com.mkyong.ws.ServerInfo;
import com.mkyong.ws.ServerInfoService;

public class WsClient{
	
	public static void main(String[] args) throws Exception {
	   
		ServerInfoService sis = new ServerInfoService();
		ServerInfo si = sis.getServerInfoPort();

		System.out.println(si.getServerName());
       
    }

}

2. SOAP Handler

Create a SOAP handler to inject client’s MAC address into the SOAP header block, for every outgoing SOAP message. See comments for the code explanation.

File : MacAddressInjectHandler.java


package com.mkyong.handler;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
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>{

   @Override
   public boolean handleMessage(SOAPMessageContext context) {

	System.out.println("Client : handleMessage()......");
		
	Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

	//if this is a request, true for outbound messages, false for inbound
	if(isRequest){
			
	try{
	    SOAPMessage soapMsg = context.getMessage();
            SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
            SOAPHeader soapHeader = soapEnv.getHeader();
	            
            //if no header, add one
            if (soapHeader == null){
            	soapHeader = soapEnv.addHeader();
            }

            //get mac address
            String mac = getMACAddress();
	            
            //add a soap header, name as "mac address"
            QName qname = new QName("http://ws.mkyong.com/", "macAddress");
            SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname);

            soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
            soapHeaderElement.addTextNode(mac);
            soapMsg.saveChanges();

            //tracking
            soapMsg.writeTo(System.out);

	             
	   }catch(SOAPException e){
		System.err.println(e);
	   }catch(IOException e){
		System.err.println(e);
	   }
            
         }

	   //continue other handler chain
	   return true;
   }

	@Override
	public boolean handleFault(SOAPMessageContext context) {
		System.out.println("Client : handleFault()......");
		return true;
	}

	@Override
	public void close(MessageContext context) {
		System.out.println("Client : close()......");
	}

	@Override
	public Set<QName> getHeaders() {
		System.out.println("Client : getHeaders()......");
		return null;
	}
	
   //return current client mac address
   private String getMACAddress(){
		
	InetAddress ip;
	StringBuilder sb = new StringBuilder();
		
	try {
			
		ip = InetAddress.getLocalHost();
		System.out.println("Current IP address : " + ip.getHostAddress());
		
		NetworkInterface network = NetworkInterface.getByInetAddress(ip);

		byte[] mac = network.getHardwareAddress();
			
		System.out.print("Current MAC address : ");
			
		for (int i = 0; i < mac.length; i++) {
				
			sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
				
		}
		System.out.println(sb.toString());
			
	} catch (UnknownHostException e) {
		
		e.printStackTrace();
		
	} catch (SocketException e){
			
		e.printStackTrace();
			
	}
		
	return sb.toString();
   }
	
}

3. SOAP Handler XML file

Create a SOAP handler XML file, and puts your SOAP handler declaration.

File : handler-chain.xml


<?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.mkyong.handler.MacAddressInjectHandler</javaee:handler-class>
    </javaee:handler>
  </javaee:handler-chain>
</javaee:handler-chains>

4. Attach SOAP Handler --> Web Service Client

To attach above SOAP handler to web service client, edit the ServerInfoService.java file (generated via wsimport), and annotate with @HandlerChain and specify the SOAP handler file name inside.

File : ServerInfoService.java


@WebServiceClient(name = "ServerInfoService", 
	targetNamespace = "http://ws.mkyong.com/", 
	wsdlLocation = "http://localhost:8888/ws/server?wsdl")
@HandlerChain(file="handler-chain.xml")
public class ServerInfoService extends Service
{
	//......
}

Done, please proceed on next article - Part 3 : JAX-WS - SOAP handler testing for client and server side.

Download Source Code

Download It - JAX-WS-Handler-Example.zip (21KB)

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Faisal
Guest
Faisal
Hi, i get the following exception on Jboss7, but it works fine on tomcat7 when i call context.getMessage(); Any help please?? 17:46:27,347 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) java.lang.LinkageError: com/sun/xml/messaging/saaj/soap/SOAPDocumentImpl 17:46:27,348 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.messaging.saaj.soap.SOAPPartImpl.(SOAPPartImpl.java:119) 17:46:27,348 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.messaging.saaj.soap.ver1_1.SOAPPart1_1Impl.(SOAPPart1_1Impl.java:89) 17:46:27,349 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl.getSOAPPart(Message1_1Impl.java:109) 17:46:27,349 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.message.AbstractMessageImpl.readAsSOAPMessage(AbstractMessageImpl.java:191) 17:46:27,349 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.handler.SOAPMessageContextImpl.getMessage(SOAPMessageContextImpl.java:79) 17:46:27,350 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.inov8.integration.soap.SOAPLoggingHandler.logToSystemOut(SOAPLoggingHandler.java:62) 17:46:27,350 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.inov8.integration.soap.SOAPLoggingHandler.handleMessage(SOAPLoggingHandler.java:32) 17:46:27,350 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.inov8.integration.soap.SOAPLoggingHandler.handleMessage(SOAPLoggingHandler.java:1) 17:46:27,350 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.handler.HandlerProcessor.callHandleMessage(HandlerProcessor.java:292) 17:46:27,351 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.handler.HandlerProcessor.callHandlersRequest(HandlerProcessor.java:133) 17:46:27,351 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.handler.ClientSOAPHandlerTube.callHandlersOnRequest(ClientSOAPHandlerTube.java:138) 17:46:27,351 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.handler.HandlerTube.processRequest(HandlerTube.java:116) 17:46:27,351… Read more »
Amitabh
Guest
Amitabh

Hi Mkyong,

I have got the way to sign my message using handler. But I have a legacy code, wherein i need to attach this handler. in which class should i attach this handler? how to figure it out?

Regards,
Roy

Amitabh
Guest
Amitabh

Hi Mkyong,
Great article!!!
I need to put a digital signature into my soap request before sending it to server. I have read your article, but i am not getting how to intercept the message and attach the xml signature to it. I have visited some blogs for digitally sign in request, but they envelope the xml request kept in a file. However, in my case the xml request is getting generated using axis toolkit. How to do it, can you help me?
Regards,
Roy

jose david villanueva villalob
Guest
jose david villanueva villalob
Hi friends, please help me with this case, I’m trying to consume a web service that has that format resquest: sasasasas asasasas tel:222222222 tel:22222222 dsdsdsdasd asdsadsadedwed= sadasdasdasdsada== 2010-03-22T20:03:49 , I’m using spring, as it should inject these service security header. Gracias
Christian Metze
Guest
Christian Metze

Thanks for this article. It helped me a lot in finding an issue in the generated code from Eclipse IDE. The differences in the handlers xml file were significant. Fixing the file helped to get the handlers running. Well done!!

Annamaria
Guest
Annamaria

In my opinion, for WS client, @HandlerChain should be put on ServerInfo class, and not on ServerInfoService, otherwise SoapHandler will be ignored. This worked for me

Christian Metze
Guest
Christian Metze

Remember, this article is strictly for the client side!

Marcos Filho
Guest
Marcos Filho

hi, i am using your example but when i go to client side the handler doesn’t work, i create my web service client in eclipse with new > web service > web service client > put the wsdl and the eclipse generate my web service client, then i put @handlerchain in the service, i start my app but the handler client never is being called

jason
Guest
jason

Awesome! Its in fact remarkable article, I have got much clear idea regarding from this post.

srini
Guest
srini

how to generate the wsdl for the project

Umberto
Guest
Umberto

Thank you so much, you helped me a lot!

Ahmet
Guest
Ahmet

Thanks for this nice tutorial, especially pointing out the “wsimport” tool which is very useful.

Daniel Zimmermann
Guest
Daniel Zimmermann
Hi, I’m currently trying to attach a HandlerChain to my Client side only to modify the HTTP headers. I’ve tried the approch you’ve mentioned, but also the “old” one via attaching the chain programatically to the service: service.setHandlerResolver(new HandlerResolver() { @Override public List<Handler> getHandlerChain(PortInfo portInfo) { AuthenticationHandler authenticationHandler = new AuthenticationHandler(); List<Handler> handlerChain = new ArrayList<Handler>(); handlerChain.add(authenticationHandler); return handlerChain; } }); and even tried to tie it to the Port –> BindingProvider BindingProvider bp = (BindingProvider) servicePort; AuthenticationHandler authenticationHandler = new AuthenticationHandler(); List<Handler> handlerChain = handlerChain = new ArrayList<Handler>(); handlerChain.add(authenticationHandler); bp.getBinding().setHandlerChain(handlerChain); Nothing of this worked. I tried to debug it,… Read more »
Karthick
Guest
Karthick

I think its the OpenJDK. Both your methods work just fine for me. Im using JavaEE6 (Websphere App Server 8.0)

Pedro Pereira
Guest
Pedro Pereira

Hi,

Great job!

Can you provide a example (server and client) with SOAPFault handling?

Thanks!

Pasqualino
Guest
Pasqualino

Thank you for posting this :)!

Zeus
Guest
Zeus

im getting this error message running this through eclipse project

Client : getHeaders()……
Client : getHeaders()……
Exception in thread “main” java.lang.IllegalArgumentException: com.mkyong.serverside.handler.ws.ServerInfo is not an interface

it seems that when i do a wsimport the ServerInfo.java is public interface ServerInfo { and not an implementation of the interface.

I also realize that we never created an interface for the ServerInfo.java so how do we get around this ?

Thanks

Zeus
Guest
Zeus

fixed it by adding wsimport … -p .. to distinguish ServerInfo client from server

Chandra
Guest
Chandra

Hi,

Can you please give me information on setting the NTLM credentials in JAX-WS Client

Thanks in Advance,
Chandra

Saroj
Guest
Saroj

Hi,

Many many thanx for your post. Actually we need to add a header in the msg. In our case the Header key is a static one, but the Header value is dynamic. So we need the header value as variable in the handler’s handleMessage method.

Please advise how can that be done. Thanx a lot for you help so far.

Regards
Saroj

Bharat
Guest
Bharat

Hi,

When I use your example and try to add a client soap header, I am getting following exception…

Client : getHeaders()......
javax.xml.soap.SOAPException: Invalid SOAPHeaderElement name: {http://webservice.domain.gcdm.wmg.com/}ArtistWebServiceBeanService
Client : handleMessage()......
Client : handleFault()......
Client : close()...... 

The code which i am executing is:

 
QName qname = new QName("http://webservice.domain.gcdm.wmg.com/", "ArtistWebServiceBeanService");
SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname); 
Dinesh
Guest
Dinesh

hey i need a help… help me to run these examples

Pramod Kumar
Guest
Pramod Kumar

In the above code (JAX-WS : SOAP handler in client side) , instead of the Mac Address I need to pass some authentication information ( user name and password ) availabe as object.

public class Authenticate_Info
{
public string UserName;
public string Password;
}

How to add this object to the SoapHeader.

WSDL Entry for the oject.


please let me know how to add this object to soap header and then how on server to validate the same.

Saroj
Guest
Saroj

Hi Pramod,

My requirement is also same as yours. Would you please help me with the way you implemented it ??

Many thanx for your help in advance.

Regards
Saroj

Saroj
Guest
Saroj

Hi Pramod,

Did you find a way to do set the userName and pwd header in handleMessage() method.
If yes, would you please put some light on that. many thanx for your help n time.

trackback
JAX-WS : SOAP handler in server side

[…] JAX-WS : SOAP handler in client side […]

trackback
JAX-WS : SOAP handler testing for client and server side

[…] is the part 3 of JAX-WS SOAP handler. A testing result for part 1 : SOAP handler in server side and part 2 : SOAP handler in client side. See following use […]

HelloWorld
Guest
HelloWorld

Thanks for the great tutorial. I also found nice tool to create SOAP web service Java clients without creating stub files. We can invoke soap web services with in few lines of codes.

http://sourceforge.net/projects/easyjaxwsclient/

Manasi
Guest
Manasi
Hi, I am using WAS 7 and generated my client code using wsimport. I have created a SOAP Handler to inject Security token in my request exactly as described here. I am getting the following error: getHeaders() called. getHeaders() called. Before call to PartyService handleMessage() called. close() called Exception in thread “main” javax.xml.ws.WebServiceException: org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. at com.sun.xml.internal.ws.handler.ClientSOAPHandlerTube.callHandlersOnRequest(ClientSOAPHandlerTube.java:147) at com.sun.xml.internal.ws.handler.HandlerTube.processRequest(HandlerTube.java:117) at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:599) at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:558) at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:543) at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:440) at com.sun.xml.internal.ws.client.Stub.process(Stub.java:223) at com.sun.xml.internal.ws.client.sei.SEIStub.doProcess(SEIStub.java:136) at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:110) at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:90) at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:119) at $Proxy36.updateParty(Unknown Source) at com.bac.cmx.component.MessageFormatter.prepareUpdatePartyRequest(MessageFormatter.java:130) at com.bac.cmx.component.MessageFormatter.main(MessageFormatter.java:57) Caused by: org.w3c.dom.DOMException:… Read more »
John Wu
Guest
John Wu

Me too. Anyone has the solution to it?

Baskar
Guest
Baskar

do you have solution for this?

Manoj Kumar M
Guest
Manoj Kumar M

Hi , all your Toutorials are very helpful, I am implementing the webservice client using the clientgen method,but in this scenario how to use handlers could you please explian, i need to pass wsse seucrity tokens in SOAPENVOLOP-HEADER.
but this header tags are not showing in my client jar please help.