All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vrap.rmf.base.client.ResponseSerializerImpl Maven / Gradle / Ivy

There is a newer version: 17.17.0
Show newest version

package io.vrap.rmf.base.client;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Default implementation of {@link ResponseSerializer} using Jackson {@link ObjectMapper}
 */
class ResponseSerializerImpl implements ResponseSerializer {

    private final ObjectMapper mapper;

    public ResponseSerializerImpl(final ObjectMapper mapper) {
        this.mapper = mapper;
    }

    public  ApiHttpResponse convertResponse(final ApiHttpResponse response, final Class outputType) {
        try {
            if (response.getBody() == null || response.getBody().length == 0) {
                return (ApiHttpResponse) response;
            }
            O newBody = mapper.readValue(response.getBody(), outputType);
            return new ApiHttpResponse<>(response.getStatusCode(), response.getHeaders(), newBody,
                response.getMessage(), response.getContextMap());
        }
        catch (IOException e) {
            throw new DeserializationException(e.getMessage(), e);
        }
    }

    public  ApiHttpResponse convertResponse(final ApiHttpResponse response, final JavaType outputType) {
        try {
            if (response.getBody() == null) {
                return (ApiHttpResponse) response;
            }
            O newBody = mapper.readValue(response.getBody(), outputType);
            return new ApiHttpResponse<>(response.getStatusCode(), response.getHeaders(), newBody,
                response.getMessage(), response.getContextMap());
        }
        catch (IOException e) {
            throw new DeserializationException(e.getMessage(), e);
        }
    }

    public  ApiHttpResponse convertResponse(final ApiHttpResponse response,
            final TypeReference outputType) {
        try {
            if (response.getBody() == null) {
                return (ApiHttpResponse) response;
            }
            O newBody = mapper.readValue(response.getBody(), outputType);
            return new ApiHttpResponse<>(response.getStatusCode(), response.getHeaders(), newBody,
                response.getMessage(), response.getContextMap());
        }
        catch (IOException e) {
            throw new DeserializationException(e.getMessage(), e);
        }
    }

    public byte[] toJsonByteArray(final Object value) throws JsonProcessingException {
        return mapper.writeValueAsBytes(value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy