Search in sources :

Example 6 with ThrottlePolicyDeployerException

use of org.wso2.carbon.apimgt.throttle.policy.deployer.exception.ThrottlePolicyDeployerException in project carbon-apimgt by wso2.

the class ThrottlePolicyJMSMessageListener method handleNotificationMessage.

private void handleNotificationMessage(String eventType, String encodedEvent) {
    byte[] eventDecoded = Base64.decodeBase64(encodedEvent);
    String eventJson = new String(eventDecoded, StandardCharsets.UTF_8);
    if (APIConstants.EventType.POLICY_CREATE.toString().equals(eventType) || APIConstants.EventType.POLICY_UPDATE.toString().equals(eventType) || APIConstants.EventType.POLICY_DELETE.toString().equals(eventType)) {
        boolean updatePolicy = APIConstants.EventType.POLICY_CREATE.toString().equals(eventType) || APIConstants.EventType.POLICY_UPDATE.toString().equals(eventType);
        boolean deletePolicy = APIConstants.EventType.POLICY_DELETE.toString().equals(eventType);
        Runnable task = null;
        PolicyEvent event = new Gson().fromJson(eventJson, PolicyEvent.class);
        if (event.getPolicyType() == APIConstants.PolicyType.SUBSCRIPTION) {
            // handle subscription policies
            SubscriptionPolicyEvent policyEvent = new Gson().fromJson(eventJson, SubscriptionPolicyEvent.class);
            if (!(APIConstants.UNLIMITED_TIER.equalsIgnoreCase(policyEvent.getPolicyName()) || APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED.equalsIgnoreCase(policyEvent.getPolicyName()) || APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED.equalsIgnoreCase(policyEvent.getPolicyName()))) {
                task = () -> {
                    try {
                        if (updatePolicy) {
                            SubscriptionPolicy subscriptionPolicy = policyRetriever.getSubscriptionPolicy(policyEvent.getPolicyName(), policyEvent.getTenantDomain());
                            PolicyUtil.deployPolicy(subscriptionPolicy, policyEvent);
                        } else if (deletePolicy) {
                            PolicyUtil.undeployPolicy(policyEvent);
                        }
                    } catch (ThrottlePolicyDeployerException e) {
                        log.error("Error in retrieving subscription policy metadata from the database", e);
                    }
                };
            }
        } else if (event.getPolicyType() == APIConstants.PolicyType.APPLICATION) {
            // handle application policies
            ApplicationPolicyEvent policyEvent = new Gson().fromJson(eventJson, ApplicationPolicyEvent.class);
            if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(policyEvent.getPolicyName())) {
                task = () -> {
                    try {
                        if (updatePolicy) {
                            ApplicationPolicy applicationPolicy = policyRetriever.getApplicationPolicy(policyEvent.getPolicyName(), policyEvent.getTenantDomain());
                            PolicyUtil.deployPolicy(applicationPolicy, policyEvent);
                        } else if (deletePolicy) {
                            PolicyUtil.undeployPolicy(policyEvent);
                        }
                    } catch (ThrottlePolicyDeployerException e) {
                        log.error("Error in retrieving application policy metadata from the database", e);
                    }
                };
            }
        } else if (event.getPolicyType() == APIConstants.PolicyType.API) {
            // handle API policies
            APIPolicyEvent policyEvent = new Gson().fromJson(eventJson, APIPolicyEvent.class);
            if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(policyEvent.getPolicyName())) {
                task = () -> {
                    try {
                        if (updatePolicy) {
                            ApiPolicy apiPolicy = policyRetriever.getApiPolicy(policyEvent.getPolicyName(), policyEvent.getTenantDomain());
                            PolicyUtil.deployPolicy(apiPolicy, policyEvent);
                        } else if (deletePolicy) {
                            PolicyUtil.undeployPolicy(policyEvent);
                        }
                    } catch (ThrottlePolicyDeployerException e) {
                        log.error("Error in retrieving API policy metadata from the database", e);
                    }
                };
            }
        } else if (event.getPolicyType() == APIConstants.PolicyType.GLOBAL) {
            // handle global policies
            GlobalPolicyEvent policyEvent = new Gson().fromJson(eventJson, GlobalPolicyEvent.class);
            task = () -> {
                try {
                    if (updatePolicy) {
                        GlobalPolicy globalPolicy = policyRetriever.getGlobalPolicy(policyEvent.getPolicyName(), policyEvent.getTenantDomain());
                        PolicyUtil.deployPolicy(globalPolicy, policyEvent);
                    } else if (deletePolicy) {
                        PolicyUtil.undeployPolicy(policyEvent);
                    }
                } catch (ThrottlePolicyDeployerException e) {
                    log.error("Error in retrieving Global policy metadata from the database", e);
                }
            };
        }
        if (task != null) {
            policyRetrievalScheduler.schedule(task, 1, TimeUnit.MILLISECONDS);
        }
    }
}
Also used : ThrottlePolicyDeployerException(org.wso2.carbon.apimgt.throttle.policy.deployer.exception.ThrottlePolicyDeployerException) GlobalPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy) ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) Gson(com.google.gson.Gson) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent) SubscriptionPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy) APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) ApplicationPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy) GlobalPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent) APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) PolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.PolicyEvent) SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) GlobalPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)

