use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class XMLStreamDataWriterTest method testWriteRPCLit2.
@Test
public void testWriteRPCLit2() throws Exception {
JAXBDataBinding db = getTestWriterFactory(MyComplexStruct.class);
DataWriter<XMLStreamWriter> dw = db.createWriter(XMLStreamWriter.class);
assertNotNull(dw);
MyComplexStruct val = new MyComplexStruct();
val.setElem1("This is element 1");
val.setElem2("This is element 2");
val.setElem3(1);
QName elName = new QName("http://apache.org/hello_world_rpclit/types", "in");
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
dw.write(val, part, streamWriter);
streamWriter.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
XMLStreamReader xr = inFactory.createXMLStreamReader(bais);
DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
StaxUtils.toNextElement(reader);
assertEquals(new QName("http://apache.org/hello_world_rpclit/types", "in"), reader.getName());
StaxUtils.nextEvent(reader);
StaxUtils.toNextElement(reader);
assertEquals(new QName("http://apache.org/hello_world_rpclit/types", "elem1"), reader.getName());
StaxUtils.nextEvent(reader);
StaxUtils.toNextText(reader);
assertEquals("This is element 1", reader.getText());
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class XMLStreamDataWriterTest method testSetProperty.
@Test
public void testSetProperty() throws Exception {
MyCustomHandler handler = new MyCustomHandler();
DataWriterImpl<XMLStreamWriter> dw = newDataWriter(handler);
// Write Stuff
TradePriceData val = new TradePriceData();
val.setTickerSymbol("This is a symbol");
val.setTickerPrice(1.0f);
QName elName = new QName("http://apache.org/hello_world_doc_lit_bare/types", "inout");
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
dw.write(val, part, streamWriter);
streamWriter.flush();
// Test MyCustomHandler
assertTrue(handler.getUsed());
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class PrettyLoggingFilter method getPrettyMessage.
/**
* Pretty-print {@linkplain LogEvent} XML payload.
*
* @param event the log event containing an XML payload which is to be pretty-printed.
* @return pretty-printed XML or original payload in case of an unexpected exception.
*/
private String getPrettyMessage(LogEvent event) {
String payload = event.getPayload();
StringWriter swriter = new StringWriter(estimatePrettySize(payload));
XMLStreamWriter xwriter = new PrettyPrintXMLStreamWriter(StaxUtils.createXMLStreamWriter(swriter), 2);
XMLStreamReader xreader = StaxUtils.createXMLStreamReader(new StringReader(payload));
try {
StaxUtils.copy(xreader, xwriter);
xwriter.flush();
} catch (XMLStreamException xse) {
if (!event.isTruncated()) {
LOG.debug("Error while pretty printing cxf message, returning raw message.", xse);
return payload;
}
// Expected behavior for truncated payloads - keep what is already written.
// This might effectively result in additional truncation,
// as the truncated XML document might result in partially parsed XML nodes,
// for example an open start tag. As long as a truncated payload is not
// mistaken for a non-truncated payload, we're good.
flush(xwriter);
return swriter.toString();
} finally {
close(xwriter);
close(xreader);
}
return swriter.toString();
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class XMLMessageOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
BindingOperationInfo boi = message.getExchange().getBindingOperationInfo();
MessageInfo mi;
BindingMessageInfo bmi;
if (isRequestor(message)) {
mi = boi.getOperationInfo().getInput();
bmi = boi.getInput();
} else {
mi = boi.getOperationInfo().getOutput();
bmi = boi.getOutput();
}
XMLBindingMessageFormat xmf = bmi.getExtensor(XMLBindingMessageFormat.class);
QName rootInModel = null;
if (xmf != null) {
rootInModel = xmf.getRootNode();
}
final int mpn = mi.getMessagePartsNumber();
if (boi.isUnwrapped() || mpn == 1) {
// wrapper out interceptor created the wrapper
// or if bare-one-param
new BareOutInterceptor().handleMessage(message);
} else {
if (rootInModel == null) {
rootInModel = boi.getName();
}
if (mpn == 0 && !boi.isUnwrapped()) {
// write empty operation qname
writeMessage(message, rootInModel, false);
} else {
// multi param, bare mode, needs write root node
writeMessage(message, rootInModel, true);
}
}
// in the end we do flush ;)
XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
try {
writer.flush();
} catch (XMLStreamException e) {
throw new Fault(new org.apache.cxf.common.i18n.Message("STAX_WRITE_EXC", BUNDLE, e));
}
}
use of javax.xml.stream.XMLStreamWriter 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