So I ended up extending a messagefactory and a soapMessage (axiom in my case) so that I could intercept the setting of the SoapAction header and insert mine (a cookie).
Arjen and others, please consider adding an addHeader(String name, String value) method to SoapMessage. Only being able to set the SoapAction header is very limiting.
Here's what I did with the Axiom classes:
Code:
public class CustomAxiomSoapMessage extends AxiomSoapMessage {
private String cookie;
public CustomAxiomSoapMessage(SOAPFactory soapFactory) {
super(soapFactory);
}
public CustomAxiomSoapMessage(SOAPFactory soapFactory, boolean payloadCaching) {
super(soapFactory, payloadCaching);
}
public CustomAxiomSoapMessage(SOAPMessage soapMessage, String soapAction, boolean payloadCaching) {
super(soapMessage, soapAction, payloadCaching);
}
public CustomAxiomSoapMessage(SOAPMessage soapMessage, Attachments attachments, String soapAction, boolean payloadCaching) {
super(soapMessage, attachments, soapAction, payloadCaching);
}
public void writeTo(OutputStream outputStream) throws IOException {
// set cookie before writing out
if (outputStream instanceof TransportOutputStream) {
TransportOutputStream transportOutputStream = (TransportOutputStream) outputStream;
if (cookie != null) {
transportOutputStream.addHeader("Cookie", cookie);
}
}
super.writeTo(outputStream);
}
public void setCookie(String cookie) {
this.cookie = cookie;
}
}
public class CustomAxiomSoapMessageFactory extends AxiomSoapMessageFactory {
private boolean payloadCaching = true;
// use SOAP 1.1 by default
private SOAPFactory soapFactory = new SOAP11Factory();
public WebServiceMessage createWebServiceMessage() {
return new CustomAxiomSoapMessage(soapFactory, payloadCaching);
}
public void setSoapVersion(SoapVersion version) {
super.setSoapVersion(version);
if (SoapVersion.SOAP_11 == version) {
soapFactory = new SOAP11Factory();
}
else if (SoapVersion.SOAP_12 == version) {
soapFactory = new SOAP12Factory();
}
else {
throw new IllegalArgumentException(
"Invalid version [" + version + "]. " + "Expected the SOAP_11 or SOAP_12 constant");
}
}
public void setPayloadCaching(boolean payloadCaching) {
super.setPayloadCaching(payloadCaching);
this.payloadCaching = payloadCaching;
}
}