use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent in project carbon-apimgt by wso2.
the class KeyManagerDataServiceImpl method getSubscriptionFromSubscriptionEvent.
private Subscription getSubscriptionFromSubscriptionEvent(SubscriptionEvent event) {
Subscription sub = new Subscription();
sub.setApiId(event.getApiId());
sub.setAppId(event.getApplicationId());
sub.setPolicyId(event.getPolicyId());
sub.setSubscriptionId(String.valueOf(event.getSubscriptionId()));
sub.setSubscriptionState(event.getSubscriptionState());
sub.setApiUUID(event.getApiUUID());
sub.setApplicationUUID(event.getApplicationUUID());
sub.setSubscriptionUUId(event.getSubscriptionUUID());
sub.setTimeStamp(event.getTimeStamp());
if (log.isDebugEnabled()) {
log.debug("Event: " + event.toString());
log.debug("Converted : " + sub.toString());
}
return sub;
}
use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent in project carbon-apimgt by wso2.
the class KeyManagerDataServiceImpl method removeSubscription.
@Override
public void removeSubscription(SubscriptionEvent event) {
if (log.isDebugEnabled()) {
log.debug("Remove 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.removeSubscription(getSubscriptionFromSubscriptionEvent(event));
}
use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent 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.impl.notifier.events.SubscriptionEvent 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