Search in sources :

Example 16 with APIMgtAdminService

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

the class ApisApiServiceImpl method apisApiIdGatewayConfigGet.

/**
 * Retrieve API's gateway configuration
 *
 * @param apiId   UUID of the API
 * @param request msf4j request object
 * @return 200 OK if the opration was successful
 * @throws NotFoundException If failed to retrieve API gateway configuration
 */
@Override
public Response apisApiIdGatewayConfigGet(String apiId, String accept, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        String apiGatewayConfig = null;
        apiGatewayConfig = apiMgtAdminService.getAPIGatewayServiceConfig(apiId);
        if (apiGatewayConfig != null) {
            return Response.ok().entity(apiGatewayConfig).build();
        } else {
            String msg = "API is not found with apiId : " + apiId;
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900314L, msg);
            log.error(msg);
            return Response.status(Response.Status.NOT_FOUND).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).entity(errorDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving gateway config of 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()).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 17 with APIMgtAdminService

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

the class ApisApiServiceImpl method apisGet.

/**
 * Retrieve a list of APIs with given gateway labels and status.
 *
 * @param labels Gateway labels
 * @param status Lifecycle status
 * @param request msf4j request object
 * @return 200 OK if the opration was successful
 * @throws NotFoundException If failed to retrieve APIs
 */
@Override
public Response apisGet(String labels, String status, Request request) throws NotFoundException {
    APIListDTO apiListDTO;
    try {
        APIMgtAdminService adminService = RestApiUtil.getAPIMgtAdminService();
        if (labels != null && !labels.isEmpty()) {
            String[] gatewayLabels = labels.split(",");
            List<String> labelList = new ArrayList<String>(Arrays.asList(gatewayLabels));
            if (status != null && !status.isEmpty()) {
                apiListDTO = MappingUtil.toAPIListDTO(adminService.getAPIsByStatus(labelList, status));
                return Response.ok().entity(apiListDTO).build();
            } else {
                apiListDTO = MappingUtil.toAPIListDTO(adminService.getAPIsByGatewayLabel(labelList));
                return Response.ok().entity(apiListDTO).build();
            }
        } else {
            apiListDTO = new APIListDTO();
            return Response.ok().entity(apiListDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving APIs";
        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) ArrayList(java.util.ArrayList) APIListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.APIListDTO)

Example 18 with APIMgtAdminService

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

the class ApplicationsApiServiceImpl method applicationsGet.

@Override
public Response applicationsGet(String accept, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        List<Application> applicationList = apiMgtAdminService.getAllApplications();
        ApplicationListDTO applicationListDTO = new ApplicationListDTO();
        applicationListDTO.setList(MappingUtil.convertToApplicationDtoList(applicationList));
        return Response.ok(applicationListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving Applications.";
        HashMap<String, String> paramList = new HashMap<String, String>();
        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 : HashMap(java.util.HashMap) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Application(org.wso2.carbon.apimgt.core.models.Application) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ApplicationListDTO)

Example 19 with APIMgtAdminService

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

the class ApisApiServiceImplTestCase method apisGetApisForExceptionTestCase.

@Test
public void apisGetApisForExceptionTestCase() throws Exception {
    String labels = "ZONE_ONE,ZONE_TWO";
    APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
    String apiID = UUID.randomUUID().toString();
    String message = "Error while retrieving gateway config of API " + apiID;
    APIManagementException apiManagementException = new APIManagementException(message, ExceptionCodes.GATEWAY_EXCEPTION);
    Mockito.when(RestApiUtil.getAPIMgtAdminService()).thenThrow(apiManagementException);
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    Response response = apisApiService.apisGet(labels, "Published", getRequest());
    Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 20 with APIMgtAdminService

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

the class BlacklistApiServiceImpl method blacklistGet.

@Override
public Response blacklistGet(String accept, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        List<BlockConditions> blockConditionsList = apiMgtAdminService.getBlockConditions();
        BlockingConditionListDTO blockingConditionListDTO = MappingUtil.fromBlockConditionListToListDTO(blockConditionsList);
        return Response.ok(blockingConditionListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving block conditions";
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).entity(errorDTO).build();
    }
}
Also used : BlockingConditionListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO) 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)

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