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