Search in sources :

Example 41 with APIMgtAdminService

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

the class LabelsApiServiceImpl method labelsLabelIdPut.

/**
 *  Update the label
 * @param body     The body of the label with fields to be modified
 * @param contentType The content type of the body
 * @param request     The ms4j request object
 * @return  200 OK response.
 * @throws NotFoundException
 */
@Override
public Response labelsLabelIdPut(String labelId, LabelDTO body, String contentType, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        body.labelUUID(labelId);
        Label updatedLabel = apiMgtAdminService.updateLabel(LabelMappingUtil.fromDTOTLabel(body));
        return Response.status(Response.Status.OK).entity(LabelMappingUtil.fromLabelToDTO(updatedLabel)).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while adding label, label name: " + body.getName();
        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) Label(org.wso2.carbon.apimgt.core.models.Label)

Example 42 with APIMgtAdminService

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

the class LabelsApiServiceImpl method labelsLabelIdDelete.

/**
 * Delete label by label id
 *
 * @param labelId           Id of the label
 * @param request           msf4j request object
 * @return 200 OK if the operation is successful
 * @throws NotFoundException If failed to find the particular resource
 */
@Override
public Response labelsLabelIdDelete(String labelId, Request request) throws NotFoundException {
    try {
        if (labelId != null) {
            APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
            apiMgtAdminService.deleteLabel(labelId);
        } else {
            // mandatory parameters not provided
            String errorMessage = "Label Id parameter should be provided";
            ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
            log.error(errorMessage);
            return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while deleting the label [labelId] " + labelId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.status(Response.Status.NO_CONTENT).build();
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 43 with APIMgtAdminService

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

the class LabelsApiServiceImpl method labelsGet.

/**
 * Gets all labels
 * @param accept Accept header value
 * @param request ms4j request object
 * @return  a list of label objects
 * @throws NotFoundException
 */
@Override
public Response labelsGet(String labelId, String accept, Request request) throws NotFoundException {
    List<Label> labels = new ArrayList<>();
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        // get all labels
        if (labelId == null) {
            labels = apiMgtAdminService.getLabels();
        } else {
            Label label = apiMgtAdminService.getLabelByID(labelId);
            labels.add(label);
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while retrieving all labels";
        ErrorHandler errorHandler = ExceptionCodes.LABEL_EXCEPTION;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
        log.error(errorMessage, e);
        return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.status(Response.Status.OK).entity(LabelMappingUtil.fromLabelArrayToListDTO(labels)).build();
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList)

Example 44 with APIMgtAdminService

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

the class PoliciesApiServiceImpl method policiesThrottlingApplicationIdGet.

/**
 * Returns a matching Application policy for the given policy id @{policyId}
 *
 * @param id          Uuid of the Application policy
 * @param ifNoneMatch       If-None-Match header value
 * @param ifModifiedSince   If-Modified-Since header value
 * @param request           msf4j request object
 * @return Response object  Response with matching {@link ApplicationThrottlePolicyDTO} object
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingApplicationIdGet(String id, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.info("Received Application Policy Get request. Policy uuid: " + id);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        Policy applicationPolicy = apiMgtAdminService.getApplicationPolicyByUuid(id);
        return Response.status(Response.Status.OK).entity(ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyToDTO(applicationPolicy)).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while getting 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 : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) 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)

Example 45 with APIMgtAdminService

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

the class PoliciesApiServiceImpl method policiesThrottlingCustomRuleIdPut.

/**
 * Updates a given Global level policy/custom rule specified by uuid.
 *
 * @param ruleId            uuid of the policy
 * @param body              DTO of policy to be updated
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return Updated policy
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingCustomRuleIdPut(String ruleId, CustomRuleDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Custom Policy PUT request " + body + " with rule ID = " + ruleId);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        CustomPolicy customPolicy = CustomPolicyMappingUtil.fromCustomPolicyDTOToModel(body);
        customPolicy.setUuid(ruleId);
        apiMgtAdminService.updateCustomRule(customPolicy);
        return Response.status(Response.Status.OK).entity(CustomPolicyMappingUtil.fromCustomPolicyToDTO(apiMgtAdminService.getCustomRuleByUUID(ruleId))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while updating Custom Policy. policy ID: " + 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)

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