Search in sources :

Example 1 with GetConsentRequest

use of net.petafuel.styx.core.xs2a.standards.berlingroup.v1_2.http.GetConsentRequest in project styx by petafuel.

the class ConsentPoll method execute.

public void execute() {
    LOG.debug("Executing Task id:{} signature:{}", getId(), getSignature());
    Consent currentConsent = persistentConsent.get(consent);
    if (currentConsent == null) {
        throw new TaskFinalFailureException("Consent queued for polling does not exist in the styx database, cannot poll", TaskFinalFailureCode.POLL_ON_NOT_EXISTING_CONSENT);
    } else if (currentConsent.getState() == ConsentStatus.VALID) {
        throw new TaskFinalFailureException("Consent with id " + currentConsent.getId() + " is already on state valid, no polling required", TaskFinalFailureCode.POLL_ON_ALREADY_VALID_CONSENT);
    }
    // TODO make the request type as a parameter
    StatusConsentRequest statusConsentRequest = new StatusConsentRequest(null, consent.getId(), null, null);
    statusConsentRequest.setPsu(consent.getPsu());
    Iterator<Integer> retryIterator = IntStream.range(0, maxRequestRetries).iterator();
    while (retryIterator.hasNext()) {
        if (Thread.interrupted()) {
            throw new TaskFinalFailureException("Task Thread was interrupted");
        }
        try {
            ConsentStatus currentStatus = this.csInterface.getStatus(statusConsentRequest);
            if (currentStatus == ConsentStatus.VALID) {
                LOG.debug("Consent is valid, SCA was successful");
                break;
            } else if (!(currentStatus.equals(ConsentStatus.RECEIVED) || currentStatus.equals(ConsentStatus.PARTIALLY_AUTHORISED))) {
                currentConsent.setState(currentStatus);
                persistentConsent.update(currentConsent);
                throw new TaskFinalFailureException("Consent cannot be polled anymore due to unrecoverable status: " + currentStatus.toString(), TaskFinalFailureCode.UNRECOVERABLE_STATUS);
            }
            LOG.debug("Consent status was not valid: {}", currentStatus);
        } catch (BankRequestFailedException e) {
            LOG.warn("Trying to poll consent resulted in an error: {} status: {} retry-iteration: {}", e.getMessage(), e.getHttpStatusCode(), retryIterator.next());
        }
        try {
            Thread.sleep(timeoutBetweenRetries);
        } catch (InterruptedException e) {
            LOG.error("Task {} execution was interrupted: {}", getId(), e.getMessage());
            Thread.currentThread().interrupt();
        }
        retryIterator.next();
    }
    // TODO make GetConsentRequest class as a parameter
    GetConsentRequest getConsentRequest = new GetConsentRequest(null, consent.getId(), null, null);
    try {
        Consent aspspConsent = csInterface.getConsent(getConsentRequest);
        if (!aspspConsent.getState().equals(ConsentStatus.VALID)) {
            throw new TaskRetryFailureException("Upon Consent Poll completion, consent is still not authorized by PSU");
        }
        currentConsent.setFrequencyPerDay(aspspConsent.getFrequencyPerDay());
        currentConsent.setValidUntil(aspspConsent.getValidUntil());
        currentConsent.setState(aspspConsent.getState());
        persistentConsent.update(currentConsent);
        LOG.info("Successfully updated consent");
    } catch (BankRequestFailedException e) {
        LOG.error("Unable to get consent information after polling was successful -> task marked as failed");
        throw new TaskRetryFailureException("Unable to get consent information after polling was successful", e);
    }
}
Also used : StatusConsentRequest(net.petafuel.styx.core.xs2a.standards.berlingroup.v1_2.http.StatusConsentRequest) PersistentConsent(net.petafuel.styx.core.persistence.layers.PersistentConsent) Consent(net.petafuel.styx.core.xs2a.entities.Consent) ConsentStatus(net.petafuel.styx.core.xs2a.entities.ConsentStatus) GetConsentRequest(net.petafuel.styx.core.xs2a.standards.berlingroup.v1_2.http.GetConsentRequest) TaskFinalFailureException(net.petafuel.styx.keepalive.entities.TaskFinalFailureException) TaskRetryFailureException(net.petafuel.styx.keepalive.entities.TaskRetryFailureException) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException)

Aggregations

PersistentConsent (net.petafuel.styx.core.persistence.layers.PersistentConsent)1 Consent (net.petafuel.styx.core.xs2a.entities.Consent)1 ConsentStatus (net.petafuel.styx.core.xs2a.entities.ConsentStatus)1 BankRequestFailedException (net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException)1 GetConsentRequest (net.petafuel.styx.core.xs2a.standards.berlingroup.v1_2.http.GetConsentRequest)1 StatusConsentRequest (net.petafuel.styx.core.xs2a.standards.berlingroup.v1_2.http.StatusConsentRequest)1 TaskFinalFailureException (net.petafuel.styx.keepalive.entities.TaskFinalFailureException)1 TaskRetryFailureException (net.petafuel.styx.keepalive.entities.TaskRetryFailureException)1