Search in sources :

Example 6 with APIMgtEntityImportExportException

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

the class FileBasedApplicationImportExportManager method exportApplication.

/**
 * Export a given Application to a file system as zip archive.
 * The export root location is given by {@link FileBasedApplicationImportExportManager#path}/exported-application.
 *
 * @param application         Application{@link Application} to be exported
 * @param exportDirectoryName Name of directory to be exported
 * @return path to the exported directory with exported artifacts
 * @throws APIMgtEntityImportExportException
 */
public String exportApplication(Application application, String exportDirectoryName) throws APIMgtEntityImportExportException {
    String applicationArtifactBaseDirectoryPath = path + File.separator + exportDirectoryName;
    try {
        Files.createDirectories(Paths.get(applicationArtifactBaseDirectoryPath));
    } catch (IOException e) {
        String errorMsg = "Unable to create the directory for export Application at :" + applicationArtifactBaseDirectoryPath;
        log.error(errorMsg, e);
        throw new APIMgtEntityImportExportException(errorMsg, e);
    }
    Application exportApplication = application;
    String applicationExportDirectory = applicationArtifactBaseDirectoryPath + File.separator + exportApplication.getName();
    try {
        // create directory per application
        Files.createDirectories(Paths.get(applicationExportDirectory));
        // export application details
        exportApplicationDetailsToFileSystem(exportApplication, applicationExportDirectory);
    } catch (IOException e) {
        log.error("Error while exporting Application: " + exportApplication.getName(), e);
    }
    // Check if no application is exported
    try {
        if (APIFileUtils.getDirectoryList(applicationArtifactBaseDirectoryPath).isEmpty()) {
            // cleanup the archive root directory
            APIFileUtils.deleteDirectory(path);
        }
    } catch (APIManagementException e) {
        String errorMsg = "Unable to find Application Details at: " + applicationArtifactBaseDirectoryPath;
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.APPLICATION_EXPORT_ERROR);
    }
    return applicationArtifactBaseDirectoryPath;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) IOException(java.io.IOException) Application(org.wso2.carbon.apimgt.core.models.Application) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 7 with APIMgtEntityImportExportException

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

the class FileBasedApplicationImportExportManager method parseApplicationFile.

/**
 * Extracts the details of an Application from a json file
 *
 * @param applicationDetailsFilePath Directory which contains the json file
 * @return an application object containing the details extracted from the json file
 * @throws APIMgtEntityImportExportException
 */
private Application parseApplicationFile(String applicationDetailsFilePath) throws APIMgtEntityImportExportException {
    String applicationDetailsString;
    try {
        applicationDetailsString = new String(Files.readAllBytes(Paths.get(applicationDetailsFilePath)), StandardCharsets.UTF_8);
    } catch (IOException e) {
        String errorMsg = "Unable to read application details from file at " + applicationDetailsFilePath;
        throw new APIMgtEntityImportExportException(errorMsg, e);
    }
    // convert to bean
    Gson gson = new GsonBuilder().create();
    // returns an application object from a json string
    Application applicationDetails = gson.fromJson(applicationDetailsString, Application.class);
    return applicationDetails;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) IOException(java.io.IOException) Application(org.wso2.carbon.apimgt.core.models.Application) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 8 with APIMgtEntityImportExportException

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

the class FileBasedApplicationImportExportManager method createArchiveFromExportedAppArtifacts.

/**
 * Creates an archive of the contained application details.
 *
 * @param sourceDirectory Directory which contains source file
 * @param archiveLocation Directory to generate the zip archive
 * @param archiveName     Name of the zip archive
 * @return path to the created archive file
 * @throws APIMgtEntityImportExportException
 */
public String createArchiveFromExportedAppArtifacts(String sourceDirectory, String archiveLocation, String archiveName) throws APIMgtEntityImportExportException {
    String archivedFilePath = null;
    try {
        APIFileUtils.archiveDirectory(sourceDirectory, archiveLocation, archiveName);
    } catch (APIManagementException e) {
        // cleanup the archive root directory
        try {
            FileUtils.deleteDirectory(new File(path));
        } catch (IOException e1) {
            log.warn("Unable to remove directory " + path);
        }
        String errorMsg = "Error while archiving directory " + sourceDirectory;
        throw new APIMgtEntityImportExportException(errorMsg, e, ExceptionCodes.APPLICATION_EXPORT_ERROR);
    }
    archivedFilePath = archiveLocation + File.separator + archiveName + ".zip";
    return archivedFilePath;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) IOException(java.io.IOException) File(java.io.File) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 9 with APIMgtEntityImportExportException

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

the class FileBasedApiImportExportManager method exportAPIs.

/**
 * Export a given set of APIs to the file system as a zip archive.
 * The export root location is given by {@link FileBasedApiImportExportManager#path}/exported-apis.
 *
 * @param apiDetailSet        Set of {@link APIDetails} objects to be exported
 * @param exportDirectoryName Name of the directory to do the export
 * @return Path to the directory  with exported artifacts
 * @throws APIMgtEntityImportExportException if an error occurred while exporting APIs to file system or
 *                                           no APIs are exported successfully
 */
