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);
}
}
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);
}
}
Aggregations