Search in sources :

Example 6 with CertificateManager

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;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) CertificateManager(org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManager) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) ByteArrayInputStream(java.io.ByteArrayInputStream) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 CertificateManager (org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManager)3 IOException (java.io.IOException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 OMElement (org.apache.axiom.om.OMElement)2 AxisFault (org.apache.axis2.AxisFault)2 EndpointAdminServiceProxy (org.wso2.carbon.apimgt.gateway.utils.EndpointAdminServiceProxy)2 LocalEntryServiceProxy (org.wso2.carbon.apimgt.gateway.utils.LocalEntryServiceProxy)2 MediationSecurityAdminServiceProxy (org.wso2.carbon.apimgt.gateway.utils.MediationSecurityAdminServiceProxy)2 RESTAPIAdminServiceProxy (org.wso2.carbon.apimgt.gateway.utils.RESTAPIAdminServiceProxy)2 SequenceAdminServiceProxy (org.wso2.carbon.apimgt.gateway.utils.SequenceAdminServiceProxy)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 Modifier (java.lang.reflect.Modifier)1