Search in sources :

Example 11 with JAXBDataBinding

use of org.apache.cxf.jaxb.JAXBDataBinding 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 12 with JAXBDataBinding

use of org.apache.cxf.jaxb.JAXBDataBinding 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 13 with JAXBDataBinding

use of org.apache.cxf.jaxb.JAXBDataBinding in project cxf by apache.

the class XMLStreamDataWriterTest method testWriteWithNamespacePrefixMapping.

@Test
public void testWriteWithNamespacePrefixMapping() throws Exception {
    JAXBDataBinding db = getTestWriterFactory(GreetMe.class);
    Map<String, String> nspref = new HashMap<>();
    nspref.put("http://apache.org/hello_world_soap_http/types", "x");
    db.setNamespaceMap(nspref);
    // use the output stream instead of XMLStreamWriter to test
    DataWriter<OutputStream> dw = db.createWriter(OutputStream.class);
    assertNotNull(dw);
    GreetMe val = new GreetMe();
    val.setRequestType("Hello");
    dw.write(val, baos);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    XMLStreamReader xr = inFactory.createXMLStreamReader(bais);
    DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
    StaxUtils.toNextElement(reader);
    QName qname = reader.getName();
    assertEquals(new QName("http://apache.org/hello_world_soap_http/types", "greetMe"), qname);
    assertEquals("x", qname.getPrefix());
    assertEquals(1, reader.getNamespaceCount());
    assertEquals("http://apache.org/hello_world_soap_http/types", reader.getNamespaceURI(0));
    assertEquals("x", reader.getNamespacePrefix(0));
    StaxUtils.nextEvent(reader);
    StaxUtils.toNextElement(reader);
    qname = reader.getName();
    assertEquals(new QName("http://apache.org/hello_world_soap_http/types", "requestType"), qname);
    assertEquals("x", qname.getPrefix());
    StaxUtils.nextEvent(reader);
    StaxUtils.toNextText(reader);
    assertEquals("Hello", reader.getText());
}
Also used : GreetMe(org.apache.hello_world_soap_http.types.GreetMe) XMLStreamReader(javax.xml.stream.XMLStreamReader) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) QName(javax.xml.namespace.QName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) Test(org.junit.Test)

Example 14 with JAXBDataBinding

use of org.apache.cxf.jaxb.JAXBDataBinding in project cxf by apache.

the class XMLStreamDataReaderTest method testReadWrapper.

@Test
public void testReadWrapper() throws Exception {
    JAXBDataBinding db = getDataBinding(GreetMe.class);
    reader = getTestReader("../resources/GreetMeDocLiteralReq.xml");
    assertNotNull(reader);
    DataReader<XMLStreamReader> dr = db.createReader(XMLStreamReader.class);
    assertNotNull(dr);
    Object val = dr.read(reader);
    assertNotNull(val);
    assertTrue(val instanceof GreetMe);
    assertEquals("TestSOAPInputPMessage", ((GreetMe) val).getRequestType());
}
Also used : GreetMe(org.apache.hello_world_soap_http.types.GreetMe) XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) Test(org.junit.Test)

Example 15 with JAXBDataBinding

use of org.apache.cxf.jaxb.JAXBDataBinding in project cxf by apache.

the class XMLStreamDataReaderTest method testReadWrapperReturn.

@Test
public void testReadWrapperReturn() throws Exception {
    JAXBDataBinding db = getDataBinding(GreetMeResponse.class);
    reader = getTestReader("../resources/GreetMeDocLiteralResp.xml");
    assertNotNull(reader);
    DataReader<XMLStreamReader> dr = db.createReader(XMLStreamReader.class);
    assertNotNull(dr);
    Object retValue = dr.read(reader);
    assertNotNull(retValue);
    assertTrue(retValue instanceof GreetMeResponse);
    assertEquals("TestSOAPOutputPMessage", ((GreetMeResponse) retValue).getResponseType());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) GreetMeResponse(org.apache.hello_world_soap_http.types.GreetMeResponse) Test(org.junit.Test)

Aggregations

JAXBDataBinding (org.apache.cxf.jaxb.JAXBDataBinding)51 QName (javax.xml.namespace.QName)24 Test (org.junit.Test)18 JAXBException (javax.xml.bind.JAXBException)15 Header (org.apache.cxf.headers.Header)15 XMLStreamReader (javax.xml.stream.XMLStreamReader)12 ArrayList (java.util.ArrayList)10 DataBinding (org.apache.cxf.databinding.DataBinding)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)7 DepthXMLStreamReader (org.apache.cxf.staxutils.DepthXMLStreamReader)7 Service (org.apache.cxf.service.Service)6 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)6 JAXBContext (javax.xml.bind.JAXBContext)5 Bus (org.apache.cxf.Bus)5 URL (java.net.URL)4 OutofBandHeader (org.apache.cxf.outofband.header.OutofBandHeader)4 HashMap (java.util.HashMap)3 List (java.util.List)3 MessageContext (javax.xml.ws.handler.MessageContext)3