Source:
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) { String errorMsg = "Error retrieving certificate meta data. For tenantId: " + tenantId + " hostname: " + url; throw new APIImportExportException(errorMsg, e); } Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonArray certificatesList = new JsonArray(); certificateMetadataDTOS.forEach(metadataDTO -> { ByteArrayInputStream certificate = null; try { certificate = certificateManager.getCertificateContent(metadataDTO.getAlias()); certificate.close(); byte[] certificateContent = IOUtils.toByteArray(certificate); String certificateContentEncoded = APIConstants.BEGIN_CERTIFICATE_STRING .concat(new String(Base64.encodeBase64(certificateContent))).concat("\n") .concat(APIConstants.END_CERTIFICATE_STRING); CommonUtil.writeFile(certDirectoryPath + File.separator + metadataDTO.getAlias() + ".crt", certificateContentEncoded); 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); } finally { if (certificate != null) { IOUtils.closeQuietly(certificate); } } }); return certificatesList; }

Target:
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) { String errorMsg = "Error retrieving certificate meta data. For tenantId: " + tenantId + " hostname: " + url; throw new APIImportExportException(errorMsg, 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(new String(Base64.encodeBase64(certificateContent))).concat("\n") .concat(APIConstants.END_CERTIFICATE_STRING); CommonUtil.writeFile(certDirectoryPath + File.separator + metadataDTO.getAlias() + ".crt", certificateContentEncoded); 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; }