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

com.commercetools.sunrise.framework.controllers.WithExecutionFlow Maven / Gradle / Ivy

The newest version!
package com.commercetools.sunrise.framework.controllers;

import io.sphere.sdk.client.ClientErrorException;
import org.slf4j.Logger;
import play.libs.concurrent.HttpExecution;
import play.mvc.Result;

import java.util.concurrent.CompletionStage;

import static io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture;
import static io.sphere.sdk.utils.CompletableFutureUtils.recoverWith;

/**
 * Approach to handle execution of data (Template Method Pattern).
 * @param  type of the input data, possibly a parameter object
 * @param  type of the output object
 */
public interface WithExecutionFlow {

    Logger getLogger();

    default CompletionStage processRequest(final I input) {
        final CompletionStage resultStage = executeAction(input)
                .thenComposeAsync(this::handleSuccessfulAction, HttpExecution.defaultContext());
        return recoverWith(resultStage, throwable -> handleFailedAction(input, throwable), HttpExecution.defaultContext());
    }

    CompletionStage executeAction(final I input);

    default CompletionStage handleFailedAction(final I input, final Throwable throwable) {
        final Throwable causeThrowable = throwable.getCause();
        if (causeThrowable instanceof ClientErrorException) {
            return handleClientErrorFailedAction(input, (ClientErrorException) causeThrowable);
        }
        return handleGeneralFailedAction(throwable);
    }

    CompletionStage handleClientErrorFailedAction(final I input, final ClientErrorException clientErrorException);

    default CompletionStage handleGeneralFailedAction(final Throwable throwable) {
        return exceptionallyCompletedFuture(throwable);
    }

    CompletionStage handleSuccessfulAction(final O output);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy