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;
}
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);
}
}
}
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);
}
}
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);
}
}
}
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));
}
Aggregations