use of org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManager in project carbon-apimgt by wso2.
the class ExportUtils method getEndpointCertificateContentAndMetaData.
/**
* Get Endpoint Certificate MetaData and Certificate detail and build JSON Array.
*
* @param tenantId Tenant id of the user
* @param url Url of the endpoint
* @param certDirectoryPath Directory path to export the certificates
* @return JSON Array of certificate details
* @throws APIImportExportException If an error occurs while retrieving endpoint certificate metadata and content
*/
private static JsonArray getEndpointCertificateContentAndMetaData(int tenantId, String url, String certDirectoryPath) throws APIImportExportException {
List<CertificateMetadataDTO> certificateMetadataDTOS;
CertificateManager certificateManager = CertificateManagerImpl.getInstance();
try {
certificateMetadataDTOS = certificateManager.getCertificates(tenantId, null, url);
} catch (APIManagementException e) {
throw new APIImportExportException("Error retrieving certificate meta data. For tenantId: " + tenantId + " hostname: " + url, e);
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonArray certificatesList = new JsonArray();
certificateMetadataDTOS.forEach(metadataDTO -> {
try (ByteArrayInputStream certificate = certificateManager.getCertificateContent(metadataDTO.getAlias())) {
byte[] certificateContent = IOUtils.toByteArray(certificate);
String certificateContentEncoded = APIConstants.BEGIN_CERTIFICATE_STRING.concat(System.lineSeparator()).concat(new String(Base64.encodeBase64(certificateContent))).concat(System.lineSeparator()).concat(APIConstants.END_CERTIFICATE_STRING);
CommonUtil.writeFile(certDirectoryPath + File.separator + metadataDTO.getAlias() + ".crt", certificateContentEncoded);
// Add the file name to the Certificate Metadata
JsonObject modifiedCertificateMetadata = (JsonObject) gson.toJsonTree(metadataDTO);
modifiedCertificateMetadata.addProperty(ImportExportConstants.CERTIFICATE_FILE, metadataDTO.getAlias() + ".crt");
certificatesList.add(modifiedCertificateMetadata);
} catch (APIManagementException e) {
log.error("Error retrieving certificate content. For tenantId: " + tenantId + " hostname: " + url + " alias: " + metadataDTO.getAlias(), e);
} catch (IOException e) {
log.error("Error while converting certificate content to Byte Array. For tenantId: " + tenantId + " hostname: " + url + " alias: " + metadataDTO.getAlias(), e);
} catch (APIImportExportException e) {
log.error("Error while writing the certificate content. For tenantId: " + tenantId + " hostname: " + url + " alias: " + metadataDTO.getAlias(), e);
}
});
return certificatesList;
}
Aggregations