Example 7 with ThrottlePolicyDeployerException

use of org.wso2.carbon.apimgt.throttle.policy.deployer.exception.ThrottlePolicyDeployerException in project carbon-apimgt by wso2.

the class PolicyRetriever method getSubscriptionPolicy.

/**
 * Get a subscription policy given the name.
 *
 * @param policyName   policy name
 * @param tenantDomain tenant domain
 * @return subscription policy
 * @throws ThrottlePolicyDeployerException if failure occurs
 */
public SubscriptionPolicy getSubscriptionPolicy(String policyName, String tenantDomain) throws ThrottlePolicyDeployerException {
    String path = APIConstants.SubscriptionValidationResources.SUBSCRIPTION_POLICIES + "?policyName=" + policyName;
    SubscriptionPolicyList subscriptionPolicyList = getPolicies(path, tenantDomain, SubscriptionPolicyList.class);
    if (subscriptionPolicyList.getList() != null && !subscriptionPolicyList.getList().isEmpty()) {
        return subscriptionPolicyList.getList().get(0);
    }
    return null;
}
Also used : SubscriptionPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicyList)

Example 8 with ThrottlePolicyDeployerException

use of org.wso2.carbon.apimgt.throttle.policy.deployer.exception.ThrottlePolicyDeployerException in project carbon-apimgt by wso2.

the class PolicyRetriever method invokeService.

private CloseableHttpResponse invokeService(String endpoint, String tenantDomain) throws IOException, ThrottlePolicyDeployerException {
    HttpGet method = new HttpGet(endpoint);
    URL url = new URL(endpoint);
    String username = eventHubConfigurationDto.getUsername();
    String password = eventHubConfigurationDto.getPassword();
    byte[] credentials = Base64.encodeBase64((username + APIConstants.DELEM_COLON + password).getBytes(APIConstants.DigestAuthConstants.CHARSET));
    int port = url.getPort();
    String protocol = url.getProtocol();
    method.setHeader(APIConstants.HEADER_TENANT, tenantDomain);
    method.setHeader(APIConstants.AUTHORIZATION_HEADER_DEFAULT, APIConstants.AUTHORIZATION_BASIC + new String(credentials, APIConstants.DigestAuthConstants.CHARSET));
    HttpClient httpClient = APIUtil.getHttpClient(port, protocol);
    try {
        return APIUtil.executeHTTPRequest(method, httpClient);
    } catch (APIManagementException e) {
        throw new ThrottlePolicyDeployerException(e);
    }
}
Also used : ThrottlePolicyDeployerException(org.wso2.carbon.apimgt.throttle.policy.deployer.exception.ThrottlePolicyDeployerException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) HttpGet(org.apache.http.client.methods.HttpGet) HttpClient(org.apache.http.client.HttpClient) URL(java.net.URL)

Aggregations

ApiPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy)3 ApiPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList)3 ApplicationPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy)3 ApplicationPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicyList)3 GlobalPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy)3 GlobalPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicyList)3 SubscriptionPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy)3 SubscriptionPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicyList)3 ThrottlePolicyDeployerException (org.wso2.carbon.apimgt.throttle.policy.deployer.exception.ThrottlePolicyDeployerException)3 Gson (com.google.gson.Gson)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HttpClient (org.apache.http.client.HttpClient)1 HttpGet (org.apache.http.client.methods.HttpGet)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent)1 ApplicationPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent)1