Search in sources :

Example 96 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingAdvancedPost.

/**
 * Create 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 policiesThrottlingAdvancedPost(AdvancedThrottlePolicyDTO body, Request request) throws NotFoundException {
    APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.api;
    if (log.isDebugEnabled()) {
        log.info("Received Advance Policy POST request " + body + " with tierLevel = " + tierLevel);
    }
    if (log.isDebugEnabled()) {
        log.info("Received Advance Policy PUT request " + body + " with tierLevel = " + tierLevel);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        APIPolicy apiPolicy = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(body);
        String policyId = apiMgtAdminService.addApiPolicy(apiPolicy);
        return Response.status(Response.Status.CREATED).entity(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(apiMgtAdminService.getApiPolicyByUuid(policyId))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while adding Advanced Throttle Policy, policy name: " + body.getPolicyName();
        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 97 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class BlacklistApiServiceImpl method blacklistConditionIdDelete.

/**
 * Delete blacklist condition using ID
 *
 * @param conditionId       condition ID of the block condition
 * @param ifMatch           IF-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return Response Object
 * @throws NotFoundException Iif an error occurred when particular resource does not exits in the system.
 */
@Override
public Response blacklistConditionIdDelete(String conditionId, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Blacklist Condition DELETE request with id: " + conditionId);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        apiMgtAdminService.deleteBlockConditionByUuid(conditionId);
        return Response.ok().build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while deleting blacklist condition with UUID " + conditionId;
        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) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 98 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class BlacklistApiServiceImpl method blacklistGet.

/**
 * Get blacklist conditions.
 *
 * @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 blacklistGet(String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received BlockCondition GET request");
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        List<BlockConditions> blockConditions = apiMgtAdminService.getBlockConditions();
        BlockingConditionListDTO listDTO = BlockingConditionMappingUtil.fromBlockConditionListToListDTO(blockConditions);
        return Response.ok().entity(listDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while getting blacklist ";
        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) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 99 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdGet.

/**
 * Get API of given ID
 *
 * @param apiId           API ID
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return API of the given ID
 * @throws NotFoundException If failed to get the API
 */
@Override
public Response apisApiIdGet(String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    APIDTO apiToReturn = null;
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = apisApiIdGetFingerprint(apiId, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        API api = apiStore.getAPIbyUUID(apiId);
        boolean isWSDLExists = apiStore.isWSDLExists(apiId);
        apiToReturn = APIMappingUtil.toAPIDTO(api);
        if (isWSDLExists) {
            String wsdlUri = RestApiConstants.WSDL_URI_TEMPLATE.replace(RestApiConstants.APIID_PARAM, api.getId());
            apiToReturn.setWsdlUri(wsdlUri);
        }
        return Response.ok().entity(apiToReturn).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving API : " + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIDTO(org.wso2.carbon.apimgt.rest.api.store.dto.APIDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) API(org.wso2.carbon.apimgt.core.models.API) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 100 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdKeysKeyTypePut.

/**
 * Update grant types/callback URL
 *
 * @param applicationId Application Id
 * @param keyType       Key Type (Production | Sandbox)
 * @param body          Grant type and callback URL information
 * @param request       msf4j request object
 * @return Updated Key Information
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response applicationsApplicationIdKeysKeyTypePut(String applicationId, String keyType, ApplicationKeysDTO body, Request request) throws NotFoundException {
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIStore apiConsumer = RestApiUtil.getConsumer(username);
        OAuthApplicationInfo oAuthApp = apiConsumer.updateGrantTypesAndCallbackURL(applicationId, keyType, body.getSupportedGrantTypes(), body.getCallbackUrl());
        ApplicationKeysDTO appKeys = ApplicationKeyMappingUtil.fromApplicationKeysToDTO(oAuthApp);
        return Response.ok().entity(appKeys).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while updating '" + keyType + "'-type grant types/callback url of " + "application: " + applicationId;
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
        paramList.put(APIMgtConstants.ExceptionsConstants.KEY_TYPE, keyType);
        paramList.put(APIMgtConstants.ExceptionsConstants.CALLBACK_URL, body.getCallbackUrl());
        String grantTypes = null;
        if (body.getSupportedGrantTypes() != null) {
            grantTypes = String.join(",", body.getSupportedGrantTypes());
        }
        paramList.put(APIMgtConstants.ExceptionsConstants.GRANT_TYPES, grantTypes);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : ApplicationKeysDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)432 Test (org.testng.annotations.Test)353 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)233 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)202 API (org.wso2.carbon.apimgt.core.models.API)200 Test (org.junit.Test)164 Response (javax.ws.rs.core.Response)160 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)159 HashMap (java.util.HashMap)148 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)146 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)134 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)129 ArrayList (java.util.ArrayList)102 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)100 BeforeTest (org.testng.annotations.BeforeTest)82 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)80 Request (org.wso2.msf4j.Request)80 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)76 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)75 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)71