use of org.wso2.carbon.apimgt.notification.event.WebhooksSubscriptionEvent in project carbon-apimgt by wso2.
the class WebhooksSubscriptionEventHandler method sendSubscriptionNotificationOnRealtime.
/**
* Method to publish the subscription request on to the realtime message broker
*
* @param event realtime notification data read from the event
*/
private void sendSubscriptionNotificationOnRealtime(WebhooksSubscriptionEvent event, boolean isSuccess) {
Object[] objects = new Object[] { event.getApiUUID(), event.getApiName(), event.getApiContext(), event.getApiVersion(), event.getAppID(), event.getTenantDomain(), event.getTenantId(), event.getCallback(), event.getTopic(), event.getMode(), event.getSecret(), event.getExpiryTime(), event.getSubscriberName(), event.getApplicationTier(), event.getTier(), event.getApiTier(), !isSuccess };
EventPublisherEvent asyncWebhooksEvent = new EventPublisherEvent(APIConstants.WEBHOOKS_SUBSCRIPTION_STREAM_ID, System.currentTimeMillis(), objects);
APIUtil.publishEvent(EventPublisherType.ASYNC_WEBHOOKS, asyncWebhooksEvent, asyncWebhooksEvent.toString());
}
use of org.wso2.carbon.apimgt.notification.event.WebhooksSubscriptionEvent in project carbon-apimgt by wso2.
the class WebhooksSubscriptionEventHandler method populateProperties.
/**
* Method to publish the subscription request on to the realtime message broker
*
* @param subscriptionEvent subscription event
* @return Properties subscription properties
*/
private Properties populateProperties(WebhooksSubscriptionEvent subscriptionEvent) throws APIManagementException {
Properties properties = new Properties();
properties.put(APIConstants.Webhooks.API_UUID, subscriptionEvent.getApiUUID());
properties.put(APIConstants.Webhooks.APP_ID, subscriptionEvent.getAppID());
properties.put(APIConstants.Webhooks.TENANT_DOMAIN, subscriptionEvent.getTenantDomain());
properties.put(APIConstants.Webhooks.TENANT_ID, subscriptionEvent.getTenantId());
properties.put(APIConstants.Webhooks.CALLBACK, subscriptionEvent.getCallback());
properties.put(APIConstants.Webhooks.TOPIC, subscriptionEvent.getTopic());
putIfNotNull(properties, APIConstants.Webhooks.SECRET, subscriptionEvent.getSecret());
String leaseSeconds = subscriptionEvent.getLeaseSeconds();
putIfNotNull(properties, APIConstants.Webhooks.LEASE_SECONDS, leaseSeconds);
Date currentTime = new Date();
Timestamp updatedTimestamp = new Timestamp(currentTime.getTime());
subscriptionEvent.setUpdatedTime(updatedTimestamp);
properties.put(APIConstants.Webhooks.UPDATED_AT, updatedTimestamp);
long expiryTime = 0;
if (!StringUtils.isEmpty(leaseSeconds)) {
long leaseSecondsInLong;
try {
leaseSecondsInLong = Long.parseLong(leaseSeconds);
} catch (NumberFormatException e) {
throw new APIManagementException("Error while parsing leaseSeconds param", e);
}
expiryTime = updatedTimestamp.toInstant().plusSeconds(leaseSecondsInLong).toEpochMilli();
}
subscriptionEvent.setExpiryTime(expiryTime);
properties.put(APIConstants.Webhooks.EXPIRY_AT, "" + expiryTime);
properties.put(APIConstants.Webhooks.TIER, "" + subscriptionEvent.getTier());
return properties;
}
use of org.wso2.carbon.apimgt.notification.event.WebhooksSubscriptionEvent in project carbon-apimgt by wso2.
the class WebhooksSubscriptionEventHandler method handleEvent.
@Override
public boolean handleEvent(String event, Map<String, List<String>> headers) throws APIManagementException {
WebhooksSubscriptionEvent subscriptionEvent = new Gson().fromJson(event, WebhooksSubscriptionEvent.class);
Properties properties = populateProperties(subscriptionEvent);
boolean isSuccess = true;
if (APIConstants.Webhooks.SUBSCRIBE_MODE.equalsIgnoreCase(subscriptionEvent.getMode())) {
isSuccess = WebhooksDAO.getInstance().addSubscription(properties);
} else if (APIConstants.Webhooks.UNSUBSCRIBE_MODE.equalsIgnoreCase(subscriptionEvent.getMode())) {
WebhooksDAO.getInstance().updateUnSubscription(properties);
} else {
throw new APIManagementException("Error while processing subscription request: Wrong subscription mode");
}
sendSubscriptionNotificationOnRealtime(subscriptionEvent, isSuccess);
if (!isSuccess) {
throw new APIManagementException("Throttled out");
}
return true;
}
Aggregations