Search in sources :

Example 36 with APIImportExportException

use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.

the class ExportUtils method addAPIProductMetaInformationToArchive.

/**
 * Retrieve meta information of the API Product to export and store it in the archive directory.
 * URL template information are stored in swagger.json definition while rest of the required
 * data are in api.json
 *
 * @param archivePath           Folder path to export meta information
 * @param apiProductDtoToReturn APIProductDTO to be exported
 * @param exportFormat          Export format of file
 * @param apiProvider           API Provider
 * @throws APIImportExportException If an error occurs while exporting meta information
 */
public static void addAPIProductMetaInformationToArchive(String archivePath, APIProductDTO apiProductDtoToReturn, ExportFormat exportFormat, APIProvider apiProvider) throws APIImportExportException {
    CommonUtil.createDirectory(archivePath + File.separator + ImportExportConstants.DEFINITIONS_DIRECTORY);
    try {
        String formattedSwaggerJson = apiProvider.getAPIDefinitionOfAPIProduct(APIMappingUtil.fromDTOtoAPIProduct(apiProductDtoToReturn, apiProductDtoToReturn.getProvider()));
        CommonUtil.writeToYamlOrJson(archivePath + ImportExportConstants.SWAGGER_DEFINITION_LOCATION, exportFormat, formattedSwaggerJson);
        if (log.isDebugEnabled()) {
            log.debug("Meta information retrieved successfully for API Product: " + apiProductDtoToReturn.getName());
        }
        CommonUtil.writeDtoToFile(archivePath + ImportExportConstants.API_PRODUCT_FILE_LOCATION, exportFormat, ImportExportConstants.TYPE_API_PRODUCT, apiProductDtoToReturn);
    } catch (APIManagementException e) {
        throw new APIImportExportException("Error while retrieving Swagger definition for API Product: " + apiProductDtoToReturn.getName(), e);
    } catch (IOException e) {
        throw new APIImportExportException("Error while saving as YAML for API Product: " + apiProductDtoToReturn.getName(), e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException)

Example 37 with APIImportExportException

use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.

the class ExportUtils method addRuntimeSequencesToArchive.

/**
 * Retrieve available custom sequences and API specific sequences for API export, and store it in the archive
 * directory.
 *
 * @param archivePath File path to export the sequences
 * @throws APIImportExportException If an error occurs while exporting sequences
 */
public static void addRuntimeSequencesToArchive(String archivePath, API api) throws APIImportExportException, APIManagementException {
    String seqArchivePath = archivePath.concat(File.separator + ImportExportConstants.SEQUENCES_RESOURCE);
    Mediation inSequenceMediation = api.getInSequenceMediation();
    Mediation outSequenceMediation = api.getOutSequenceMediation();
    Mediation faultSequenceMediation = api.getFaultSequenceMediation();
    if (inSequenceMediation != null || outSequenceMediation != null || faultSequenceMediation != null) {
        CommonUtil.createDirectory(seqArchivePath);
        if (inSequenceMediation != null) {
            String individualSequenceExportPath;
            if (inSequenceMediation.isGlobal()) {
                individualSequenceExportPath = seqArchivePath + File.separator + ImportExportConstants.IN_SEQUENCE_PREFIX + ImportExportConstants.SEQUENCE_LOCATION_POSTFIX;
            } else {
                individualSequenceExportPath = seqArchivePath + File.separator + ImportExportConstants.IN_SEQUENCE_PREFIX + ImportExportConstants.SEQUENCE_LOCATION_POSTFIX + File.separator + ImportExportConstants.CUSTOM_TYPE;
            }
            if (!CommonUtil.checkFileExistence(individualSequenceExportPath)) {
                CommonUtil.createDirectory(individualSequenceExportPath);
            }
            writeSequenceToArchive(inSequenceMediation.getConfig(), individualSequenceExportPath, inSequenceMediation.getName());
        }
        if (outSequenceMediation != null) {
            String individualSequenceExportPath;
            if (outSequenceMediation.isGlobal()) {
                individualSequenceExportPath = seqArchivePath + File.separator + ImportExportConstants.OUT_SEQUENCE_PREFIX + ImportExportConstants.SEQUENCE_LOCATION_POSTFIX;
            } else {
                individualSequenceExportPath = seqArchivePath + File.separator + ImportExportConstants.OUT_SEQUENCE_PREFIX + ImportExportConstants.SEQUENCE_LOCATION_POSTFIX + File.separator + ImportExportConstants.CUSTOM_TYPE;
            }
            if (!CommonUtil.checkFileExistence(individualSequenceExportPath)) {
                CommonUtil.createDirectory(individualSequenceExportPath);
            }
            writeSequenceToArchive(outSequenceMediation.getConfig(), individualSequenceExportPath, outSequenceMediation.getName());
        }
        if (faultSequenceMediation != null) {
            String individualSequenceExportPath;
            if (faultSequenceMediation.isGlobal()) {
                individualSequenceExportPath = seqArchivePath + File.separator + ImportExportConstants.FAULT_SEQUENCE_PREFIX + ImportExportConstants.SEQUENCE_LOCATION_POSTFIX;
            } else {
                individualSequenceExportPath = seqArchivePath + File.separator + ImportExportConstants.FAULT_SEQUENCE_PREFIX + ImportExportConstants.SEQUENCE_LOCATION_POSTFIX + File.separator + ImportExportConstants.CUSTOM_TYPE;
            }
            if (!CommonUtil.checkFileExistence(individualSequenceExportPath)) {
                CommonUtil.createDirectory(individualSequenceExportPath);
            }
            writeSequenceToArchive(faultSequenceMediation.getConfig(), individualSequenceExportPath, faultSequenceMediation.getName());
        }
    }
}
Also used : Mediation(org.wso2.carbon.apimgt.api.model.Mediation)

Example 38 with APIImportExportException

use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.

the class ExportUtils method addDependentAPIsToArchive.

/**
 * Retrieve dependent APIs by checking the resources of the API Product and store those in the archive directory.
 *
 * @param archivePath           Temp location to save the API artifacts
 * @param apiProductDtoToReturn API Product DTO which the resources should be considered
 * @param userName              User name of the requester
 * @param provider              API Product Provider
 * @param exportFormat          Export format of the API meta data, could be yaml or json
 * @param isStatusPreserved     Whether API status is preserved while export
 * @param organization          Organization
 * @throws APIImportExportException If an error occurs while creating the directory or extracting the archive
 * @throws APIManagementException   If an error occurs while retrieving API related resources
 */
public static void addDependentAPIsToArchive(String archivePath, APIProductDTO apiProductDtoToReturn, ExportFormat exportFormat, APIProvider provider, String userName, Boolean isStatusPreserved, boolean preserveDocs, boolean preserveCredentials, String organization) throws APIImportExportException, APIManagementException {
    String apisDirectoryPath = archivePath + File.separator + ImportExportConstants.APIS_DIRECTORY;
    CommonUtil.createDirectory(apisDirectoryPath);
    List<ProductAPIDTO> apisList = apiProductDtoToReturn.getApis();
    for (ProductAPIDTO productAPIDTO : apisList) {
        String apiProductRequesterDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
        API api = provider.getAPIbyUUID(productAPIDTO.getApiId(), apiProductRequesterDomain);
        APIDTO apiDtoToReturn = APIMappingUtil.fromAPItoDTO(api, preserveCredentials, null);
        File dependentAPI = exportApi(provider, api.getId(), apiDtoToReturn, api, userName, exportFormat, isStatusPreserved, preserveDocs, StringUtils.EMPTY, organization);
        CommonUtil.extractArchive(dependentAPI, apisDirectoryPath);
    }
}
Also used : ProductAPIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ProductAPIDTO) APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) API(org.wso2.carbon.apimgt.api.model.API) ProductAPIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ProductAPIDTO) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) File(java.io.File)

