Search in sources :

Example 1 with HTTPResponseReaderException

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);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStreamReader(java.io.InputStreamReader) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) HTTPResponseReaderException(com.github.ljtfreitas.julian.http.codec.HTTPResponseReaderException) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) BufferedReader(java.io.BufferedReader) Unmarshaller(jakarta.xml.bind.Unmarshaller)

Example 2 with HTTPResponseReaderException

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());
}
Also used : HTTPClient(com.github.ljtfreitas.julian.http.client.HTTPClient) HTTPMessageException(com.github.ljtfreitas.julian.http.codec.HTTPMessageException) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Message.format(com.github.ljtfreitas.julian.Message.format) HTTPResponseReaderException(com.github.ljtfreitas.julian.http.codec.HTTPResponseReaderException) HTTPResponseReader(com.github.ljtfreitas.julian.http.codec.HTTPResponseReader) ExecutionException(java.util.concurrent.ExecutionException) CONTENT_TYPE(com.github.ljtfreitas.julian.http.HTTPHeader.CONTENT_TYPE) HTTPMessageCodecs(com.github.ljtfreitas.julian.http.codec.HTTPMessageCodecs) HTTPClientException(com.github.ljtfreitas.julian.http.client.HTTPClientException) Promise(com.github.ljtfreitas.julian.Promise) Optional(java.util.Optional) HTTPClientResponse(com.github.ljtfreitas.julian.http.client.HTTPClientResponse) HTTPResponseReaderException(com.github.ljtfreitas.julian.http.codec.HTTPResponseReaderException)

Aggregations

HTTPResponseReaderException (com.github.ljtfreitas.julian.http.codec.HTTPResponseReaderException)2 IOException (java.io.IOException)2 Message.format (com.github.ljtfreitas.julian.Message.format)1 Promise (com.github.ljtfreitas.julian.Promise)1 CONTENT_TYPE (com.github.ljtfreitas.julian.http.HTTPHeader.CONTENT_TYPE)1 HTTPClient (com.github.ljtfreitas.julian.http.client.HTTPClient)1 HTTPClientException (com.github.ljtfreitas.julian.http.client.HTTPClientException)1 HTTPClientResponse (com.github.ljtfreitas.julian.http.client.HTTPClientResponse)1 HTTPMessageCodecs (com.github.ljtfreitas.julian.http.codec.HTTPMessageCodecs)1 HTTPMessageException (com.github.ljtfreitas.julian.http.codec.HTTPMessageException)1 HTTPResponseReader (com.github.ljtfreitas.julian.http.codec.HTTPResponseReader)1 JAXBContext (jakarta.xml.bind.JAXBContext)1 JAXBException (jakarta.xml.bind.JAXBException)1 Unmarshaller (jakarta.xml.bind.Unmarshaller)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 ExecutionException (java.util.concurrent.ExecutionException)1