Search in sources :

Example 1 with WebhooksSubscriptionEvent

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());
}
Also used : EventPublisherEvent(org.wso2.carbon.apimgt.eventing.EventPublisherEvent)

Example 2 with WebhooksSubscriptionEvent

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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Properties(java.util.Properties) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 3 with WebhooksSubscriptionEvent

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;
}
Also used : WebhooksSubscriptionEvent(org.wso2.carbon.apimgt.notification.event.WebhooksSubscriptionEvent) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Gson(com.google.gson.Gson) Properties(java.util.Properties)

Aggregations

Properties (java.util.Properties)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 Gson (com.google.gson.Gson)1 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 EventPublisherEvent (org.wso2.carbon.apimgt.eventing.EventPublisherEvent)1 WebhooksSubscriptionEvent (org.wso2.carbon.apimgt.notification.event.WebhooksSubscriptionEvent)1