Search in sources :

Example 1 with APIMgtEntityImportExportException

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

the class FileBasedApiImportExportManager method getFileFromPrefix.

/**
 * Return the file with given prefix
 *
 * @param apiDirectoryPath Path to find the file
 * @param prefix
 * @return File with given prefix
 * @throws APIMgtEntityImportExportException if file not found or more than one file is present with given prefix
 */
private File getFileFromPrefix(String apiDirectoryPath, String prefix) throws APIMgtEntityImportExportException {
    File dir = new File(apiDirectoryPath);
    File[] files = dir.listFiles((d, name) -> name.startsWith(prefix));
    if (files == null) {
        String errorMsg = "Unable find file with prefix: " + prefix + " at path: " + apiDirectoryPath;
        log.error(errorMsg);
        throw new APIMgtEntityImportExportException(errorMsg);
    }
    if (files.length != 1) {
        String errorMsg = "More than one file with prefix: " + prefix + " found at path: " + apiDirectoryPath;
        log.error(errorMsg);
        throw new APIMgtEntityImportExportException(errorMsg);
    }
    return files[0];
}
Also used : File(java.io.File) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 2 with APIMgtEntityImportExportException

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

the class FileBasedApiImportExportManager method decodeApiInformationFromDirectoryStructure.

/**
 * Reads and decodes APIs and relevant information from the given set of paths
 *
 * @param apiArtifactsBasePath path to the directory with API related artifacts
 * @param newApiProvider       API newApiProvider to be updated
 * @return Set of {@link APIDetails} objects
 * @throws APIMgtEntityImportExportException if any error occurs while decoding the APIs
 */
public Set<APIDetails> decodeApiInformationFromDirectoryStructure(String apiArtifactsBasePath, String newApiProvider) throws APIMgtEntityImportExportException {
    Set<String> apiDefinitionsRootDirectoryPaths = null;
    try {
        apiDefinitionsRootDirectoryPaths = APIFileUtils.getDirectoryList(apiArtifactsBasePath);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Unable to find API definitions at: " + apiArtifactsBasePath;
        log.error(errorMsg, e);
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
    }
    if (apiDefinitionsRootDirectoryPaths.isEmpty()) {
        try {
            APIFileUtils.deleteDirectory(path);
        } catch (APIMgtDAOException e) {
            log.warn("Unable to remove directory " + path);
        }
        String errorMsg = "Unable to find API definitions at: " + apiArtifactsBasePath;
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
    }
    Set<APIDetails> apiDetailsSet = new HashSet<>();
    for (String apiDefinitionDirectoryPath : apiDefinitionsRootDirectoryPaths) {
        File apiDefinitionFile = getFileFromPrefix(apiDefinitionDirectoryPath, APIMgtConstants.APIFileUtilConstants.API_DEFINITION_FILE_PREFIX);
        File swaggerDefinitionFile = getFileFromPrefix(apiDefinitionDirectoryPath, APIMgtConstants.APIFileUtilConstants.SWAGGER_DEFINITION_FILE_PREFIX);
        API api;
        String swaggerDefinition, gatewayConfiguration;
        Set<Endpoint> endpoints;
        try {
            api = getApiDefinitionFromExtractedArchive(apiDefinitionFile.getPath());
            swaggerDefinition = getSwaggerDefinitionFromExtractedArchive(swaggerDefinitionFile.getPath());
            gatewayConfiguration = getGatewayConfigurationFromExtractedArchive(apiDefinitionDirectoryPath + File.separator + APIMgtConstants.APIFileUtilConstants.GATEWAY_CONFIGURATION_DEFINITION_FILE);
            endpoints = getEndpointsFromExtractedArchive(apiDefinitionDirectoryPath + File.separator + ENDPOINTS_ROOT_DIRECTORY, api.getName(), api.getVersion());
        } catch (APIManagementException e) {
            log.error("Error occurred while importing api from path: " + apiDefinitionDirectoryPath, e);
            // skip this API
            continue;
        }
        if (newApiProvider != null && !newApiProvider.isEmpty()) {
            // update the newApiProvider
            api = new API.APIBuilder(api).provider(newApiProvider).build();
        }
        String documentsRootDirectory = apiDefinitionDirectoryPath + File.separator + DOCUMENTS_ROOT_DIRECTORY;
        Set<DocumentInfo> documentInfoSet = getDocumentInfoFromExtractedArchive(documentsRootDirectory, api.getName(), api.getVersion());
        Set<DocumentContent> documentContents = new HashSet<>();
        for (DocumentInfo aDocumentInfo : documentInfoSet) {
            DocumentContent aDocumentContent = getDocumentContentFromExtractedArchive(aDocumentInfo, documentsRootDirectory + File.separator + aDocumentInfo.getId());
            if (aDocumentContent != null) {
                documentContents.add(aDocumentContent);
            }
        }
        InputStream thumbnailStream = null;
        try {
            thumbnailStream = APIFileUtils.getThumbnailImage(apiDefinitionDirectoryPath + File.separator + APIMgtConstants.APIFileUtilConstants.THUMBNAIL_FILE_NAME);
        } catch (APIMgtDAOException e) {
            // log and ignore
            log.error("Error occurred while reading thumbnail image.", e);
        }
        APIDetails apiDetails = new APIDetails(api, swaggerDefinition);
        apiDetails.setGatewayConfiguration(gatewayConfiguration);
        apiDetails.setEndpoints(endpoints);
        if (!documentInfoSet.isEmpty()) {
            apiDetails.addDocumentInformation(documentInfoSet);
        }
        if (!documentContents.isEmpty()) {
            apiDetails.addDocumentContents(documentContents);
        }
        if (thumbnailStream != null) {
            apiDetails.setThumbnailStream(thumbnailStream);
        }
        apiDetailsSet.add(apiDetails);
    }
    return apiDetailsSet;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) InputStream(java.io.InputStream) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) API(org.wso2.carbon.apimgt.core.models.API) File(java.io.File) HashSet(java.util.HashSet) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 3 with APIMgtEntityImportExportException

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

the class FileBasedApiImportExportManager method getEndpointsFromExtractedArchive.

private Set<Endpoint> getEndpointsFromExtractedArchive(String endpointLocation, String apiName, String version) throws APIMgtEntityImportExportException {
    File endpointsRootDirectory = new File(endpointLocation);
    Set<Endpoint> endpoints = new HashSet<>();
    if (endpointsRootDirectory.isDirectory()) {
        File[] endpointFiles = endpointsRootDirectory.listFiles(File::isFile);
        if (endpointFiles == null) {
            // no endpoints in the given location, can't continue
            String errorMsg = "No endpoints found at " + endpointsRootDirectory;
            log.error(errorMsg);
            throw new APIMgtEntityImportExportException(errorMsg);
        }
        Gson gson = new GsonBuilder().create();
        for (File endpointFile : endpointFiles) {
            // read everything
            String content = null;
            try {
                content = APIFileUtils.readFileContentAsText(endpointFile.getPath());
            } catch (APIMgtDAOException e) {
                String errorMsg = "Unable to read endpoints from " + endpointFile.getPath();
                log.error(errorMsg, e);
                throw new APIMgtEntityImportExportException(errorMsg, e);
            }
            endpoints.add(gson.fromJson(content, Endpoint.class));
        }
    }
    return endpoints;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) File(java.io.File) HashSet(java.util.HashSet) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 4 with APIMgtEntityImportExportException

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

the class FileBasedApiImportExportManager method importAndCreateAPIs.

/**
 * Imports and creates a set of new APIs to API Manager by reading and decoding the
 * input stream. Will fail if the APIs already exists
 *
 * @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 importAndCreateAPIs(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(importAndCreateApi(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 5 with APIMgtEntityImportExportException

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

the class FileBasedApplicationImportExportManager method importApplication.

/**
 * Import a given Application from an InputStream
 *
 * @param uploadedAppArchiveInputStream Content stream of the zip file which contains exported Application
 * @return the imported application
 * @throws APIMgtEntityImportExportException
 */
public Application importApplication(InputStream uploadedAppArchiveInputStream) throws APIMgtEntityImportExportException {
    String appArchiveLocation = path + File.separator + IMPORTED_APPLICATIONS_DIRECTORY_NAME + ".zip";
    String archiveExtractLocation = null;
    try {
        archiveExtractLocation = extractUploadedArchiveApplication(uploadedAppArchiveInputStream, IMPORTED_APPLICATIONS_DIRECTORY_NAME, appArchiveLocation, path);
    } catch (APIManagementException e) {
        String errorMsg = "Error in accessing uploaded Application archive" + appArchiveLocation;
        log.error(errorMsg, e);
        throw new APIMgtEntityImportExportException(errorMsg, e, ExceptionCodes.APPLICATION_IMPORT_ERROR);
    }
    Application applicationDetails = parseApplicationFile(archiveExtractLocation);
    return applicationDetails;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Application(org.wso2.carbon.apimgt.core.models.Application) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Aggregations

APIMgtEntityImportExportException (org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)11 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)7 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)6 API (org.wso2.carbon.apimgt.core.models.API)5 File (java.io.File)4 APIDetails (org.wso2.carbon.apimgt.core.models.APIDetails)4 Gson (com.google.gson.Gson)3 GsonBuilder (com.google.gson.GsonBuilder)3 IOException (java.io.IOException)3 Application (org.wso2.carbon.apimgt.core.models.Application)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)2 InputStream (java.io.InputStream)1 DocumentContent (org.wso2.carbon.apimgt.core.models.DocumentContent)1 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)1 FileApi (org.wso2.carbon.apimgt.core.models.FileApi)1