use of org.wso2.carbon.apimgt.api.APIManagementException in project carbon-apimgt by wso2.
the class RestCallUtilImpl method rsaSignedFetchUserRequest.
/**
* {@inheritDoc}
*/
@Override
public HttpResponse rsaSignedFetchUserRequest(URI uri, String username, String userTenantDomain, String rsaSignedToken, MediaType acceptContentType) throws APIManagementException {
if (uri == null) {
throw new IllegalArgumentException("The URI must not be null");
}
if (username == null) {
throw new IllegalArgumentException("UserName must not be null");
}
if (userTenantDomain == null) {
throw new IllegalArgumentException("User tenant domain must not be null");
}
if (rsaSignedToken == null) {
throw new IllegalArgumentException("RSA signed token must not be null");
}
HttpURLConnection httpConnection = null;
try {
JSONObject loginInfoJsonObj = new JSONObject();
loginInfoJsonObj.put(APIMgtConstants.FunctionsConstants.USERNAME, username);
loginInfoJsonObj.put(APIMgtConstants.FunctionsConstants.USER_TENANT_DOMAIN, userTenantDomain);
httpConnection = (HttpURLConnection) uri.toURL().openConnection();
httpConnection.setRequestMethod(APIMgtConstants.FunctionsConstants.POST);
httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON);
httpConnection.setDoOutput(true);
httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.RSA_SIGNED_TOKEN, rsaSignedToken);
if (acceptContentType != null) {
httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.ACCEPT, acceptContentType.toString());
}
OutputStream outputStream = httpConnection.getOutputStream();
outputStream.write(loginInfoJsonObj.toString().getBytes(StandardCharsets.UTF_8));
outputStream.flush();
outputStream.close();
return getResponse(httpConnection);
} catch (IOException e) {
throw new APIManagementException("Connection not established properly ", e);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
}
use of org.wso2.carbon.apimgt.api.APIManagementException in project carbon-apimgt by wso2.
the class WSO2ISKeyManagerImpl method revokeAccessToken.
// TODO: Remove after revoke endpoint implementation done in key manager.
@Override
public void revokeAccessToken(String accessToken, String clientId, String clientSecret) throws KeyManagementException {
log.debug("Revoking access token");
Response response;
try {
response = oAuth2ServiceStubs.getRevokeServiceStub().revokeAccessToken(accessToken, clientId, clientSecret);
} catch (APIManagementException e) {
throw new KeyManagementException("Error occurred while revoking current access token", e, ExceptionCodes.ACCESS_TOKEN_REVOKE_FAILED);
}
if (response == null) {
throw new KeyManagementException("Error occurred while revoking current access token. " + "Response is null", ExceptionCodes.ACCESS_TOKEN_REVOKE_FAILED);
}
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
if (log.isDebugEnabled()) {
log.debug("Successfully revoked access token: " + accessToken);
}
} else {
throw new KeyManagementException("Token revocation failed. HTTP error code: " + response.status() + " Error Response Body: " + response.body().toString(), ExceptionCodes.ACCESS_TOKEN_REVOKE_FAILED);
}
}
use of org.wso2.carbon.apimgt.api.APIManagementException in project carbon-apimgt by wso2.
the class ThrottlerUtil method addDefaultAdvancedThrottlePolicies.
/**
* Deploy default throttle polices at startup
*
* @throws APIManagementException throws if any exception occured
*/
public static void addDefaultAdvancedThrottlePolicies() throws APIManagementException {
int[] requestCount = new int[] { 50, 20, 10, Integer.MAX_VALUE };
// Adding application level throttle policies
String[] appPolicies = new String[] { ThrottleConstants.DEFAULT_APP_POLICY_FIFTY_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_TWENTY_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_TEN_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_UNLIMITED };
String[] appPolicyDecs = new String[] { ThrottleConstants.DEFAULT_APP_POLICY_LARGE_DESC, ThrottleConstants.DEFAULT_APP_POLICY_MEDIUM_DESC, ThrottleConstants.DEFAULT_APP_POLICY_SMALL_DESC, ThrottleConstants.DEFAULT_APP_POLICY_UNLIMITED_DESC };
PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
String policyName;
// Add application level throttle policies
for (int i = 0; i < appPolicies.length; i++) {
policyName = appPolicies[i];
if (!isPolicyExist(APIMgtAdminService.PolicyLevel.application, policyName)) {
ApplicationPolicy applicationPolicy = new ApplicationPolicy(policyName);
applicationPolicy.setUuid(UUID.randomUUID().toString());
applicationPolicy.setDisplayName(policyName);
applicationPolicy.setDescription(appPolicyDecs[i]);
applicationPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCount[i]);
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
defaultQuotaPolicy.setLimit(requestCountLimit);
applicationPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
policyDAO.addApplicationPolicy(applicationPolicy);
}
}
// Adding Subscription level policies
int[] requestCountSubPolicies = new int[] { 5000, 2000, 1000, 500, Integer.MAX_VALUE };
String[] subPolicies = new String[] { ThrottleConstants.DEFAULT_SUB_POLICY_GOLD, ThrottleConstants.DEFAULT_SUB_POLICY_SILVER, ThrottleConstants.DEFAULT_SUB_POLICY_BRONZE, ThrottleConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED, ThrottleConstants.DEFAULT_SUB_POLICY_UNLIMITED };
String[] subPolicyDecs = new String[] { ThrottleConstants.DEFAULT_SUB_POLICY_GOLD_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_SILVER_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_BRONZE_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_UNLIMITED_DESC };
for (int i = 0; i < subPolicies.length; i++) {
policyName = subPolicies[i];
if (!isPolicyExist(APIMgtAdminService.PolicyLevel.subscription, policyName)) {
SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(policyName);
subscriptionPolicy.setUuid(UUID.randomUUID().toString());
subscriptionPolicy.setDisplayName(policyName);
subscriptionPolicy.setDescription(subPolicyDecs[i]);
subscriptionPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCountSubPolicies[i]);
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
defaultQuotaPolicy.setLimit(requestCountLimit);
subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
subscriptionPolicy.setStopOnQuotaReach(true);
subscriptionPolicy.setBillingPlan(ThrottleConstants.BILLING_PLAN_FREE);
policyDAO.addSubscriptionPolicy(subscriptionPolicy);
}
}
// Adding Resource level policies
String[] apiPolicies = new String[] { ThrottleConstants.DEFAULT_API_POLICY_FIFTY_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_TWENTY_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_TEN_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_UNLIMITED };
String[] apiPolicyDecs = new String[] { ThrottleConstants.DEFAULT_API_POLICY_ULTIMATE_DESC, ThrottleConstants.DEFAULT_API_POLICY_PLUS_DESC, ThrottleConstants.DEFAULT_API_POLICY_BASIC_DESC, ThrottleConstants.DEFAULT_API_POLICY_UNLIMITED_DESC };
int[] requestCountApiPolicies = new int[] { 50000, 20000, 10000, Integer.MAX_VALUE };
for (int i = 0; i < apiPolicies.length; i++) {
policyName = apiPolicies[i];
if (!isPolicyExist(APIMgtAdminService.PolicyLevel.api, policyName)) {
APIPolicy apiPolicy = new APIPolicy(policyName);
apiPolicy.setUuid(UUID.randomUUID().toString());
apiPolicy.setDisplayName(policyName);
apiPolicy.setDescription(apiPolicyDecs[i]);
apiPolicy.setUserLevel(ThrottleConstants.API_POLICY_API_LEVEL);
apiPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCountApiPolicies[i]);
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
defaultQuotaPolicy.setLimit(requestCountLimit);
apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
policyDAO.addApiPolicy(apiPolicy);
}
}
}
use of org.wso2.carbon.apimgt.api.APIManagementException in project carbon-apimgt by wso2.
the class ApplicationDeletionWorkflow method completeWorkflow.
public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
if (application == null) {
// this is when complete method is executed through workflow rest api
this.application = applicationDAO.getApplication(getWorkflowReference());
}
WorkflowResponse response = workflowExecutor.complete(this);
setStatus(response.getWorkflowStatus());
if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
if (log.isDebugEnabled()) {
log.debug("Application Deletion workflow complete: Approved");
}
applicationDAO.deleteApplication(getWorkflowReference());
try {
getApiGateway().deleteApplication(application.getId());
} catch (GatewayException ex) {
// This log is not harm to therefore not rethrow
log.warn("Failed to send the Application Deletion Event ", ex);
}
} else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
if (log.isDebugEnabled()) {
log.debug("Application Deletion workflow complete: Rejected");
}
}
updateWorkflowEntries(this);
return response;
}
use of org.wso2.carbon.apimgt.api.APIManagementException in project carbon-apimgt by wso2.
the class ApplicationUpdateWorkflow method completeWorkflow.
public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
String appId = getWorkflowReference();
String name = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_NAME);
String updatedUser = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_UPDATEDBY);
String applicationId = getWorkflowReference();
String tier = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_TIER);
String policyId = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_POLICY_ID);
String description = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_DESCRIPTION);
String permission = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_PERMISSION);
Application application = new Application(name, updatedUser);
application.setPolicy(new ApplicationPolicy(policyId, tier));
application.setDescription(description);
application.setId(applicationId);
application.setUpdatedUser(updatedUser);
application.setPermissionString(permission);
application.setUpdatedTime(LocalDateTime.now());
if (existingApplication == null && updatedApplication == null) {
// this is when complete method is executed through workflow rest api
existingApplication = applicationDAO.getApplication(appId);
updatedApplication = application;
}
WorkflowResponse response = workflowExecutor.complete(this);
setStatus(response.getWorkflowStatus());
if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
if (log.isDebugEnabled()) {
log.debug("Application update workflow complete: Approved");
}
application.setStatus(APIMgtConstants.ApplicationStatus.APPLICATION_APPROVED);
applicationDAO.updateApplication(appId, application);
try {
getApiGateway().updateApplication(application);
} catch (GatewayException ex) {
// This log is not harm to therefore not rethrow
log.warn("Failed to send the Application Update Event ", ex);
}
} else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
if (log.isDebugEnabled()) {
log.debug("Application update workflow complete: Rejected");
}
String existingAppStatus = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_EXISTIN_APP_STATUS);
applicationDAO.updateApplicationState(appId, existingAppStatus);
}
updateWorkflowEntries(this);
return response;
}
Aggregations