use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.
the class APIMappingUtil method setPaginationParams.
/**
* Sets pagination urls for a APIProductListDTO object given pagination parameters and url parameters.
*
* @param apiProductListDTO a APIProductListDTO object
* @param query search condition
* @param limit max number of objects returned
* @param offset starting index
* @param size max offset
*/
public static void setPaginationParams(APIProductListDTO apiProductListDTO, String query, int offset, int limit, int size) {
// acquiring pagination parameters and setting pagination urls
Map<String, Integer> paginatedParams = RestApiCommonUtil.getPaginationParams(offset, limit, size);
String paginatedPrevious = "";
String paginatedNext = "";
if (paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET) != null) {
paginatedPrevious = RestApiCommonUtil.getAPIProductPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT), query);
}
if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
paginatedNext = RestApiCommonUtil.getAPIProductPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT), query);
}
PaginationDTO paginationDTO = CommonMappingUtil.getPaginationDTO(limit, offset, size, paginatedNext, paginatedPrevious);
apiProductListDTO.setPagination(paginationDTO);
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.
the class APIMappingUtil method setPaginationParams.
/**
* Sets pagination urls for a APIListDTO object given pagination parameters and url parameters.
*
* @param apiListDTO a APIListDTO object
* @param query search condition
* @param limit max number of objects returned
* @param offset starting index
* @param size max offset
*/
public static void setPaginationParams(Object apiListDTO, String query, int offset, int limit, int size) {
// acquiring pagination parameters and setting pagination urls
Map<String, Integer> paginatedParams = RestApiCommonUtil.getPaginationParams(offset, limit, size);
String paginatedPrevious = "";
String paginatedNext = "";
if (paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET) != null) {
paginatedPrevious = RestApiCommonUtil.getAPIPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT), query);
}
if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
paginatedNext = RestApiCommonUtil.getAPIPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT), query);
}
PaginationDTO paginationDTO = CommonMappingUtil.getPaginationDTO(limit, offset, size, paginatedNext, paginatedPrevious);
((APIListDTO) apiListDTO).setPagination(paginationDTO);
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.
the class MediationPoliciesApiServiceImpl method getGlobalMediationPolicyContent.
/**
* Returns content of a global Mediation policy
*
* @param mediationPolicyId search condition
* @param ifNoneMatch If-None-Match header value
* @return Matched global mediation policies for given search condition
*/
@Override
public Response getGlobalMediationPolicyContent(String mediationPolicyId, String ifNoneMatch, MessageContext messageContext) throws APIManagementException {
String username = RestApiCommonUtil.getLoggedInUsername();
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
try {
// Get registry resource correspond to identifier
Resource mediationResource = apiProvider.getCustomMediationResourceFromUuid(mediationPolicyId);
if (mediationResource != null) {
// get the registry resource path
String resource = mediationResource.getPath();
resource = RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH + resource;
Map<String, Object> mediationPolicyResourceMap = APIUtil.getDocument(username, resource, tenantDomain);
Object fileDataStream = mediationPolicyResourceMap.get(APIConstants.DOCUMENTATION_RESOURCE_MAP_DATA);
Object contentType = mediationPolicyResourceMap.get(APIConstants.DOCUMENTATION_RESOURCE_MAP_CONTENT_TYPE);
contentType = contentType == null ? RestApiConstants.APPLICATION_OCTET_STREAM : contentType;
String name = mediationPolicyResourceMap.get(APIConstants.DOCUMENTATION_RESOURCE_MAP_NAME).toString();
return Response.ok(fileDataStream).header(RestApiConstants.HEADER_CONTENT_TYPE, contentType).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + name + "\"").build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving global mediation policies";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
return null;
}
return null;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.
the class ThrottlePolicyTemplateBuilder method getPolicyConditionJson.
/**
* Produces final condition inside a pipeline
*
* @param conditions set of conditions
* @return conditions as a JSON
*/
private static JSONObject getPolicyConditionJson(Set<Condition> conditions) {
JSONObject tempCondition = new JSONObject();
for (Condition condition : conditions) {
org.wso2.carbon.apimgt.api.model.policy.Condition mappedCondition = PolicyMappingUtil.mapCondition(condition);
JSONObject conditionJson;
if (tempCondition.containsKey(mappedCondition.getType().toLowerCase(Locale.ENGLISH))) {
conditionJson = (JSONObject) tempCondition.get(mappedCondition.getType().toLowerCase(Locale.ENGLISH));
} else {
conditionJson = new JSONObject();
}
tempCondition.put(mappedCondition.getType().toLowerCase(Locale.ENGLISH), conditionJson);
if (PolicyConstants.IP_SPECIFIC_TYPE.equals(mappedCondition.getType())) {
IPCondition ipCondition = (IPCondition) mappedCondition;
if (IPCondition.isIPv6Address(ipCondition.getSpecificIP())) {
conditionJson.put("specificIp", String.valueOf(APIUtil.ipToBigInteger(ipCondition.getSpecificIP())));
} else {
conditionJson.put("specificIp", ipCondition.ipToLong(ipCondition.getSpecificIP()));
}
} else if (PolicyConstants.IP_RANGE_TYPE.equals(mappedCondition.getType())) {
IPCondition ipRangeCondition = (IPCondition) mappedCondition;
if (IPCondition.isIPv6Address(ipRangeCondition.getStartingIP()) && IPCondition.isIPv6Address(ipRangeCondition.getEndingIP())) {
conditionJson.put("startingIp", String.valueOf(APIUtil.ipToBigInteger(ipRangeCondition.getStartingIP())));
conditionJson.put("endingIp", String.valueOf(APIUtil.ipToBigInteger(ipRangeCondition.getEndingIP())));
} else {
conditionJson.put("startingIp", ipRangeCondition.ipToLong(ipRangeCondition.getStartingIP()));
conditionJson.put("endingIp", ipRangeCondition.ipToLong(ipRangeCondition.getEndingIP()));
}
} else if (mappedCondition instanceof QueryParameterCondition) {
QueryParameterCondition queryParameterCondition = (QueryParameterCondition) mappedCondition;
JSONObject values;
if (conditionJson.containsKey("values")) {
values = (JSONObject) conditionJson.get("values");
} else {
values = new JSONObject();
conditionJson.put("values", values);
}
values.put(queryParameterCondition.getParameter(), queryParameterCondition.getValue());
} else if (mappedCondition instanceof HeaderCondition) {
HeaderCondition headerCondition = (HeaderCondition) mappedCondition;
JSONObject values;
if (conditionJson.containsKey("values")) {
values = (JSONObject) conditionJson.get("values");
} else {
values = new JSONObject();
conditionJson.put("values", values);
}
values.put(headerCondition.getHeaderName(), headerCondition.getValue());
} else if (mappedCondition instanceof JWTClaimsCondition) {
JWTClaimsCondition jwtClaimsCondition = (JWTClaimsCondition) mappedCondition;
JSONObject values;
if (conditionJson.containsKey("values")) {
values = (JSONObject) conditionJson.get("values");
} else {
values = new JSONObject();
conditionJson.put("values", values);
}
values.put(jwtClaimsCondition.getClaimUrl(), jwtClaimsCondition.getAttribute());
}
conditionJson.put("invert", mappedCondition.isInvertCondition());
}
return tempCondition;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.
the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForGlobalLevel.
/**
* Generate policy for global level
*
* @param policy policy with level 'global'. Multiple pipelines are not allowed. Can define more than one condition
* as set of conditions. all these conditions should be passed as a single pipeline
* @return the generated execution plan for the policy
* @throws APITemplateException if failed to generate policy
*/
public String getThrottlePolicyForGlobalLevel(GlobalPolicy policy) throws APITemplateException {
StringWriter writer = new StringWriter();
if (log.isDebugEnabled()) {
log.debug("Generating policy for global level :" + policy.toString());
}
try {
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
velocityengine.init();
Template template = velocityengine.getTemplate(getTemplatePathForGlobal());
VelocityContext context = new VelocityContext();
setConstantContext(context);
context.put("policy", policy);
if (log.isDebugEnabled()) {
log.debug("Policy : " + writer.toString());
}
template.merge(context, writer);
} catch (VelocityException e) {
log.error("Velocity Error", e);
throw new APITemplateException("Velocity Error", e);
}
return writer.toString();
}
Aggregations