Jonathan,
Using the MessageContext to retreive/set SOAP headers is the right way to do
things.
Here's how I do things in my code:
1 - Retreiving a SOAP Header Element from the SOAP message
----------------------------------------------------------
// Current SOAP request
Message inMessage = msgContext.getRequestMessage();
// SOAP Message envelope
SOAPEnvelope envelope = inMessage.getSOAPEnvelope();
// Retreiving a header element
// SOAPHeaderElement header =
envelope.getHeaderByName(__STR_NAMESPACE_URI__,
__STR_LOCALPART__);
In your code, there's no trace of a SOAPEnvelope object...
2 - Setting a new SOAP Header Element in the response
------------------------------------------------------
Here again, you must use the envelope of the SOAP message
// Current SOAP Response
Message outMessage = msgContext.getResponseMessage();
// SOAP Message envelope
SOAPEnvelope enveloppe = outMessage.getSOAPEnvelope();
// First create a SOAP Header Element
SOAPHeaderElement headerRoot =
new SOAPHeaderElement(__STR_NAMESPACE_URI__,
__STR_LOCALPART__);
// Insert the required element in headerRoot
headerRoot.addChildElement(...);
// Insert headerRoot in the message envelope
enveloppe.addHeader(headerRoot);
// Last, and here's the trick, create a NEW output message
// with the new modified envelope in order for the changes to
// take effect.
msgContext.setResponseMessage(new Message(envelope));
Enjoy Axis,
--
Tony ELSOKHON
France Telecom R&D/bizz/cil
-----Message d'origine-----
De : Jonathan J. Vargas R. [mailto:[EMAIL PROTECTED]
Envoyé : jeudi 18 août 2005 00:49
À : [email protected]
Objet : Modify SOAP Headers for Authentication
Greets,
I want to get and set the SOAP Headers for the messages transmited
to and from my webservice.
I saw I was able to retrieve SOAP request and response messages from
MessageContext, though, after I save headers they are not really seen
in the response message so i wondered if this was the right procedure
or it should be done in another way?
I want to work authentication through SOAP Headers, and this is the trivial
web service implementation class that I am using:
--
public class AuthImpl {
public boolean authenticate (String username, String password)
throws AuthServiceException {
boolean isValidUser = false;
try {
MessageContext mc = MessageContext.getCurrentContext ();
SOAPMessage req_message = mc.getRequestMessage ();
SOAPHeader sh = req_message.getSOAPHeader ();
// here how can i retrieve the SOAP Header element named
"AuthRequest" ?
// ...
// and then i compare its elements against my user's data base
// ...
// and then set isValidUser to true if found..
} catch (SOAPException se) {
// ...
}
if (!isValidUser) {
throw new AuthServiceException (
"Authentication failed for supplied credentials");
}
try {
MessageContext mc = MessageContext.getCurrentContext ();
SOAPMessage resp_message = mc.getResponseMessage ();
SOAPHeader sh = resp_message.getSOAPHeader ();
// and here examine SOAP header element named "AuthResponse" and
// set username, userid and those values retrieved from database
// to proper Soap header elements to be returned to caller.. how ?
} catch (SOAPException se) {
// ...
}
// set this object values with user info extracted from data base..
// and then return it..
return isValidUser;
}
---
--
Jonathan J. Vargas Rodriguez
[EMAIL PROTECTED]
"Conservar el silencio interior cuando vuelo en la tormenta me es
mejor, porque asi ya no soy otra gota de la tempestad"