Differences between revisions 3 and 4
Revision 3 as of 2005-09-21 03:53:55
Size: 4192
Editor: NeilHarvey
Comment:
Revision 4 as of 2009-09-20 22:48:27
Size: 4192
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

Q: How do I set a header when using  WSDL2Java  stubs?

A: There are two styles of headers, explicit and implicit. Explicit headers are defined in the WSDL of the service. The  WSDL2Java  generation tool will recognize these headers in most cases and emit stub class methods that include the headers as arguments to the methods.

In other cases, you may want to set headers that are not explicitly called out in the WSDL. For instance, you want to do some custom processing in a handler or add security. In this case you can add headers to request before you invoke the stub method.

There are are two setHeader APIs on the org.apache.axis.client.Stub class. The first takes the namespace, name and value of the header.

  •  setHeader(String namespace, String partName, Object headerValue) 

The second takes a SoapHeaderElement:

  •  setHeader(SOAPHeaderElement header) 

Here is an example of using the first API

 FooServiceLocator loc = new FooServiceLocator();
 FooService binding = loc.getFooService(); 
 org.apache.axis.client.Stub s = (Stub) binding;
 s.setHeader("http://my.name.space/headers", "mysecurityheader", "This guy is OK");
 result = binding.myOperation(...);

Q: Can someone explain why the variable "binding" (in the above code) needs to be cast to a Stub if the FooService class (as typically generated by WSDL2Java) already extends the org.apache.axis.client.Stub class?

A: Because getFooService() will return the stub as a FooService object, which is an Interface that contains only the service operations. Casting this to a Stub object allows access to the JAX-RPC and Axis Stub APIs.

Q: How do I get response headers when using  WSDL2Java  stubs?

A: You use the  getResponseHeader()  API in the Axis Stub object.

  •  ["SOAPHeaderElement"] getResponseHeader(String namespace, String partName) 

Or to get the entire list of headers

  •  ["SOAPHeaderElement"][] getResponseHeaders() 

Here is a code snippet that gets a header returned from myOperation().

  FooServiceLocator loc = new FooServiceLocator(); 
  FooService binding = loc.getFooService(); 
  // Call web service operation that returns a header 
  result = binding.myOperation(...); 
  org.apache.axis.client.Stub s = (Stub) binding; 
  s.getResponseHeader("http://my.name.space/headers", "mysecurityheader"); 

Q: What do the getHeader() APIs do in the Stub object? Why don't they return the response headers?

A: These APIs return the headers in the Stub/Call that will be/have been sent in an operation request. This changed in Axis 1.1 so there was no way to get headers from the Stub (you could still get them from the Call object) and the { { { getResponseHeader } } } APIs were added post 1.1 to address this.

Q: If I am using Axis 1.1, how do I get the response headers?

A: Use the Call object, which is available from the Service object, to get the SOAPEnvelope. This object has a slew of header functions in it. Or you can get the JAX-RPC javax.xml.soap.SOAPHeader object by calling getHeader() on the SOAPEnvelope. You will have to do your own enumeration on the headers, as the SOAPEnvelope class generally returns Iterators.

Here is an example of how to use the Axis APIs on the SOAPEnvelope

  FooServiceLocator loc = new FooServiceLocator(); 
  FooService binding = loc.getFooService(); 
  // Call web service operation that returns a header 
  result = binding.myOperation(...); 
  Call lastCall = ((org.apache.axis.client.Service)service).getCall(); 
  org.apache.axis.message.SOAPEnvelope env = lastCall.getResponseMessage().getSOAPEnvelope; 
  ["SOAPHeaderElement"] h = env.getHeaderByName(("http://my.name.space/headers", "mysecurityheader"); 

Q: When adding multiple headers of the same namespace how does one limit the number of xmlns attributes when using Stub.setHeader()? Ideally one could set a namespace declaration on the SOAPEnvelope and then Stub.setHeader() would just use the prefix instead of the entire namespace in each header element.

FrontPage/Axis/WSDLJavaHeader (last edited 2009-09-20 22:48:27 by localhost)