Java – a handler that does not add an HTTP header to an HTTP request when using the axis client API
I am using the axis API to access the axis HTTP server
I use the following code to add a handler to the server The type of service is 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 the service name is correct because I get the correct output when I call the service object later
Somehow, the handler was not called This is the handler class My goal is to add custom headers to HTTP requests before forwarding them 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's wrong with the code above?
Is there any other way to modify HTTP headers using the Apache axis API?
Solution
Okie. This should be done:
1 – create a wsdd file containing the following (for example, / TMP / test. Wsdd):
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <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 – make sure all axis libraries are in your classpath, and then run:
java org.apache.axis.utils.Admin client /tmp/test.wsdd
3 – step 2 will generate client config wsdd. Copy it to the project and make sure it will be in the classpath when the project runs
4 – all web service calls (transmitted via HTTP) will be routed through the testhandler1 class
This is my testhandler1 class (slightly modified the ur handler for accessing 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 it on my box, I see that these handler methods are being called:
- 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 . . .