use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class TunedDocumentLoader method loadFastinfosetDocument.
static Document loadFastinfosetDocument(URL url) throws IOException, ParserConfigurationException, XMLStreamException {
try (InputStream in = new BufferedInputStream(url.openStream())) {
XMLStreamReader staxReader = new StAXDocumentParser(in);
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
StaxUtils.copy(staxReader, writer);
staxReader.close();
return writer.getDocument();
}
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class FaultOutInterceptor method handleMessage.
public void handleMessage(Message message) {
Fault f = (Fault) message.getContent(Exception.class);
if (f == null) {
return;
}
Throwable cause = f.getCause();
if (cause == null) {
return;
}
BindingOperationInfo bop = message.getExchange().getBindingOperationInfo();
if (bop == null) {
return;
}
FaultInfo fi = getFaultForClass(bop, cause.getClass());
if (cause instanceof Exception && fi != null) {
Exception ex = (Exception) cause;
Object bean = getFaultBean(cause, fi, message);
Service service = message.getExchange().getService();
MessagePartInfo part = fi.getFirstMessagePart();
DataBinding db = service.getDataBinding();
try {
if (isDOMSupported(db)) {
DataWriter<Node> writer = db.createWriter(Node.class);
if (f.hasDetails()) {
writer.write(bean, part, f.getDetail());
} else {
writer.write(bean, part, f.getOrCreateDetail());
if (!f.getDetail().hasChildNodes()) {
f.setDetail(null);
}
}
} else {
if (f.hasDetails()) {
XMLStreamWriter xsw = new W3CDOMStreamWriter(f.getDetail());
DataWriter<XMLStreamWriter> writer = db.createWriter(XMLStreamWriter.class);
writer.write(bean, part, xsw);
} else {
XMLStreamWriter xsw = new W3CDOMStreamWriter(f.getOrCreateDetail());
DataWriter<XMLStreamWriter> writer = db.createWriter(XMLStreamWriter.class);
writer.write(bean, part, xsw);
if (!f.getDetail().hasChildNodes()) {
f.setDetail(null);
}
}
}
f.setMessage(ex.getMessage());
} catch (Exception fex) {
// ignore - if any exceptions occur here, we'll ignore them
// and let the default fault handling of the binding convert
// the fault like it was an unchecked exception.
LOG.log(Level.WARNING, "EXCEPTION_WHILE_WRITING_FAULT", fex);
}
}
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class ReadHeadersInterceptor method handleMessage.
// CHECKSTYLE:OFF MethodLength
public void handleMessage(SoapMessage message) {
if (isGET(message)) {
LOG.fine("ReadHeadersInterceptor skipped in HTTP GET method");
return;
}
/*
* Reject OPTIONS, and any other noise that is not allowed in SOAP.
*/
final String verb = (String) message.get(org.apache.cxf.message.Message.HTTP_REQUEST_METHOD);
if (verb != null && !"POST".equals(verb)) {
Fault formula405 = new Fault("HTTP verb was not GET or POST", LOG);
formula405.setStatusCode(405);
throw formula405;
}
XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
boolean closeNeeded = false;
if (xmlReader == null) {
InputStream in = message.getContent(InputStream.class);
if (in == null) {
throw new RuntimeException("Can't find input stream in message");
}
xmlReader = StaxUtils.createXMLStreamReader(in);
closeNeeded = true;
}
try {
if (xmlReader.getEventType() == XMLStreamConstants.START_ELEMENT || xmlReader.nextTag() == XMLStreamConstants.START_ELEMENT) {
SoapVersion soapVersion = readVersion(xmlReader, message);
if (soapVersion == Soap12.getInstance() && version == Soap11.getInstance()) {
message.setVersion(version);
throw new SoapFault(new Message("INVALID_11_VERSION", LOG), version.getVersionMismatch());
}
XMLStreamReader filteredReader = new PartialXMLStreamReader(xmlReader, message.getVersion().getBody());
Node nd = message.getContent(Node.class);
W3CDOMStreamWriter writer = message.get(W3CDOMStreamWriter.class);
final Document doc;
if (writer != null) {
StaxUtils.copy(filteredReader, writer);
doc = writer.getDocument();
} else if (nd instanceof Document) {
doc = (Document) nd;
StaxUtils.readDocElements(doc, doc, filteredReader, false, false);
} else {
final boolean addNC = MessageUtils.getContextualBoolean(message, "org.apache.cxf.binding.soap.addNamespaceContext", false);
Map<String, String> bodyNC = addNC ? new HashMap<String, String>() : null;
if (addNC) {
// add the Envelope-Level declarations
addCurrentNamespaceDecls(xmlReader, bodyNC);
}
HeadersProcessor processor = new HeadersProcessor(soapVersion);
doc = processor.process(filteredReader);
if (doc != null) {
message.setContent(Node.class, doc);
} else {
message.put(ENVELOPE_EVENTS, processor.getEnvAttributeAndNamespaceEvents());
message.put(BODY_EVENTS, processor.getBodyAttributeAndNamespaceEvents());
message.put(ENVELOPE_PREFIX, processor.getEnvelopePrefix());
message.put(BODY_PREFIX, processor.getBodyPrefix());
}
if (addNC) {
// add the Body-level declarations
addCurrentNamespaceDecls(xmlReader, bodyNC);
message.put("soap.body.ns.context", bodyNC);
}
}
List<Element> soapBody = null;
// Find header
if (doc != null) {
Element element = doc.getDocumentElement();
QName header = soapVersion.getHeader();
QName body = soapVersion.getBody();
List<Element> elemList = DOMUtils.findAllElementsByTagNameNS(element, header.getNamespaceURI(), header.getLocalPart());
soapBody = DOMUtils.getChildrenWithName(element, body.getNamespaceURI(), body.getLocalPart());
for (Element elem : elemList) {
Element hel = DOMUtils.getFirstElement(elem);
while (hel != null) {
// which otherwise would be lost.
if (elem.hasAttributes()) {
NamedNodeMap nnp = elem.getAttributes();
for (int ct = 0; ct < nnp.getLength(); ct++) {
Node attr = nnp.item(ct);
Node headerAttrNode = hel.hasAttributes() ? hel.getAttributes().getNamedItemNS(attr.getNamespaceURI(), attr.getLocalName()) : null;
if (headerAttrNode == null) {
Attr attribute = hel.getOwnerDocument().createAttributeNS(attr.getNamespaceURI(), attr.getNodeName());
attribute.setNodeValue(attr.getNodeValue());
hel.setAttributeNodeNS(attribute);
}
}
}
HeaderProcessor p = bus == null ? null : bus.getExtension(HeaderManager.class).getHeaderProcessor(hel.getNamespaceURI());
Object obj;
DataBinding dataBinding = null;
if (p == null || p.getDataBinding() == null) {
obj = hel;
} else {
dataBinding = p.getDataBinding();
DataReader<Node> dataReader = dataBinding.createReader(Node.class);
dataReader.setAttachments(message.getAttachments());
dataReader.setProperty(DataReader.ENDPOINT, message.getExchange().getEndpoint());
dataReader.setProperty(Message.class.getName(), message);
obj = dataReader.read(hel);
}
SoapHeader shead = new SoapHeader(new QName(hel.getNamespaceURI(), hel.getLocalName()), obj, dataBinding);
String mu = hel.getAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameMustUnderstand());
String act = hel.getAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameRole());
if (!StringUtils.isEmpty(act)) {
shead.setActor(act);
}
shead.setMustUnderstand(Boolean.valueOf(mu) || "1".equals(mu));
// mark header as inbound header.(for distinguishing between the direction to
// avoid piggybacking of headers from request->server->response.
shead.setDirection(SoapHeader.Direction.DIRECTION_IN);
message.getHeaders().add(shead);
hel = DOMUtils.getNextElement(hel);
}
}
}
if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message)) {
message.getInterceptorChain().add(new CheckClosingTagsInterceptor());
}
if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message) && soapBody != null && soapBody.isEmpty()) {
throw new SoapFault(new Message("NO_SOAP_BODY", LOG, "no soap body"), soapVersion.getSender());
}
}
} catch (XMLStreamException e) {
throw new SoapFault(new Message("XML_STREAM_EXC", LOG, e.getMessage()), e, message.getVersion().getSender());
} finally {
if (closeNeeded) {
try {
StaxUtils.close(xmlReader);
} catch (XMLStreamException e) {
throw new SoapFault(new Message("XML_STREAM_EXC", LOG, e.getMessage()), e, message.getVersion().getSender());
}
}
}
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class RetransmissionQueueImpl method readHeaders.
private void readHeaders(XMLStreamReader xmlReader, SoapMessage message) throws XMLStreamException {
// read header portion of SOAP document into DOM
SoapVersion version = message.getVersion();
XMLStreamReader filteredReader = new PartialXMLStreamReader(xmlReader, version.getBody());
Node nd = message.getContent(Node.class);
W3CDOMStreamWriter writer = message.get(W3CDOMStreamWriter.class);
final Document doc;
if (writer != null) {
StaxUtils.copy(filteredReader, writer);
doc = writer.getDocument();
} else if (nd instanceof Document) {
doc = (Document) nd;
StaxUtils.readDocElements(doc, doc, filteredReader, false, false);
} else {
doc = StaxUtils.read(filteredReader);
message.setContent(Node.class, doc);
}
// get the actual SOAP header
Element element = doc.getDocumentElement();
QName header = version.getHeader();
List<Element> elemList = DOMUtils.findAllElementsByTagNameNS(element, header.getNamespaceURI(), header.getLocalPart());
for (Element elem : elemList) {
// set all child elements as headers for message transmission
Element hel = DOMUtils.getFirstElement(elem);
while (hel != null) {
SoapHeader sheader = new SoapHeader(DOMUtils.getElementQName(hel), hel);
message.getHeaders().add(sheader);
hel = DOMUtils.getNextElement(hel);
}
}
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class AbstractBindingBuilder method cloneElement.
protected Element cloneElement(Element el) {
Document doc = secHeader.getSecurityHeaderElement().getOwnerDocument();
if (!doc.equals(el.getOwnerDocument())) {
XMLStreamReader reader = StaxUtils.createXMLStreamReader(el);
DocumentFragment fragment = doc.createDocumentFragment();
W3CDOMStreamWriter writer = new W3CDOMStreamWriter(fragment);
try {
StaxUtils.copy(reader, writer);
return (Element) fragment.getFirstChild();
} catch (XMLStreamException ex) {
LOG.log(Level.FINE, "Error cloning security element", ex);
}
}
return el;
}
Aggregations