Search in sources :

Example 11 with ExportFormat

use of org.wso2.carbon.apimgt.impl.importexport.ExportFormat 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 12 with ExportFormat

use of org.wso2.carbon.apimgt.impl.importexport.ExportFormat 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 13 with ExportFormat

use of org.wso2.carbon.apimgt.impl.importexport.ExportFormat 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 14 with ExportFormat

use of org.wso2.carbon.apimgt.impl.importexport.ExportFormat 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)

Example 15 with ExportFormat

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

the class ImportExportAPIServiceImpl method exportAPIProduct.

@Override
public File exportAPIProduct(String apiId, String revisionUUID, boolean preserveStatus, ExportFormat format, boolean preserveDocs, boolean preserveCredentials, String organization) throws APIManagementException, APIImportExportException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String userName = RestApiCommonUtil.getLoggedInUsername();
    APIProductIdentifier apiProductIdentifier = APIUtil.getAPIProductIdentifierFromUUID(apiId);
    APIProduct product = apiProvider.getAPIProductbyUUID(revisionUUID, organization);
    APIProductDTO apiProductDtoToReturn = APIMappingUtil.fromAPIProducttoDTO(product);
    return ExportUtils.exportApiProduct(apiProvider, apiProductIdentifier, apiProductDtoToReturn, userName, format, preserveStatus, preserveDocs, preserveCredentials, organization);
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

APIImportExportException (org.wso2.carbon.apimgt.impl.importexport.APIImportExportException)10 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 IOException (java.io.IOException)8 File (java.io.File)7 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)5 Gson (com.google.gson.Gson)3 GsonBuilder (com.google.gson.GsonBuilder)3 JsonArray (com.google.gson.JsonArray)3 API (org.wso2.carbon.apimgt.api.model.API)3 ExportFormat (org.wso2.carbon.apimgt.impl.importexport.ExportFormat)3 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)3 JsonObject (com.google.gson.JsonObject)2 JSONObject (org.json.JSONObject)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)2 APIDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO)2 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)2 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1