use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.
the class SAAJOutInterceptor method handleMessage.
public void handleMessage(SoapMessage message) throws Fault {
SOAPMessage saaj = message.getContent(SOAPMessage.class);
try {
if (message.hasHeaders() && saaj != null && saaj.getSOAPPart().getEnvelope().getHeader() == null) {
// creating an empty SOAPHeader at this point in the
// pre-existing SOAPMessage avoids the <soap:body> and
// <soap:header> appearing in reverse order when the envolope
// is written to the wire
//
saaj.getSOAPPart().getEnvelope().addHeader();
}
} catch (SOAPException e) {
throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, message.getVersion().getSender());
}
if (saaj == null) {
SoapVersion version = message.getVersion();
try {
MessageFactory factory = getFactory(message);
SOAPMessage soapMessage = factory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
XMLStreamWriter origWriter = (XMLStreamWriter) message.get(ORIGINAL_XML_WRITER);
if (origWriter == null) {
origWriter = message.getContent(XMLStreamWriter.class);
}
message.put(ORIGINAL_XML_WRITER, origWriter);
W3CDOMStreamWriter writer = new SAAJStreamWriter(soapPart);
// Replace stax writer with DomStreamWriter
message.setContent(XMLStreamWriter.class, writer);
message.setContent(SOAPMessage.class, soapMessage);
message.setContent(Node.class, soapMessage.getSOAPPart());
} catch (SOAPException e) {
throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, version.getSender());
}
} else if (!message.containsKey(ORIGINAL_XML_WRITER)) {
// as the SOAPMessage already has everything in place, we do not need XMLStreamWriter to write
// anything for us, so we just set XMLStreamWriter's output to a dummy output stream.
XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class);
message.put(ORIGINAL_XML_WRITER, origWriter);
XMLStreamWriter dummyWriter = StaxUtils.createXMLStreamWriter(new OutputStream() {
public void write(int b) throws IOException {
}
public void write(byte[] b, int off, int len) throws IOException {
}
});
message.setContent(XMLStreamWriter.class, dummyWriter);
}
// Add a final interceptor to write the message
message.getInterceptorChain().add(SAAJOutEndingInterceptor.INSTANCE);
}
use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.
the class HandlerChainInvoker method setFaultMessage.
/*
* When the message direction is reversed, if the message is not already a
* fault message then it is replaced with a fault message
*/
private void setFaultMessage(MessageContext mc, Exception exception) {
Message msg = ((WrappedMessageContext) mc).getWrappedMessage();
msg.setContent(Exception.class, exception);
msg.removeContent(XMLStreamReader.class);
msg.removeContent(Source.class);
try {
SOAPMessage soapMessage = null;
SoapVersion version = null;
if (msg instanceof SoapMessage) {
version = ((SoapMessage) msg).getVersion();
}
soapMessage = SAAJFactoryResolver.createMessageFactory(version).createMessage();
msg.setContent(SOAPMessage.class, soapMessage);
SOAPBody body = SAAJUtils.getBody(soapMessage);
SOAPFault soapFault = body.addFault();
if (exception instanceof SOAPFaultException) {
SOAPFaultException sf = (SOAPFaultException) exception;
soapFault.setFaultString(sf.getFault().getFaultString());
SAAJUtils.setFaultCode(soapFault, sf.getFault().getFaultCodeAsQName());
soapFault.setFaultActor(sf.getFault().getFaultActor());
if (sf.getFault().hasDetail()) {
Node nd = soapMessage.getSOAPPart().importNode(sf.getFault().getDetail(), true);
nd = nd.getFirstChild();
soapFault.addDetail();
while (nd != null) {
soapFault.getDetail().appendChild(nd);
nd = nd.getNextSibling();
}
}
} else if (exception instanceof Fault) {
SoapFault sf = SoapFault.createFault((Fault) exception, ((SoapMessage) msg).getVersion());
soapFault.setFaultString(sf.getReason());
SAAJUtils.setFaultCode(soapFault, sf.getFaultCode());
if (sf.hasDetails()) {
soapFault.addDetail();
Node nd = soapMessage.getSOAPPart().importNode(sf.getDetail(), true);
nd = nd.getFirstChild();
while (nd != null) {
soapFault.getDetail().appendChild(nd);
nd = nd.getNextSibling();
}
}
} else {
SAAJUtils.setFaultCode(soapFault, new QName("http://cxf.apache.org/faultcode", "HandlerFault"));
soapFault.setFaultString(exception.getMessage());
}
} catch (SOAPException e) {
e.printStackTrace();
// do nothing
}
}
use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.
the class SOAPHandlerInterceptorTest method testChangeSOAPHeaderInBound.
@Test
public void testChangeSOAPHeaderInBound() throws Exception {
@SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
list.add(new SOAPHandler<SOAPMessageContext>() {
public boolean handleMessage(SOAPMessageContext smc) {
try {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (!outboundProperty.booleanValue()) {
// change mustUnderstand to false
SOAPMessage message = smc.getMessage();
SOAPHeader soapHeader = message.getSOAPHeader();
Element headerElementNew = (Element) soapHeader.getFirstChild();
SoapVersion soapVersion = Soap11.getInstance();
Attr attr = headerElementNew.getOwnerDocument().createAttributeNS(soapVersion.getNamespace(), "SOAP-ENV:mustUnderstand");
attr.setValue("false");
headerElementNew.setAttributeNodeNS(attr);
}
} catch (Exception e) {
throw new Fault(e);
}
return true;
}
public boolean handleFault(SOAPMessageContext smc) {
return true;
}
public Set<QName> getHeaders() {
return null;
}
public void close(MessageContext messageContext) {
}
});
HandlerChainInvoker invoker = new HandlerChainInvoker(list);
IMocksControl control = createNiceControl();
Binding binding = control.createMock(Binding.class);
expect(binding.getHandlerChain()).andReturn(list).anyTimes();
Exchange exchange = control.createMock(Exchange.class);
expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
// This is to set direction to inbound
expect(exchange.getOutMessage()).andReturn(null);
SoapMessage message = new SoapMessage(new MessageImpl());
message.setExchange(exchange);
XMLStreamReader reader = preparemXMLStreamReader("resources/greetMeRpcLitReq.xml");
message.setContent(XMLStreamReader.class, reader);
Object[] headerInfo = prepareSOAPHeader();
message.setContent(Node.class, headerInfo[0]);
Node node = ((Element) headerInfo[1]).getFirstChild();
message.getHeaders().add(new Header(new QName(node.getNamespaceURI(), node.getLocalName()), node));
control.replay();
SOAPHandlerInterceptor li = new SOAPHandlerInterceptor(binding);
li.handleMessage(message);
control.verify();
// Verify SOAPMessage header
SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
Element headerElementNew = DOMUtils.getFirstElement(soapMessageNew.getSOAPHeader());
SoapVersion soapVersion = Soap11.getInstance();
assertEquals("false", headerElementNew.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
// Verify XMLStreamReader
XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
QName qn = xmlReader.getName();
assertEquals("sendReceiveData", qn.getLocalPart());
// Verify Header Element
Iterator<Header> iter = message.getHeaders().iterator();
Element requiredHeader = null;
while (iter.hasNext()) {
Header localHdr = iter.next();
if (localHdr.getObject() instanceof Element) {
Element elem = (Element) localHdr.getObject();
if (elem.getNamespaceURI().equals("http://apache.org/hello_world_rpclit/types") && elem.getLocalName().equals("header1")) {
requiredHeader = (Element) localHdr.getObject();
break;
}
}
}
assertNotNull("Should have found header1", requiredHeader);
assertEquals("false", requiredHeader.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
}
use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.
the class SOAPHandlerInterceptorTest method preparemXMLStreamReader.
private XMLStreamReader preparemXMLStreamReader(String resouceName) throws Exception {
InputStream is = this.getClass().getResourceAsStream(resouceName);
XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(is);
// skip until soap body
if (xmlReader.nextTag() == XMLStreamConstants.START_ELEMENT) {
String ns = xmlReader.getNamespaceURI();
SoapVersion soapVersion = SoapVersionFactory.getInstance().getSoapVersion(ns);
// message.setVersion(soapVersion);
QName qn = xmlReader.getName();
while (!qn.equals(soapVersion.getBody()) && !qn.equals(soapVersion.getHeader())) {
while (xmlReader.nextTag() != XMLStreamConstants.START_ELEMENT) {
// nothing to do
}
qn = xmlReader.getName();
}
if (qn.equals(soapVersion.getHeader())) {
XMLStreamReader filteredReader = new PartialXMLStreamReader(xmlReader, soapVersion.getBody());
StaxUtils.read(filteredReader);
}
// advance just past body.
xmlReader.next();
while (xmlReader.isWhiteSpace()) {
xmlReader.next();
}
}
return xmlReader;
}
use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.
the class SOAPHandlerInterceptorTest method testChangeSOAPHeaderOutBound.
@Test
public void testChangeSOAPHeaderOutBound() throws Exception {
@SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
list.add(new SOAPHandler<SOAPMessageContext>() {
public boolean handleMessage(SOAPMessageContext smc) {
try {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
// change mustUnderstand to false
SOAPMessage message = smc.getMessage();
SOAPHeader soapHeader = message.getSOAPHeader();
Iterator<?> it = soapHeader.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "header1"));
SOAPHeaderElement headerElementNew = (SOAPHeaderElement) it.next();
SoapVersion soapVersion = Soap11.getInstance();
Attr attr = headerElementNew.getOwnerDocument().createAttributeNS(soapVersion.getNamespace(), "SOAP-ENV:mustUnderstand");
attr.setValue("false");
headerElementNew.setAttributeNodeNS(attr);
}
} catch (Exception e) {
throw new Fault(e);
}
return true;
}
public boolean handleFault(SOAPMessageContext smc) {
return true;
}
public Set<QName> getHeaders() {
return null;
}
public void close(MessageContext messageContext) {
}
});
HandlerChainInvoker invoker = new HandlerChainInvoker(list);
IMocksControl control = createNiceControl();
Binding binding = control.createMock(Binding.class);
expect(binding.getHandlerChain()).andReturn(list).anyTimes();
Exchange exchange = control.createMock(Exchange.class);
expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
SoapMessage message = new SoapMessage(new MessageImpl());
message.setExchange(exchange);
// This is to set direction to outbound
expect(exchange.getOutMessage()).andReturn(message).anyTimes();
CachedStream originalEmptyOs = new CachedStream();
message.setContent(OutputStream.class, originalEmptyOs);
InterceptorChain chain = new PhaseInterceptorChain((new PhaseManagerImpl()).getOutPhases());
// Interceptors after SOAPHandlerInterceptor DOMXMLStreamWriter to write
chain.add(new AbstractProtocolHandlerInterceptor<SoapMessage>(binding, Phase.MARSHAL) {
public void handleMessage(SoapMessage message) throws Fault {
try {
XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
SoapVersion soapVersion = Soap11.getInstance();
writer.setPrefix("soap", soapVersion.getNamespace());
writer.writeStartElement("soap", soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
writer.writeNamespace("soap", soapVersion.getNamespace());
Object[] headerInfo = prepareSOAPHeader();
StaxUtils.writeElement((Element) headerInfo[1], writer, true, false);
writer.writeEndElement();
writer.flush();
} catch (Exception e) {
// do nothing
}
}
});
chain.add(new SOAPHandlerInterceptor(binding));
message.setInterceptorChain(chain);
control.replay();
chain.doIntercept(message);
control.verify();
// Verify SOAPMessage header
SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
SOAPHeader soapHeader = soapMessageNew.getSOAPHeader();
Iterator<?> itNew = soapHeader.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "header1"));
SOAPHeaderElement headerElementNew = (SOAPHeaderElement) itNew.next();
SoapVersion soapVersion = Soap11.getInstance();
assertEquals("false", headerElementNew.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
originalEmptyOs.close();
}
Aggregations