Search in sources :

Example 11 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project cxf by apache.

the class JAXRSClientServerSpringBookTest method testPostBookXsiType.

@Test
public void testPostBookXsiType() throws Exception {
    String address = "http://localhost:" + PORT + "/the/thebooksxsi/bookstore/books/xsitype";
    JAXBElementProvider<Book> provider = new JAXBElementProvider<>();
    provider.setExtraClass(new Class[] { SuperBook.class });
    provider.setJaxbElementClassNames(Collections.singletonList(Book.class.getName()));
    WebClient wc = WebClient.create(address, Collections.singletonList(provider));
    wc.accept("application/xml");
    wc.type("application/xml");
    SuperBook book = new SuperBook("SuperBook2", 999L, true);
    Book book2 = wc.invoke("POST", book, Book.class, Book.class);
    assertEquals("SuperBook2", book2.getName());
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 12 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project ddf by codice.

the class WfsSource method initProviders.

private List<?> initProviders() {
    // We need to tell the JAXBElementProvider to marshal the GetFeatureType
    // class as an element because it is missing the @XmlRootElement Annotation
    JAXBElementProvider<ExtendedGetFeatureType> provider = new JAXBElementProvider<>();
    Map<String, String> jaxbClassMap = new HashMap<>();
    // Ensure a namespace is used when the GetFeature request is generated
    String expandedName = new QName(Wfs11Constants.WFS_NAMESPACE, Wfs11Constants.GET_FEATURE).toString();
    jaxbClassMap.put(ExtendedGetFeatureType.class.getName(), expandedName);
    provider.setJaxbElementClassMap(jaxbClassMap);
    provider.setMarshallAsJaxbElement(true);
    return Arrays.asList(provider, new WfsResponseExceptionMapper(), new XmlSchemaMessageBodyReaderWfs11(), new WfsMessageBodyReader(featureTransformationService, () -> wfsMetadata));
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) XmlSchemaMessageBodyReaderWfs11(org.codice.ddf.spatial.ogc.wfs.v110.catalog.source.reader.XmlSchemaMessageBodyReaderWfs11)

Example 13 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project ddf by codice.

the class CswResponseExceptionMapper method fromResponse.

@Override
public CswException fromResponse(Response response) {
    CswException cswException = null;
    if (response != null) {
        if (response.getEntity() instanceof InputStream) {
            String msg = null;
            try {
                InputStream is = (InputStream) response.getEntity();
                if (is.markSupported()) {
                    is.reset();
                }
                msg = IOUtils.toString(is);
            } catch (IOException e) {
                LOGGER.info("Unable to parse exception report: {}", e.getMessage());
                LOGGER.debug("Unable to parse exception report", e);
            }
            if (msg != null) {
                try {
                    JAXBElementProvider<ExceptionReport> provider = new JAXBElementProvider<>();
                    Unmarshaller um = provider.getJAXBContext(ExceptionReport.class, ExceptionReport.class).createUnmarshaller();
                    XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
                    xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
                    xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
                    xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
                    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(msg));
                    ExceptionReport report = (ExceptionReport) um.unmarshal(xmlStreamReader);
                    cswException = convertToCswException(report);
                } catch (JAXBException | XMLStreamException e) {
                    cswException = new CswException("Error received from remote Csw server: " + msg, e);
                    LOGGER.info("Error parsing the exception report: {}", e.getMessage());
                    LOGGER.debug("Error parsing the exception report", e);
                }
            } else {
                cswException = new CswException("Error received from remote Csw server.");
            }
        } else {
            cswException = new CswException("Error reading response, entity type not understood: " + response.getEntity().getClass().getName());
        }
        cswException.setHttpStatus(response.getStatus());
    } else {
        cswException = new CswException("Error handling response, response is null");
    }
    return cswException;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) ExceptionReport(net.opengis.ows.v_1_0_0.ExceptionReport) JAXBException(javax.xml.bind.JAXBException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) IOException(java.io.IOException) JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 14 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project ddf by codice.

the class WfsResponseExceptionMapper method unmarshalResponseEntity.

private WfsException unmarshalResponseEntity(String msg) {
    WfsException wfsEx;
    try {
        JAXBElementProvider<ExceptionReport> provider = new JAXBElementProvider<>();
        Unmarshaller um = provider.getJAXBContext(ExceptionReport.class, ExceptionReport.class).createUnmarshaller();
        XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
        XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(msg));
        ExceptionReport report = (ExceptionReport) um.unmarshal(xmlStreamReader);
        wfsEx = convertToWfsException(report);
    } catch (JAXBException | XMLStreamException e) {
        wfsEx = new WfsParseException(msg, e);
    }
    return wfsEx;
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) WfsParseException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsParseException) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) WfsException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException) ExceptionReport(net.opengis.ows.v_1_0_0.ExceptionReport) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 15 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project tesb-rt-se by Talend.

the class RESTClient method createJAXRSProviders.

/**
 * Creates a custom JAX-RS JAXBElementProvider which can handle
 * generated JAXB clasess with no XmlRootElement annotation
 * @return providers
 */
private List<Object> createJAXRSProviders() {
    JAXBElementProvider provider = new JAXBElementProvider();
    provider.setUnmarshallAsJaxbElement(true);
    provider.setMarshallAsJaxbElement(true);
    List<Object> providers = new ArrayList<Object>();
    providers.add(provider);
    return providers;
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) ArrayList(java.util.ArrayList)

Aggregations

JAXBElementProvider (org.apache.cxf.jaxrs.provider.JAXBElementProvider)30 Test (org.junit.Test)9 HashMap (java.util.HashMap)8 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)6 WebClient (org.apache.cxf.jaxrs.client.WebClient)6 ArrayList (java.util.ArrayList)5 Unmarshaller (javax.xml.bind.Unmarshaller)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 InputStream (java.io.InputStream)4 StringReader (java.io.StringReader)4 JAXBException (javax.xml.bind.JAXBException)4 QName (javax.xml.namespace.QName)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)4 IOException (java.io.IOException)3 Response (javax.ws.rs.core.Response)3 RuntimeDelegate (javax.ws.rs.ext.RuntimeDelegate)3 LoggingInInterceptor (org.apache.cxf.ext.logging.LoggingInInterceptor)3 Type (java.lang.reflect.Type)2 TypeVariable (java.lang.reflect.TypeVariable)2