Search in sources :

Example 6 with NotificationParamsBuilder

use of io.gravitee.management.service.notification.NotificationParamsBuilder in project gravitee-management-rest-api by gravitee-io.

the class ApiKeyServiceImpl method setExpiration.

private void setExpiration(Date expirationDate, ApiKey key) throws TechnicalException {
    ApiKey oldkey = new ApiKey(key);
    if (!key.isRevoked() && key.getExpireAt() == null) {
        key.setUpdatedAt(new Date());
        key.setExpireAt(expirationDate);
        apiKeyRepository.update(key);
        // notify
        final ApplicationEntity application = applicationService.findById(key.getApplication());
        final PlanEntity plan = planService.findById(key.getPlan());
        final ApiModelEntity api = apiService.findByIdForTemplates(plan.getApis().iterator().next());
        final PrimaryOwnerEntity owner = application.getPrimaryOwner();
        NotificationParamsBuilder paramsBuilder = new NotificationParamsBuilder();
        paramsBuilder.api(api).application(application).apikey(key).plan(plan).owner(owner);
        if (key.getExpireAt() != null && new Date().before(key.getExpireAt())) {
            paramsBuilder.expirationDate(key.getExpireAt());
        }
        final Map<String, Object> params = paramsBuilder.build();
        notifierService.trigger(ApiHook.APIKEY_EXPIRED, api.getId(), params);
        // Audit
        auditService.createApiAuditLog(plan.getApis().iterator().next(), Collections.singletonMap(API_KEY, key.getKey()), APIKEY_EXPIRED, key.getUpdatedAt(), oldkey, key);
    }
}
Also used : ApiKey(io.gravitee.repository.management.model.ApiKey) NotificationParamsBuilder(io.gravitee.management.service.notification.NotificationParamsBuilder)

Example 7 with NotificationParamsBuilder

use of io.gravitee.management.service.notification.NotificationParamsBuilder in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionServiceImpl method close.

@Override
public SubscriptionEntity close(String subscriptionId) {
    try {
        logger.debug("Close subscription {}", subscriptionId);
        Optional<Subscription> optSubscription = subscriptionRepository.findById(subscriptionId);
        if (!optSubscription.isPresent()) {
            throw new SubscriptionNotFoundException(subscriptionId);
        }
        Subscription subscription = optSubscription.get();
        if (subscription.getStatus() == Subscription.Status.ACCEPTED) {
            Subscription previousSubscription = new Subscription(subscription);
            final Date now = new Date();
            subscription.setUpdatedAt(now);
            subscription.setStatus(Subscription.Status.CLOSED);
            subscription.setClosedAt(new Date());
            subscription = subscriptionRepository.update(subscription);
            // Send an email to subscriber
            final ApplicationEntity application = applicationService.findById(subscription.getApplication());
            final PlanEntity plan = planService.findById(subscription.getPlan());
            String apiId = plan.getApis().iterator().next();
            final ApiModelEntity api = apiService.findByIdForTemplates(apiId);
            final PrimaryOwnerEntity owner = application.getPrimaryOwner();
            final Map<String, Object> params = new NotificationParamsBuilder().owner(owner).api(api).plan(plan).application(application).build();
            notifierService.trigger(ApiHook.SUBSCRIPTION_CLOSED, apiId, params);
            notifierService.trigger(ApplicationHook.SUBSCRIPTION_CLOSED, application.getId(), params);
            createAudit(apiId, subscription.getApplication(), SUBSCRIPTION_CLOSED, subscription.getUpdatedAt(), previousSubscription, subscription);
            // API Keys are automatically revoked
            Set<ApiKeyEntity> apiKeys = apiKeyService.findBySubscription(subscription.getId());
            for (ApiKeyEntity apiKey : apiKeys) {
                Date expireAt = apiKey.getExpireAt();
                if (!apiKey.isRevoked() && (expireAt == null || expireAt.equals(now) || expireAt.before(now))) {
                    apiKey.setExpireAt(now);
                    apiKey.setRevokedAt(now);
                    apiKey.setRevoked(true);
                    apiKeyService.revoke(apiKey.getKey(), false);
                }
            }
            return convert(subscription);
        }
        throw new SubscriptionNotClosableException(subscription);
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to close subscription {}", subscriptionId, ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to close subscription %s", subscriptionId), ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NotificationParamsBuilder(io.gravitee.management.service.notification.NotificationParamsBuilder) Subscription(io.gravitee.repository.management.model.Subscription)

Aggregations

NotificationParamsBuilder (io.gravitee.management.service.notification.NotificationParamsBuilder)7 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 Subscription (io.gravitee.repository.management.model.Subscription)3 EmailNotificationBuilder (io.gravitee.management.service.builder.EmailNotificationBuilder)2 ApiKey (io.gravitee.repository.management.model.ApiKey)2 JWTSigner (com.auth0.jwt.JWTSigner)1 Page (io.gravitee.common.data.domain.Page)1 UUID (io.gravitee.common.utils.UUID)1 io.gravitee.management.model (io.gravitee.management.model)1 Pageable (io.gravitee.management.model.common.Pageable)1 Metadata (io.gravitee.management.model.pagedresult.Metadata)1 SubscriptionQuery (io.gravitee.management.model.subscription.SubscriptionQuery)1 io.gravitee.management.service (io.gravitee.management.service)1 io.gravitee.management.service.exceptions (io.gravitee.management.service.exceptions)1 ApiKeyNotFoundException (io.gravitee.management.service.exceptions.ApiKeyNotFoundException)1 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)1 ApiHook (io.gravitee.management.service.notification.ApiHook)1 ApplicationHook (io.gravitee.management.service.notification.ApplicationHook)1 SubscriptionRepository (io.gravitee.repository.management.api.SubscriptionRepository)1 SubscriptionCriteria (io.gravitee.repository.management.api.search.SubscriptionCriteria)1