All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vrap.rmf.base.client.http.NotFoundExceptionMiddlewareImpl Maven / Gradle / Ivy

There is a newer version: 17.17.0
Show newest version

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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy