views:

68

answers:

3

I am using the Axis API to access Axis HTTP server. The documentation of the API can be found here.

I am using the following code to add handlers to the server. service is of type java.xml.rpc.Service

    HandlerRegistry registry = service.getHandlerRegistry();
    QName serviceName = new QName(url, "MyServiceClass");

    List<HandlerInfo> handlerChain = new ArrayList<HandlerInfo>();
    HandlerInfo handlerInfo = new HandlerInfo(MyHandler.class, null, null);
    handlerChain.add(handlerInfo);
    registry.setHandlerChain(serviceName, handlerChain);

I know that the service name is correct as I am getting the correct output in subsequent calls to the service object.

Somehow the handler is not being invoked. Here is the Handler class. My intention is to add custom headers to the HTTP request before forwarding the request to the server.

import javax.xml.namespace.QName;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;

public class MyHandler extends BasicHandler {

    @Override
    public void init() {
        System.out.println("init called");
        super.init();
        System.out.println("init called");
    }

    @Override
    public void cleanup() {
        super.cleanup();
        System.out.println("cleanup called");
    }

    @Override
    public void invoke(MessageContext mc) throws AxisFault {
        System.out.println("invoke called");
    }

    public QName[] getHeaders() {
        System.out.println("getHeaders");
        return new QName[1];
    }
}

What is wrong with the above code?

Is there any other way to modify HTTP Headers using Apache Axis API?

A: 

We are adding custom headers to a SOAP request. However, we have implemented this by injecting the headers into the Axis Stub object for the webservice at runtime. Thus, they dont change for every request, but the headers injected are used for the entire run of our test cases

If you think that's your use case as well, I can find that code and update with what we did.

madhurtanwani
I need to add HTTP headers and not SOAP headers.
iamrohitbanga
Sorry :(. Nevertheless, I did a search to find this. Please check if it helps : http://faq.javaranch.com/java/WebServicesHowTo#http-headers
madhurtanwani
trying to call the setProperty function on the stub object. It does not work. Can you please give me the code snippet that you are using?Thanks
iamrohitbanga
what? the link i pasted has steps for Axis and JAX-RPC - decide what you want to do? The Axis method does not call _setProperty();
madhurtanwani
As for the code snippet, please read the last answer i've given :(
madhurtanwani
+1  A: 

BTW reading on the Handlers usage, I found this page. See if it helps : http://soa.sys-con.com/node/39721

madhurtanwani
I have a feeling, with all the QNames and stuff, that the HandlerConfig is to manage SOAP headers.
madhurtanwani
+1  A: 

Okie. This should do the trick :

1 - Create a wsdd file (say /tmp/test.wsdd) containing this :

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"&gt;
 <handler name="test" type="java:axistest.TestHandler" />
 <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender">
   <requestFlow>
    <handler type="test"/>
   </requestFlow>
 </transport>
</deployment>

2 - Ensure all axis libs are in your class path and then run :

java org.apache.axis.utils.Admin client /tmp/test.wsdd

3 - Step 2 will generate a client-config.wsdd. Copy this to your project and ensure it will be in the class path when the project is run.

4 - ALL webservice calls (via Http transport) will route via the TestHandler1 class

Here is my TestHandler1 class (a slight modification of ur handler to access the MIME headers) :

package axistest;

import javax.xml.namespace.QName;
import javax.xml.soap.MimeHeaders;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;

public class TestHandler1 extends BasicHandler {

@Override
public void init() {
    System.out.println("init called");
    super.init();
    System.out.println("init called");
}

@Override
public void cleanup() {
    super.cleanup();
    System.out.println("cleanup called");
}

@Override
public void invoke(MessageContext mc) throws AxisFault {
    System.out.println("invoke called");
    System.out.println("=----------------------------------=");
    MimeHeaders mimeHeaders = mc.getMessage().getMimeHeaders();
    mimeHeaders.addHeader("X-Test", "Hello");
    System.out.println("Headers : \n " + mimeHeaders);
}

public QName[] getHeaders() {
    System.out.println("getHeaders");
    return new QName[1];
}

}

when I run this on my box, I see that these handler methods are being invoked :

- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
init called
init called
invoke called
=----------------------------------=
Headers : 
 org.apache.axis.message.MimeHeaders@761eec35
.
.
.
madhurtanwani
this works for me. there was some problem with my client-config.wsdd. Thankfully this tool generates the correct file.
iamrohitbanga