Search in sources :

Example 66 with XMLStreamWriter

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());
}
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) MyComplexStruct(org.apache.hello_world_rpclit.types.MyComplexStruct) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Test(org.junit.Test)

Example 67 with XMLStreamWriter

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());
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) QName(javax.xml.namespace.QName) TradePriceData(org.apache.hello_world_doc_lit_bare.types.TradePriceData) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Test(org.junit.Test)

Example 68 with XMLStreamWriter

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();
}
Also used : PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) XMLStreamReader(javax.xml.stream.XMLStreamReader) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) StringReader(java.io.StringReader)

Example 69 with XMLStreamWriter

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));
    }
}
Also used : BareOutInterceptor(org.apache.cxf.wsdl.interceptors.BareOutInterceptor) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Message(org.apache.cxf.message.Message) QName(javax.xml.namespace.QName) Fault(org.apache.cxf.interceptor.Fault) MessageInfo(org.apache.cxf.service.model.MessageInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) XMLStreamException(javax.xml.stream.XMLStreamException) XMLBindingMessageFormat(org.apache.cxf.bindings.xformat.XMLBindingMessageFormat) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 70 with XMLStreamWriter

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);
    }
}
Also used : StaxOutInterceptor(org.apache.cxf.interceptor.StaxOutInterceptor) MessageContentsList(org.apache.cxf.message.MessageContentsList) Message(org.apache.cxf.message.Message) Fault(org.apache.cxf.interceptor.Fault) XMLStreamException(javax.xml.stream.XMLStreamException) Response(javax.ws.rs.core.Response) NSStack(org.apache.cxf.helpers.NSStack) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

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