Search in sources :

Example 46 with APIMgtAdminService

use of org.wso2.carbon.apimgt.core.api.APIMgtAdminService in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingCustomRuleIdGet.

/**
 * Get a specific custom rule by its id.
 *
 * @param ruleId          uuid of the policy
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return Matched Global Throttle Policy by the given name
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingCustomRuleIdGet(String ruleId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Custom Policy GET request with rule ID = " + ruleId);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        CustomPolicy customPolicy = apiMgtAdminService.getCustomRuleByUUID(ruleId);
        CustomRuleDTO dto = CustomPolicyMappingUtil.fromCustomPolicyToDTO(customPolicy);
        return Response.status(Response.Status.OK).entity(dto).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while getting custom policy. policy uuid: " + ruleId;
        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) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 47 with APIMgtAdminService

use of org.wso2.carbon.apimgt.core.api.APIMgtAdminService 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 48 with APIMgtAdminService

use of org.wso2.carbon.apimgt.core.api.APIMgtAdminService in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method deletePolicy.

private Response deletePolicy(String policyId, APIMgtAdminService.PolicyLevel tierLevel) {
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        apiMgtAdminService.deletePolicyByUuid(policyId, tierLevel);
        return Response.ok().build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while deleting a Policy uuid : " + policyId;
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.TIER, policyId);
        org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        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) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 49 with APIMgtAdminService

use of org.wso2.carbon.apimgt.core.api.APIMgtAdminService in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingAdvancedIdPut.

/**
 * Update policy by Id
 * @param id          Uuid of the Advanced 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 policiesThrottlingAdvancedIdPut(String id, AdvancedThrottlePolicyDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.api;
    if (log.isDebugEnabled()) {
        log.info("Received Advance Policy PUT request " + body + " with tierLevel = " + tierLevel);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        APIPolicy apiPolicy = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(body);
        apiPolicy.setUuid(id);
        apiMgtAdminService.updateApiPolicy(apiPolicy);
        return Response.status(Response.Status.CREATED).entity(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(apiMgtAdminService.getApiPolicyByUuid(id))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while updating Advanced 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) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 50 with APIMgtAdminService

use of org.wso2.carbon.apimgt.core.api.APIMgtAdminService 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)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)47 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)46 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)36 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)17 Response (javax.ws.rs.core.Response)16 Test (org.junit.Test)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 HashMap (java.util.HashMap)10 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)9 ArrayList (java.util.ArrayList)8 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)6 CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)6 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)6 Map (java.util.Map)5 BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)5 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)5 Label (org.wso2.carbon.apimgt.core.models.Label)3 ThreatProtectionPolicy (org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)3 Workflow (org.wso2.carbon.apimgt.core.workflow.Workflow)3 File (java.io.File)2