Search in sources :

Example 6 with FileApi

use of org.wso2.carbon.apimgt.core.models.FileApi in project carbon-apimgt by wso2.

the class ApiFileDAOImpl method getImage.

/**
 * @see ApiDAO#getImage(String apiID)
 */
@Override
public InputStream getImage(String apiID) throws APIMgtDAOException {
    API api = getAPI(apiID);
    if (api == null) {
        String errorMsg = "Unable to find API with Id: " + apiID;
        log.error(errorMsg);
        throw new APIMgtDAOException(errorMsg, ExceptionCodes.API_NOT_FOUND);
    }
    String thumbnailPath = APIFileUtils.getAPIBaseDirectory(storagePath, new FileApi(api)) + File.separator + APIMgtConstants.APIFileUtilConstants.THUMBNAIL_FILE_NAME;
    return APIFileUtils.getThumbnailImage(thumbnailPath);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) FileApi(org.wso2.carbon.apimgt.core.models.FileApi)

Example 7 with FileApi

use of org.wso2.carbon.apimgt.core.models.FileApi 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)

Aggregations

FileApi (org.wso2.carbon.apimgt.core.models.FileApi)7 API (org.wso2.carbon.apimgt.core.models.API)6 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)4 File (java.io.File)3 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)3 InputStream (java.io.InputStream)1 Test (org.testng.annotations.Test)1 APIMgtEntityImportExportException (org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)1 APIDetails (org.wso2.carbon.apimgt.core.models.APIDetails)1 GatewayConfigDTO (org.wso2.carbon.apimgt.core.template.dto.GatewayConfigDTO)1