use of javax.xml.soap.SOAPMessage in project cxf by apache.
the class LogicalHandlerOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
if (binding.getHandlerChain().isEmpty()) {
return;
}
HandlerChainInvoker invoker = getInvoker(message);
if (invoker.getLogicalHandlers().isEmpty()) {
return;
}
XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class);
Node nd = message.getContent(Node.class);
SOAPMessage m = message.getContent(SOAPMessage.class);
Document document = null;
if (m != null) {
document = m.getSOAPPart();
} else if (nd != null) {
document = nd.getOwnerDocument();
} else {
document = DOMUtils.newDocument();
message.setContent(Node.class, document);
}
W3CDOMStreamWriter writer = new W3CDOMStreamWriter(document.createDocumentFragment());
// Replace stax writer with DomStreamWriter
message.setContent(XMLStreamWriter.class, writer);
message.put(ORIGINAL_WRITER, origWriter);
message.getInterceptorChain().add(ending);
}
use of javax.xml.soap.SOAPMessage in project cxf by apache.
the class LogicalMessageImpl method setPayload.
public void setPayload(Source s) {
Message message = msgContext.getWrappedMessage();
Service.Mode mode = (Service.Mode) msgContext.getWrappedMessage().getContextualProperty(Service.Mode.class.getName());
SOAPMessage m = message.getContent(SOAPMessage.class);
if (m != null && !MessageUtils.isOutbound(message)) {
try {
SAAJUtils.getBody(m).removeContents();
W3CDOMStreamWriter writer = new W3CDOMStreamWriter(SAAJUtils.getBody(m));
StaxUtils.copy(s, writer);
writer.flush();
writer.close();
if (mode == Service.Mode.MESSAGE) {
s = new DOMSource(m.getSOAPPart());
} else {
s = new DOMSource(SAAJUtils.getBody(m).getFirstChild());
}
W3CDOMStreamReader r = new W3CDOMStreamReader(DOMUtils.getFirstElement(SAAJUtils.getBody(m)));
message.setContent(XMLStreamReader.class, r);
} catch (Exception e) {
throw new Fault(e);
}
} else if (mode != null) {
if (message instanceof SoapMessage) {
if (mode == Service.Mode.MESSAGE) {
try {
// REVISIT: should try to use the original SOAPMessage
// instead of creating a new empty one.
SOAPMessage msg = initSOAPMessage(null);
write(s, SAAJUtils.getBody(msg));
s = new DOMSource(msg.getSOAPPart());
} catch (Exception e) {
throw new Fault(e);
}
}
} else if (message instanceof XMLMessage && message.getContent(DataSource.class) != null) {
throw new Fault(new org.apache.cxf.common.i18n.Message("GETPAYLOAD_OF_DATASOURCE_NOT_VALID_XMLHTTPBINDING", LOG));
}
} else {
XMLStreamReader reader = StaxUtils.createXMLStreamReader(s);
msgContext.getWrappedMessage().setContent(XMLStreamReader.class, reader);
}
msgContext.getWrappedMessage().setContent(Source.class, s);
}
use of javax.xml.soap.SOAPMessage in project cxf by apache.
the class LogicalMessageImpl method getPayload.
public Source getPayload() {
Source source = null;
Service.Mode mode = msgContext.getWrappedMessage().getExchange().get(Service.Mode.class);
if (mode != null) {
// Dispatch/Provider case
source = handleDispatchProviderCase(mode);
} else {
Message message = msgContext.getWrappedMessage();
source = message.getContent(Source.class);
if (source == null) {
// need to convert
SOAPMessage msg = message.getContent(SOAPMessage.class);
XMLStreamReader reader = null;
if (msg != null) {
try {
Node node = SAAJUtils.getBody(msg).getFirstChild();
while (node != null && !(node instanceof Element)) {
node = node.getNextSibling();
}
source = new DOMSource(node);
reader = StaxUtils.createXMLStreamReader(source);
} catch (SOAPException e) {
throw new Fault(e);
}
}
if (source == null) {
try {
DocumentFragment doc = DOMUtils.getEmptyDocument().createDocumentFragment();
W3CDOMStreamWriter writer = new W3CDOMStreamWriter(doc);
reader = message.getContent(XMLStreamReader.class);
// content must be an element thing, skip over any whitespace
StaxUtils.toNextTag(reader);
StaxUtils.copy(reader, writer, true);
doc.appendChild(DOMUtils.getFirstElement(writer.getCurrentFragment()));
source = new DOMSource(DOMUtils.getFirstElement(doc));
reader = StaxUtils.createXMLStreamReader(DOMUtils.getFirstElement(doc));
} catch (XMLStreamException e) {
throw new Fault(e);
}
}
message.setContent(XMLStreamReader.class, reader);
message.setContent(Source.class, source);
} else if (!(source instanceof DOMSource)) {
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
XMLStreamReader reader = message.getContent(XMLStreamReader.class);
if (reader == null) {
reader = StaxUtils.createXMLStreamReader(source);
}
try {
StaxUtils.copy(reader, writer);
} catch (XMLStreamException e) {
throw new Fault(e);
}
source = new DOMSource(writer.getDocument().getDocumentElement());
reader = StaxUtils.createXMLStreamReader(writer.getDocument());
message.setContent(XMLStreamReader.class, reader);
message.setContent(Source.class, source);
}
}
return source;
}
use of javax.xml.soap.SOAPMessage in project cxf by apache.
the class LogicalMessageImpl method handleDispatchProviderCase.
private Source handleDispatchProviderCase(Service.Mode mode) {
Source source = null;
Message message = msgContext.getWrappedMessage();
Source obj = message.getContent(Source.class);
if (message instanceof SoapMessage) {
// StreamSource may only be used once, need to make a copy
if (obj instanceof StreamSource) {
try (CachedOutputStream cos = new CachedOutputStream()) {
StaxUtils.copy(obj, cos);
obj = new StreamSource(cos.getInputStream());
message.setContent(Source.class, new StreamSource(cos.getInputStream()));
} catch (Exception e) {
throw new Fault(e);
}
}
if (mode == Service.Mode.PAYLOAD) {
source = obj;
} else {
try (CachedOutputStream cos = new CachedOutputStream()) {
StaxUtils.copy(obj, cos);
InputStream in = cos.getInputStream();
SOAPMessage msg = initSOAPMessage(in);
source = new DOMSource(SAAJUtils.getBody(msg).getFirstChild());
in.close();
} catch (Exception e) {
throw new Fault(e);
}
}
} else if (message instanceof XMLMessage) {
if (obj != null) {
source = obj;
} else if (message.getContent(DataSource.class) != null) {
throw new Fault(new org.apache.cxf.common.i18n.Message("GETPAYLOAD_OF_DATASOURCE_NOT_VALID_XMLHTTPBINDING", LOG));
}
}
return source;
}
use of javax.xml.soap.SOAPMessage in project cxf by apache.
the class SOAPHandlerFaultOutInterceptor method handleMessageInternal.
private void handleMessageInternal(SoapMessage message) {
MessageContext context = createProtocolMessageContext(message);
HandlerChainInvoker invoker = getInvoker(message);
invoker.setProtocolMessageContext(context);
try {
if (!invoker.invokeProtocolHandlersHandleFault(isRequestor(message), context)) {
// handleAbort(message, context);
}
} catch (RuntimeException exception) {
/*
* handleFault throws exception, in this case we need to replace
* SOAPFault with the exception thrown from HandleFault so that the
* exception can be dispatched.
*/
try {
SOAPMessage originalMsg = message.getContent(SOAPMessage.class);
SOAPBody body = originalMsg.getSOAPPart().getEnvelope().getBody();
body.removeContents();
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 = originalMsg.getSOAPPart().importNode(sf.getFault().getDetail().getFirstChild(), true);
soapFault.addDetail().appendChild(nd);
}
} else if (exception instanceof Fault) {
SoapFault sf = SoapFault.createFault((Fault) exception, message.getVersion());
soapFault.setFaultString(sf.getReason());
SAAJUtils.setFaultCode(soapFault, sf.getFaultCode());
if (sf.hasDetails()) {
soapFault.addDetail();
Node nd = originalMsg.getSOAPPart().importNode(sf.getDetail(), true);
nd = nd.getFirstChild();
while (nd != null) {
soapFault.getDetail().appendChild(nd);
nd = nd.getNextSibling();
}
}
} else {
soapFault.setFaultString(exception.getMessage());
SAAJUtils.setFaultCode(soapFault, new QName("http://cxf.apache.org/faultcode", "HandleFault"));
}
} catch (SOAPException e) {
// do nothing
e.printStackTrace();
}
}
onCompletion(message);
}
Aggregations