Search in sources :

Example 21 with APIListDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.APIListDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisGet.

/**
 * Retrieves APIs qualifying under given search condition
 *
 * @param limit       maximum number of APIs returns
 * @param offset      starting index
 * @param labels      Labels of the store for which the apis need to be retrieved
 * @param query       search condition
 * @param ifNoneMatch If-None-Match header value
 * @param request     msf4j request object
 * @return matched APIs for the given search condition
 */
@Override
public Response apisGet(Integer limit, Integer offset, String labels, String query, String ifNoneMatch, Request request) throws NotFoundException {
    List<API> apisResult = null;
    APIListDTO apiListDTO = null;
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIStore apiStore = RestApiUtil.getConsumer(username);
        List<String> labelList = new ArrayList<>();
        if (labels != null) {
            labelList = Arrays.asList(labels.split(","));
        }
        apisResult = apiStore.searchAPIsByStoreLabels(query, offset, limit, labelList);
        // convert API
        apiListDTO = APIMappingUtil.toAPIListDTO(apisResult);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving APIs ";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_NAME, query);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(apiListDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) ArrayList(java.util.ArrayList) APIListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.APIListDTO) API(org.wso2.carbon.apimgt.core.models.API) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 22 with APIListDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.APIListDTO in project carbon-apimgt by wso2.

the class FileBasedApiImportExportManager method importAPIs.

/**
 * Imports a set of APIs to API Manager by reading and decoding the input stream
 *
 * @param uploadedApiArchiveInputStream InputStream to be read ana decoded to a set of APIs
 * @param provider                      API provider, if needs to be updated
 * @return {@link APIListDTO} object comprising of successfully imported APIs
 * @throws APIMgtEntityImportExportException if any error occurs while importing or no APIs are imported successfully
 */
public APIListDTO importAPIs(InputStream uploadedApiArchiveInputStream, String provider) throws APIMgtEntityImportExportException {
    String apiArchiveLocation = path + File.separator + IMPORTED_APIS_DIRECTORY_NAME + ".zip";
    String archiveExtractLocation = null;
    try {
        archiveExtractLocation = APIFileUtils.extractUploadedArchive(uploadedApiArchiveInputStream, IMPORTED_APIS_DIRECTORY_NAME, apiArchiveLocation, path);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error in accessing uploaded API archive " + apiArchiveLocation;
        log.error(errorMsg, e);
        throw new APIMgtEntityImportExportException(errorMsg, e, ExceptionCodes.API_IMPORT_ERROR);
    }
    // List to contain newly created/updated APIs
    Set<APIDetails> apiDetailsSet = decodeApiInformationFromDirectoryStructure(archiveExtractLocation, provider);
    List<API> apis = new ArrayList<>();
    for (APIDetails apiDetails : apiDetailsSet) {
        try {
            apis.add(importApi(apiDetails));
        } catch (APIManagementException e) {
            log.error("Error while importing API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
            // skip importing the API
            continue;
        }
        log.info("Successfully imported API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
    }
    try {
        APIFileUtils.deleteDirectory(path);
    } catch (APIMgtDAOException e) {
        log.warn("Unable to remove directory " + path);
    }
    // if no APIs are corrected exported, throw an error
    if (apis.isEmpty()) {
        String errorMsg = "No APIs imported successfully";
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
    }
    return MappingUtil.toAPIListDTO(apis);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.core.models.API) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 23 with APIListDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.APIListDTO in project carbon-apimgt by wso2.

the class SubscriptionValidationDataUtil method fromAPIToAPIListDTO.

public static APIListDTO fromAPIToAPIListDTO(API model) {
    APIListDTO apiListdto = new APIListDTO();
    if (model != null) {
        APIDTO apidto = new APIDTO();
        apidto.setUuid(model.getApiUUID());
        apidto.setApiId(model.getApiId());
        apidto.setVersion(model.getVersion());
        apidto.setContext(model.getContext());
        apidto.setPolicy(model.getPolicy());
        apidto.setProvider(model.getProvider());
        apidto.setApiType(model.getApiType());
        apidto.setName(model.getName());
        apidto.setStatus(model.getStatus());
        apidto.setIsDefaultVersion(model.isDefaultVersion());
        Map<String, URLMapping> urlMappings = model.getAllResources();
        List<URLMappingDTO> urlMappingsDTO = new ArrayList<>();
        for (URLMapping urlMapping : urlMappings.values()) {
            URLMappingDTO urlMappingDTO = new URLMappingDTO();
            urlMappingDTO.setAuthScheme(urlMapping.getAuthScheme());
            urlMappingDTO.setHttpMethod(urlMapping.getHttpMethod());
            urlMappingDTO.setThrottlingPolicy(urlMapping.getThrottlingPolicy());
            urlMappingDTO.setUrlPattern(urlMapping.getUrlPattern());
            urlMappingDTO.setScopes(urlMapping.getScopes());
            urlMappingsDTO.add(urlMappingDTO);
        }
        apidto.setUrlMappings(urlMappingsDTO);
        apiListdto.setCount(1);
        apiListdto.getList().add(apidto);
    } else {
        apiListdto.setCount(0);
    }
    return apiListdto;
}
Also used : APIDTO(org.wso2.carbon.apimgt.internal.service.dto.APIDTO) URLMapping(org.wso2.carbon.apimgt.api.model.subscription.URLMapping) ArrayList(java.util.ArrayList) APIListDTO(org.wso2.carbon.apimgt.internal.service.dto.APIListDTO) URLMappingDTO(org.wso2.carbon.apimgt.internal.service.dto.URLMappingDTO)

Example 24 with APIListDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.APIListDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisGet.

@Override
public Response apisGet(String xWSO2Tenant, String apiId, String context, String version, String gatewayLabel, String accept, MessageContext messageContext) throws APIManagementException {
    SubscriptionValidationDAO subscriptionValidationDAO = new SubscriptionValidationDAO();
    xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
    APIListDTO apiListDTO;
    if (StringUtils.isNotEmpty(gatewayLabel)) {
        if (StringUtils.isNotEmpty(apiId)) {
            API api = subscriptionValidationDAO.getApiByUUID(apiId, gatewayLabel, xWSO2Tenant);
            apiListDTO = SubscriptionValidationDataUtil.fromAPIToAPIListDTO(api);
        } else if (StringUtils.isNotEmpty(context) && StringUtils.isNotEmpty(version)) {
            if (!context.startsWith("/t/" + xWSO2Tenant.toLowerCase())) {
                apiListDTO = new APIListDTO();
            }
            API api = subscriptionValidationDAO.getAPIByContextAndVersion(context, version, gatewayLabel);
            apiListDTO = SubscriptionValidationDataUtil.fromAPIToAPIListDTO(api);
        } else {
            // Retrieve API Detail according to Gateway label.
            apiListDTO = SubscriptionValidationDataUtil.fromAPIListToAPIListDTO(subscriptionValidationDAO.getAllApis(xWSO2Tenant, gatewayLabel));
        }
    } else {
        apiListDTO = SubscriptionValidationDataUtil.fromAPIListToAPIListDTO(subscriptionValidationDAO.getAllApis(xWSO2Tenant));
    }
    if (APIConstants.APPLICATION_GZIP.equals(accept)) {
        try {
            File zippedResponse = GZIPUtils.constructZippedResponse(apiListDTO);
            return Response.ok().entity(zippedResponse).header("Content-Disposition", "attachment").header("Content-Encoding", "gzip").build();
        } catch (APIManagementException e) {
            RestApiUtil.handleInternalServerError(e.getMessage(), e, log);
        }
    } else {
        return Response.ok().entity(apiListDTO).build();
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIListDTO(org.wso2.carbon.apimgt.internal.service.dto.APIListDTO) API(org.wso2.carbon.apimgt.api.model.subscription.API) File(java.io.File) SubscriptionValidationDAO(org.wso2.carbon.apimgt.impl.dao.SubscriptionValidationDAO)

Example 25 with APIListDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.APIListDTO in project carbon-apimgt by wso2.

the class GatewayUtils method generateAPIListDTO.

public static APIListDTO generateAPIListDTO(List<API> apiList) {
    APIListDTO apiListDTO = new APIListDTO();
    List<APIMetaDataDTO> apiMetaDataDTOList = new ArrayList<>();
    for (API api : apiList) {
        APIMetaDataDTO apiMetaDataDTO = new APIMetaDataDTO().apiId(api.getApiId()).name(api.getApiName()).version(api.getApiVersion()).apiUUID(api.getUuid()).apiType(api.getApiType()).provider(api.getApiProvider()).context(api.getContext()).isDefaultVersion(api.isDefaultVersion()).name(api.getApiName()).policy(api.getApiTier()).apiType(api.getApiType());
        apiMetaDataDTOList.add(apiMetaDataDTO);
    }
    apiListDTO.setList(apiMetaDataDTOList);
    apiListDTO.count(apiMetaDataDTOList.size());
    return apiListDTO;
}
Also used : ArrayList(java.util.ArrayList) APIListDTO(org.wso2.carbon.apimgt.rest.api.gateway.dto.APIListDTO) APIMetaDataDTO(org.wso2.carbon.apimgt.rest.api.gateway.dto.APIMetaDataDTO) API(org.wso2.carbon.apimgt.keymgt.model.entity.API)

Aggregations

ArrayList (java.util.ArrayList)13 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)8 API (org.wso2.carbon.apimgt.core.models.API)7 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)6 APIListDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.APIListDTO)5 HashMap (java.util.HashMap)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 API (org.wso2.carbon.apimgt.api.model.API)4 APIListDTO (org.wso2.carbon.apimgt.rest.api.core.dto.APIListDTO)4 JSONObject (org.json.simple.JSONObject)3 APIListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.APIListDTO)3 File (java.io.File)2 Set (java.util.Set)2 Test (org.junit.Test)2 Test (org.testng.annotations.Test)2 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)2 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)2 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)2 APIMgtEntityImportExportException (org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)2 APIDetails (org.wso2.carbon.apimgt.core.models.APIDetails)2