public interface MyEndpoint {
@GET
@Path("/my-resource")
CompletionStage<Response> getMyResource(@QueryParam("param1") String param1,
@QueryParam("param2") String param2);
}
MyEndpoint endpointWithHeader = RestClientBuilder.newBuilder()
.baseUri("http://example.com")
.build(MyEndpoint.class);
MyEndpoint endpointWithoutHeader = RestClientBuilder.newBuilder()
.baseUri("http://example.com")
.build(MyEndpoint.class);
String missingHeader = "value-from-mobile-app";
BiFunction<Response, Throwable, CompletionStage<Response>> handleStatus = (response, throwable) -> {
if (response != null && response.getStatus() == 409) {
MultivaluedMap<String, Object> headers = response.getHeaders();
if (headers.containsKey("Missing-Header")) {
headers.add("pin", missingHeader);
// Call the endpoint again with the missing header using the endpointWithHeader client
return endpointWithHeader.getMyResource("value1", "value2", missingHeader);
}
}
// Handle other response codes here using the endpointWithoutHeader client
return CompletableFuture.completedFuture(response);
};
CompletionStage<Response> responseStage = endpointWithoutHeader.getMyResource("value1", "value2");
responseStage.thenCompose(handleStatus)
.thenApply(response -> {
if (response.getStatus() == 200) {
return response.readEntity(MyResource.class);
} else {
throw new MyException("Failed to get resource");
}
});