use of org.apache.cxf.staxutils.CachingXmlEventWriter in project cxf by apache.
the class JAXRSOutInterceptor method checkCachedStream.
private void checkCachedStream(Message m, OutputStream osOriginal, boolean enabled) throws Exception {
final XMLStreamWriter writer;
if (enabled) {
writer = m.getContent(XMLStreamWriter.class);
} else {
writer = (XMLStreamWriter) m.get(XMLStreamWriter.class.getName());
}
if (writer instanceof CachingXmlEventWriter) {
CachingXmlEventWriter cache = (CachingXmlEventWriter) writer;
if (!cache.getEvents().isEmpty()) {
XMLStreamWriter origWriter = null;
try {
origWriter = StaxUtils.createXMLStreamWriter(osOriginal);
for (XMLEvent event : cache.getEvents()) {
StaxUtils.writeEvent(event, origWriter);
}
} finally {
StaxUtils.close(origWriter);
}
}
m.setContent(XMLStreamWriter.class, null);
return;
}
if (enabled) {
OutputStream os = m.getContent(OutputStream.class);
if (os != osOriginal && os instanceof CachedOutputStream) {
CachedOutputStream cos = (CachedOutputStream) os;
if (cos.size() != 0) {
cos.writeCacheTo(osOriginal);
}
}
}
}
use of org.apache.cxf.staxutils.CachingXmlEventWriter in project cxf by apache.
the class JAXRSOutInterceptor method checkBufferingMode.
private boolean checkBufferingMode(Message m, List<WriterInterceptor> writers, boolean firstTry) {
if (!firstTry) {
return false;
}
WriterInterceptor last = writers.get(writers.size() - 1);
MessageBodyWriter<Object> w = ((WriterInterceptorMBW) last).getMBW();
Object outBuf = m.getContextualProperty(OUT_BUFFERING);
boolean enabled = PropertyUtils.isTrue(outBuf);
boolean configurableProvider = w instanceof AbstractConfigurableProvider;
if (!enabled && outBuf == null && configurableProvider) {
enabled = ((AbstractConfigurableProvider) w).getEnableBuffering();
}
if (enabled) {
boolean streamingOn = configurableProvider && ((AbstractConfigurableProvider) w).getEnableStreaming();
if (streamingOn) {
m.setContent(XMLStreamWriter.class, new CachingXmlEventWriter());
} else {
m.setContent(OutputStream.class, new CachedOutputStream());
}
}
return enabled;
}
use of org.apache.cxf.staxutils.CachingXmlEventWriter in project cxf by apache.
the class AbstractOutDatabindingInterceptor method writeParts.
protected void writeParts(Message message, Exchange exchange, BindingOperationInfo operation, MessageContentsList objs, List<MessagePartInfo> parts) {
OutputStream out = message.getContent(OutputStream.class);
XMLStreamWriter origXmlWriter = message.getContent(XMLStreamWriter.class);
Service service = exchange.getService();
XMLStreamWriter xmlWriter = origXmlWriter;
CachingXmlEventWriter cache = null;
// configure endpoint and operation level schema validation
setOperationSchemaValidation(message);
// need to cache the events in case validation fails or buffering is enabled
if (shouldBuffer(message)) {
if (!(xmlWriter instanceof CachingXmlEventWriter)) {
cache = new CachingXmlEventWriter();
try {
cache.setNamespaceContext(origXmlWriter.getNamespaceContext());
} catch (XMLStreamException e) {
// ignorable, will just get extra namespace decls
}
xmlWriter = cache;
}
out = null;
}
if (out != null && writeToOutputStream(message, operation.getBinding(), service) && !MessageUtils.getContextualBoolean(message, DISABLE_OUTPUTSTREAM_OPTIMIZATION, false)) {
if (xmlWriter != null) {
try {
xmlWriter.writeCharacters("");
xmlWriter.flush();
} catch (XMLStreamException e) {
throw new Fault(e);
}
}
DataWriter<OutputStream> osWriter = getDataWriter(message, service, OutputStream.class);
for (MessagePartInfo part : parts) {
if (objs.hasValue(part)) {
Object o = objs.get(part);
osWriter.write(o, part, out);
}
}
} else {
DataWriter<XMLStreamWriter> dataWriter = getDataWriter(message, service, XMLStreamWriter.class);
for (MessagePartInfo part : parts) {
if (objs.hasValue(part)) {
NamespaceContext c = null;
if (!part.isElement() && xmlWriter instanceof CachingXmlEventWriter) {
try {
c = xmlWriter.getNamespaceContext();
xmlWriter.setNamespaceContext(new CachingXmlEventWriter.NSContext(null));
} catch (XMLStreamException e) {
// ignore
}
}
Object o = objs.get(part);
dataWriter.write(o, part, xmlWriter);
if (c != null) {
try {
xmlWriter.setNamespaceContext(c);
} catch (XMLStreamException e) {
// ignore
}
}
}
}
}
if (cache != null) {
try {
for (XMLEvent event : cache.getEvents()) {
StaxUtils.writeEvent(event, origXmlWriter);
}
} catch (XMLStreamException e) {
throw new Fault(e);
}
}
}
use of org.apache.cxf.staxutils.CachingXmlEventWriter 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;
}
final List<MessagePartInfo> parts;
final boolean output;
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.staxutils.CachingXmlEventWriter in project midpoint by Evolveum.
the class CustomWrappedOutInterceptor method writeParts.
@Override
protected void writeParts(Message message, Exchange exchange, BindingOperationInfo operation, MessageContentsList objs, List<MessagePartInfo> parts) {
// TODO Auto-generated method stub
OutputStream out = message.getContent(OutputStream.class);
XMLStreamWriter origXmlWriter = message.getContent(XMLStreamWriter.class);
Service service = exchange.getService();
XMLStreamWriter xmlWriter = origXmlWriter;
CachingXmlEventWriter cache = null;
Object en = message.getContextualProperty(OUT_BUFFERING);
boolean allowBuffer = true;
boolean buffer = false;
if (en != null) {
buffer = Boolean.TRUE.equals(en) || "true".equals(en);
allowBuffer = !(Boolean.FALSE.equals(en) || "false".equals(en));
}
// need to cache the events in case validation fails or buffering is enabled
if (buffer || (allowBuffer && shouldValidate(message) && !isRequestor(message))) {
cache = new CachingXmlEventWriter();
try {
cache.setNamespaceContext(origXmlWriter.getNamespaceContext());
} catch (XMLStreamException e) {
//ignorable, will just get extra namespace decls
}
xmlWriter = cache;
out = null;
}
if (out != null && writeToOutputStream(message, operation.getBinding(), service) && !MessageUtils.isTrue(message.getContextualProperty(DISABLE_OUTPUTSTREAM_OPTIMIZATION))) {
if (xmlWriter != null) {
try {
xmlWriter.writeCharacters("");
xmlWriter.flush();
} catch (XMLStreamException e) {
throw new Fault(e);
}
}
DataWriter<OutputStream> osWriter = getDataWriter(message, service, OutputStream.class);
for (MessagePartInfo part : parts) {
if (objs.hasValue(part)) {
Object o = objs.get(part);
osWriter.write(o, part, out);
}
}
} else {
DataWriter<XMLStreamWriter> dataWriter = new CustomDataWriter(prismContext);
for (MessagePartInfo part : parts) {
if (objs.hasValue(part)) {
Object o = objs.get(part);
dataWriter.write(o, part, xmlWriter);
}
}
}
if (cache != null) {
try {
for (XMLEvent event : cache.getEvents()) {
StaxUtils.writeEvent(event, origXmlWriter);
}
} catch (XMLStreamException e) {
throw new Fault(e);
}
}
}
Aggregations