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

com.github.nagyesta.lowkeyvault.service.certificate.impl.CertificateLifetimeActionPolicy Maven / Gradle / Ivy

There is a newer version: 2.5.81
Show newest version
package com.github.nagyesta.lowkeyvault.service.certificate.impl;

import com.github.nagyesta.lowkeyvault.service.certificate.CertificateLifetimeActionActivity;
import com.github.nagyesta.lowkeyvault.service.certificate.CertificateLifetimeActionTrigger;
import com.github.nagyesta.lowkeyvault.service.certificate.LifetimeActionPolicy;
import com.github.nagyesta.lowkeyvault.service.certificate.id.CertificateEntityId;
import com.github.nagyesta.lowkeyvault.service.common.impl.BaseLifetimePolicy;
import lombok.NonNull;
import org.springframework.util.Assert;

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class CertificateLifetimeActionPolicy extends BaseLifetimePolicy implements LifetimeActionPolicy {

    private Map lifetimeActions;

    public CertificateLifetimeActionPolicy(
            @org.springframework.lang.NonNull final CertificateEntityId certificateEntityId,
            @NonNull final Map lifetimeActions) {
        super(certificateEntityId);
        this.lifetimeActions = Map.copyOf(lifetimeActions);
    }

    @Override
    public Map getLifetimeActions() {
        return lifetimeActions;
    }

    @Override
    public void setLifetimeActions(final Map lifetimeActions) {
        this.lifetimeActions = Map.copyOf(lifetimeActions);
        this.markUpdate();
    }

    @Override
    public boolean isAutoRenew() {
        return getLifetimeActions().containsKey(CertificateLifetimeActionActivity.AUTO_RENEW);
    }

    @Override
    public void validate(final int validityMonths) {
        lifetimeActions.values().forEach(a -> a.validate(validityMonths));
    }

    @Override
    public List missedRenewalDays(final OffsetDateTime validityStart,
                                                  final Function createdToExpiryFunction) {
        Assert.isTrue(isAutoRenew(), "Cannot have missed renewals without an \"AutoRenew\" lifetime action.");
        final CertificateLifetimeActionTrigger trigger = lifetimeActions.get(CertificateLifetimeActionActivity.AUTO_RENEW);
        final Function triggerAfterDaysFunction = s -> trigger
                .triggersAfterDays(s, createdToExpiryFunction.apply(s));
        final OffsetDateTime startPoint = findTriggerTimeOffset(validityStart, triggerAfterDaysFunction);
        return collectMissedTriggerDays(triggerAfterDaysFunction, startPoint);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy