Search in sources :

Example 21 with JAXBElementProvider

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

the class WfsResponseExceptionMapper method fromResponse.

@Override
public WfsException fromResponse(Response response) {
    WfsException wfsEx = null;
    if (response != null) {
        if (response.getEntity() instanceof InputStream) {
            String msg = null;
            try {
                InputStream is = (InputStream) response.getEntity();
                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);
                    wfsEx = convertToWfsException(report);
                } catch (JAXBException | XMLStreamException e) {
                    wfsEx = new WfsException("Error parsing Response: " + msg, e);
                }
            } else {
                wfsEx = new WfsException("Error reading Response");
            }
        } else {
            wfsEx = new WfsException("Error reading response, entity type not understood: " + response.getEntity().getClass().getName());
        }
        wfsEx.setHttpStatus(response.getStatus());
    } else {
        wfsEx = new WfsException("Error handling response, response is null");
    }
    return wfsEx;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) ExceptionReport(net.opengis.ows.v_1_1_0.ExceptionReport) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) XMLStreamException(javax.xml.stream.XMLStreamException) WfsException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 22 with JAXBElementProvider

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

the class ResourceUtils method checkJaxbType.

private static void checkJaxbType(Class<?> serviceClass, Class<?> type, Type genericType, ResourceTypes types, Annotation[] anns, MessageBodyWriter<?> jaxbWriter, Class<?> jaxbElement) {
    boolean isCollection = false;
    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
        type = InjectionUtils.getActualType(genericType);
        isCollection = true;
    }
    if (type == Object.class && !(genericType instanceof Class) || genericType instanceof TypeVariable) {
        Type theType = InjectionUtils.processGenericTypeIfNeeded(serviceClass, Object.class, genericType);
        type = InjectionUtils.getActualType(theType);
    }
    if (type == null || InjectionUtils.isPrimitive(type) || (jaxbElement != null && jaxbElement.isAssignableFrom(type)) || Response.class.isAssignableFrom(type) || type.isInterface()) {
        return;
    }
    MessageBodyWriter<?> writer = jaxbWriter;
    if (writer == null) {
        JAXBElementProvider<Object> defaultWriter = new JAXBElementProvider<>();
        defaultWriter.setMarshallAsJaxbElement(true);
        defaultWriter.setXmlTypeAsJaxbElementOnly(true);
        writer = defaultWriter;
    }
    if (writer.isWriteable(type, type, anns, MediaType.APPLICATION_XML_TYPE)) {
        types.getAllTypes().put(type, type);
        Class<?> genCls = InjectionUtils.getActualType(genericType);
        if (genCls != type && genCls != null && genCls != Object.class && !InjectionUtils.isSupportedCollectionOrArray(genCls)) {
            types.getAllTypes().put(genCls, genCls);
        }
        XMLName name = AnnotationUtils.getAnnotation(anns, XMLName.class);
        QName qname = name != null ? JAXRSUtils.convertStringToQName(name.value()) : null;
        if (isCollection) {
            types.getCollectionMap().put(type, qname);
        } else {
            types.getXmlNameMap().put(type, qname);
        }
    }
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) QName(javax.xml.namespace.QName) ElementClass(org.apache.cxf.jaxrs.ext.xml.ElementClass) XMLName(org.apache.cxf.jaxrs.ext.xml.XMLName)

Example 23 with JAXBElementProvider

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

the class XMLSource method readNode.

