use of com.github.ljtfreitas.julian.http.codec.HTTPResponseReaderException in project julian-http-client by ljtfreitas.
the class JaxBHTTPMessageCodec method deserialize.
private Object deserialize(InputStream bodyAsStream, JavaType javaType) {
Class<?> expectedClassType = javaType.rawClassType();
JAXBContext context = context(expectedClassType);
try (InputStreamReader reader = new InputStreamReader(bodyAsStream);
BufferedReader buffered = new BufferedReader(reader)) {
Unmarshaller unmarshaller = context.createUnmarshaller();
StreamSource source = new StreamSource(buffered);
XMLStreamReader xmlReader = XML_INPUT_FACTORY.createXMLStreamReader(source);
boolean isXmlRoot = expectedClassType.isAnnotationPresent(XmlRootElement.class);
Object deserialized = isXmlRoot ? unmarshaller.unmarshal(xmlReader) : unmarshaller.unmarshal(xmlReader, expectedClassType).getValue();
return deserialized;
} catch (IOException | JAXBException | XMLStreamException e) {
throw new HTTPResponseReaderException("JSON deserialization failed. The target type was: " + javaType, e);
}
}
use of com.github.ljtfreitas.julian.http.codec.HTTPResponseReaderException in project julian-http-client by ljtfreitas.
the class DefaultHTTPRequestIO method deserialize.
private Optional<CompletableFuture<T>> deserialize(HTTPClientResponse response) {
MediaType mediaType = response.headers().select(CONTENT_TYPE).map(h -> MediaType.valueOf(h.value())).orElseGet(MediaType::wildcard);
HTTPResponseReader<?> reader = codecs.readers().select(mediaType, source.returnType()).orElseThrow(() -> new HTTPResponseReaderException(format("There is no a HTTPResponseReader able to convert {0} to {1}", mediaType, source.returnType())));
return reader.read(response.body(), source.returnType()).map(c -> c.handleAsync(this::handle).toCompletableFuture());
}
Aggregations