public String exportAPIs(Set<APIDetails> apiDetailSet, String exportDirectoryName) throws APIMgtEntityImportExportException {
    // this is the base directory for the archive. after export happens, this directory will
    // be archived to be sent as a application/zip response to the client
    String apiArtifactsBaseDirectoryPath = path + File.separator + exportDirectoryName;
    try {
        APIFileUtils.createDirectory(apiArtifactsBaseDirectoryPath);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Unable to create directory for export API at :" + apiArtifactsBaseDirectoryPath;
        throw new APIMgtEntityImportExportException(errorMsg, e);
    }
    for (APIDetails apiDetails : apiDetailSet) {
        // derive the folder structure
        String apiExportDirectory = APIFileUtils.getAPIBaseDirectory(apiArtifactsBaseDirectoryPath, new FileApi(apiDetails.getApi()));
        API exportAPI = apiDetails.getApi();
        try {
            // create per-api export directory
            APIFileUtils.createDirectory(apiExportDirectory);
            // export API definition
            APIFileUtils.exportApiDefinitionToFileSystem(new FileApi(exportAPI), apiExportDirectory);
            // export swagger definition
            APIFileUtils.exportSwaggerDefinitionToFileSystem(apiDetails.getSwaggerDefinition(), exportAPI, apiExportDirectory);
            // export gateway configs
            APIFileUtils.exportGatewayConfigToFileSystem(apiDetails.getGatewayConfiguration(), exportAPI, apiExportDirectory);
            if (apiDetails.getEndpoints() != null && !apiDetails.getEndpoints().isEmpty()) {
                exportEndpointsToFileSystem(apiDetails.getEndpoints(), exportAPI, apiExportDirectory);
            }
        } catch (APIMgtDAOException e) {
            // no need to throw, log
            log.error("Error in exporting API: " + exportAPI.getName() + ", version: " + apiDetails.getApi().getVersion(), e);
            // cleanup the API directory
            try {
                APIFileUtils.deleteDirectory(path);
            } catch (APIMgtDAOException e1) {
                log.warn("Unable to remove directory " + path);
            }
            // skip this API
            continue;
        }
        // as exported correctly.
        if (apiDetails.getThumbnailStream() != null) {
            try {
                APIFileUtils.exportThumbnailToFileSystem(apiDetails.getThumbnailStream(), apiExportDirectory);
            } catch (APIMgtDAOException warn) {
                // log the warning without throwing
                log.warn("Error in exporting thumbnail to file system for api: " + exportAPI.getName() + ", version: " + exportAPI.getVersion());
            }
        }
        exportDocumentationToFileSystem(apiDetails.getAllDocumentInformation(), apiDetails, apiExportDirectory);
        log.info("Successfully exported API: " + exportAPI.getName() + ", version: " + exportAPI.getVersion());
    }
    // if the directory is empty, no APIs have been exported!
    try {
        if (APIFileUtils.getDirectoryList(apiArtifactsBaseDirectoryPath).isEmpty()) {
            // cleanup the archive root directory
            APIFileUtils.deleteDirectory(path);
            String errorMsg = "No APIs exported successfully";
            throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_EXPORT_ERROR);
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Unable to find API definitions at: " + apiArtifactsBaseDirectoryPath;
        log.error(errorMsg, e);
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
    }
    return apiArtifactsBaseDirectoryPath;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) API(org.wso2.carbon.apimgt.core.models.API) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException) FileApi(org.wso2.carbon.apimgt.core.models.FileApi)

Example 10 with APIMgtEntityImportExportException

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

the class FileBasedApiImportExportManager method getApiDefinitionFromExtractedArchive.

/**
 * Creates {@link APIDTO} instance from API Definition file
 *
 * @param apiDefinitionFilePath path to api definition file
 * @return {@link APIDTO} instance
 * @throws APIManagementException if an error occurs while creating API definition object
 */
private API getApiDefinitionFromExtractedArchive(String apiDefinitionFilePath) throws APIMgtEntityImportExportException {
    String apiDefinitionString;
    try {
        apiDefinitionString = APIFileUtils.readFileContentAsText(apiDefinitionFilePath);
    } catch (APIMgtDAOException e) {
        // Unable to read the API definition file, skip this API
        String errorMsg = "Error reading API definition from file at: " + apiDefinitionFilePath;
        throw new APIMgtEntityImportExportException(errorMsg, e);
    }
    // convert to bean
    Gson gson = new GsonBuilder().create();
    try {
        return new API.APIBuilder(gson.fromJson(apiDefinitionString, FileApi.class)).build();
    } catch (Exception e) {
        String errorMsg = "Error in building APIDTO from api definition read from file at: " + apiDefinitionFilePath;
        throw new APIMgtEntityImportExportException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) API(org.wso2.carbon.apimgt.core.models.API) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) 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