use of org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO in project carbon-apimgt by wso2.
the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionPolicyArrayToListDTO.
/**
* Converts an array of Subscription Policy objects into a List DTO
*
* @param subscriptionPolicies Array of Subscription Policies
* @return A List DTO of converted Subscription Policies
* @throws SubscriptionThrottlePolicyException - If error occurs
*/
public static SubscriptionThrottlePolicyListDTO fromSubscriptionPolicyArrayToListDTO(List<SubscriptionPolicy> subscriptionPolicies) throws SubscriptionThrottlePolicyException {
SubscriptionThrottlePolicyListDTO listDTO = new SubscriptionThrottlePolicyListDTO();
List<SubscriptionThrottlePolicyDTO> subscriptionPolicyDTOList = new ArrayList<>();
if (subscriptionPolicies != null) {
for (SubscriptionPolicy policy : subscriptionPolicies) {
SubscriptionThrottlePolicyDTO dto = fromSubscriptionThrottlePolicyToDTO(policy);
subscriptionPolicyDTOList.add(dto);
}
}
listDTO.setCount(subscriptionPolicyDTOList.size());
listDTO.setList(subscriptionPolicyDTOList);
return listDTO;
}
use of org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO in project carbon-apimgt by wso2.
the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionThrottlePolicyToDTO.
/**
* Converts a single Subscription Policy model into REST API DTO
*
* @param policy Subscription Policy model object
* @return Converted Subscription policy REST API DTO object
* @throws SubscriptionThrottlePolicyException - If error occurs
*/
public static SubscriptionThrottlePolicyDTO fromSubscriptionThrottlePolicyToDTO(SubscriptionPolicy policy) throws SubscriptionThrottlePolicyException {
try {
SubscriptionThrottlePolicyDTO policyDTO = new SubscriptionThrottlePolicyDTO();
policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(policy, policyDTO);
SubscriptionPolicy subscriptionPolicy = policy;
policyDTO.setBillingPlan(subscriptionPolicy.getBillingPlan());
policyDTO.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
policyDTO.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
policyDTO.setStopOnQuotaReach(subscriptionPolicy.isStopOnQuotaReach());
byte[] customAttributes = subscriptionPolicy.getCustomAttributes();
if (customAttributes != null && customAttributes.length > 0) {
List<CustomAttributeDTO> customAttributeDTOs = new ArrayList<>();
JSONParser parser = new JSONParser();
JSONArray attributeArray = (JSONArray) parser.parse(new String(customAttributes, StandardCharsets.UTF_8));
for (Object attributeObj : attributeArray) {
JSONObject attribute = (JSONObject) attributeObj;
CustomAttributeDTO customAttributeDTO = CommonThrottleMappingUtil.getCustomAttribute(attribute.get(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_NAME).toString(), attribute.get(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_VALUE).toString());
customAttributeDTOs.add(customAttributeDTO);
}
policyDTO.setCustomAttributes(customAttributeDTOs);
}
if (policy.getDefaultQuotaPolicy() != null) {
policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(policy.getDefaultQuotaPolicy()));
}
return policyDTO;
} catch (ParseException | UnsupportedThrottleLimitTypeException e) {
throw new SubscriptionThrottlePolicyException(e.getMessage(), e);
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingSubscriptionPost.
/**
* Add subscription level policy
*
* @param body DTO object including the Policy meta information
* @param request msf4j request object
* @return Response object
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingSubscriptionPost(SubscriptionThrottlePolicyDTO body, Request request) throws NotFoundException {
APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.subscription;
if (log.isDebugEnabled()) {
log.info("Received Subscription Policy POST request " + body + " with tierLevel = " + tierLevel);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
SubscriptionPolicy subscriptionPolicy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(body);
String policyId = apiMgtAdminService.addSubscriptionPolicy(subscriptionPolicy);
return Response.status(Response.Status.CREATED).entity(SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(apiMgtAdminService.getSubscriptionPolicyByUuid(policyId))).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while adding Subscription Policy. policy name: " + body.getPolicyName();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingSubscriptionIdPut.
/**
* Update subscription level policy
*
* @param id Uuid of the Subscription policy.
* @param body DTO object including the Policy meta information
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return Response object
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingSubscriptionIdPut(String id, SubscriptionThrottlePolicyDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.subscription;
if (log.isDebugEnabled()) {
log.info("Received Subscription Policy PUT request " + body + " with tierLevel = " + tierLevel);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
SubscriptionPolicy subscriptionPolicy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(body);
subscriptionPolicy.setUuid(id);
apiMgtAdminService.updateSubscriptionPolicy(subscriptionPolicy);
return Response.status(Response.Status.CREATED).entity(SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(apiMgtAdminService.getSubscriptionPolicyByUuid(subscriptionPolicy.getUuid()))).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while updating Application Policy. policy uuid: " + id;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingSubscriptionIdGet.
/**
* Get subscription level policy by ID
*
* @param id Uuid of the Subscription policy
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return Response object
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingSubscriptionIdGet(String id, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
if (log.isDebugEnabled()) {
log.info("Received Subscription Policy Get request. Policy uuid: " + id);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
SubscriptionPolicy subscriptionPolicy = apiMgtAdminService.getSubscriptionPolicyByUuid(id);
SubscriptionThrottlePolicyDTO subscriptionThrottlePolicyDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(subscriptionPolicy);
return Response.status(Response.Status.OK).entity(subscriptionThrottlePolicyDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while getting Subscription Policy. policy uuid: " + id;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
Aggregations