use of javax.xml.stream.XMLStreamWriter 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 javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class JAXBDataBindingTest method testContextProperties.
@Test
public void testContextProperties() throws Exception {
JAXBDataBinding db = new JAXBDataBinding();
Map<String, String> nsMap = new HashMap<>();
nsMap.put("uri:ultima:thule", "");
db.setNamespaceMap(nsMap);
Map<String, Object> contextProperties = new HashMap<>();
contextProperties.put("com.sun.xml.bind.defaultNamespaceRemap", "uri:ultima:thule");
db.setContextProperties(contextProperties);
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(UnqualifiedBean.class);
db.setContext(db.createJAXBContext(classes));
DataWriter<XMLStreamWriter> writer = db.createWriter(XMLStreamWriter.class);
XMLOutputFactory writerFactory = XMLOutputFactory.newInstance();
StringWriter stringWriter = new StringWriter();
XMLStreamWriter xmlWriter = writerFactory.createXMLStreamWriter(stringWriter);
UnqualifiedBean bean = new UnqualifiedBean();
bean.setAriadne("spider");
writer.write(bean, xmlWriter);
xmlWriter.flush();
String xml = stringWriter.toString();
assertTrue(xml, xml.contains("uri:ultima:thule"));
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class JAXBDataBindingTest method doNamespaceMappingTest.
void doNamespaceMappingTest(boolean internal, boolean asm) throws Exception {
if (internal) {
try {
Class.forName("com.sun.xml.internal.bind.v2.ContextFactory");
} catch (Throwable t) {
// on a JVM (likely IBM's) that doesn't rename the ContextFactory package to include "internal"
return;
}
}
try {
if (!asm) {
ReflectionUtil.setAccessible(ReflectionUtil.getDeclaredField(ASMHelper.class, "badASM")).set(null, Boolean.TRUE);
}
JAXBDataBinding db = createJaxbContext(internal);
DataWriter<XMLStreamWriter> writer = db.createWriter(XMLStreamWriter.class);
XMLOutputFactory writerFactory = XMLOutputFactory.newInstance();
StringWriter stringWriter = new StringWriter();
XMLStreamWriter xmlWriter = writerFactory.createXMLStreamWriter(stringWriter);
QualifiedBean bean = new QualifiedBean();
bean.setAriadne("spider");
writer.write(bean, xmlWriter);
xmlWriter.flush();
String xml = stringWriter.toString();
assertTrue("Failed to map namespace " + xml, xml.contains("greenland=\"uri:ultima:thule"));
} finally {
if (!asm) {
ReflectionUtil.setAccessible(ReflectionUtil.getDeclaredField(ASMHelper.class, "badASM")).set(null, Boolean.FALSE);
}
}
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class XMLStreamDataWriterTest method newDataWriter.
private DataWriterImpl<XMLStreamWriter> newDataWriter(ValidationEventHandler handler) throws Exception {
JAXBDataBinding db = getTestWriterFactory();
DataWriterImpl<XMLStreamWriter> dw = (DataWriterImpl<XMLStreamWriter>) db.createWriter(XMLStreamWriter.class);
assertNotNull(dw);
// Build message to set custom event handler
org.apache.cxf.message.Message message = new org.apache.cxf.message.MessageImpl();
message.put(JAXBDataBinding.WRITER_VALIDATION_EVENT_HANDLER, handler);
dw.setProperty("org.apache.cxf.message.Message", message);
return dw;
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class XMLStreamDataWriterTest method testWriteRPCLit1.
@Test
public void testWriteRPCLit1() throws Exception {
JAXBDataBinding db = getTestWriterFactory();
DataWriter<XMLStreamWriter> dw = db.createWriter(XMLStreamWriter.class);
assertNotNull(dw);
String val = new String("TESTOUTPUTMESSAGE");
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.toNextText(reader);
assertEquals("TESTOUTPUTMESSAGE", reader.getText());
}
Aggregations