com.jslsolucoes.jax.rs.client.se.api.impl.DefaultJaxRsApiClientRequestAsync Maven / Gradle / Ivy
package com.jslsolucoes.jax.rs.client.se.api.impl;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jslsolucoes.jax.rs.client.se.HttpMethod;
import com.jslsolucoes.jax.rs.client.se.api.JaxRsApiClient;
import com.jslsolucoes.jax.rs.client.se.api.JaxRsApiClientRequestAsync;
import com.jslsolucoes.jax.rs.client.se.api.JaxRsApiClientResponse;
public class DefaultJaxRsApiClientRequestAsync implements JaxRsApiClientRequestAsync {
private static final Logger logger = LoggerFactory.getLogger(DefaultJaxRsApiClientRequestAsync.class);
private Set accepts = new HashSet<>();
private Map headers = new HashMap();
private WebTarget webTarget;
private JaxRsApiClient jaxRsApiClient;
private Integer retryAttempts;
private Integer retryDelay;
public DefaultJaxRsApiClientRequestAsync(JaxRsApiClient jaxRsApiClient, WebTarget webTarget, Set accepts,
Map headers, Integer retryAttempts, Integer retryDelay) {
this.accepts = accepts;
this.headers = headers;
this.webTarget = webTarget;
this.jaxRsApiClient = jaxRsApiClient;
this.retryAttempts = retryAttempts;
this.retryDelay = retryDelay;
}
@Override
public CompletableFuture get() {
return CompletableFuture.supplyAsync(() -> {
return new DefaultJaxRsApiClientResponse(jaxRsApiClient, retry(HttpMethod.GET));
});
}
@Override
public CompletableFuture delete() {
return CompletableFuture.supplyAsync(() -> {
return new DefaultJaxRsApiClientResponse(jaxRsApiClient, retry(HttpMethod.DELETE));
});
}
@Override
public CompletableFuture post(Entity> entity) {
return CompletableFuture.supplyAsync(() -> {
return new DefaultJaxRsApiClientResponse(jaxRsApiClient, retry(HttpMethod.POST, entity));
});
}
@Override
public CompletableFuture put(Entity> entity) {
return CompletableFuture.supplyAsync(() -> {
return new DefaultJaxRsApiClientResponse(jaxRsApiClient, retry(HttpMethod.POST, entity));
});
}
@Override
public CompletableFuture post(Object entity) {
return CompletableFuture.supplyAsync(() -> {
return new DefaultJaxRsApiClientResponse(jaxRsApiClient, retry(HttpMethod.POST, entity(entity)));
});
}
@Override
public CompletableFuture put(Object entity) {
return CompletableFuture.supplyAsync(() -> {
return new DefaultJaxRsApiClientResponse(jaxRsApiClient, retry(HttpMethod.PUT, entity(entity)));
});
}
private Response retry(HttpMethod httpMethod) {
return retry(httpMethod, 1, null);
}
private Response retry(HttpMethod httpMethod, Entity> entity) {
return retry(httpMethod, 1, entity);
}
private Response retry(HttpMethod httpMethod, Integer retryAttempt, Entity> entity) {
if (retryAttempt <= retryAttempts) {
try {
return execute(httpMethod, entity);
} catch (ProcessingException e) {
logger.error("Request failed due to {}, retrying attempt {} of {} with delay {}", e.getMessage(),
retryAttempt, retryAttempts, retryDelay, e);
try {
Thread.sleep(retryDelay);
} catch (InterruptedException ie) {
logger.error("Could not wait for retry delay {}", retryDelay, ie);
}
return retry(httpMethod, ++retryAttempt, entity);
}
} else {
return new ServiceUnavailableResponse();
}
}
private Entity> entity(Object object) {
return Entity.entity(object, MediaType.APPLICATION_JSON);
}
private Response execute(HttpMethod httpMethod, Entity> entity) {
if (httpMethod.equals(HttpMethod.GET)) {
return builder().get();
} else if (httpMethod.equals(HttpMethod.DELETE)) {
return builder().delete();
} else if (httpMethod.equals(HttpMethod.POST)) {
return builder().post(entity);
} else if (httpMethod.equals(HttpMethod.PUT)) {
return builder().put(entity);
} else {
return builder().get();
}
}
private Builder builder() {
return webTarget.request().accept(accepts()).headers(headers());
}
private MultivaluedMap headers() {
return new MultivaluedHashMap(Clonables.clone(headers));
}
private String[] accepts() {
return Clonables.clone(accepts).toArray(new String[] {});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy