Search in sources :

Example 16 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)

Aggregations

ArrayList (java.util.ArrayList)8 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 APIListDTO (org.wso2.carbon.apimgt.rest.api.core.dto.APIListDTO)4 HashMap (java.util.HashMap)3 APIListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.APIListDTO)3 Test (org.junit.Test)2 Test (org.testng.annotations.Test)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 FileBasedApiImportExportManager (org.wso2.carbon.apimgt.rest.api.publisher.utils.FileBasedApiImportExportManager)2 CompositeAPIListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIListDTO)2 Response (javax.ws.rs.core.Response)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)1 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)1