use of org.apache.cxf.helpers.NSStack in project cxf by apache.
the class RPCOutInterceptor method handleMessage.
public void handleMessage(Message message) {
XMLStreamWriter origXmlWriter = null;
try {
NSStack nsStack = new NSStack();
nsStack.push();
BindingOperationInfo operation = message.getExchange().getBindingOperationInfo();
assert operation.getName() != null;
XMLStreamWriter xmlWriter = getXMLStreamWriter(message);
CachingXmlEventWriter cache = null;
// need to cache the events in case validation fails or buffering is enabled
if (shouldBuffer(message)) {
origXmlWriter = xmlWriter;
cache = new CachingXmlEventWriter();
try {
cache.setNamespaceContext(xmlWriter.getNamespaceContext());
} catch (XMLStreamException e) {
// ignorable, will just get extra namespace decls
}
message.setContent(XMLStreamWriter.class, cache);
xmlWriter = cache;
}
List<MessagePartInfo> parts = null;
boolean output = false;
if (!isRequestor(message)) {
if (operation.getOutput() == null) {
return;
}
parts = operation.getOutput().getMessageParts();
output = true;
} else {
parts = operation.getInput().getMessageParts();
output = false;
}
MessageContentsList objs = MessageContentsList.getContentsList(message);
if (objs == null) {
addOperationNode(nsStack, message, xmlWriter, output, operation);
xmlWriter.writeEndElement();
return;
}
for (MessagePartInfo part : parts) {
if (objs.hasValue(part)) {
Object o = objs.get(part);
if (o == null) {
// WSI-BP R2211 - RPC/Lit parts are not allowed to be xsi:nil
throw new Fault(new org.apache.cxf.common.i18n.Message("BP_2211_RPCLIT_CANNOT_BE_NULL", LOG, part.getConcreteName()));
}
// WSI-BP R2737 -RPC/LIG part name space is empty
// part.setConcreteName(new QName("", part.getConcreteName().getLocalPart()));
}
}
addOperationNode(nsStack, message, xmlWriter, output, operation);
writeParts(message, message.getExchange(), operation, objs, parts);
// Finishing the writing.
xmlWriter.writeEndElement();
if (cache != null) {
try {
for (XMLEvent event : cache.getEvents()) {
StaxUtils.writeEvent(event, origXmlWriter);
}
} catch (XMLStreamException e) {
throw new Fault(e);
}
}
} catch (XMLStreamException e) {
throw new Fault(e);
} finally {
if (origXmlWriter != null) {
message.setContent(XMLStreamWriter.class, origXmlWriter);
}
}
}
use of org.apache.cxf.helpers.NSStack in project cxf by apache.
the class XMLFaultOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
if (mustPropogateException(message)) {
throw (Fault) message.getContent(Exception.class);
}
Fault f = (Fault) message.getContent(Exception.class);
message.put(org.apache.cxf.message.Message.RESPONSE_CODE, f.getStatusCode());
NSStack nsStack = new NSStack();
nsStack.push();
XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
XMLFault xmlFault = XMLFault.createFault(f);
try {
nsStack.add(XMLConstants.NS_XML_FORMAT);
String prefix = nsStack.getPrefix(XMLConstants.NS_XML_FORMAT);
StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_ROOT, XMLConstants.NS_XML_FORMAT);
StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_STRING, XMLConstants.NS_XML_FORMAT);
Throwable t = xmlFault.getCause();
writer.writeCharacters(t == null ? xmlFault.getMessage() : t.toString());
// fault string
writer.writeEndElement();
if (xmlFault.getDetail() != null) {
Element detail = xmlFault.getDetail();
StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_DETAIL, XMLConstants.NS_XML_FORMAT);
Node node = detail.getFirstChild();
while (node != null) {
StaxUtils.writeNode(node, writer, false);
node = node.getNextSibling();
}
writer.writeEndElement();
}
// fault root
writer.writeEndElement();
writer.flush();
} catch (XMLStreamException xe) {
throw new Fault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), xe);
}
}
use of org.apache.cxf.helpers.NSStack in project cxf by apache.
the class JAXRSDefaultFaultOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
if (PropertyUtils.isTrue(message.getExchange().get(JAXRSUtils.SECOND_JAXRS_EXCEPTION))) {
return;
}
final Fault f = (Fault) message.getContent(Exception.class);
Response r = JAXRSUtils.convertFaultToResponse(f.getCause(), message);
if (r != null) {
JAXRSUtils.setMessageContentType(message, r);
message.setContent(List.class, new MessageContentsList(r));
if (message.getExchange().getOutMessage() == null && message.getExchange().getOutFaultMessage() != null) {
message.getExchange().setOutMessage(message.getExchange().getOutFaultMessage());
}
new JAXRSOutInterceptor().handleMessage(message);
return;
}
ServerProviderFactory.releaseRequestState(message);
if (mustPropogateException(message)) {
throw f;
}
new StaxOutInterceptor().handleMessage(message);
message.put(org.apache.cxf.message.Message.RESPONSE_CODE, f.getStatusCode());
NSStack nsStack = new NSStack();
nsStack.push();
XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
try {
nsStack.add("http://cxf.apache.org/bindings/xformat");
String prefix = nsStack.getPrefix("http://cxf.apache.org/bindings/xformat");
StaxUtils.writeStartElement(writer, prefix, "XMLFault", "http://cxf.apache.org/bindings/xformat");
StaxUtils.writeStartElement(writer, prefix, "faultstring", "http://cxf.apache.org/bindings/xformat");
Throwable t = f.getCause();
writer.writeCharacters(t == null ? f.getMessage() : t.toString());
// fault string
writer.writeEndElement();
if (f.getDetail() != null) {
StaxUtils.writeStartElement(writer, prefix, "detail", "http://cxf.apache.org/bindings/xformat");
StaxUtils.writeNode(DOMUtils.getChild(f.getDetail(), Node.ELEMENT_NODE), writer, false);
writer.writeEndElement();
}
// fault root
writer.writeEndElement();
writer.flush();
} catch (XMLStreamException xe) {
throw new Fault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), xe);
}
}
Aggregations