io.vrap.rmf.base.client.http.NotFoundExceptionMiddlewareImpl 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.http;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.Function;
import java.util.function.Predicate;
import com.spotify.futures.CompletableFutures;
import io.vrap.rmf.base.client.ApiHttpRequest;
import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.error.NotFoundException;
class NotFoundExceptionMiddlewareImpl implements NotFoundExceptionMiddleware {
private final Predicate requestPredicate;
public NotFoundExceptionMiddlewareImpl() {
requestPredicate = apiHttpRequest -> true;
}
public NotFoundExceptionMiddlewareImpl(final Predicate requestPredicate) {
this.requestPredicate = requestPredicate;
}
@Override
public CompletableFuture> invoke(ApiHttpRequest request,
Function>> next) {
return CompletableFutures.exceptionallyCompose(next.apply(request), (throwable) -> {
Throwable cause = throwable instanceof CompletionException ? throwable.getCause() : throwable;
if (cause instanceof NotFoundException && requestPredicate.test(request)) {
ApiHttpResponse response = ((NotFoundException) throwable.getCause()).getResponse();
return CompletableFuture.completedFuture(new ApiHttpResponse<>(response.getStatusCode(),
response.getHeaders(), null, response.getMessage(), response.getContextMap()));
}
CompletableFuture> future = new CompletableFuture<>();
future.completeExceptionally(throwable.getCause());
return future;
}).toCompletableFuture();
}
}