io.vrap.rmf.base.client.ResponseSerializerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rmf-java-base Show documentation
Show all versions of rmf-java-base Show documentation
The e-commerce SDK from commercetools Composable Commerce for Java
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);
}
}