use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException in project carbon-apimgt by wso2.
the class CertificateMgtDAO method getCertificates.
/**
* Method to retrieve certificate metadata from db for specific tenant which matches alias or endpoint.
* From alias and endpoint, only one parameter is required.
*
* @param tenantId : The id of the tenant which the certificate belongs to.
* @param alias : Alias for the certificate. (Optional)
* @param endpoint : The endpoint/ server url which the certificate is mapped to. (Optional)
* @return : A CertificateMetadataDTO object if the certificate is retrieved successfully, null otherwise.
*/
public List<CertificateMetadataDTO> getCertificates(String alias, String endpoint, int tenantId) throws CertificateManagementException {
String getCertQuery;
CertificateMetadataDTO certificateMetadataDTO;
List<CertificateMetadataDTO> certificateMetadataList = new ArrayList<>();
if (StringUtils.isNotEmpty(alias) || StringUtils.isNotEmpty(endpoint)) {
if (log.isDebugEnabled()) {
log.debug("The alias and endpoint are not empty. Invoking the search query with parameters " + "alias = " + alias + " endpoint = " + endpoint);
}
getCertQuery = SQLConstants.CertificateConstants.GET_CERTIFICATE_TENANT;
} else {
if (log.isDebugEnabled()) {
log.debug("The alias and endpoint are empty. Invoking the get all certificates for tenant " + tenantId);
}
getCertQuery = SQLConstants.CertificateConstants.GET_CERTIFICATES;
}
try (Connection connection = APIMgtDBUtil.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(getCertQuery)) {
preparedStatement.setInt(1, tenantId);
if (StringUtils.isNotEmpty(alias) || StringUtils.isNotEmpty(endpoint)) {
preparedStatement.setString(2, alias);
preparedStatement.setString(3, endpoint);
}
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
certificateMetadataDTO = new CertificateMetadataDTO();
certificateMetadataDTO.setAlias(resultSet.getString("ALIAS"));
certificateMetadataDTO.setEndpoint(resultSet.getString("END_POINT"));
try (InputStream certificate = resultSet.getBinaryStream("CERTIFICATE")) {
certificateMetadataDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(certificate));
}
certificateMetadataList.add(certificateMetadataDTO);
}
}
}
} catch (SQLException | IOException e) {
handleException("Error while retrieving certificate metadata.", e);
}
return certificateMetadataList;
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException in project carbon-apimgt by wso2.
the class CertificateMgtUtilTest method testGetCertificateInformation.
@Test
public void testGetCertificateInformation() throws CertificateManagementException {
certificateMgtUtils.addCertificateToTrustStore(BASE64_ENCODED_CERT_STRING, ALIAS);
CertificateInformationDTO certificateInformationDTO = certificateMgtUtils.getCertificateInformation(ALIAS);
Assert.assertNotNull(certificateInformationDTO);
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException in project carbon-apimgt by wso2.
the class CertificateMgtUtilTest method testUpdateCertificate.
@Test
public void testUpdateCertificate() throws CertificateManagementException {
ResponseCode responseCode = certificateMgtUtils.updateCertificate(BASE64_ENCODED_CERT_STRING, ALIAS);
Assert.assertEquals(ResponseCode.SUCCESS, responseCode);
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException in project carbon-apimgt by wso2.
the class CertificateMgtUtilTest method testUpdateCertificateWithExpiredCertificate.
@Test
public void testUpdateCertificateWithExpiredCertificate() throws CertificateManagementException {
ResponseCode responseCode = certificateMgtUtils.updateCertificate(EXPIRED_CERTIFICATE, ALIAS);
Assert.assertEquals(ResponseCode.CERTIFICATE_EXPIRED, responseCode);
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException in project carbon-apimgt by wso2.
the class CertificateMgtUtilTest method testUpdateCertificateWithEmptyCertificate.
@Test
public void testUpdateCertificateWithEmptyCertificate() throws CertificateManagementException {
ResponseCode responseCode = certificateMgtUtils.updateCertificate("", ALIAS);
Assert.assertEquals(ResponseCode.INTERNAL_SERVER_ERROR, responseCode);
}
Aggregations