Search in sources :

Example 1 with ConsentStatus

use of net.petafuel.styx.core.xs2a.entities.ConsentStatus in project styx by petafuel.

the class GetConsentResource method getConsentStatus.

/**
 * Returns the consent status
 *
 * @param consentId of the target consent
 * @return a GetConsentStatusResponse object
 * @throws BankRequestFailedException if something went wrong between the core service and the aspsp
 */
@AcceptsPreStepAuth
@GET
@Path("/consents/{consentId}/status")
public Response getConsentStatus(@NotEmpty @NotBlank @PathParam("consentId") String consentId) throws BankRequestFailedException {
    XS2AFactoryInput xs2AFactoryInput = new XS2AFactoryInput();
    xs2AFactoryInput.setConsentId(consentId);
    IOProcessor ioProcessor = new IOProcessor(getXS2AStandard());
    ioProcessor.modifyInput(xs2AFactoryInput);
    AISRequest request = new AISRequestFactory().create(getXS2AStandard().getRequestClassProvider().consentStatus(), xs2AFactoryInput);
    request.getHeaders().putAll(getAdditionalHeaders());
    ioProcessor.modifyRequest(request, xs2AFactoryInput);
    ConsentStatus state = getXS2AStandard().getCs().getStatus(request);
    GetConsentStatusResponse response = new GetConsentStatusResponse(state);
    LOG.info("Successfully fetched consent status entity for bic={}, consentId={}", getXS2AStandard().getAspsp().getBic(), consentId);
    return Response.status(ResponseConstant.OK).entity(response).build();
}
Also used : AISRequest(net.petafuel.styx.core.xs2a.contracts.AISRequest) ConsentStatus(net.petafuel.styx.core.xs2a.entities.ConsentStatus) GetConsentStatusResponse(net.petafuel.styx.api.v1.consent.entity.GetConsentStatusResponse) XS2AFactoryInput(net.petafuel.styx.core.xs2a.factory.XS2AFactoryInput) AISRequestFactory(net.petafuel.styx.core.xs2a.factory.AISRequestFactory) IOProcessor(net.petafuel.styx.core.ioprocessing.IOProcessor) AcceptsPreStepAuth(net.petafuel.styx.api.filter.authentication.boundary.AcceptsPreStepAuth) Path(javax.ws.rs.Path) ApplicationPath(javax.ws.rs.ApplicationPath) GET(javax.ws.rs.GET)

Example 2 with ConsentStatus

use of net.petafuel.styx.core.xs2a.entities.ConsentStatus 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

ConsentStatus (net.petafuel.styx.core.xs2a.entities.ConsentStatus)2 ApplicationPath (javax.ws.rs.ApplicationPath)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 AcceptsPreStepAuth (net.petafuel.styx.api.filter.authentication.boundary.AcceptsPreStepAuth)1 GetConsentStatusResponse (net.petafuel.styx.api.v1.consent.entity.GetConsentStatusResponse)1 IOProcessor (net.petafuel.styx.core.ioprocessing.IOProcessor)1 PersistentConsent (net.petafuel.styx.core.persistence.layers.PersistentConsent)1 AISRequest (net.petafuel.styx.core.xs2a.contracts.AISRequest)1 Consent (net.petafuel.styx.core.xs2a.entities.Consent)1 BankRequestFailedException (net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException)1 AISRequestFactory (net.petafuel.styx.core.xs2a.factory.AISRequestFactory)1 XS2AFactoryInput (net.petafuel.styx.core.xs2a.factory.XS2AFactoryInput)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