use of io.gravitee.repository.management.model.Audit.AuditProperties.API in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionServiceImpl method create.
@Override
public SubscriptionEntity create(NewSubscriptionEntity newSubscriptionEntity) {
String plan = newSubscriptionEntity.getPlan();
String application = newSubscriptionEntity.getApplication();
try {
logger.debug("Create a new subscription for plan {} and application {}", plan, application);
PlanEntity planEntity = planService.findById(plan);
if (planEntity.getStatus() == PlanStatus.CLOSED) {
throw new PlanAlreadyClosedException(plan);
}
if (planEntity.getStatus() == PlanStatus.STAGING) {
throw new PlanNotYetPublishedException(plan);
}
if (planEntity.getSecurity() == PlanSecurityType.KEY_LESS) {
throw new PlanNotSubscribableException("A key_less plan is not subscribable !");
}
ApplicationEntity applicationEntity = applicationService.findById(application);
// Check existing subscriptions
List<Subscription> subscriptions = subscriptionRepository.search(new SubscriptionCriteria.Builder().applications(Collections.singleton(application)).apis(planEntity.getApis()).build());
if (!subscriptions.isEmpty()) {
Predicate<Subscription> onlyValidSubs = subscription -> subscription.getStatus() != Subscription.Status.REJECTED && subscription.getStatus() != Subscription.Status.CLOSED;
// First, check that there is no subscription to the same plan
long subscriptionCount = subscriptions.stream().filter(onlyValidSubs).filter(subscription -> subscription.getPlan().equals(plan)).count();
if (subscriptionCount > 0) {
throw new PlanAlreadySubscribedException(plan);
}
// Check that there is no existing subscription based on an OAuth2 or JWT plan
if (planEntity.getSecurity() == PlanSecurityType.OAUTH2 || planEntity.getSecurity() == PlanSecurityType.JWT) {
long count = subscriptions.stream().filter(onlyValidSubs).map(Subscription::getPlan).distinct().map(plan1 -> planService.findById(plan1)).filter(subPlan -> subPlan.getSecurity() == PlanSecurityType.OAUTH2 || subPlan.getSecurity() == PlanSecurityType.JWT).count();
if (count > 0) {
throw new PlanNotSubscribableException("An other OAuth2 or JWT plan is already subscribed by the same application.");
}
}
}
if (planEntity.getSecurity() == PlanSecurityType.OAUTH2 || planEntity.getSecurity() == PlanSecurityType.JWT) {
// Check that the application contains a client_id
if (applicationEntity.getClientId() == null || applicationEntity.getClientId().trim().isEmpty()) {
throw new PlanNotSubscribableException("A client_id is required to subscribe to an OAuth2 or JWT plan.");
}
}
Subscription subscription = new Subscription();
subscription.setPlan(plan);
subscription.setId(UUID.toString(UUID.random()));
subscription.setApplication(application);
subscription.setCreatedAt(new Date());
subscription.setUpdatedAt(subscription.getCreatedAt());
subscription.setStatus(Subscription.Status.PENDING);
subscription.setRequest(newSubscriptionEntity.getRequest());
subscription.setSubscribedBy(getAuthenticatedUser().getUsername());
subscription.setClientId(applicationEntity.getClientId());
String apiId = planEntity.getApis().iterator().next();
subscription.setApi(apiId);
subscription = subscriptionRepository.create(subscription);
createAudit(apiId, application, SUBSCRIPTION_CREATED, subscription.getCreatedAt(), null, subscription);
final ApiModelEntity api = apiService.findByIdForTemplates(apiId);
final PrimaryOwnerEntity apiOwner = api.getPrimaryOwner();
// final PrimaryOwnerEntity appOwner = applicationEntity.getPrimaryOwner();
String portalUrl = environment.getProperty("portalURL");
String subscriptionsUrl = "";
if (portalUrl != null) {
if (portalUrl.endsWith("/")) {
portalUrl = portalUrl.substring(0, portalUrl.length() - 1);
}
subscriptionsUrl = portalUrl + "/#!/management/apis/" + api.getId() + "/subscriptions/" + subscription.getId();
}
final Map<String, Object> params = new NotificationParamsBuilder().api(api).plan(planEntity).application(applicationEntity).owner(apiOwner).subscription(convert(subscription)).subscriptionsUrl(subscriptionsUrl).build();
if (PlanValidationType.AUTO == planEntity.getValidation()) {
ProcessSubscriptionEntity process = new ProcessSubscriptionEntity();
process.setId(subscription.getId());
process.setAccepted(true);
process.setStartingAt(new Date());
// Do process
return process(process, SUBSCRIPTION_SYSTEM_VALIDATOR);
} else {
notifierService.trigger(ApiHook.SUBSCRIPTION_NEW, apiId, params);
notifierService.trigger(ApplicationHook.SUBSCRIPTION_NEW, application, params);
return convert(subscription);
}
} catch (TechnicalException ex) {
logger.error("An error occurs while trying to subscribe to the plan {}", plan, ex);
throw new TechnicalManagementException(String.format("An error occurs while trying to subscribe to the plan %s", plan), ex);
}
}
Aggregations