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

com.github.nagyesta.lowkeyvault.controller.common.GenericEntityController Maven / Gradle / Ivy

There is a newer version: 2.5.81
Show newest version
package com.github.nagyesta.lowkeyvault.controller.common;

import com.github.nagyesta.lowkeyvault.mapper.common.BaseEntityConverterRegistry;
import com.github.nagyesta.lowkeyvault.mapper.common.RecoveryAwareConverter;
import com.github.nagyesta.lowkeyvault.model.common.KeyVaultItemListModel;
import com.github.nagyesta.lowkeyvault.model.v7_2.BasePropertiesModel;
import com.github.nagyesta.lowkeyvault.model.v7_2.BasePropertiesUpdateModel;
import com.github.nagyesta.lowkeyvault.model.v7_2.common.BaseBackupListItem;
import com.github.nagyesta.lowkeyvault.model.v7_2.common.BaseBackupModel;
import com.github.nagyesta.lowkeyvault.model.v7_2.key.BackupListContainer;
import com.github.nagyesta.lowkeyvault.service.EntityId;
import com.github.nagyesta.lowkeyvault.service.common.BaseVaultEntity;
import com.github.nagyesta.lowkeyvault.service.common.BaseVaultFake;
import com.github.nagyesta.lowkeyvault.service.exception.NotFoundException;
import com.github.nagyesta.lowkeyvault.service.vault.VaultFake;
import com.github.nagyesta.lowkeyvault.service.vault.VaultService;
import lombok.NonNull;

import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * The base implementation of the entity controllers.
 *
 * @param    The type of the key (not versioned).
 * @param    The versioned key type.
 * @param    The entity type.
 * @param    The entity model type.
 * @param   The deleted entity model type.
 * @param    The item model type.
 * @param   The deleted item model type.
 * @param   The model converter, converting entities to entity models.
 * @param   The item converter, converting version item entities to item models.
 * @param  The versioned item converter, converting version item entities to item models.
 * @param    The fake type holding the entities.
 * @param   The type of the PropertiesModel.
 * @param  The list item type of the backup lists.
 * @param   The type of the backup list.
 * @param    The type of the backup model.
 * @param    The ConverterRegistry used for conversions.
 */
