com.jslsolucoes.jax.rs.client.se.api.impl.DefaultJaxRsApiClientResponse Maven / Gradle / Ivy
package com.jslsolucoes.jax.rs.client.se.api.impl;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.lang3.StringUtils;
import com.jslsolucoes.jax.rs.client.se.JaxRsApiClientException;
import com.jslsolucoes.jax.rs.client.se.api.JaxRsApiClient;
import com.jslsolucoes.jax.rs.client.se.api.JaxRsApiClientResponse;
public class DefaultJaxRsApiClientResponse implements JaxRsApiClientResponse {
private Response response;
private Object entity;
private JaxRsApiClient jaxRsApiClient;
public DefaultJaxRsApiClientResponse(JaxRsApiClient jaxRsApiClient,Response response) {
this.jaxRsApiClient = jaxRsApiClient;
this.response = response;
}
@Override
public Boolean wasStatus(Status status) {
return status().equals(status);
}
public Boolean hasEntity() {
return response.hasEntity();
}
@Override
@SuppressWarnings("unchecked")
public T onStatus(Status status, Class classType) {
if (wasStatus(status)) {
if (entity == null && hasEntity()) {
entity = response.readEntity(classType);
}
if(entity == null) {
throw new JaxRsApiClientException("Could not read body entity form response because it does not exists, check if body exists on response before try read it");
}
return (T) entity;
}
throw new RuntimeException(
"Server response was " + status() + " instead of " + status + " while trying serialize " + classType);
}
@Override
public T onStatusFollow(Status status, Class clazz) {
return onStatusFollow(status, clazz, new HashMap());
}
@Override
public T onStatusFollow(Status status, Class clazz, Map headers) {
if (wasStatus(status)) {
String location = response.getHeaderString("Location");
if (StringUtils.isEmpty(location)) {
throw new JaxRsApiClientException("Location header was not found. Impossible follow resource");
} else {
try (JaxRsApiClientResponse JaxRsApiClientResponse = jaxRsApiClient.target(location).request()
.headers(headers).get()) {
return JaxRsApiClientResponse.onOk(clazz);
}
}
}
throw new JaxRsApiClientException(
"Could not follow resource because expected status was " + status() + " instead of " + status);
}
@Override
public T onOk(Class clazz) {
return onStatus(Status.OK, clazz);
}
@Override
public T onOkFollow(Class clazz) {
return onStatusFollow(Status.OK, clazz);
}
@Override
public T onCreatedFollow(Class clazz) {
return onStatusFollow(Status.CREATED, clazz);
}
@Override
public T onCreated(Class clazz) {
return onStatus(Status.CREATED, clazz);
}
@Override
public T onBadRequest(Class clazz) {
return onStatus(Status.BAD_REQUEST, clazz);
}
@Override
public Boolean wasServiceUnavailable() {
return wasStatus(Status.SERVICE_UNAVAILABLE);
}
@Override
public Boolean wasOk() {
return wasStatus(Status.OK);
}
@Override
public Boolean wasCreated() {
return wasStatus(Status.CREATED);
}
@Override
public Boolean wasBadRequest() {
return wasStatus(Status.BAD_REQUEST);
}
@Override
public Status status() {
return response.getStatusInfo().toEnum();
}
@Override
public Boolean wasNotFound() {
return wasStatus(Status.NOT_FOUND);
}
@Override
public T onOkFollow(Class clazz, Map headers) {
return onStatusFollow(Status.OK, clazz, headers);
}
@Override
public T onCreatedFollow(Class clazz, Map headers) {
return onStatusFollow(Status.CREATED, clazz, headers);
}
@Override
public Boolean wasUnauthorized() {
return wasStatus(Status.UNAUTHORIZED);
}
@Override
public Boolean wasForbidden() {
return wasStatus(Status.FORBIDDEN);
}
@Override
public T onUnauthorized(Class clazz) {
return onStatus(Status.UNAUTHORIZED, clazz);
}
@Override
public T onForbidden(Class clazz) {
return onStatus(Status.FORBIDDEN, clazz);
}
@Override
public T onNotFound(Class clazz) {
return onStatus(Status.NOT_FOUND, clazz);
}
@Override
public void close() {
response.close();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy