SOAPResponse does not retain soap headers - java

When I add soap headers from handler.handlResponse(), I can see the headers added in the handler but these headers do not make it to the client.
Here is my handleResponse() method.
public static final String WEB_SERVICE_NAMESPACE_PREIFX = "dm";
public static final String WEB_SERVICE_NAMESPACE_URI = "urn:com.qwest.dms.dto";
public boolean handleResponse(MessageContext context)
{
logger.debug("TransactionLoggerHandler.handleResponse invoked");
try
{
SOAPMessageContext soapContext;
soapContext = (SOAPMessageContext)context;
SOAPMessage message = soapContext.getMessage();
SOAPHeader soapHeader = message.getSOAPHeader();
String version = "version";
SOAPHeaderElement header;
SOAPFactory soapFactory;
Name name;
logger.debug("Adding soap header ["+version+"] with value [2.0].");
soapHeader.addNamespaceDeclaration(Constants.WEB_SERVICE_NAMESPACE_PREIFX, Constants.WEB_SERVICE_NAMESPACE_URI)
SOAPHeaderElement headerElement
= (SOAPHeaderElement)message.getSOAPPart().getEnvelope().getHeader().addChildElement(
"version",
Constants.WEB_SERVICE_NAMESPACE_PREIFX,
Constants.WEB_SERVICE_NAMESPACE_URI );
headerElement.addTextNode("2.0");
String headerName="protocol";
String headerValue="2.0.0";
logger.debug("Adding soap header ["+headerName+"] with value ["+headerValue+"].");
soapFactory = SOAPFactory.newInstance();
name = soapFactory.createName(headerName,
Constants.WEB_SERVICE_NAMESPACE_PREIFX,
Constants.WEB_SERVICE_NAMESPACE_URI );
header = soapHeader.addHeaderElement( name );
header.addTextNode(headerValue);
message.saveChanges();
DmsUtil.printSOAPMessage(message);
logger.debug("Soap header ["+version+"] with value [2.0] added.");
}
catch (Exception e)
{
logger.error(e);
}
return true;
}
I see the output from this method as the following:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:dm="urn:com.qwest.dms.dto">
<dm:version>2.0</dm:version>
<dm:protocol>2.0.0</dm:protocol>
</env:Header>
<env:Body>
From the Client i get the following:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
</env:Envelope>
I am not sure why these headers not sent over the wire. Any help is appreciated. BTW, I am using jax rpc webservices under jboss4 (I know, i have to upgrade this but can not due to some constraints :( ).

Related

Remove empty ns attribute from SOAP request using EndpointInterceptor

There is this legacy system which is sending SOAP requests with an empty namespace attribute causing fields values to be null, when namespace attribute is removed while testing from SOAP UI, it work fine.
This is what problematic SOAP request looks like
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ADD xmlns="http://www.cc.com/ws">
<ID xmlns="">dummy</ID>
<PWD xmlns="">password</PWD>
</ADD>
</soapenv:Body>
</soapenv:Envelope>
I tried by adding an interceptor using addInterceptors method of WsConfigurerAdapter, code of which is pasted below, but it doesn't work
#Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
SOAPMessage soapMessage = ((SaajSoapMessage) messageContext.getRequest()).getSaajMessage();
SOAPBody body = soapMessage.getSOAPBody();
Iterator<SOAPBodyElement> it = body.getChildElements();
while (it.hasNext()) {
Object node = it.next();
if (!isTextNode(node)) {
com.sun.xml.internal.messaging.saaj.soap.ver1_1.BodyElement1_1Impl e =
(com.sun.xml.internal.messaging.saaj.soap.ver1_1.BodyElement1_1Impl) node;
Iterator<SOAPBodyElement> subItr = e.getChildElements();
while(subItr.hasNext()) {
Object subNode = subItr.next();
if(subNode instanceof com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl) {
SOAPElement el = (SOAPElement) subNode;
el.removeAttribute("xmlns");
}
}
}
}
soapMessage.writeTo(System.out);
return true;
}
Note that a namespace declaration in XML is not an attribute. Don't be confused by the similar syntax!
So I would not expect e.removeAttribute to do anything, because there is no attribute with the name xmlns.
What you actually need to do is to change the name of the element, e.g. using its setElementQName() method.
#Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
SOAPMessage soapMessage = ((SaajSoapMessage) messageContext.getRequest()).getSaajMessage();
SOAPBody body = soapMessage.getSOAPBody();
Iterator<SOAPBodyElement> it = body.getChildElements();
while (it.hasNext()) {
Object node = it.next();
if (!isTextNode(node)) {
com.sun.xml.internal.messaging.saaj.soap.ver1_1.BodyElement1_1Impl e =
(com.sun.xml.internal.messaging.saaj.soap.ver1_1.BodyElement1_1Impl) node;
Iterator<SOAPBodyElement> subItr = e.getChildElements();
while(subItr.hasNext()) {
Object subNode = subItr.next();
if(subNode instanceof com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl) {
SOAPElement el = (SOAPElement) subNode;
el.setElementQName(
new QName(
e.getElementName().getURI(),
el.getElementName().getLocalName()
)
);
}
}
}
}
soapMessage.writeTo(System.out);
return true;
}

