use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.
the class ExportUtils method writeSOAPToRESTSequencesToArchive.
/**
* Retrieve SOAP to REST mediation logic for the exporting API for a particular type (in/out) and store it
* in the archive directory.
*
* @param api API
* @param sequencePathInArchive Path to the SOAP to REST sequences in the archive
* @param type Seqeunce type
* @throws APIManagementException If an error occurs while reading/writing SOAP to REST sequences
* @throws APIImportExportException If an error occurs while creating the directory
*/
private static void writeSOAPToRESTSequencesToArchive(API api, String sequencePathInArchive, String type) throws APIManagementException, APIImportExportException {
String resourcePolicy = SequenceUtils.getRestToSoapConvertedSequence(api, type);
ResourcePolicyListDTO resourcePolicyInListDTO = APIMappingUtil.fromResourcePolicyStrToDTO(resourcePolicy);
String individualSequencePathInArchive = sequencePathInArchive + File.separator + type;
CommonUtil.createDirectory(individualSequencePathInArchive);
for (ResourcePolicyInfoDTO resourcePolicyInfoDTO : resourcePolicyInListDTO.getList()) {
String sequenceContent = resourcePolicyInfoDTO.getContent();
String sequenceName = resourcePolicyInfoDTO.getResourcePath() + "_" + resourcePolicyInfoDTO.getHttpVerb();
writeSequenceToArchive(sequenceContent, individualSequencePathInArchive, sequenceName);
}
}
use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.
the class ExportUtils method addClientCertificatesToArchive.
/**
* Retrieve Mutual SSL related certificates and store those in the archive directory.
*
* @param archivePath Folder path to export client certificates
* @param identifier Identifier
* @param tenantId Tenant id of the user
* @param provider Api Provider
* @param exportFormat Export format of file
* @param organization Organization
* @throws APIImportExportException If an error occurs when writing to file or retrieving certificate metadata
*/
public static void addClientCertificatesToArchive(String archivePath, Identifier identifier, int tenantId, APIProvider provider, ExportFormat exportFormat, String organization) throws APIImportExportException {
List<ClientCertificateDTO> certificateMetadataDTOs;
try {
if (identifier instanceof APIProductIdentifier) {
certificateMetadataDTOs = provider.searchClientCertificates(tenantId, null, (APIProductIdentifier) identifier, organization);
} else {
certificateMetadataDTOs = provider.searchClientCertificates(tenantId, null, (APIIdentifier) identifier, organization);
}
if (!certificateMetadataDTOs.isEmpty()) {
String clientCertsDirectoryPath = archivePath + File.separator + ImportExportConstants.CLIENT_CERTIFICATES_DIRECTORY;
CommonUtil.createDirectory(clientCertsDirectoryPath);
JsonArray certificateList = getClientCertificateContentAndMetaData(certificateMetadataDTOs, clientCertsDirectoryPath);
if (certificateList.size() > 0) {
CommonUtil.writeDtoToFile(clientCertsDirectoryPath + ImportExportConstants.CLIENT_CERTIFICATE_FILE, exportFormat, ImportExportConstants.TYPE_CLIENT_CERTIFICATES, certificateList);
}
}
} catch (IOException e) {
throw new APIImportExportException("Error while saving as YAML or JSON", e);
} catch (APIManagementException e) {
throw new APIImportExportException("Error retrieving certificate meta data. tenantId [" + tenantId + "] api [" + tenantId + "]", e);
}
}
use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.
the class ExportUtils method exportApiProduct.
/**
* Exports an API Product from API Manager for a given API Product. MMeta information, API Product icon,
* documentation, client certificates and dependent APIs are exported.
*
* @param apiProvider API Provider
* @param apiProductIdentifier API Product Identifier
* @param apiProductDtoToReturn API Product DTO
* @param userName Username
* @param exportFormat Format of output documents. Can be YAML or JSON
* @param preserveStatus Preserve API Product status on export
* @param organization Organization Identifier
* @return
* @throws APIManagementException If an error occurs while getting governance registry
*/
public static File exportApiProduct(APIProvider apiProvider, APIProductIdentifier apiProductIdentifier, APIProductDTO apiProductDtoToReturn, String userName, ExportFormat exportFormat, Boolean preserveStatus, boolean preserveDocs, boolean preserveCredentials, String organization) throws APIManagementException, APIImportExportException {
int tenantId = 0;
// Create temp location for storing API Product data
File exportFolder = CommonUtil.createTempDirectory(apiProductIdentifier);
String exportAPIBasePath = exportFolder.toString();
String archivePath = exportAPIBasePath.concat(File.separator + apiProductIdentifier.getName() + "-" + apiProductIdentifier.getVersion());
tenantId = APIUtil.getTenantId(userName);
CommonUtil.createDirectory(archivePath);
if (preserveDocs) {
addThumbnailToArchive(archivePath, apiProductIdentifier, apiProvider);
addDocumentationToArchive(archivePath, apiProductIdentifier, exportFormat, apiProvider, APIConstants.API_PRODUCT_IDENTIFIER_TYPE);
}
// Set API Product status to created if the status is not preserved
if (!preserveStatus) {
apiProductDtoToReturn.setState(APIProductDTO.StateEnum.CREATED);
}
addGatewayEnvironmentsToArchive(archivePath, apiProductDtoToReturn.getId(), exportFormat, apiProvider);
addAPIProductMetaInformationToArchive(archivePath, apiProductDtoToReturn, exportFormat, apiProvider);
addDependentAPIsToArchive(archivePath, apiProductDtoToReturn, exportFormat, apiProvider, userName, Boolean.TRUE, preserveDocs, preserveCredentials, organization);
// Export mTLS authentication related certificates
if (log.isDebugEnabled()) {
log.debug("Mutual SSL enabled. Exporting client certificates.");
}
addClientCertificatesToArchive(archivePath, apiProductIdentifier, tenantId, apiProvider, exportFormat, organization);
CommonUtil.archiveDirectory(exportAPIBasePath);
FileUtils.deleteQuietly(new File(exportAPIBasePath));
return new File(exportAPIBasePath + APIConstants.ZIP_FILE_EXTENSION);
}
use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.
the class ExportUtils method addAPIMetaInformationToArchive.
/**
* Retrieve meta information of the API to export and store those 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 to export
* @param apiDtoToReturn API DTO to be exported
* @param exportFormat Export format of file
* @param apiProvider API Provider
* @param apiIdentifier API Identifier
* @param organization Organization Identifier
* @throws APIImportExportException If an error occurs while exporting meta information
*/
public static void addAPIMetaInformationToArchive(String archivePath, APIDTO apiDtoToReturn, ExportFormat exportFormat, APIProvider apiProvider, APIIdentifier apiIdentifier, String organization) throws APIImportExportException {
CommonUtil.createDirectory(archivePath + File.separator + ImportExportConstants.DEFINITIONS_DIRECTORY);
try {
// If a streaming API is exported, it does not contain a swagger file.
// Therefore swagger export is only required for REST or SOAP based APIs
String apiType = apiDtoToReturn.getType().toString();
API api = APIMappingUtil.fromDTOtoAPI(apiDtoToReturn, apiDtoToReturn.getProvider());
api.setOrganization(organization);
api.setId(apiIdentifier);
if (!PublisherCommonUtils.isStreamingAPI(apiDtoToReturn)) {
// For Graphql APIs, the graphql schema definition should be exported.
if (StringUtils.equals(apiType, APIConstants.APITransportType.GRAPHQL.toString())) {
String schemaContent = apiProvider.getGraphqlSchema(apiIdentifier);
CommonUtil.writeFile(archivePath + ImportExportConstants.GRAPHQL_SCHEMA_DEFINITION_LOCATION, schemaContent);
GraphqlComplexityInfo graphqlComplexityInfo = apiProvider.getComplexityDetails(apiDtoToReturn.getId());
if (graphqlComplexityInfo.getList().size() != 0) {
GraphQLQueryComplexityInfoDTO graphQLQueryComplexityInfoDTO = GraphqlQueryAnalysisMappingUtil.fromGraphqlComplexityInfotoDTO(graphqlComplexityInfo);
CommonUtil.writeDtoToFile(archivePath + ImportExportConstants.GRAPHQL_COMPLEXITY_INFO_LOCATION, exportFormat, ImportExportConstants.GRAPHQL_COMPLEXITY, graphQLQueryComplexityInfoDTO);
}
}
// For GraphQL APIs, swagger export is not needed
if (!APIConstants.APITransportType.GRAPHQL.toString().equalsIgnoreCase(apiType)) {
String formattedSwaggerJson = RestApiCommonUtil.retrieveSwaggerDefinition(api, apiProvider);
CommonUtil.writeToYamlOrJson(archivePath + ImportExportConstants.SWAGGER_DEFINITION_LOCATION, exportFormat, formattedSwaggerJson);
}
if (log.isDebugEnabled()) {
log.debug("Meta information retrieved successfully for API: " + apiDtoToReturn.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + apiDtoToReturn.getVersion());
}
} else {
String asyncApiJson = RestApiCommonUtil.retrieveAsyncAPIDefinition(api, apiProvider);
// fetching the callback URL from asyncAPI definition.
JsonParser jsonParser = new JsonParser();
JsonObject parsedObject = jsonParser.parse(asyncApiJson).getAsJsonObject();
if (parsedObject.has(ASYNC_DEFAULT_SUBSCRIBER)) {
String callBackEndpoint = parsedObject.get(ASYNC_DEFAULT_SUBSCRIBER).getAsString();
if (!StringUtils.isEmpty(callBackEndpoint)) {
// add openAPI definition to asyncAPI
String formattedSwaggerJson = RestApiCommonUtil.generateOpenAPIForAsync(apiDtoToReturn.getName(), apiDtoToReturn.getVersion(), apiDtoToReturn.getContext(), callBackEndpoint);
CommonUtil.writeToYamlOrJson(archivePath + ImportExportConstants.OPENAPI_FOR_ASYNCAPI_DEFINITION_LOCATION, exportFormat, formattedSwaggerJson);
// Adding endpoint config since adapter validates api.json for endpoint urls.
HashMap<String, Object> endpointConfig = new HashMap<>();
endpointConfig.put(API_ENDPOINT_CONFIG_PROTOCOL_TYPE, "http");
endpointConfig.put("failOver", "false");
HashMap<String, Object> productionEndpoint = new HashMap<>();
productionEndpoint.put("template_not_supported", "false");
productionEndpoint.put("url", callBackEndpoint);
HashMap<String, Object> sandboxEndpoint = new HashMap<>();
sandboxEndpoint.put("template_not_supported", "false");
sandboxEndpoint.put("url", callBackEndpoint);
endpointConfig.put(API_DATA_PRODUCTION_ENDPOINTS, productionEndpoint);
endpointConfig.put(API_DATA_SANDBOX_ENDPOINTS, sandboxEndpoint);
apiDtoToReturn.setEndpointConfig(endpointConfig);
}
}
CommonUtil.writeToYamlOrJson(archivePath + ImportExportConstants.ASYNCAPI_DEFINITION_LOCATION, exportFormat, asyncApiJson);
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement apiObj = gson.toJsonTree(apiDtoToReturn);
JsonObject apiJson = (JsonObject) apiObj;
apiJson.addProperty("organizationId", organization);
CommonUtil.writeDtoToFile(archivePath + ImportExportConstants.API_FILE_LOCATION, exportFormat, ImportExportConstants.TYPE_API, apiJson);
} catch (APIManagementException e) {
throw new APIImportExportException("Error while retrieving Swagger definition for API: " + apiDtoToReturn.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + apiDtoToReturn.getVersion(), e);
} catch (IOException e) {
throw new APIImportExportException("Error while retrieving saving as YAML for API: " + apiDtoToReturn.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + apiDtoToReturn.getVersion(), e);
}
}
use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.
the class ImportUtils method importApiProduct.
/**
* This method imports an API Product.
*
* @param extractedFolderPath Location of the extracted folder of the API Product
* @param preserveProvider Decision to keep or replace the provider
* @param overwriteAPIProduct Whether to update the API Product or not
* @param overwriteAPIs Whether to update the dependent APIs or not
* @param organization Organization Identifier
* @param importAPIs Whether to import the dependent APIs or not
* @throws APIImportExportException If there is an error in importing an API
*/
public static APIProduct importApiProduct(String extractedFolderPath, Boolean preserveProvider, Boolean rotateRevision, Boolean overwriteAPIProduct, Boolean overwriteAPIs, Boolean importAPIs, String[] tokenScopes, String organization) throws APIManagementException {
String userName = RestApiCommonUtil.getLoggedInUsername();
String currentTenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(userName));
APIProduct importedApiProduct = null;
JsonArray deploymentInfoArray = null;
String currentStatus;
String targetStatus;
String lifecycleAction;
try {
JsonElement jsonObject = retrieveValidatedDTOObject(extractedFolderPath, preserveProvider, userName, ImportExportConstants.TYPE_API_PRODUCT);
APIProductDTO importedApiProductDTO = new Gson().fromJson(jsonObject, APIProductDTO.class);
// If the provided dependent APIs params config is null, it means this happening when importing an API (not
// because when importing a dependent API of an API Product). Hence, try to retrieve the definition from
// the API folder path
JsonObject paramsConfigObject = APIControllerUtil.resolveAPIControllerEnvParams(extractedFolderPath);
// If above the params configurations are not null, then resolve those
if (paramsConfigObject != null) {
importedApiProductDTO = APIControllerUtil.injectEnvParamsToAPIProduct(importedApiProductDTO, paramsConfigObject, extractedFolderPath);
JsonElement deploymentsParam = paramsConfigObject.get(ImportExportConstants.DEPLOYMENT_ENVIRONMENTS);
if (deploymentsParam != null && !deploymentsParam.isJsonNull()) {
deploymentInfoArray = deploymentsParam.getAsJsonArray();
}
}
APIProvider apiProvider = RestApiCommonUtil.getProvider(importedApiProductDTO.getProvider());
// Check whether the API resources are valid
checkAPIProductResourcesValid(extractedFolderPath, userName, apiProvider, importedApiProductDTO, preserveProvider, organization);
targetStatus = importedApiProductDTO.getState().toString();
if (importAPIs) {
// Import dependent APIs only if it is asked (the UUIDs of the dependent APIs will be updated here if a
// fresh import happens)
importedApiProductDTO = importDependentAPIs(extractedFolderPath, userName, preserveProvider, apiProvider, overwriteAPIs, rotateRevision, importedApiProductDTO, tokenScopes, organization);
} else {
// Even we do not import APIs, the UUIDs of the dependent APIs should be updated if the APIs are
// already in the APIM
importedApiProductDTO = updateDependentApiUuids(importedApiProductDTO, apiProvider, currentTenantDomain, organization);
}
APIProduct targetApiProduct = retrieveApiProductToOverwrite(importedApiProductDTO.getName(), currentTenantDomain, apiProvider, Boolean.TRUE, organization);
// If the overwrite is set to true (which means an update), retrieve the existing API
if (Boolean.TRUE.equals(overwriteAPIProduct) && targetApiProduct != null) {
log.info("Existing API Product found, attempting to update it...");
currentStatus = targetApiProduct.getState();
importedApiProduct = PublisherCommonUtils.updateApiProduct(targetApiProduct, importedApiProductDTO, RestApiCommonUtil.getLoggedInUserProvider(), userName, currentTenantDomain);
} else {
if (targetApiProduct == null && Boolean.TRUE.equals(overwriteAPIProduct)) {
log.info("Cannot find : " + importedApiProductDTO.getName() + ". Creating it.");
}
currentStatus = APIStatus.CREATED.toString();
importedApiProduct = PublisherCommonUtils.addAPIProductWithGeneratedSwaggerDefinition(importedApiProductDTO, importedApiProductDTO.getProvider(), organization);
}
// Retrieving the life cycle action to do the lifecycle state change explicitly later
lifecycleAction = getLifeCycleAction(currentTenantDomain, currentStatus, targetStatus, apiProvider);
// Add/update swagger of API Product
importedApiProduct = updateApiProductSwagger(extractedFolderPath, importedApiProduct.getUuid(), importedApiProduct, apiProvider, currentTenantDomain);
// Since Image, documents and client certificates are optional, exceptions are logged and ignored in
// implementation
ApiTypeWrapper apiTypeWrapperWithUpdatedApiProduct = new ApiTypeWrapper(importedApiProduct);
addThumbnailImage(extractedFolderPath, apiTypeWrapperWithUpdatedApiProduct, apiProvider);
addDocumentation(extractedFolderPath, apiTypeWrapperWithUpdatedApiProduct, apiProvider, organization);
if (log.isDebugEnabled()) {
log.debug("Mutual SSL enabled. Importing client certificates.");
}
addClientCertificates(extractedFolderPath, apiProvider, preserveProvider, importedApiProduct.getId().getProviderName(), organization);
// Change API Product lifecycle if state transition is required
if (StringUtils.isNotEmpty(lifecycleAction)) {
apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
log.info("Changing lifecycle from " + currentStatus + " to " + targetStatus);
apiProvider.changeLifeCycleStatus(currentTenantDomain, new ApiTypeWrapper(importedApiProduct), lifecycleAction, new HashMap<>());
}
importedApiProduct.setState(targetStatus);
if (deploymentInfoArray == null) {
// If the params have not overwritten the deployment environments, yaml file will be read
deploymentInfoArray = retrieveDeploymentLabelsFromArchive(extractedFolderPath, false);
}
List<APIRevisionDeployment> apiProductRevisionDeployments = getValidatedDeploymentsList(deploymentInfoArray, currentTenantDomain, apiProvider, organization);
if (apiProductRevisionDeployments.size() > 0) {
String importedAPIUuid = importedApiProduct.getUuid();
String revisionId;
APIRevision apiProductRevision = new APIRevision();
apiProductRevision.setApiUUID(importedAPIUuid);
apiProductRevision.setDescription("Revision created after importing the API Product");
try {
revisionId = apiProvider.addAPIProductRevision(apiProductRevision, organization);
if (log.isDebugEnabled()) {
log.debug("A new revision has been created for API Product " + importedApiProduct.getId().getName() + "_" + importedApiProduct.getId().getVersion() + " with ID: " + revisionId);
}
} catch (APIManagementException e) {
// rotateRevision enabled, earliest revision will be deleted before creating a revision again
if (e.getErrorHandler().getErrorCode() == ExceptionCodes.from(ExceptionCodes.MAXIMUM_REVISIONS_REACHED).getErrorCode() && rotateRevision) {
String earliestRevisionUuid = apiProvider.getEarliestRevisionUUID(importedAPIUuid);
List<APIRevisionDeployment> deploymentsList = apiProvider.getAPIRevisionDeploymentList(earliestRevisionUuid);
// if the earliest revision is already deployed in gateway environments, it will be undeployed
// before deleting
apiProvider.undeployAPIProductRevisionDeployment(importedAPIUuid, earliestRevisionUuid, deploymentsList);
apiProvider.deleteAPIProductRevision(importedAPIUuid, earliestRevisionUuid, organization);
revisionId = apiProvider.addAPIProductRevision(apiProductRevision, organization);
if (log.isDebugEnabled()) {
log.debug("Revision ID: " + earliestRevisionUuid + " has been undeployed from " + deploymentsList.size() + " gateway environments and created a new revision ID: " + revisionId + " for API Product " + importedApiProduct.getId().getName() + "_" + importedApiProduct.getId().getVersion());
}
} else {
throw new APIManagementException(e);
}
}
// Once the new revision successfully created, artifacts will be deployed in mentioned gateway
// environments
apiProvider.deployAPIProductRevision(importedAPIUuid, revisionId, apiProductRevisionDeployments);
} else {
log.info("Valid deployment environments were not found for the imported artifact. Hence not deployed" + " in any of the gateway environments.");
}
return importedApiProduct;
} catch (IOException e) {
// mandatory steps
throw new APIManagementException("Error while reading API Product meta information from path: " + extractedFolderPath, e);
} catch (FaultGatewaysException e) {
throw new APIManagementException("Error while updating API Product: " + importedApiProduct.getId().getName(), e);
} catch (APIManagementException e) {
String errorMessage = "Error while importing API Product: ";
if (importedApiProduct != null) {
errorMessage += importedApiProduct.getId().getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + importedApiProduct.getId().getVersion();
}
throw new APIManagementException(errorMessage + " " + e.getMessage(), e);
}
}
Aggregations