use of org.wso2.carbon.apimgt.impl.importexport.ExportFormat in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method exportAPIProduct.
/**
* Exports an API Product from API Manager. Meta information, API Product icon, documentation, client certificates
* and dependent APIs are exported. This service generates a zipped archive which contains all the above mentioned
* resources for a given API Product.
*
* @param name Name of the API Product that needs to be exported
* @param version Version of the API Product that needs to be exported
* @param providerName Provider name of the API Product that needs to be exported
* @param format Format of output documents. Can be YAML or JSON
* @param preserveStatus Preserve API Product status on export
* @param messageContext Message Context
* @return Zipped file containing exported API Product
* @throws APIManagementException
*/
@Override
public Response exportAPIProduct(String name, String version, String providerName, String revisionNumber, String format, Boolean preserveStatus, Boolean exportLatestRevision, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// If not specified status is preserved by default
preserveStatus = preserveStatus == null || preserveStatus;
// Default export format is YAML
ExportFormat exportFormat = StringUtils.isNotEmpty(format) ? ExportFormat.valueOf(format.toUpperCase()) : ExportFormat.YAML;
ImportExportAPI importExportAPI = APIImportExportUtil.getImportExportAPI();
try {
File file = importExportAPI.exportAPIProduct(null, name, version, providerName, revisionNumber, exportFormat, preserveStatus, true, true, exportLatestRevision, organization);
return Response.ok(file).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"").build();
} catch (APIImportExportException e) {
throw new APIManagementException("Error while exporting " + RestApiConstants.RESOURCE_API_PRODUCT, e);
}
}
use of org.wso2.carbon.apimgt.impl.importexport.ExportFormat in project carbon-apimgt by wso2.
the class ExportUtils method exportApplication.
/**
* Export a given Application to a file system as zip archive.
* The export root location is given by {@link @path}/exported-application.
*
* @param exportApplication Application{@link Application} to be exported
* @param apiConsumer API Consumer
* @param exportFormat Format to export
* @param withKeys Export the Application with keys or not
* @return Path to the exported directory with exported artifacts
* @throws APIManagementException If an error occurs while exporting an application to a file system
*/
public static File exportApplication(Application exportApplication, APIConsumer apiConsumer, ExportFormat exportFormat, Boolean withKeys) throws APIManagementException {
String archivePath = null;
String exportApplicationBasePath;
String appName = exportApplication.getName();
String appOwner = exportApplication.getOwner();
try {
// Creates a temporary directory to store the exported application artifact
File exportFolder = createTempApplicationDirectory(appName, appOwner);
exportApplicationBasePath = exportFolder.toString();
archivePath = exportApplicationBasePath.concat(File.separator + appOwner.replace(File.separator, "#") + "-" + appName);
} catch (APIImportExportException e) {
throw new APIManagementException("Unable to create the temporary directory to export the Application", e);
}
ExportedApplication applicationDtoToExport = createApplicationDTOToExport(exportApplication, apiConsumer, withKeys);
try {
createDirectory(archivePath);
// Export application details
CommonUtil.writeDtoToFile(archivePath + File.separator + ImportExportConstants.TYPE_APPLICATION, exportFormat, ImportExportConstants.TYPE_APPLICATION, applicationDtoToExport);
CommonUtil.archiveDirectory(exportApplicationBasePath);
FileUtils.deleteQuietly(new File(exportApplicationBasePath));
return new File(exportApplicationBasePath + APIConstants.ZIP_FILE_EXTENSION);
} catch (IOException | APIImportExportException e) {
throw new APIManagementException("Error while exporting Application: " + exportApplication.getName(), e);
}
}
use of org.wso2.carbon.apimgt.impl.importexport.ExportFormat in project carbon-apimgt by wso2.
the class ExportUtils method addDocumentationToArchive.
/**
* Retrieve documentation for the exporting API or API Product and store it in the archive directory.
* FILE, INLINE, MARKDOWN and URL documentations are handled.
*
* @param archivePath File path to export the documents
* @param identifier ID of the requesting API or API Product
* @param exportFormat Format for export
* @param apiProvider API Provider
* @param type Type of the Project (whether an API or an API Product)
* @throws APIImportExportException If an error occurs while retrieving documents from the
* registry or storing in the archive directory
* @throws APIManagementException If an error occurs while retrieving document details
*/
public static void addDocumentationToArchive(String archivePath, Identifier identifier, ExportFormat exportFormat, APIProvider apiProvider, String type) throws APIImportExportException, APIManagementException {
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
List<Documentation> docList = StringUtils.equals(type, APIConstants.API_IDENTIFIER_TYPE) ? apiProvider.getAllDocumentation(identifier.getUUID(), tenantDomain) : apiProvider.getAllDocumentation(identifier);
if (!docList.isEmpty()) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String docDirectoryPath = archivePath + File.separator + ImportExportConstants.DOCUMENT_DIRECTORY;
CommonUtil.createDirectory(docDirectoryPath);
try {
for (Documentation doc : docList) {
// Retrieving the document again since objects in docList might have missing fields
Documentation individualDocument = apiProvider.getDocumentation(identifier.getUUID(), doc.getId(), tenantDomain);
String sourceType = individualDocument.getSourceType().name();
String resourcePath = null;
InputStream inputStream = null;
String localFileName = null;
String individualDocDirectoryPath = docDirectoryPath + File.separator + cleanFolderName(individualDocument.getName());
CommonUtil.createDirectory(individualDocDirectoryPath);
DocumentationContent documentationContent = apiProvider.getDocumentationContent(identifier.getUUID(), doc.getId(), tenantDomain);
if (documentationContent != null) {
if (Documentation.DocumentSourceType.FILE.toString().equalsIgnoreCase(sourceType)) {
localFileName = individualDocument.getFilePath().substring(individualDocument.getFilePath().lastIndexOf(RegistryConstants.PATH_SEPARATOR) + 1);
inputStream = documentationContent.getResourceFile().getContent();
individualDocument.setFilePath(localFileName);
} else if (Documentation.DocumentSourceType.INLINE.toString().equalsIgnoreCase(sourceType) || Documentation.DocumentSourceType.MARKDOWN.toString().equalsIgnoreCase(sourceType)) {
// Inline/Markdown content file name would be same as the documentation name
localFileName = individualDocument.getName();
inputStream = new ByteArrayInputStream(documentationContent.getTextContent().getBytes());
}
}
CommonUtil.writeDtoToFile(individualDocDirectoryPath + ImportExportConstants.DOCUMENT_FILE_NAME, exportFormat, ImportExportConstants.TYPE_DOCUMENTS, DocumentationMappingUtil.fromDocumentationToDTO(individualDocument));
if (inputStream != null) {
// Check whether resource exists in the registry
try (OutputStream outputStream = new FileOutputStream(individualDocDirectoryPath + File.separator + localFileName)) {
IOUtils.copy(inputStream, outputStream);
}
} else {
// Log error and avoid throwing as we give capability to export document artifact without
// the content if does not exists
log.error("Documentation resource for API/API Product: " + identifier.getName() + " not found in " + resourcePath);
}
}
if (log.isDebugEnabled()) {
log.debug("Documentation retrieved successfully for API/API Product: " + identifier.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + identifier.getVersion());
}
} catch (IOException e) {
throw new APIImportExportException("I/O error while writing documentation to file for API/API Product: " + identifier.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + identifier.getVersion(), e);
}
} else if (log.isDebugEnabled()) {
log.debug("No documentation found for API/API Product: " + identifier + ". Skipping documentation export.");
}
}
use of org.wso2.carbon.apimgt.impl.importexport.ExportFormat in project carbon-apimgt by wso2.
the class ExportUtils method exportApi.
/**
* Exports an API from API Manager for a given API. Meta information, API icon, documentation,
* WSDL and sequences are exported.
*
* @param apiProvider API Provider
* @param apiIdentifier API Identifier
* @param apiDtoToReturn API DTO
* @param userName Username
* @param exportFormat Format of output documents. Can be YAML or JSON
* @param preserveStatus Preserve API status on export
* @param preserveDocs Preserve documentation on Export.
* @param originalDevPortalUrl Original DevPortal URL (redirect URL) for the original Store
* (This is used for advertise only APIs).
* @param organization Organization
* @return
* @throws APIManagementException If an error occurs while getting governance registry
*/
public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifier, APIDTO apiDtoToReturn, API api, String userName, ExportFormat exportFormat, boolean preserveStatus, boolean preserveDocs, String originalDevPortalUrl, String organization) throws APIManagementException, APIImportExportException {
int tenantId;
// If explicitly advertise only property has been specified as true, make it true and update the API DTO.
if (StringUtils.isNotBlank(originalDevPortalUrl)) {
setAdvertiseOnlySpecificPropertiesToDTO(apiDtoToReturn, originalDevPortalUrl);
}
// Create temp location for storing API data
File exportFolder = CommonUtil.createTempDirectory(apiIdentifier);
String exportAPIBasePath = exportFolder.toString();
String archivePath = exportAPIBasePath.concat(File.separator + apiIdentifier.getApiName() + "-" + apiIdentifier.getVersion());
tenantId = APIUtil.getTenantId(userName);
CommonUtil.createDirectory(archivePath);
if (preserveDocs) {
addThumbnailToArchive(archivePath, apiIdentifier, apiProvider);
addDocumentationToArchive(archivePath, apiIdentifier, exportFormat, apiProvider, APIConstants.API_IDENTIFIER_TYPE);
} else {
if (StringUtils.equals(apiDtoToReturn.getType().toString().toLowerCase(), APIConstants.API_TYPE_SOAPTOREST.toLowerCase())) {
addSOAPToRESTMediationToArchive(archivePath, api);
}
}
if (StringUtils.equals(apiDtoToReturn.getType().toString().toLowerCase(), APIConstants.API_TYPE_SOAP.toLowerCase()) && preserveDocs) {
addWSDLtoArchive(archivePath, apiIdentifier, apiProvider);
} else if (log.isDebugEnabled()) {
log.debug("No WSDL URL found for API: " + apiIdentifier + ". Skipping WSDL export.");
}
// Set API status to created if the status is not preserved
if (!preserveStatus) {
apiDtoToReturn.setLifeCycleStatus(APIConstants.CREATED);
}
String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
addOperationPoliciesToArchive(archivePath, apiDtoToReturn.getId(), tenantDomain, exportFormat, apiProvider, api);
addGatewayEnvironmentsToArchive(archivePath, apiDtoToReturn.getId(), exportFormat, apiProvider);
if (!ImportUtils.isAdvertiseOnlyAPI(apiDtoToReturn)) {
addEndpointCertificatesToArchive(archivePath, apiDtoToReturn, tenantId, exportFormat);
addRuntimeSequencesToArchive(archivePath, api);
if (preserveDocs) {
addMultipleAPISpecificSequencesToArchive(archivePath, api, apiProvider);
}
// Export mTLS authentication related certificates
if (log.isDebugEnabled()) {
log.debug("Mutual SSL enabled. Exporting client certificates.");
}
addClientCertificatesToArchive(archivePath, apiIdentifier, tenantId, apiProvider, exportFormat, organization);
}
addAPIMetaInformationToArchive(archivePath, apiDtoToReturn, exportFormat, apiProvider, apiIdentifier, 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.ExportFormat in project carbon-apimgt by wso2.
the class ExportUtils method addOperationPoliciesToArchive.
/**
* Retrieve the operation policies 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 operation policies
*/
public static void addOperationPoliciesToArchive(String archivePath, String apiID, String tenantDomain, ExportFormat exportFormat, APIProvider apiProvider, API api) throws APIManagementException {
try {
CommonUtil.createDirectory(archivePath + File.separator + ImportExportConstants.POLICIES_DIRECTORY);
Set<URITemplate> uriTemplates = api.getUriTemplates();
for (URITemplate uriTemplate : uriTemplates) {
List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();
if (operationPolicies != null && !operationPolicies.isEmpty()) {
for (OperationPolicy policy : operationPolicies) {
OperationPolicyData policyData = apiProvider.getAPISpecificOperationPolicyByPolicyId(policy.getPolicyId(), apiID, tenantDomain, true);
if (policyData != null) {
String policyName = archivePath + File.separator + ImportExportConstants.POLICIES_DIRECTORY + File.separator + policyData.getSpecification().getName();
// Policy specification and definition will have the same name
if (policyData.getSpecification() != null) {
CommonUtil.writeDtoToFile(policyName, exportFormat, ImportExportConstants.TYPE_POLICY_SPECIFICATION, policyData.getSpecification());
}
if (policyData.getSynapsePolicyDefinition() != null) {
CommonUtil.writeFile(policyName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION, policyData.getSynapsePolicyDefinition().getContent());
}
if (policyData.getCcPolicyDefinition() != null) {
CommonUtil.writeFile(policyName + APIConstants.CC_POLICY_DEFINITION_EXTENSION, policyData.getCcPolicyDefinition().getContent());
}
}
}
}
}
} catch (IOException | APIImportExportException e) {
throw new APIManagementException("Error while adding operation policy details for API: " + apiID, e);
}
}
Aggregations