public abstract class GenericEntityController,
        M, DM extends M, I, DI extends I,
        MC extends RecoveryAwareConverter,
        IC extends RecoveryAwareConverter,
        VIC extends RecoveryAwareConverter,
        S extends BaseVaultFake,
        PM extends BasePropertiesModel,
        BLI extends BaseBackupListItem,
        BL extends BackupListContainer,
        B extends BaseBackupModel,
        R extends BaseEntityConverterRegistry> extends BaseEntityReadController {
    /**
     * Default page size used when returning available versions of an entity.
     */
    protected static final String DEFAULT_MAX = "25";
    /**
     * Default offset used  when returning available versions of an entity.
     */
    protected static final String SKIP_ZERO = "0";
    /**
     * Parameter name for the page size when returning versions of an entity.
     */
    protected static final String MAX_RESULTS_PARAM = "maxresults";
    /**
     * Parameter name for the offset when returning versions of an entity.
     */
    protected static final String SKIP_TOKEN_PARAM = "$skiptoken";
    private final R registry;

    protected R registry() {
        return registry;
    }

    protected GenericEntityController(@NonNull final R registry,
                                      @org.springframework.lang.NonNull final VaultService vaultService,
                                      @org.springframework.lang.NonNull final Function toEntityVault) {
        super(vaultService, toEntityVault);
        this.registry = registry;
    }

    @Override
    protected final V versionedEntityId(final URI baseUri, final String name, final String version) {
        return registry.versionedEntityId(baseUri, name, version);
    }

    @Override
    protected final K entityId(final URI baseUri, final String name) {
        return registry.entityId(baseUri, name);
    }

    protected M getModelById(final S entityVaultFake, final V entityId, final URI baseUri, final boolean includeDisabled) {
        final E entity = entityVaultFake.getEntities().getReadOnlyEntity(entityId);
        if (!includeDisabled && !entity.isEnabled()) {
            throw new NotFoundException("Operation get is not allowed on a disabled entity.");
        }
        return registry.modelConverter(apiVersion()).convert(entity, baseUri);
    }

    protected DM getDeletedModelById(final S entityVaultFake, final V entityId, final URI baseUri, final boolean includeDisabled) {
        final E entity = entityVaultFake.getDeletedEntities().getReadOnlyEntity(entityId);
        if (!includeDisabled && !entity.isEnabled()) {
            throw new NotFoundException("Operation get is not allowed on a disabled entity.");
        }
        return registry.modelConverter(apiVersion()).convertDeleted(entity, baseUri);
    }

    protected M convertDetails(final E entity, final URI vaultUri) {
        return registry.modelConverter(apiVersion()).convert(entity, vaultUri);
    }

    protected KeyVaultItemListModel getPageOfItemVersions(
            final URI baseUri, final String name, final PaginationContext pagination) {
        final S entityVaultFake = getVaultByUri(baseUri);
        final K entityId = entityId(baseUri, name);
        final List allItems = entityVaultFake.getEntities().getVersions(entityId)
                .stream()
                .sorted()
                .collect(Collectors.toList());
        final List items = filterList(pagination.getLimit(), pagination.getOffset(), allItems, v -> {
            final E entity = getEntityByNameAndVersion(baseUri, name, v);
            return registry.versionedItemConverter(apiVersion()).convert(entity, baseUri);
        });
        final URI nextUri = PaginationContext.builder()
                .base(pagination.getBase())
                .apiVersion(pagination.getApiVersion())
                .currentItems(items.size())
                .totalItems(allItems.size())
                .limit(pagination.getLimit())
                .offset(pagination.getOffset())
                .additionalParameters(pagination.getAdditionalParameters())
                .build()
                .asNextUri();
        return listModel(items, nextUri);
    }

    @SuppressWarnings("SameParameterValue")
    protected KeyVaultItemListModel getPageOfItems(final URI baseUri, final PaginationContext pagination) {
        final S entityVaultFake = getVaultByUri(baseUri);
        final List allItems = entityVaultFake.getEntities().listLatestEntities();
        final List items = filterList(pagination.getLimit(), pagination.getOffset(), allItems,
                source -> registry.itemConverter(apiVersion()).convert(source, baseUri));
        final URI nextUri = PaginationContext.builder()
                .base(pagination.getBase())
                .apiVersion(pagination.getApiVersion())
                .currentItems(items.size())
                .totalItems(allItems.size())
                .limit(pagination.getLimit())
                .offset(pagination.getOffset())
                .additionalParameters(pagination.getAdditionalParameters())
                .build()
                .asNextUri();
        return listModel(items, nextUri);
    }

    @SuppressWarnings("SameParameterValue")
    protected KeyVaultItemListModel getPageOfDeletedItems(final URI baseUri, final PaginationContext pagination) {
        final S entityVaultFake = getVaultByUri(baseUri);
        final List allItems = entityVaultFake.getDeletedEntities().listLatestEntities();
        final List items = filterList(pagination.getLimit(), pagination.getOffset(), allItems,
                source -> registry.itemConverter(apiVersion()).convertDeleted(source, baseUri));
        final URI nextUri = PaginationContext.builder()
                .base(pagination.getBase())
                .apiVersion(apiVersion())
                .currentItems(items.size())
                .totalItems(allItems.size())
                .limit(pagination.getLimit())
                .offset(pagination.getOffset())
                .additionalParameters(pagination.getAdditionalParameters())
                .build()
                .asNextUri();
        return listModel(items, nextUri);
    }

    protected M getLatestEntityModel(final URI baseUri, final String name) {
        final S vaultFake = getVaultByUri(baseUri);
        final V entityId = vaultFake.getEntities().getLatestVersionOfEntity(entityId(baseUri, name));
        return getModelById(vaultFake, entityId, baseUri, false);
    }

    protected M getSpecificEntityModel(final URI baseUri, final String name, final String version) {
        final S vaultFake = getVaultByUri(baseUri);
        final V entityId = versionedEntityId(baseUri, name, version);
        return getModelById(vaultFake, entityId, baseUri, false);
    }

    protected void updateAttributes(final BaseVaultFake vaultFake, final V entityId, final BasePropertiesUpdateModel properties) {
        Optional.ofNullable(properties)
                .ifPresent(attributes -> {
                    Optional.ofNullable(attributes.getEnabled())
                            .ifPresent(enabled -> vaultFake.setEnabled(entityId, enabled));
                    if (attributes.getExpiresOn() != null || attributes.getNotBefore() != null) {
                        vaultFake.setExpiry(entityId, attributes.getNotBefore(), attributes.getExpiresOn());
                    }
                });
    }

    protected void updateTags(final BaseVaultFake vaultFake, final V entityId, final Map requestTags) {
        Optional.ofNullable(requestTags)
                .ifPresent(tags -> {
                    vaultFake.clearTags(entityId);
                    vaultFake.addTags(entityId, tags);
                });
    }

    protected 
  • KeyVaultItemListModel
  • listModel(final List
  • items, final URI nextUri) { return new KeyVaultItemListModel<>(items, nextUri); } private List
  • filterList( final int limit, final int offset, final Collection allItems, final Function mapper) { return allItems.stream() .skip(offset) .limit(limit) .map(mapper) .collect(Collectors.toList()); } }




  • © 2015 - 2024 Weber Informatics LLC | Privacy Policy