use of org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy in project carbon-apimgt by wso2.
the class ApiMgtDAO method getGlobalPolicies.
/**
* Get all Global level policeis belongs to specific tenant
*
* @param tenantID
* @return
* @throws APIManagementException
*/
public GlobalPolicy[] getGlobalPolicies(int tenantID) throws APIManagementException {
List<GlobalPolicy> policies = new ArrayList<GlobalPolicy>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sqlQuery = SQLConstants.GET_GLOBAL_POLICIES;
if (forceCaseInsensitiveComparisons) {
sqlQuery = SQLConstants.GET_GLOBAL_POLICIES;
}
try {
conn = APIMgtDBUtil.getConnection();
ps = conn.prepareStatement(sqlQuery);
ps.setInt(1, tenantID);
rs = ps.executeQuery();
while (rs.next()) {
String siddhiQuery = null;
GlobalPolicy globalPolicy = new GlobalPolicy(rs.getString(ThrottlePolicyConstants.COLUMN_NAME));
globalPolicy.setDescription(rs.getString(ThrottlePolicyConstants.COLUMN_DESCRIPTION));
globalPolicy.setPolicyId(rs.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
globalPolicy.setUUID(rs.getString(ThrottlePolicyConstants.COLUMN_UUID));
globalPolicy.setTenantId(rs.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
globalPolicy.setKeyTemplate(rs.getString(ThrottlePolicyConstants.COLUMN_KEY_TEMPLATE));
globalPolicy.setDeployed(rs.getBoolean(ThrottlePolicyConstants.COLUMN_DEPLOYED));
InputStream siddhiQueryBlob = rs.getBinaryStream(ThrottlePolicyConstants.COLUMN_SIDDHI_QUERY);
if (siddhiQueryBlob != null) {
siddhiQuery = APIMgtDBUtil.getStringFromInputStream(siddhiQueryBlob);
}
globalPolicy.setSiddhiQuery(siddhiQuery);
policies.add(globalPolicy);
}
} catch (SQLException e) {
handleException("Error while executing SQL", e);
} finally {
APIMgtDBUtil.closeAllConnections(ps, conn, rs);
}
return policies.toArray(new GlobalPolicy[policies.size()]);
}
use of org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesCustomGet.
/**
* Retrieves all Global level policies
*
* @param accept Accept header value
* @return All matched Global Throttle policies to the given request
*/
@Override
public Response throttlingPoliciesCustomGet(String accept, MessageContext messageContext) {
try {
APIAdmin apiAdmin = new APIAdminImpl();
String userName = RestApiCommonUtil.getLoggedInUsername();
int tenantId = APIUtil.getTenantId(userName);
// only super tenant is allowed to access global policies/custom rules
checkTenantDomainForCustomRules();
Policy[] globalPolicies = apiAdmin.getPolicies(tenantId, PolicyConstants.POLICY_LEVEL_GLOBAL);
List<GlobalPolicy> policies = new ArrayList<>();
for (Policy policy : globalPolicies) {
policies.add((GlobalPolicy) policy);
}
CustomRuleListDTO listDTO = GlobalThrottlePolicyMappingUtil.fromGlobalPolicyArrayToListDTO(policies.toArray(new GlobalPolicy[policies.size()]));
return Response.ok().entity(listDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving Global level policies";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesCustomRuleIdDelete.
/**
* Delete a Global level policy/custom rule specified by uuid
*
* @param ruleId uuid of the policy
* @return 200 OK response if successfully deleted the policy
*/
@Override
public Response throttlingPoliciesCustomRuleIdDelete(String ruleId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// only super tenant is allowed to access global policies/custom rules
checkTenantDomainForCustomRules();
String username = RestApiCommonUtil.getLoggedInUsername();
// This will give PolicyNotFoundException if there's no policy exists with UUID
GlobalPolicy existingPolicy = apiProvider.getGlobalPolicyByUUID(ruleId);
if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, log);
}
apiProvider.deletePolicy(username, PolicyConstants.POLICY_LEVEL_GLOBAL, existingPolicy.getPolicyName());
return Response.ok().build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, e, log);
} else {
String errorMessage = "Error while deleting custom rule : " + ruleId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesCustomPost.
/**
* Add an Global Level Throttle Policy
*
* @param body DTO of new policy to be created
* @param contentType Content-Type header
* @return Created policy along with the location of it with Location header
*/
@Override
public Response throttlingPoliciesCustomPost(String contentType, CustomRuleDTO body, MessageContext messageContext) throws APIManagementException {
RestApiAdminUtils.validateCustomRuleRequiredProperties(body, (String) messageContext.get(Message.HTTP_REQUEST_METHOD));
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// only super tenant is allowed to access global policies/custom rules
checkTenantDomainForCustomRules();
GlobalPolicy globalPolicy = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyDTOToModel(body);
// Check if there's a policy exists before adding the new policy
try {
Policy policyIfExists = apiProvider.getGlobalPolicy(globalPolicy.getPolicyName());
if (policyIfExists != null) {
RestApiUtil.handleResourceAlreadyExistsError("Custom rule with name " + globalPolicy.getPolicyName() + " already exists", log);
}
} catch (PolicyNotFoundException ignore) {
}
// Add the policy
apiProvider.addPolicy(globalPolicy);
// retrieve the new policy and send back as the response
GlobalPolicy newGlobalPolicy = apiProvider.getGlobalPolicy(body.getPolicyName());
CustomRuleDTO policyDTO = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyToDTO(newGlobalPolicy);
return Response.created(new URI(RestApiConstants.RESOURCE_PATH_THROTTLING_POLICIES_GLOBAL + "/" + policyDTO.getPolicyId())).entity(policyDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding a custom rule: " + body.getPolicyName();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving Global Throttle policy location : " + body.getPolicyName();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy in project carbon-apimgt by wso2.
the class GlobalThrottlePolicyMappingUtil method fromGlobalThrottlePolicyDTOToModel.
/**
* Converts a single Global policy DTO object into model object
*
* @param dto Global policy DTO object
* @return Model object derived from DTO
* @throws UnsupportedThrottleLimitTypeException
*/
public static GlobalPolicy fromGlobalThrottlePolicyDTOToModel(CustomRuleDTO dto) throws UnsupportedThrottleLimitTypeException {
// update mandatory fields such as tenantDomain etc.
dto = CommonThrottleMappingUtil.updateDefaultMandatoryFieldsOfThrottleDTO(dto);
GlobalPolicy globalPolicy = new GlobalPolicy(dto.getPolicyName());
globalPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, globalPolicy);
globalPolicy.setKeyTemplate(dto.getKeyTemplate());
globalPolicy.setSiddhiQuery(dto.getSiddhiQuery());
return globalPolicy;
}
Aggregations