Search in sources :

Example 61 with XMLStreamWriter

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);
    }
}
Also used : Message(org.apache.cxf.message.Message) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) XMLFault(org.apache.cxf.binding.xml.XMLFault) Fault(org.apache.cxf.interceptor.Fault) XMLStreamException(javax.xml.stream.XMLStreamException) NSStack(org.apache.cxf.helpers.NSStack) XMLFault(org.apache.cxf.binding.xml.XMLFault) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 62 with XMLStreamWriter

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"));
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) HashMap(java.util.HashMap) UnqualifiedBean(org.apache.cxf.jaxb.fortest.unqualified.UnqualifiedBean) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 63 with XMLStreamWriter

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);
        }
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) QualifiedBean(org.apache.cxf.jaxb.fortest.QualifiedBean)

Example 64 with XMLStreamWriter

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;
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding)

Example 65 with XMLStreamWriter

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());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) QName(javax.xml.namespace.QName) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Test(org.junit.Test)

Aggregations

XMLStreamWriter (javax.xml.stream.XMLStreamWriter)209 XMLStreamException (javax.xml.stream.XMLStreamException)84 StringWriter (java.io.StringWriter)47 Test (org.junit.Test)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)40 XMLStreamReader (javax.xml.stream.XMLStreamReader)32 QName (javax.xml.namespace.QName)26 Document (org.w3c.dom.Document)26 Fault (org.apache.cxf.interceptor.Fault)25 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)21 ByteArrayInputStream (java.io.ByteArrayInputStream)17 StreamSource (javax.xml.transform.stream.StreamSource)17 StringReader (java.io.StringReader)16 JAXBException (javax.xml.bind.JAXBException)14 DOMSource (javax.xml.transform.dom.DOMSource)14 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)14 Message (org.apache.cxf.message.Message)13 Element (org.w3c.dom.Element)11