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)

33 comments on “JAX-WS : SOAP handler in client side

  1. 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.

  2. 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

  3. 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

  4. 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!!

  5. 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

  6. 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

  7. 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 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:595)

    17:46:27,352 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:554)

    17:46:27,352 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:539)

    17:46:27,352 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:436)

    17:46:27,352 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.client.Stub.process(Stub.java:248)

    17:46:27,353 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:135)

    17:46:27,353 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:109)

    17:46:27,353 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:89)

    17:46:27,353 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:118)

    17:46:27,353 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at $Proxy124.balanceUpdate(Unknown Source)

    17:46:27,354 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at com.inov8.integration.service.WaridTopupService.topup(WaridTopupService.java:58)

    17:46:27,354 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    17:46:27,354 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

    17:46:27,354 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

    17:46:27,354 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at java.lang.reflect.Method.invoke(Method.java:597)

    17:46:27,355 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)

    17:46:27,355 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)

    17:46:27,355 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)

    17:46:27,355 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.remoting.support.RemoteInvocationTraceInterceptor.invoke(RemoteInvocationTraceInterceptor.java:77)

    17:46:27,355 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

    17:46:27,356 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

    17:46:27,356 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at $Proxy122.topup(Unknown Source)

    17:46:27,356 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    17:46:27,356 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

    17:46:27,356 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

    17:46:27,357 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at java.lang.reflect.Method.invoke(Method.java:597)

    17:46:27,357 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.remoting.support.RemoteInvocation.invoke(RemoteInvocation.java:205)

    17:46:27,357 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.remoting.support.DefaultRemoteInvocationExecutor.invoke(DefaultRemoteInvocationExecutor.java:38)

    17:46:27,357 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.remoting.support.RemoteInvocationBasedExporter.invoke(RemoteInvocationBasedExporter.java:78)

    17:46:27,358 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.remoting.support.RemoteInvocationBasedExporter.invokeAndCreateResult(RemoteInvocationBasedExporter.java:114)

    17:46:27,358 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter.handleRequest(HttpInvokerServiceExporter.java:73)

    17:46:27,358 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)

    17:46:27,358 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)

    17:46:27,359 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)

    17:46:27,359 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)

    17:46:27,359 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)

    17:46:27,359 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)

    17:46:27,359 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

    17:46:27,360 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

    17:46:27,360 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

    17:46:27,360 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

    17:46:27,360 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

    17:46:27,360 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:139)

    17:46:27,361 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57)

    17:46:27,361 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154)

    17:46:27,361 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

    17:46:27,362 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

    17:46:27,362 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)

    17:46:27,363 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

    17:46:27,363 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667)

    17:46:27,363 INFO [stdout] (http-127.0.0.1-127.0.0.1-8080-1) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:952)

  8. 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: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
    at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(Unknown Source)
    at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)
    at com.ibm.ws.webservices.engine.xmlsoap.SOAPPart.appendChild(SOAPPart.java:282)
    at com.sun.xml.internal.bind.marshaller.SAX2DOMEx.startElement(SAX2DOMEx.java:177)
    at com.sun.xml.internal.ws.message.AbstractMessageImpl.writeTo(AbstractMessageImpl.java:159)
    at com.sun.xml.internal.ws.message.AbstractMessageImpl.readAsSOAPMessage(AbstractMessageImpl.java:194)
    at com.sun.xml.internal.ws.handler.SOAPMessageContextImpl.getMessage(SOAPMessageContextImpl.java:80)
    at com.bankofamerica.wsdl.wcc.party.service.v012.ClientHandler.handleMessage(ClientHandler.java:39)
    at com.bankofamerica.wsdl.wcc.party.service.v012.ClientHandler.handleMessage(ClientHandler.java:1)
    at com.sun.xml.internal.ws.handler.HandlerProcessor.callHandleMessage(HandlerProcessor.java:293)
    at com.sun.xml.internal.ws.handler.HandlerProcessor.callHandlersRequest(HandlerProcessor.java:134)
    at com.sun.xml.internal.ws.handler.ClientSOAPHandlerTube.callHandlersOnRequest(ClientSOAPHandlerTube.java:139)
    … 13 more

    The error is on line SOAPMessage soapMsg = context.getMessage();

    Any help please?

  9. 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, but it seams, that my Handler is never invoked.
    Now I’m quite uncertain, how to continue, since I have no more ideas… Maybe it’s the OpenJDK (java-6-openjdk-amd64) I’m currently using under linux?

  10. 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

  11. 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

  12. 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); 
    
        1. Hi mkykong,

          I just get the example and i run it in eclipse successful. But i saw a file war in the project. Can you tell me how to use the file war in tomcat? Thanks!

  13. 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.

    1. 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

    2. 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.

      1. Hi,
        We have also same type of requirement. Please let me know if you find information on how to pass dynamic values to handler class.

        Thanks in advance.

Leave a Comment

Your email address will not be published. Required fields are marked *