Search in sources :

Example 96 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingSubscriptionGet.

/**
 * Get subscription level policies
 *
 * @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 policiesThrottlingSubscriptionGet(String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Application Throttle Policy GET request");
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        List<SubscriptionPolicy> policies = apiMgtAdminService.getSubscriptionPolicies();
        SubscriptionThrottlePolicyListDTO subscriptionThrottlePolicyListDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionPolicyArrayToListDTO(policies);
        return Response.ok().entity(subscriptionThrottlePolicyListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while retrieving Application Policies";
        org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 97 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy 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();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 98 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy 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();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 99 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.

the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionThrottlePolicyDTOToModel.

/**
 * Converts a single Subscription Policy DTO into a model object
 *
 * @param dto Subscription policy DTO object
 * @return Converted Subscription policy model object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
@SuppressWarnings("unchecked")
public static SubscriptionPolicy fromSubscriptionThrottlePolicyDTOToModel(SubscriptionThrottlePolicyDTO dto) throws APIManagementException {
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(dto.getPolicyName());
    subscriptionPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, subscriptionPolicy);
    subscriptionPolicy.setBillingPlan(dto.getBillingPlan());
    subscriptionPolicy.setRateLimitTimeUnit(dto.getRateLimitTimeUnit());
    subscriptionPolicy.setRateLimitCount(dto.getRateLimitCount());
    subscriptionPolicy.setStopOnQuotaReach(dto.getStopOnQuotaReach());
    List<CustomAttributeDTO> customAttributes = dto.getCustomAttributes();
    if (customAttributes != null && customAttributes.size() > 0) {
        JSONArray customAttrJsonArray = new JSONArray();
        for (CustomAttributeDTO customAttributeDTO : customAttributes) {
            JSONObject attrJsonObj = new JSONObject();
            attrJsonObj.put(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_NAME, customAttributeDTO.getName());
            attrJsonObj.put(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_VALUE, customAttributeDTO.getValue());
            customAttrJsonArray.add(attrJsonObj);
        }
        try {
            subscriptionPolicy.setCustomAttributes(customAttrJsonArray.toJSONString().getBytes(System.getProperty(ENCODING_SYSTEM_PROPERTY, ENCODING_UTF_8)));
        } catch (UnsupportedEncodingException e) {
            String errorMsg = "Error while setting custom attributes for Subscription Policy: " + dto.getPolicyName();
            log.error(errorMsg, e);
            throw new APIManagementException(errorMsg, e, ExceptionCodes.INTERNAL_ERROR);
        }
    }
    if (dto.getDefaultLimit() != null) {
        subscriptionPolicy.setDefaultQuotaPolicy(CommonThrottleMappingUtil.fromDTOToQuotaPolicy(dto.getDefaultLimit()));
    }
    return subscriptionPolicy;
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) JSONArray(org.json.simple.JSONArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CustomAttributeDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomAttributeDTO)

Example 100 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.

the class PoliciesApiServiceImplTest method policiesThrottlingSubscriptionGetTest.

@Test
public void policiesThrottlingSubscriptionGetTest() throws APIManagementException, NotFoundException {
    printTestMethodName();
    PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
    List<SubscriptionPolicy> policies = new ArrayList<>();
    policies.add(new SubscriptionPolicy("Policy1"));
    policies.add(new SubscriptionPolicy("Policy2"));
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
    Mockito.doReturn(policies).doThrow(new IllegalArgumentException()).when(adminService).getSubscriptionPolicies();
    Response response = policiesApiService.policiesThrottlingSubscriptionGet(null, null, getRequest());
    Assert.assertEquals(response.getStatus(), 200);
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) PoliciesApiServiceImpl(org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)74 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)42 Test (org.testng.annotations.Test)41 ArrayList (java.util.ArrayList)36 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)33 API (org.wso2.carbon.apimgt.core.models.API)30 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)30 Test (org.junit.Test)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)25 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)24 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)19 HashMap (java.util.HashMap)18 HashSet (java.util.HashSet)18 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)16 SQLException (java.sql.SQLException)15 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)15 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)15 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)15 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)15