use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent in project carbon-apimgt by wso2.
the class APIGatewayPublisherImpl method addAPISubscription.
@Override
public void addAPISubscription(List<SubscriptionValidationData> subscriptionValidationDataList) throws GatewayException {
SubscriptionEvent subscriptionAddEvent = new SubscriptionEvent(APIMgtConstants.GatewayEventTypes.SUBSCRIPTION_CREATE);
subscriptionAddEvent.setSubscriptionsList(subscriptionValidationDataList);
publishToStoreTopic(subscriptionAddEvent);
if (log.isDebugEnabled()) {
log.debug("Subscription created event has been successfully published to broker");
}
}
use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent in project carbon-apimgt by wso2.
the class WorkflowUtils method sendNotificationAfterWFComplete.
public static void sendNotificationAfterWFComplete(WorkflowDTO workflowDTO, String wfType) throws APIManagementException {
if (WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION.equalsIgnoreCase(wfType)) {
String applicationId = workflowDTO.getWorkflowReference();
int appId = Integer.parseInt(applicationId);
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
Application application = apiMgtDAO.getApplicationById(appId);
ApplicationWorkflowDTO appWFDto = (ApplicationWorkflowDTO) workflowDTO;
appWFDto.setApplication(application);
ApplicationEvent applicationEvent = new ApplicationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_CREATE.name(), appWFDto.getTenantId(), appWFDto.getTenantDomain(), appWFDto.getApplication().getId(), appWFDto.getApplication().getUUID(), appWFDto.getApplication().getName(), appWFDto.getApplication().getTokenType(), appWFDto.getApplication().getTier(), appWFDto.getApplication().getGroupId(), appWFDto.getApplication().getApplicationAttributes(), application.getSubscriber().getName());
APIUtil.sendNotification(applicationEvent, APIConstants.NotifierType.APPLICATION.name());
} else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION.equalsIgnoreCase(wfType)) {
ApplicationWorkflowDTO appWFDto = (ApplicationWorkflowDTO) workflowDTO;
ApplicationEvent applicationEvent = new ApplicationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_DELETE.name(), appWFDto.getTenantId(), appWFDto.getTenantDomain(), appWFDto.getApplication().getId(), appWFDto.getApplication().getUUID(), appWFDto.getApplication().getName(), appWFDto.getApplication().getTokenType(), appWFDto.getApplication().getTier(), appWFDto.getApplication().getGroupId(), appWFDto.getApplication().getApplicationAttributes(), "");
APIUtil.sendNotification(applicationEvent, APIConstants.NotifierType.APPLICATION.name());
} else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION.equalsIgnoreCase(wfType)) {
SubscriptionWorkflowDTO subWFDto = (SubscriptionWorkflowDTO) workflowDTO;
SubscribedAPI sub = ApiMgtDAO.getInstance().getSubscriptionById(Integer.parseInt(subWFDto.getWorkflowReference()));
SubscriptionEvent subscriptionEvent;
if (sub.getApiId() != null) {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_CREATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getIdentifier().getId(), sub.getIdentifier().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
} else {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_CREATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getProductId().getId(), sub.getProductId().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
}
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
} else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE.equalsIgnoreCase(wfType)) {
SubscriptionWorkflowDTO subWFDto = (SubscriptionWorkflowDTO) workflowDTO;
SubscribedAPI sub = ApiMgtDAO.getInstance().getSubscriptionById(Integer.parseInt(subWFDto.getWorkflowReference()));
SubscriptionEvent subscriptionEvent;
if (sub.getApiId() != null) {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_UPDATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getIdentifier().getId(), sub.getIdentifier().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
} else {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_UPDATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getProductId().getId(), sub.getProductId().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
}
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
} else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION.equalsIgnoreCase(wfType)) {
SubscriptionWorkflowDTO subWFDto = (SubscriptionWorkflowDTO) workflowDTO;
SubscribedAPI sub = ApiMgtDAO.getInstance().getSubscriptionById(Integer.parseInt(subWFDto.getWorkflowReference()));
SubscriptionEvent subscriptionEvent;
if (sub.getApiId() != null) {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getIdentifier().getId(), sub.getIdentifier().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
} else {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getProductId().getId(), sub.getProductId().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
}
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
} else if (WorkflowConstants.WF_TYPE_AM_API_STATE.equalsIgnoreCase(wfType) || WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE.equalsIgnoreCase(wfType)) {
APIStateWorkflowDTO apiStateWFDto = (APIStateWorkflowDTO) workflowDTO;
APIEvent apiEvent = new APIEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.API_LIFECYCLE_CHANGE.name(), apiStateWFDto.getTenantId(), apiStateWFDto.getTenantDomain(), apiStateWFDto.getApiName(), Integer.parseInt(apiStateWFDto.getWorkflowReference()), apiStateWFDto.getApiUUID(), apiStateWFDto.getApiVersion(), apiStateWFDto.getApiType(), apiStateWFDto.getApiContext(), apiStateWFDto.getApiProvider(), apiStateWFDto.getApiLCAction());
APIUtil.sendNotification(apiEvent, APIConstants.NotifierType.API.name());
} else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_PRODUCTION.equalsIgnoreCase(wfType)) {
ApplicationRegistrationWorkflowDTO appRegWFDto = (ApplicationRegistrationWorkflowDTO) workflowDTO;
ApplicationRegistrationEvent applicationRegistrationEvent = new ApplicationRegistrationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_REGISTRATION_CREATE.name(), appRegWFDto.getTenantId(), appRegWFDto.getTenantDomain(), appRegWFDto.getApplication().getId(), appRegWFDto.getApplication().getUUID(), appRegWFDto.getApplicationInfo().getClientId(), appRegWFDto.getApplication().getTokenType(), appRegWFDto.getKeyManager());
APIUtil.sendNotification(applicationRegistrationEvent, APIConstants.NotifierType.APPLICATION_REGISTRATION.name());
} else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_SANDBOX.equalsIgnoreCase(wfType)) {
ApplicationRegistrationWorkflowDTO appRegWFDto = (ApplicationRegistrationWorkflowDTO) workflowDTO;
ApplicationRegistrationEvent applicationRegistrationEvent = new ApplicationRegistrationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_REGISTRATION_CREATE.name(), appRegWFDto.getTenantId(), appRegWFDto.getTenantDomain(), appRegWFDto.getApplication().getId(), appRegWFDto.getApplication().getUUID(), appRegWFDto.getApplicationInfo().getClientId(), appRegWFDto.getApplication().getTokenType(), appRegWFDto.getKeyManager());
APIUtil.sendNotification(applicationRegistrationEvent, APIConstants.NotifierType.APPLICATION_REGISTRATION.name());
}
}
use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent in project carbon-apimgt by wso2.
the class ApiMgtDAO method addSubscription.
private int addSubscription(Connection connection, ApiTypeWrapper apiTypeWrapper, Application application, String subscriptionStatus, String subscriber) throws APIManagementException, SQLException {
final boolean isProduct = apiTypeWrapper.isAPIProduct();
int subscriptionId = -1;
int id = -1;
String apiUUID;
Identifier identifier;
String tier;
// Query to check if this subscription already exists
String checkDuplicateQuery = SQLConstants.CHECK_EXISTING_SUBSCRIPTION_API_SQL;
if (!isProduct) {
identifier = apiTypeWrapper.getApi().getId();
apiUUID = apiTypeWrapper.getApi().getUuid();
if (apiUUID != null) {
id = getAPIID(apiUUID);
}
if (id == -1) {
id = identifier.getId();
}
} else {
identifier = apiTypeWrapper.getApiProduct().getId();
id = apiTypeWrapper.getApiProduct().getProductId();
apiUUID = apiTypeWrapper.getApiProduct().getUuid();
}
int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
try (PreparedStatement ps = connection.prepareStatement(checkDuplicateQuery)) {
ps.setInt(1, id);
ps.setInt(2, application.getId());
try (ResultSet resultSet = ps.executeQuery()) {
// If the subscription already exists
if (resultSet.next()) {
String subStatus = resultSet.getString("SUB_STATUS");
String subCreationStatus = resultSet.getString("SUBS_CREATE_STATE");
if ((APIConstants.SubscriptionStatus.UNBLOCKED.equals(subStatus) || APIConstants.SubscriptionStatus.ON_HOLD.equals(subStatus) || APIConstants.SubscriptionStatus.REJECTED.equals(subStatus)) && APIConstants.SubscriptionCreatedStatus.SUBSCRIBE.equals(subCreationStatus)) {
// Throw error saying subscription already exists.
log.error(String.format("Subscription already exists for API/API Prouct %s in Application %s", apiTypeWrapper.getName(), application.getName()));
throw new SubscriptionAlreadyExistingException(String.format("Subscription already exists for" + " API/API Prouct %s in Application %s", apiTypeWrapper.getName(), application.getName()));
} else if (APIConstants.SubscriptionStatus.UNBLOCKED.equals(subStatus) && APIConstants.SubscriptionCreatedStatus.UN_SUBSCRIBE.equals(subCreationStatus)) {
deleteSubscriptionByApiIDAndAppID(id, application.getId(), connection);
} else if (APIConstants.SubscriptionStatus.BLOCKED.equals(subStatus) || APIConstants.SubscriptionStatus.PROD_ONLY_BLOCKED.equals(subStatus)) {
log.error(String.format(String.format("Subscription to API/API Prouct %%s through application" + " %%s was blocked"), apiTypeWrapper.getName(), application.getName()));
throw new SubscriptionBlockedException(String.format("Subscription to API/API Product %s " + "through application %s was blocked", apiTypeWrapper.getName(), application.getName()));
} else if (APIConstants.SubscriptionStatus.REJECTED.equals(subStatus)) {
throw new SubscriptionBlockedException("Subscription to API " + apiTypeWrapper.getName() + " through application " + application.getName() + " was rejected");
}
}
}
}
// This query to update the AM_SUBSCRIPTION table
String sqlQuery = SQLConstants.ADD_SUBSCRIPTION_SQL;
// Adding data to the AM_SUBSCRIPTION table
// ps = conn.prepareStatement(sqlQuery, Statement.RETURN_GENERATED_KEYS);
String subscriptionIDColumn = "SUBSCRIPTION_ID";
String subscriptionUUID = UUID.randomUUID().toString();
if (connection.getMetaData().getDriverName().contains("PostgreSQL")) {
subscriptionIDColumn = "subscription_id";
}
try (PreparedStatement preparedStForInsert = connection.prepareStatement(sqlQuery, new String[] { subscriptionIDColumn })) {
if (!isProduct) {
tier = apiTypeWrapper.getApi().getId().getTier();
preparedStForInsert.setString(1, tier);
preparedStForInsert.setString(10, tier);
} else {
tier = apiTypeWrapper.getApiProduct().getId().getTier();
preparedStForInsert.setString(1, tier);
preparedStForInsert.setString(10, tier);
}
preparedStForInsert.setInt(2, id);
preparedStForInsert.setInt(3, application.getId());
preparedStForInsert.setString(4, subscriptionStatus != null ? subscriptionStatus : APIConstants.SubscriptionStatus.UNBLOCKED);
preparedStForInsert.setString(5, APIConstants.SubscriptionCreatedStatus.SUBSCRIBE);
preparedStForInsert.setString(6, subscriber);
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
preparedStForInsert.setTimestamp(7, timestamp);
preparedStForInsert.setTimestamp(8, timestamp);
preparedStForInsert.setString(9, subscriptionUUID);
preparedStForInsert.executeUpdate();
try (ResultSet rs = preparedStForInsert.getGeneratedKeys()) {
while (rs.next()) {
// subscriptionId = rs.getInt(1);
subscriptionId = Integer.parseInt(rs.getString(1));
}
}
}
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
SubscriptionEvent subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_CREATE.name(), tenantId, tenantDomain, subscriptionId, subscriptionUUID, id, apiUUID, application.getId(), application.getUUID(), tier, (subscriptionStatus != null ? subscriptionStatus : APIConstants.SubscriptionStatus.UNBLOCKED));
return subscriptionId;
}
use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent in project carbon-apimgt by wso2.
the class APIConsumerImpl method removeSubscription.
/**
* Removes a subscription specified by SubscribedAPI object
*
* @param subscription SubscribedAPI object
* @param organization Organization
* @throws APIManagementException
*/
@Override
public void removeSubscription(SubscribedAPI subscription, String organization) throws APIManagementException {
String uuid = subscription.getUUID();
if (subscription != null) {
Application application = subscription.getApplication();
Identifier identifier = subscription.getApiId() != null ? subscription.getApiId() : subscription.getProductId();
String userId = application.getSubscriber().getName();
removeSubscription(identifier, userId, application.getId(), organization);
if (log.isDebugEnabled()) {
String appName = application.getName();
String logMessage = "Identifier: " + identifier.toString() + " subscription (uuid : " + uuid + ") removed from app " + appName;
log.debug(logMessage);
}
// get the workflow state once the executor is executed.
WorkflowDTO wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(application.getId()), WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
// wfDTO is null when simple wf executor is used because wf state is not stored in the db and is always approved.
if (wfDTO != null) {
if (WorkflowStatus.APPROVED.equals(wfDTO.getStatus())) {
SubscriptionEvent subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), tenantId, tenantDomain, subscription.getSubscriptionId(), subscription.getUUID(), identifier.getId(), identifier.getUUID(), application.getId(), application.getUUID(), identifier.getTier(), subscription.getSubStatus());
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
}
} else {
SubscriptionEvent subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), tenantId, tenantDomain, subscription.getSubscriptionId(), subscription.getUUID(), identifier.getId(), identifier.getUUID(), application.getId(), application.getUUID(), identifier.getTier(), subscription.getSubStatus());
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
}
} else {
throw new APIManagementException(String.format("Subscription for UUID:%s does not exist.", subscription.getUUID()));
}
}
use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent in project carbon-apimgt by wso2.
the class KeyManagerDataServiceImpl method addOrUpdateSubscription.
@Override
public void addOrUpdateSubscription(SubscriptionEvent event) {
if (log.isDebugEnabled()) {
log.debug("Add or Update Subscription in datastore in tenant " + event.getTenantDomain());
}
SubscriptionDataStore store = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(event.getTenantDomain());
if (store == null) {
if (log.isDebugEnabled()) {
log.debug("Ignoring the Event due to tenant " + event.getTenantDomain() + " not loaded");
}
return;
}
store.addOrUpdateSubscription(getSubscriptionFromSubscriptionEvent(event));
}
Aggregations