How to add a ResponseHeader using Axis?

S

stacnebben

I am able to set my RequestHeader using the Axis API, but have not
found a way to set the ResponseHeader. Has anyone else had this issue,
source code on how to set the ResponseHeader would be greatly
appreciate.
Thank you.
 
I

iksrazal

Not sure how you are setting the request header, as they can be
manipulated on both the client and server side. I haven't used axis 1.2
yet, however. The way I do it, for xml encryption and digital
signatures, is via the javax.xml.rpc.handler.Handler interface and the
javax.xml.rpc.handler.MessageContext class. In your case, you would
implement:

handleResponse(MessageContext context)

And via the message context convert the message to a Document. Plenty
of examples out there. Then, you would call a method such as:

public static boolean sign(Document doc, X509Certificate cert,
PrivateKey privateKey, boolean debug) throws WSSecurityException
{
try
{
//Add header to the SOAP message if it does not exist
String soap_header = "http://schemas.xmlsoap.org/soap/envelope/";
// Initialize the library - this is now done inside servlet
WSSInit
org.apache.xml.security.Init.init();

/******************* XML SIGNATURE INIT ***********************
Append the signature element to proper location before signing
***************************************************************/
// Look for the SOAP header
Element headerElement = null;
NodeList nodes = doc.getElementsByTagNameNS (soap_header,
"Header");
//No nodes are expected to be found (length of zero) - add
//header here.
if(nodes.getLength() == 0)
{
headerElement = doc.createElementNS (soap_header, "Header");
nodes = doc.getElementsByTagNameNS (soap_header, "Envelope");
if(nodes != null)
{
Element envelopeElement = (Element)nodes.item(0);
headerElement.setPrefix(envelopeElement.getPrefix());
envelopeElement.appendChild(headerElement);
}
}
else
{
//This shouldn't happen unless explicity done elsewhere
Fwlog.debug(SecurityHelper.class, Fwlog.WI, "Unexpectedly Found
" + nodes.getLength() + " SOAP Header elements... probably ok but not
tested");
headerElement = (Element)nodes.item(0);
}

... sign the Doc
}

This method, as I use it, is called by both client side and server side
handlers.

HTH,
iksrazal
http://www.braziloutsource.com/
 
S

stacnebben

Thank you for the response. I probably should have included how I am
setting the requestHeader (client side). I want the response message
(server) to have a similar custom SOAPHeader element. Most of the
classes used below were auto generated using the Axis WSDL2Java
utility.

Test service = new TestLocator();
((TestLocator)service).setTestSoapEndpointAddress(endpointURL);
TestSoap_PortType port = service.getTestSoap();

SOAPHeaderElement header = new SOAPHeaderElement("http://www.test.org",
"TestMsgHeader", new TestCustomMsgHeader());
((TestSoap_BindingStub)port).setHeader(header);


I was hoping there was something similar to this that would allow me to
set the ResponseHeader message.
Thank you again for any thoughts/comments/code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top