Example 39 with APIImportExportException

use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.

the class ExportUtils method addEndpointCertificatesToArchive.

/**
 * Retrieve the endpoint certificates and store those in the archive directory.
 *
 * @param archivePath  File path to export the endpoint certificates
 * @param apiDto       API DTO to be exported
 * @param tenantId     Tenant id of the user
 * @param exportFormat Export format of file
 * @throws APIImportExportException If an error occurs while exporting endpoint certificates
 */
public static void addEndpointCertificatesToArchive(String archivePath, APIDTO apiDto, int tenantId, ExportFormat exportFormat) throws APIImportExportException {
    List<String> productionEndpoints;
    List<String> sandboxEndpoints;
    List<String> productionFailovers;
    List<String> sandboxFailovers;
    Set<String> uniqueEndpointURLs = new HashSet<>();
    JsonArray endpointCertificatesDetails = new JsonArray();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String endpointConfigString = gson.toJson(apiDto.getEndpointConfig());
    String endpointCertsDirectoryPath = archivePath + File.separator + ImportExportConstants.ENDPOINT_CERTIFICATES_DIRECTORY;
    CommonUtil.createDirectory(endpointCertsDirectoryPath);
    if (StringUtils.isEmpty(endpointConfigString) || "null".equals(endpointConfigString)) {
        if (log.isDebugEnabled()) {
            log.debug("Endpoint Details are empty for API: " + apiDto.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + apiDto.getVersion());
        }
        return;
    }
    try {
        JSONTokener tokener = new JSONTokener(endpointConfigString);
        JSONObject endpointConfig = new JSONObject(tokener);
        productionEndpoints = getEndpointURLs(endpointConfig, API_DATA_PRODUCTION_ENDPOINTS, apiDto.getName());
        sandboxEndpoints = getEndpointURLs(endpointConfig, API_DATA_SANDBOX_ENDPOINTS, apiDto.getName());
        productionFailovers = getEndpointURLs(endpointConfig, APIConstants.ENDPOINT_PRODUCTION_FAILOVERS, apiDto.getName());
        sandboxFailovers = getEndpointURLs(endpointConfig, APIConstants.ENDPOINT_SANDBOX_FAILOVERS, apiDto.getName());
        // Remove duplicate and append result
        uniqueEndpointURLs.addAll(productionEndpoints);
        uniqueEndpointURLs.addAll(sandboxEndpoints);
        uniqueEndpointURLs.addAll(productionFailovers);
        uniqueEndpointURLs.addAll(sandboxFailovers);
        for (String url : uniqueEndpointURLs) {
            JsonArray certificateListOfUrl = getEndpointCertificateContentAndMetaData(tenantId, url, endpointCertsDirectoryPath);
            endpointCertificatesDetails.addAll(certificateListOfUrl);
        }
        if (endpointCertificatesDetails.size() > 0) {
            CommonUtil.writeDtoToFile(endpointCertsDirectoryPath + ImportExportConstants.ENDPOINTS_CERTIFICATE_FILE, exportFormat, ImportExportConstants.TYPE_ENDPOINT_CERTIFICATES, endpointCertificatesDetails);
        } else if (log.isDebugEnabled()) {
            log.debug("No endpoint certificates available for API: " + apiDto.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + apiDto.getVersion() + ". Skipping certificate export.");
        }
    } catch (JSONException e) {
        throw new APIImportExportException("Error in converting Endpoint config to JSON object in API: " + apiDto.getName(), e);
    } catch (IOException e) {
        throw new APIImportExportException("Error while retrieving saving endpoint certificate details for API: " + apiDto.getName() + " as YAML", e);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) GsonBuilder(com.google.gson.GsonBuilder) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) Gson(com.google.gson.Gson) JSONException(org.json.JSONException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 40 with APIImportExportException

use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.

the class ExportUtils method addGatewayEnvironmentsToArchive.

/**
 * Retrieve the deployed gateway environments and store those in the archive directory.
 *
 * @param archivePath  File path to export the endpoint certificates
 * @param apiID        UUID of the API/ API Product
 * @param exportFormat Export format of file
 * @param apiProvider  API Provider
 * @throws APIImportExportException If an error occurs while exporting gateway environments
 */
public static void addGatewayEnvironmentsToArchive(String archivePath, String apiID, ExportFormat exportFormat, APIProvider apiProvider) throws APIManagementException {
    try {
        List<APIRevisionDeployment> deploymentsList = apiProvider.getAPIRevisionDeploymentList(apiID);
        JsonArray deploymentsArray = new JsonArray();
        for (APIRevisionDeployment deployment : deploymentsList) {
            JsonObject deploymentObject = new JsonObject();
            // Do not set vhost in deployment environment file when export API (or API Project)
            // So when importing the exported API, the default vhost of the new environment is selected.
            deploymentObject.addProperty(ImportExportConstants.DEPLOYMENT_NAME, deployment.getDeployment());
            deploymentObject.addProperty(ImportExportConstants.DISPLAY_ON_DEVPORTAL_OPTION, deployment.isDisplayOnDevportal());
            deploymentsArray.add(deploymentObject);
        }
        if (deploymentsArray.size() > 0) {
            CommonUtil.writeDtoToFile(archivePath + ImportExportConstants.DEPLOYMENT_INFO_LOCATION, exportFormat, ImportExportConstants.TYPE_DEPLOYMENT_ENVIRONMENTS, deploymentsArray);
        }
    } catch (APIImportExportException e) {
        throw new APIManagementException("Error in converting deployment environment details to JSON object in API: " + apiID, e);
    } catch (IOException e) {
        throw new APIManagementException("Error while saving deployment environment details for API: " + apiID + " as YAML", e);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) JsonObject(com.google.gson.JsonObject) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) IOException(java.io.IOException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)30 APIImportExportException (org.wso2.carbon.apimgt.impl.importexport.APIImportExportException)30 IOException (java.io.IOException)24 File (java.io.File)20 Gson (com.google.gson.Gson)11 JsonObject (com.google.gson.JsonObject)10 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)10 JsonArray (com.google.gson.JsonArray)9 JsonElement (com.google.gson.JsonElement)9 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)8 API (org.wso2.carbon.apimgt.api.model.API)6 GsonBuilder (com.google.gson.GsonBuilder)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 FileOutputStream (java.io.FileOutputStream)5 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)4 APIDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO)4 JsonParser (com.google.gson.JsonParser)3