Ashish Jagtap
Ashish Jagtap

Reputation: 2819

How to Add/Read Custom Header from each SOAP Response/Request throw Java

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

Upvotes: 3

Views: 3273

Answers (1)

Balaji Krishnan
Balaji Krishnan

Reputation: 1017

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

Upvotes: 2

Related Questions