Wednesday, July 20, 2011

Register dynamic Web Service Client Handler


package javawiki.webservice.consumer;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javawiki.webservice.common.LogHandler;
import javawiki.webservice.provider.GetBookDetails;
import javawiki.webservice.provider.LibraryService;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.handler.Handler;
public class ConsumeLibraryService {
 public static void main(String arg[])
 {
  try
  {
   URL url = new URL("http://localhost:8080/libraryservice/LibraryService");
   QName qName = new QName("http://javawikiforbeginners.blogspot.com/JavaLibraryService/","JavaLibraryService");
   Service service = Service.create(url, qName);
   LibraryService libraryService = service.getPort(LibraryService.class);
   BindingProvider bindingProvider = (BindingProvider) libraryService;
   
   List<Handler> handlerChain = new ArrayList<Handler>();
   
   handlerChain.add(new LogHandler());
   
   bindingProvider.getBinding().setHandlerChain(handlerChain);
   
   GetBookDetails aBookDtls = libraryService.getBook("123456");
   System.out.println(aBookDtls.getBook());
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
 }
}



package javawiki.webservice.common;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class LogHandler implements SOAPHandler<SOAPMessageContext>{
 @Override
 public Set<QName> getHeaders() {
  // TODO Auto-generated method stub
  return null;
 }
 @Override
 public void close(MessageContext context) {
  System.out.println("Closing message");
 }
 @Override
 public boolean handleFault(SOAPMessageContext context) {
  // TODO Auto-generated method stub
  return false;
 }
 @Override
 public boolean handleMessage(SOAPMessageContext context) {
  Boolean outbound = (Boolean) context
  .get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY);
  
  System.out.println(context.keySet());
  
  if(outbound)
  {
   System.out.println("=====================this is outbound log handler===============");
   return true;
  }
  else
  {
   System.out.println("=====================this is inbound log handler===============");
   return false;
  }
 }
}

No comments:

Post a Comment