Spring-WS: How to include a soapheader in the response message

I'm using Spring-WS to build a web service (contract first). I defined an endpoint like below
#Endpoint
public class ReportingEndpoint {
private static final Logger LOGGER = LoggerFactory.getLogger(ReportingEndpoint.class);
private static final String NAMESPACE_URI = "http://localhost/reporting";
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "RequestDocument")
#ResponsePayload
public ResponseDocument accountReporting(
#RequestPayload JAXBElement<RequestDocument> request,
#SoapHeader(value = "{http://localhost/reporting}Hdr") SoapHeaderElement header) {
try {
ApplicationHeader headers = ((JAXBElement<ApplicationHeader>) JAXBUtils
.unmarshal(header.getSource(), ObjectFactory.class)).getValue();
LOGGER.info(headers.getSystemName());
LOGGER.info("Hello world.");
ResponseDocument response = new ResponseDocument();
response.setReportTitle("Report Title");
return response;
} catch (Exception ex) {
return null;
}
}
}
This code can receive and read the soap header sent from client but when I return a response message, I don't know how to send back to client the server soap header as the client did.
Can anybody help me to solve this issue?

Set Header of A Soap Request in Spring

I have been working on integrating a SOAP API with my Spring API. I have to add headers to my request,but i'm getting this is,
Could not complete request
org.springframework.ws.soap.client.SoapFaultClientException: No WS-Security header found
Please find my code below
public String createSaleOrder(Suborder suborder)
{
SaleOrder saleorder = new SaleOrder();
saleorder = setSaleOrderObject(suborder);
CreateSaleOrderRequest request = new CreateSaleOrderRequest();
request.setSaleOrder(saleorder);
this.getWebServiceTemplate().marshalSendAndReceive(uri, request,
new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException
{
SoapMessage soapmessage = (SoapMessage)message;
SoapHeader header = soapmessage.getSoapHeader();
StringBuilder soapheader = new StringBuilder();
soapheader.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://wewewqdad.com/services/\">");
soapheader.append("<soapenv:Header>");
soapheader.append("<wsse:Security soapenv:mustUnderstand=\"1\" xmlns:wsse=\"http://docs.oasis-open.asdasda-1.0.xsd\"> ");
soapheader.append("<wsse:UsernameToken wsu:Id=\"UsernameToken-2\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oaasdasd-dasdasd-1.0.xsd\">");
soapheader.append("<wsse:Username>username</wsse:Username>");
soapheader.append("<wsse:Password Type=\"http://docs.aseasdasd-wss-username-token-profile-1.0#PasswordText\">password</wsse:Password>");
soapheader.append("<wsse:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">VMEZT//J0bZw7HfZZyXAZQ==</wsse:Nonce>");
soapheader.append("<wsu:Created>2014-09-04 T1015.41.649Z</wsu:Created>");
soapheader.append("</wsse:UsernameToken>");
soapheader.append("</wsse:Security>");
soapheader.append("</soapenv:Header>");
soapheader.append("<soapenv:Body>");
soapheader.append("</soapenv:Body>");
soapheader.append("</soapenv:Envelope>");
StringSource HeaderSource = new StringSource(soapheader.toString());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(HeaderSource,header.getResult());
}
});
return "Pushed";
}
UPDATE
The header is set. But the request object now contains extra tags which is not required and hence throwing errors. And also the tags like how can we change these to
Raw soaprequest is given below :
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><soapenv:Envelope xmlns:ser="http://abc.efg.com/services/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/asdasdasd-1.0.xsd" soapenv:mustUnderstand="1"> <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="UsernameToken-2"><wsse:Username>asdasdasd</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oaasdasda-1.0#PasswordText">adsasdasdasda</wsse:Password><wsse:Nonce EncodingType="http://dasdasda004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">VMESASDASZT//asdasdasd</wsse:Nonce><wsu:Created>2014-09-04 T1015.41.649Z</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header></soapenv:Envelope></SOAP-ENV:Header><SOAP-ENV:Body><ns2:CreateSaleOrderRequest xmlns:ns2="http://asdasd.com/services/"><ns2:SaleOrder><ns2:DisplayOrderCode>156</ns2:DisplayOrderCode></ns2:SaleOrder></ns2:CreateSaleOrderRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>
In your soapHeader you wrote more than the actual header but the whole Soap Envelope. Not sure this is expected.
Anyway, you could use a library to handle WS Security like WSS4J (often included in other frameworks).

