Search in sources :

Example 21 with APIMgtAdminService

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

the class EndpointsApiServiceImpl method endpointsGet.

@Override
public Response endpointsGet(Integer limit, String accept, Request request) throws NotFoundException {
    EndpointListDTO endpointListDTO = new EndpointListDTO();
    try {
        APIMgtAdminService adminService = RestApiUtil.getAPIMgtAdminService();
        List<Endpoint> endpointList = adminService.getAllEndpoints();
        endpointListDTO.setList(MappingUtil.toEndpointListDto(endpointList));
        endpointListDTO.setCount(endpointList.size());
        return Response.ok().entity(endpointListDTO).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) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) EndpointListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.EndpointListDTO)

Example 22 with APIMgtAdminService

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

the class EndpointsApiServiceImpl method endpointsEndpointIdGatewayConfigGet.

@Override
public Response endpointsEndpointIdGatewayConfigGet(String endpointId, String accept, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        String endpointGatewayConfig = apiMgtAdminService.getEndpointGatewayConfig(endpointId);
        if (endpointGatewayConfig != null) {
            return Response.ok().entity(endpointGatewayConfig).build();
        } else {
            String msg = "Endpoint is not found with apiId : " + endpointId;
            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 Endpoint : " + endpointId;
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.ENDPOINT_ID, endpointId);
        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) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with APIMgtAdminService

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

the class ExportApiServiceImpl method exportPoliciesThrottleGet.

/**
 * Export throttle policies containing zip.
 *
 * @param accept  Accept 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 exportPoliciesThrottleGet(String accept, Request request) throws NotFoundException {
    String archiveName = "exported-policies";
    // files will be written to following directory
    String exportedPoliciesDirName = "exported-policies";
    // archive will be here at following location tmp directory
    String archiveDir = System.getProperty("java.io.tmpdir");
    if (log.isDebugEnabled()) {
        log.debug("Received export policies GET request ");
    }
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        PolicyExportManager policyExportManager = new PolicyExportManager(apiMgtAdminService);
        // create archive and get the archive location
        String zippedFilePath = policyExportManager.createArchiveFromExecutionPlans(exportedPoliciesDirName, archiveDir, archiveName);
        APIFileUtils.deleteDirectory(exportedPoliciesDirName);
        File exportedApiArchiveFile = new File(zippedFilePath);
        Response.ResponseBuilder responseBuilder = Response.status(Response.Status.OK).entity(exportedApiArchiveFile);
        responseBuilder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM).header("Content-Disposition", "attachment; filename=\"" + exportedApiArchiveFile.getName() + "\"");
        Response response = responseBuilder.build();
        return response;
    } catch (APIManagementException e) {
        String errorMessage = "Error while exporting policies";
        log.error(errorMessage, e);
        org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyExportManager(org.wso2.carbon.apimgt.rest.api.core.utils.PolicyExportManager) File(java.io.File)

Example 24 with APIMgtAdminService

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

the class GatewaysApiServiceImpl method gatewaysRegisterPost.

/**
 * Register gateway
 *
 * @param body        RegistrationDTO
 * @param contentType Content-Type header value
 * @return Registration summary details
 * @throws NotFoundException If failed to register gateway
 */
@Override
public Response gatewaysRegisterPost(RegistrationDTO body, String contentType, Request request) throws NotFoundException {
    try {
        LabelInfoDTO labelInfoDTO = body.getLabelInfo();
        if (labelInfoDTO != null) {
            APIMgtAdminService adminService = RestApiUtil.getAPIMgtAdminService();
            String overwriteLabels = labelInfoDTO.getOverwriteLabels();
            List<Label> labels = MappingUtil.convertToLabels(labelInfoDTO.getLabelList());
            adminService.registerGatewayLabels(labels, overwriteLabels);
            RegistrationSummary registrationSummary = adminService.getRegistrationSummary();
            return Response.ok().entity(MappingUtil.toRegistrationSummaryDTO(registrationSummary)).build();
        } else {
            String errorMessage = "Label information cannot be null";
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.LABEL_INFORMATION_CANNOT_BE_NULL);
            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();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while registering the gateway";
        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) Label(org.wso2.carbon.apimgt.core.models.Label) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) LabelInfoDTO(org.wso2.carbon.apimgt.rest.api.core.dto.LabelInfoDTO) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) RegistrationSummary(org.wso2.carbon.apimgt.core.models.RegistrationSummary)

Example 25 with APIMgtAdminService

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

the class PoliciesApiServiceImpl method policiesGet.

@Override
public Response policiesGet(String accept, Request request) throws NotFoundException {
    PolicyListDTO policyListDTO = new PolicyListDTO();
    try {
        APIMgtAdminService adminService = RestApiUtil.getAPIMgtAdminService();
        List<PolicyDTO> policyDTOList = MappingUtil.convertToPolicyDtoList(adminService.getAllPolicies());
        policyListDTO.setList(policyDTOList);
        policyListDTO.setCount(policyDTOList.size());
        return Response.ok().entity(policyListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving Policies";
        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) PolicyDTO(org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.PolicyListDTO) 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