use of org.apache.cxf.staxutils.FragmentStreamReader in project cxf by apache.
the class XMLStreamDataReader method resetForStreaming.
private XMLStreamReader resetForStreaming(XMLStreamReader input) throws XMLStreamException {
// is not closed and additional parts are not read and such
if (message != null) {
if (message.getInterceptorChain() != null) {
message.getInterceptorChain().remove(StaxInEndingInterceptor.INSTANCE);
message.getInterceptorChain().add(new StaxInEndingInterceptor(Phase.POST_INVOKE));
}
message.removeContent(XMLStreamReader.class);
final InputStream ins = message.getContent(InputStream.class);
message.removeContent(InputStream.class);
input = new FragmentStreamReader(input, true) {
boolean closed;
public boolean hasNext() throws XMLStreamException {
boolean b = super.hasNext();
if (!b && !closed) {
close();
}
return b;
}
public void close() throws XMLStreamException {
closed = true;
try {
super.close();
} catch (XMLStreamException e) {
// ignore
}
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
// ignore
}
}
}
};
}
return input;
}
use of org.apache.cxf.staxutils.FragmentStreamReader in project cxf by apache.
the class DocumentType method readObject.
@Override
public Object readObject(MessageReader mreader, Context context) throws DatabindingException {
try {
XMLStreamReader reader = ((ElementReader) mreader).getXMLStreamReader();
// we need to eat the surrounding element.
reader.nextTag();
Object tree = StaxUtils.read(null, new FragmentStreamReader(reader), true);
// eat the end tag.
reader.nextTag();
return tree;
} catch (XMLStreamException e) {
throw new DatabindingException("Could not parse xml.", e);
}
}
use of org.apache.cxf.staxutils.FragmentStreamReader in project cxf by apache.
the class Soap12FaultInInterceptor method unmarshalFault.
public static SoapFault unmarshalFault(SoapMessage message, XMLStreamReader reader) {
String exMessage = null;
QName faultCode = null;
List<QName> subCodes = null;
String role = null;
String node = null;
Element detail = null;
String lang = null;
Map<String, String> ns = new HashMap<>();
ns.put("s", Soap12.SOAP_NAMESPACE);
XPathUtils xu = new XPathUtils(ns);
try {
Node mainNode = message.getContent(Node.class);
Node fault = null;
if (reader instanceof W3CDOMStreamReader) {
W3CDOMStreamReader dr = (W3CDOMStreamReader) reader;
fault = dr.getCurrentElement();
dr.consumeFrame();
} else if (mainNode != null) {
Node bodyNode = (Node) xu.getValue("//s:Body", mainNode, XPathConstants.NODE);
StaxUtils.readDocElements(bodyNode.getOwnerDocument(), bodyNode, new FragmentStreamReader(reader), false, false);
fault = (Element) xu.getValue("//s:Fault", bodyNode, XPathConstants.NODE);
} else {
fault = StaxUtils.read(new FragmentStreamReader(reader));
}
fault = DOMUtils.getDomElement(fault);
Element el = (Element) xu.getValue("//s:Fault/s:Code/s:Value", fault, XPathConstants.NODE);
if (el != null) {
faultCode = DOMUtils.createQName(el.getTextContent(), el);
}
el = (Element) xu.getValue("//s:Fault/s:Code/s:Subcode", fault, XPathConstants.NODE);
if (el != null) {
subCodes = new LinkedList<QName>();
NodeList vlist = el.getElementsByTagNameNS(Soap12.SOAP_NAMESPACE, "Value");
for (int i = 0; i < vlist.getLength(); i++) {
Node v = vlist.item(i);
subCodes.add(DOMUtils.createQName(v.getTextContent(), v));
}
}
exMessage = (String) xu.getValue("//s:Fault/s:Reason/s:Text/text()", fault, XPathConstants.STRING);
lang = (String) xu.getValue("//s:Fault/s:Reason/s:Text/@xml:lang", fault, XPathConstants.STRING);
Node detailNode = (Node) xu.getValue("//s:Fault/s:Detail", fault, XPathConstants.NODE);
if (detailNode != null) {
detail = (Element) detailNode;
}
role = (String) xu.getValue("//s:Fault/s:Role/text()", fault, XPathConstants.STRING);
node = (String) xu.getValue("//s:Fault/s:Node/text()", fault, XPathConstants.STRING);
} catch (XMLStreamException e) {
throw new SoapFault("Could not parse message.", e, message.getVersion().getSender());
}
// if the fault's content is invalid and fautlCode is not found, blame the receiver
if (faultCode == null) {
faultCode = Soap12.getInstance().getReceiver();
exMessage = new Message("INVALID_FAULT", LOG).toString();
}
SoapFault fault = new SoapFault(exMessage, faultCode);
fault.setSubCodes(subCodes);
fault.setDetail(detail);
fault.setRole(role);
fault.setNode(node);
fault.setLang(lang);
return fault;
}
use of org.apache.cxf.staxutils.FragmentStreamReader in project cxf by apache.
the class XMLFaultInInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
XMLStreamReader xsr = message.getContent(XMLStreamReader.class);
DepthXMLStreamReader reader = new DepthXMLStreamReader(xsr);
try {
reader.nextTag();
if (!StaxUtils.toNextElement(reader)) {
throw new Fault(new org.apache.cxf.common.i18n.Message("ILLEGAL_XMLFAULT_FORMAT", BUNDLE));
}
String exMessage = reader.getElementText();
Fault fault = new XMLFault(exMessage);
reader.nextTag();
if (StaxUtils.toNextElement(reader)) {
// handling detail
Element detail = StaxUtils.read(new FragmentStreamReader(reader)).getDocumentElement();
fault.setDetail(detail);
}
message.setContent(Exception.class, fault);
} catch (XMLStreamException xse) {
throw new Fault(new org.apache.cxf.common.i18n.Message("STAX_READ_EXC", BUNDLE));
}
}
Aggregations