mharindu 9 years ago
commit a3741bda7e

@ -72,4 +72,6 @@ public interface CertificateDAO {
* @return whether the certificate was removed or not.
*/
boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException;
public List<CertificateResponse> searchCertificate(String serialNumber) throws CertificateManagementDAOException;
}

@ -116,6 +116,44 @@ public class GenericCertificateDAOImpl implements CertificateDAO {
return certificateResponse;
}
@Override
public List<CertificateResponse> searchCertificate(String serialNumber)
throws CertificateManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
CertificateResponse certificateResponse = null;
List<CertificateResponse> certificates = new ArrayList<>();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query =
"SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID FROM DM_DEVICE_CERTIFICATE WHERE SERIAL_NUMBER LIKE ?" +
" AND TENANT_ID = ? ";
stmt = conn.prepareStatement(query);
stmt.setString(1, "%" + serialNumber + "%");
stmt.setInt(2, tenantId);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
certificateResponse = new CertificateResponse();
byte [] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
certificates.add(certificateResponse);
}
} catch (SQLException e) {
String errorMsg =
"Unable to get the read the certificate with serial" + serialNumber;
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
}
return certificates;
}
@Override
public PaginationResult getAllCertificates(PaginationRequest request) throws CertificateManagementDAOException {
PreparedStatement stmt = null;

@ -76,4 +76,6 @@ public interface CertificateManagementService {
boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException;
public List<CertificateResponse> getCertificates() throws CertificateManagementDAOException;
public List<CertificateResponse> searchCertificates(String serialNumber) throws CertificateManagementDAOException;
}

@ -192,4 +192,18 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe
}
}
@Override public List<CertificateResponse> searchCertificates(String serialNumber) throws CertificateManagementDAOException {
try {
CertificateManagementDAOFactory.openConnection();
CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO();
return certificateDAO.searchCertificate(serialNumber);
} catch (SQLException e) {
String errorMsg = "Error when opening connection";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOFactory.closeConnection();
}
}
}

Loading…
Cancel
Save