private <T> T readNode(Node node, Class<T> cls) {
    if (Node.class.isAssignableFrom(cls)) {
        return cls.cast(node);
    }
    DOMSource s = new DOMSource(node);
    if (Source.class == cls || DOMSource.class == cls) {
        return cls.cast(s);
    }
    try {
        JAXBElementProvider<?> provider = new JAXBElementProvider<>();
        JAXBContext c = provider.getPackageContext(cls);
        if (c == null) {
            c = provider.getClassContext(cls);
        }
        Unmarshaller u = c.createUnmarshaller();
        try {
            if (cls.getAnnotation(XmlRootElement.class) != null) {
                return cls.cast(u.unmarshal(s));
            }
            return u.unmarshal(s, cls).getValue();
        } finally {
            JAXBUtils.closeUnmarshaller(u);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) DOMSource(javax.xml.transform.dom.DOMSource) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) XPathExpressionException(javax.xml.xpath.XPathExpressionException) XPathFactoryConfigurationException(javax.xml.xpath.XPathFactoryConfigurationException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 24 with JAXBElementProvider

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

the class ResourceUtils method checkJaxbType.

private static void checkJaxbType(Class<?> serviceClass, Class<?> type, Type genericType, ResourceTypes types, Annotation[] anns, MessageBodyWriter<?> jaxbWriter, Class<?> jaxbElement) {
    boolean isCollection = false;
    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
        type = InjectionUtils.getActualType(genericType);
        isCollection = true;
    }
    if (type == Object.class && !(genericType instanceof Class) || genericType instanceof TypeVariable) {
        Type theType = InjectionUtils.processGenericTypeIfNeeded(serviceClass, Object.class, genericType);
        type = InjectionUtils.getActualType(theType);
    }
    if (type == null || InjectionUtils.isPrimitive(type) || (jaxbElement != null && jaxbElement.isAssignableFrom(type)) || Response.class.isAssignableFrom(type) || type.isInterface()) {
        return;
    }
    MessageBodyWriter<?> writer = jaxbWriter;
    if (writer == null) {
        JAXBElementProvider<Object> defaultWriter = new JAXBElementProvider<>();
        defaultWriter.setMarshallAsJaxbElement(true);
        defaultWriter.setXmlTypeAsJaxbElementOnly(true);
        writer = defaultWriter;
    }
    if (writer.isWriteable(type, type, anns, MediaType.APPLICATION_XML_TYPE)) {
        types.getAllTypes().put(type, type);
        Class<?> genCls = InjectionUtils.getActualType(genericType);
        if (genCls != type && genCls != null && genCls != Object.class && !InjectionUtils.isSupportedCollectionOrArray(genCls)) {
            types.getAllTypes().put(genCls, genCls);
        }
        XMLName name = AnnotationUtils.getAnnotation(anns, XMLName.class);
        QName qname = name != null ? JAXRSUtils.convertStringToQName(name.value()) : null;
        if (isCollection) {
            types.getCollectionMap().put(type, qname);
        } else {
            types.getXmlNameMap().put(type, qname);
        }
    }
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) QName(javax.xml.namespace.QName) ElementClass(org.apache.cxf.jaxrs.ext.xml.ElementClass) XMLName(org.apache.cxf.jaxrs.ext.xml.XMLName)

Example 25 with JAXBElementProvider

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

the class JAXRSSoapBookTest method testPostGetBookFastinfosetProxyInterceptors.

@Test
public void testPostGetBookFastinfosetProxyInterceptors() throws Exception {
    JAXBElementProvider<Object> p = new JAXBElementProvider<>();
    p.setConsumeMediaTypes(Collections.singletonList("application/fastinfoset"));
    p.setProduceMediaTypes(Collections.singletonList("application/fastinfoset"));
    BookStoreJaxrsJaxws client = JAXRSClientFactory.create("http://localhost:" + PORT + "/test/services/rest5", BookStoreSoapRestFastInfoset3.class, Collections.singletonList(p));
    Book b = new Book("CXF", 1L);
    // Just to make sure it is enforced
    Map<String, Object> props = WebClient.getConfig(client).getRequestContext();
    props.put(FIStaxOutInterceptor.FI_ENABLED, Boolean.TRUE);
    Book b2 = client.addFastinfoBook(b);
    assertEquals(b2.getName(), b.getName());
    assertEquals(b2.getId(), b.getId());
    checkFiInterceptors(WebClient.getConfig(client));
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) BookStoreJaxrsJaxws(org.apache.cxf.systest.jaxrs.jaxws.BookStoreJaxrsJaxws) Test(org.junit.Test)

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