Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger a handler class when consuming web-service in java

Using Axis 1.4 I built client application that will consume external server services.

The server application response with soap message that include header tag along with body tag.

My problem with the header tag, I am trying to find away to get the header element.

What is done so far:

I found that I need to use a handler that extends BasicHandler using this class I can get the header tag. source: Dealing with SOAP Headers in Axis

But how to make this handler work when consuming web-service? I mean how to invoke this handler when ever I am receiving response from server to get its header.

Some blogs suggest I need to use .wsdd file. I am using Jdeveloper 11g with weblogic 10.3.6 environment where I am only aware of web.xml file for configuration.

Question: How to link those information(handler class, .wsdd file and web.xml) to gather and make the handler works to get the header tags?

like image 614
Salman Avatar asked Feb 10 '23 18:02

Salman


2 Answers

The best start was to check Axis guide on: Apache-Axis Reference Guide where you will have an overview of the work flow.

To configure handlers to be trigger from the client side you need to do the following:

1- Create handler class basically something similar to the following:

package mypackge;

import javax.xml.soap.SOAPException;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.axis.message.SOAPHeader;
import org.apache.axis.message.SOAPHeaderElement;

public class SoapHeaderConsumerHandler
  extends BasicHandler
{
  public void invoke(MessageContext messageContext)
    throws AxisFault
  {
    // Your logic for request or response handling goes here. 
    // Basically you need to make use of the parameter
   // messageContext where you can access the soap header and body tags.
  }
}

2- Create the client-config.wsdd file. it will look like the following:

<deployment
    xmlns="http://xml.apache.org/axis/wsdd/"
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <globalConfiguration>
        <responseFlow>
            <handler name="log" type="java:mypackge.SoapHeaderConsumerHandler"/>
        </responseFlow>
    </globalConfiguration>
    <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/>
</deployment>

You can see that I am using only handlers for the incoming response from the server side. So when ever the client application receive a response from the server the handler class SoapHeaderConsumerHandler will be triggered and the method invoke will be called by default.

Note: if you want to access the outgoing request before send it to the server you need to add extra tag for <requestFlow> to add request handler.

Check Deployment(WSDD) Reference from Axis guide:

3- Where to place the client-config.wsdd file ?

You should place the .wsdd file in the working directory. You can easily find out the working directory location using :

System.out.println("Working Directory = " + System.getProperty("user.dir"));

Source: Getting the Current Working Directory in Java

UPDATE:

I found out that it is not necessary to put the client-config.wsdd file in the working directory. You can specify the path of this file in your code as follow:

System.setProperty("axis.ClientConfigFile", "[Path goes here]\\client-config.wsdd");

You just need to place the .wsdd file there.

Extra Useful Links:

Where to place the client-config.wsdd file in Railo

V Axis handler This is an example for the server side handlers.

Dealing with SOAP Headers in Axis

like image 104
Salman Avatar answered Feb 13 '23 06:02

Salman


To avoid file location problems you can programatically configure axis :

    ConsultationServiceLocator stub = new ConsultationServiceLocator();
    SimpleProvider clientConfig = new SimpleProvider();
    SoapHeaderConsumerHandler logHandler = new SoapHeaderConsumerHandler();
    SimpleChain reqHandler = new SimpleChain();
    SimpleChain respHandler = new SimpleChain();
    reqHandler.addHandler(logHandler);
    respHandler.addHandler(logHandler);
    Handler pivot = new HTTPSender();
    Handler transport = new SimpleTargetedChain(reqHandler, pivot, respHandler);
    clientConfig.deployTransport(HTTPTransport.DEFAULT_TRANSPORT_NAME, transport);
like image 29
Lhoussin Ghanem Avatar answered Feb 13 '23 08:02

Lhoussin Ghanem