SOAP: Migrate method from AXIS 1.4 to CXF for adding a new header

Hi! I'm trying to add a header to a SOAP call.
I'm new at working with web services.
Every time I need to use the web service for download a file, I need to add a header like:
<UserIdHeader xmlns="http://www.stats.com/">
<TicketId>defe3a08-4c8a-47c4-9303-98e09c475532</TicketId>
</UserIdHeader>
I'm using CXF. I have one example made with Axis 1.4.
The only thing that is remaining (I hope) is to add this header.
This is what I have to update:
private static void addHeader(String ticketId) {
SOAPHeaderElement header = new SOAPHeaderElement("http://www.stats.com/", "UserIdHeader");
SOAPElement node;
org.apache.axis.client.Stub s = (Stub) service;
s.clearHeaders();
try {
node = header.addChildElement("TicketId");
node.addTextNode(ticketId);
s.setHeader((org.apache.axis.message.SOAPHeaderElement) header);
} catch (SOAPException e) {
e.printStackTrace();
}
}
And this is how I tried to update that:
private static void addHeader(String ticketId) throws JAXBException, ParserConfigurationException {
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element eTicketId = doc.createElement("TicketId");
eTicketId.setTextContent(ticketId);
List<Header> headers = new ArrayList<Header>();
Header header = new Header(new QName("http://www.stats.com/", "UserIdHeader"), eTicketId,
new JAXBDataBinding(String.class));
headers.add(header);
BindingProvider.class.cast(service).getRequestContext().put(Header.HEADER_LIST, headers);
}
But I'm getting the following exception:
[PhaseInterceptorChain] Interceptor for {http://www.stats.com/}Service#{http://www.stats.com/}GetFileList has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Marshalling Error: org.apache.xerces.dom.ElementImpl is not known to this context
at org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(JAXBEncoderDecoder.java:261)
at org.apache.cxf.jaxb.io.DataWriterImpl.write(DataWriterImpl.java:168)
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.writeSoapEnvelopeStart(SoapOutInterceptor.java:156)
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.handleMessage(SoapOutInterceptor.java:81)
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.handleMessage(SoapOutInterceptor.java:61)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:531)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:464)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:367)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:320)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:89)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
I tried even add an interceptor, but I didn't know how to pass the ticketId.
Any idea?
If you are creating a DOM element, you shouldn't set a databinding into the Header object. You only need the Databinding if you are using a JAXB (or other) type. CXF handles DOM elements directly.
This is how I have the method working (for some reason, with the previous approach the header wasn't written):
private static void addHeader(String ticketId) {
try {
SOAPFactory sf = SOAPFactory.newInstance();
SOAPElement userIdHeaderElement = sf.createElement(new QName("http://www.stats.com/", "UserIdHeader"));
SOAPElement ticketIdElement = sf.createElement(new QName(null, "TicketId"));
ticketIdElement.addTextNode(ticketId);
userIdHeaderElement.addChildElement(ticketIdElement);
List<Header> headers = new ArrayList<Header>();
Header dummyHeader = new Header(new QName("http://www.stats.com/"), userIdHeaderElement);
headers.add(dummyHeader);
BindingProvider.class.cast(service).getRequestContext().put(Header.HEADER_LIST, headers);
} catch (SOAPException e) {
e.printStackTrace();
}
}

How to get SOAP headers

Here is the request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soap="http://soap.ws.server.wst.fit.cvut.cz/">
<soapenv:Header>
<userId>someId</userId>
</soapenv:Header>
<soapenv:Body>
...
</soapenv:Body>
</soapenv:Envelope>
and I want to get that userId.
I tried this
private List<Header> getHeaders() {
MessageContext messageContext = context.getMessageContext();
if (messageContext == null || !(messageContext instanceof WrappedMessageContext)) {
return null;
}
Message message = ((WrappedMessageContext) messageContext).getWrappedMessage();
return CastUtils.cast((List<?>) message.get(Header.HEADER_LIST));
}
private String getHeader(String name) {
List<Header> headers = getHeaders();
if (headers != null) {
for (Header header : headers) {
logger.debug(header.getObject());
// return header by the given name
}
}
return null;
}
And it logs [userId : null]. How can I get the value and why is null there?
"[userId : null]" is generally the "toString" printout of a DOM element. Most likely if you do something like
logger.debug(header.getObject().getClass())
you will see that it is a DOM Element subclass of somesort. Thus, something like:
logger.debug(((Element)header.getObject()).getTextContent())
might print the text node.
import javax.xml.soap.*;
SOAPPart part = request.getSOAPPart();
SOAPEnvelope env = part.getEnvelope();
SOAPHeader header = env.getHeader();
if (header == null) {
// Throw an exception
}
NodeList userIdNode = header.getElementsByTagNameNS("*", "userId");
String userId = userIdNode.item(0).getChildNodes().item(0).getNodeValue();
You can get soap headers without Interceptors and without JAXB.
In your service_impl class add :
public class YourFunctionNameImpl implements YourFunctionName{
#Resource
private WebServiceContext context;
private List<Header> getHeaders() {
MessageContext messageContext = context.getMessageContext();
if (messageContext == null || !(messageContext instanceof WrappedMessageContext)) {
return null;
}
Message message = ((WrappedMessageContext) messageContext).getWrappedMessage();
List<Header> headers = CastUtils.cast((List<?>) message.get(Header.HEADER_LIST));
return headers;
}
...
Then in your function you can use:
List<Header> headers = getHeaders();
for(Iterator<Header> i = headers.iterator(); i.hasNext();) {
Header h = i.next();
Element n = (Element)h.getObject();
System.out.println("header name="+n.getLocalName());
System.out.println("header content="+n.getTextContent());
}
We can get SOAP header in server side by adding following code in CXF interceptor.
Create a class like
public class ServerCustomHeaderInterceptor extends AbstractSoapInterceptor {
#Resource
private WebServiceContext context;
public ServerCustomHeaderInterceptor() {
super(Phase.INVOKE);
}
#Override
public void handleMessage(SoapMessage message) throws Fault,JAXBException {
System.out.println("ServerCustomHeaderInterceptor handleMessage");
JAXBContext jc=null;
Unmarshaller unmarshaller=null;
try {
jc = JAXBContext.newInstance("org.example.hello_ws");
unmarshaller = jc.createUnmarshaller();
} catch (JAXBException e) {
e.printStackTrace();
}
List<Header> list = message.getHeaders();
for (Header header : list) {
ElementNSImpl el = (ElementNSImpl) header.getObject();
ParentNode pn= (ParentNode) el.getFirstChild();
//Node n1= (Node) pn;
//Node n1= (Node) el.getFirstChild();
CustomHeader customHeader=(CustomHeader) unmarshaller.unmarshal(el.getFirstChild());
}
}
After this we need to inject this as a interceptor like
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean class="org.example.hellows.soap12.ServerCustomHeaderInterceptor" />
</jaxws:inInterceptors>
in your server context xml.
We may need to change few lines as per your requirements. Basic flow will work like this.
Having a MessageContext messageContext, you can use this code:
HeaderList hl = (HeaderList) messageContext.get(JAXWSProperties.INBOUND_HEADER_LIST_PROPERTY);
which gives you access to all SOAP headers.

Categories