3

I have to add a custom Header into my SOAP Response and Read Header from SOAP Request what I did so far referring to this links link1 and link2 as follows

Web Service Class:

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService()
@HandlerChain(name = "SoapHandler", file = "soaphandler.xml")
public class FooService {

    @WebMethod()
    public String sayHello(String name) {
        System.out.println("Hello: " + name);
        return "Hello " + name + "!";
    }
}

SOAP Handler Class:

package com.webservice;

import java.util.Set;
import java.util.logging.Logger;
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 SoapHandler implements SOAPHandler<SOAPMessageContext> {

    private static final Logger LOGGER = Logger.getLogger(SoapHandler.class.getName());

    @Override
    public void close(MessageContext arg0) {
        System.out.println("Colse Method");
        LOGGER.info("Close Method");
    }

    @Override
    public boolean handleFault(SOAPMessageContext arg0) {
        System.out.println("handleFault Method");
        LOGGER.info("handleFault Method");
        return false;
    }

    @Override
    public boolean handleMessage(SOAPMessageContext arg0) {
        System.out.println("handleMessage Method");
        LOGGER.info("handleMessage Method");
        return false;
    }

    @Override
    public Set<QName> getHeaders() {
        System.out.println("getHeaders Method");
        LOGGER.info("getHeaders Method");
        return null;
    }
}

Tester Class

public class Tester {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            FooServiceServiceLocator locator = new FooServiceServiceLocator();
            FooService fooService = locator.getFooServicePort();
            System.out.println(fooService.sayHello("ashish"));
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Handler Chain Configuration:

<?xml version="1.0" encoding="UTF-8"?>
<jws:handler-chains xmlns:jws="http://java.sun.com/xml/ns/javaee">
  <jws:handler-chain>
    <jws:handler>
      <jws:handler-name>SoapHandler</jws:handler-name>
      <jws:handler-class>com.webservice.SoapHandler</jws:handler-class>
    </jws:handler>
  </jws:handler-chain>
</jws:handler-chains>

when I am calling this Tester class it gives me correct output as "Hello ashish!" and my handleMessage(SOAPMessageContext arg0) method is getting executed when request comes in and goes out so how can I differentiate between Incoming Request and outgoing Response in my

handleMessage(SOAPMessageContext arg0) method ? so that when request comes in I can read Header and when response goes out then I can Add my header into it Thanks....
Community
  • 1
  • 1
Ashish Jagtap
  • 2,799
  • 3
  • 30
  • 45
  • have you configured the handler chain? – Balaji Krishnan Oct 08 '13 at 13:52
  • @balaji krishnan yes I have configured the handler chain as given in Link2 and my SOAP Handler is working but handleMessage() method is executing twice when request comes in and goes out, now problem is how can I differentiate between the incoming request and outgoing response ? – Ashish Jagtap Oct 09 '13 at 05:23

1 Answers1

2

use the context argument that you get in handleMessage

arg0.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

and check the retunred boolean to identify the msg as request/response

Balaji Krishnan
  • 1,007
  • 3
  • 12
  • 29
  • I am using same code as you mentioned as Object bOutbound = messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); it means when it return Boolean.TRUE value then it is Response and when Boolean.FALSE then it is Request is it so ? – Ashish Jagtap Oct 09 '13 at 06:45
  • yes, you are right. javadoc here:http://docs.oracle.com/javaee/5/api/javax/xml/ws/handler/MessageContext.html#MESSAGE_OUTBOUND_PROPERTY – Balaji Krishnan Oct 09 '13 at 06:55
  • hey thanks it works for me but one more thing How can I exclude some methods from Handler Chain configuration ? i.e suppose I have one more method in FooService class as sayHey() and I want to exclude this method from this SoapHandler class ? – Ashish Jagtap Oct 09 '13 at 07:04
  • i dont think you can do that.. those are the methods defined in the SOAPHandler & SOAPMessageContext interfaces..Not suggested way but, may be you can create another class overriding all the methods and in your needed class just extend it and override getMessage. As alwasys with interfaces, you need to provide the implementation in some way or the other – Balaji Krishnan Oct 09 '13 at 07:12
  • you can have handlers implemented both at the server and client end. if you are inclunding some content in all the request headers, do it in the client side handler and extract the header content & validate it in the server end handler.. just like the Filters we use in web apps – Balaji Krishnan Oct 09 '13 at 07:19