Search in sources :

Example 16 with SubscriptionEvent

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;
}
Also used : Subscription(org.wso2.carbon.apimgt.keymgt.model.entity.Subscription)

Example 17 with SubscriptionEvent

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

Example 18 with SubscriptionEvent

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

Example 19 with SubscriptionEvent

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;
}
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

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 SubscriptionEvent (org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent)8 API (org.wso2.carbon.apimgt.api.model.API)5 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)5 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)5 Application (org.wso2.carbon.apimgt.api.model.Application)5 Identifier (org.wso2.carbon.apimgt.api.model.Identifier)5 ApplicationRegistrationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO)4 ApplicationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO)4 SubscriptionWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO)4 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)3 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)3 WorkflowDTO (org.wso2.carbon.apimgt.impl.dto.WorkflowDTO)3 Gson (com.google.gson.Gson)2 IOException (java.io.IOException)2 Timestamp (java.sql.Timestamp)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 JSONObject (org.json.simple.JSONObject)2 JSONParser (org.json.simple.parser.JSONParser)2