use of io.gravitee.management.service.exceptions.SubscriptionClosedException in project gravitee-management-rest-api by gravitee-io.
the class ApiKeyServiceImpl method generateForSubscription.
/**
* Generate an {@link ApiKey} from a subscription.
*
* @param subscription
* @return An Api Key
*/
private ApiKey generateForSubscription(String subscription) {
SubscriptionEntity subscriptionEntity = subscriptionService.findById(subscription);
Date now = new Date();
if (subscriptionEntity.getEndingAt() != null && subscriptionEntity.getEndingAt().before(now)) {
throw new SubscriptionClosedException(subscription);
}
ApiKey apiKey = new ApiKey();
apiKey.setSubscription(subscription);
apiKey.setApplication(subscriptionEntity.getApplication());
apiKey.setPlan(subscriptionEntity.getPlan());
apiKey.setCreatedAt(new Date());
apiKey.setUpdatedAt(apiKey.getCreatedAt());
apiKey.setKey(apiKeyGenerator.generate());
// By default, the API Key will expire when subscription is closed
apiKey.setExpireAt(subscriptionEntity.getEndingAt());
return apiKey;
}
Aggregations