|
|
@ -18,13 +18,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
package io.entgra.device.mgt.core.certificate.mgt.core.dao.impl;
|
|
|
|
package io.entgra.device.mgt.core.certificate.mgt.core.dao.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.CertificatePaginationRequest;
|
|
|
|
import io.entgra.device.mgt.core.certificate.mgt.core.dto.CertificateResponse;
|
|
|
|
import io.entgra.device.mgt.core.certificate.mgt.core.dto.CertificateResponse;
|
|
|
|
import io.entgra.device.mgt.core.certificate.mgt.core.impl.CertificateGenerator;
|
|
|
|
import io.entgra.device.mgt.core.certificate.mgt.core.impl.CertificateGenerator;
|
|
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOException;
|
|
|
|
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOException;
|
|
|
|
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOFactory;
|
|
|
|
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOFactory;
|
|
|
|
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOUtil;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult;
|
|
|
|
import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
|
|
|
|
|
|
@ -44,45 +45,75 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
|
|
|
|
private static final Log log = LogFactory.getLog(SQLServerCertificateDAOImpl.class);
|
|
|
|
private static final Log log = LogFactory.getLog(SQLServerCertificateDAOImpl.class);
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public PaginationResult getAllCertificates(int rowNum, int limit) throws CertificateManagementDAOException {
|
|
|
|
public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException {
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
ResultSet resultSet = null;
|
|
|
|
|
|
|
|
CertificateResponse certificateResponse;
|
|
|
|
CertificateResponse certificateResponse;
|
|
|
|
List<CertificateResponse> certificates = new ArrayList<>();
|
|
|
|
List<CertificateResponse> certificates = new ArrayList<>();
|
|
|
|
PaginationResult paginationResult;
|
|
|
|
PaginationResult paginationResult;
|
|
|
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
String serialNumber = request.getSerialNumber();
|
|
|
|
|
|
|
|
String deviceIdentifier = request.getDeviceIdentifier();
|
|
|
|
|
|
|
|
String username = request.getUsername();
|
|
|
|
|
|
|
|
boolean isCertificateSerialNumberProvided = false;
|
|
|
|
|
|
|
|
boolean isCertificateDeviceIdentifierProvided = false;
|
|
|
|
|
|
|
|
boolean isCertificateUsernameProvided = false;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Connection conn = this.getConnection();
|
|
|
|
Connection conn = this.getConnection();
|
|
|
|
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, ID, DEVICE_IDENTIFIER, TENANT_ID, USERNAME FROM "
|
|
|
|
String query = "SELECT * " +
|
|
|
|
+ "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
|
|
|
|
"FROM DM_DEVICE_CERTIFICATE " +
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
"WHERE TENANT_ID = ? ";
|
|
|
|
stmt.setInt(1, tenantId);
|
|
|
|
if (StringUtils.isNotEmpty(serialNumber)) {
|
|
|
|
stmt.setInt(2, rowNum);
|
|
|
|
query += "AND SERIAL_NUMBER = ? ";
|
|
|
|
stmt.setInt(3, limit);
|
|
|
|
isCertificateSerialNumberProvided = true;
|
|
|
|
resultSet = stmt.executeQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int resultCount = 0;
|
|
|
|
if (StringUtils.isNotEmpty(deviceIdentifier)) {
|
|
|
|
while (resultSet.next()) {
|
|
|
|
query += "AND DEVICE_IDENTIFIER = ? ";
|
|
|
|
certificateResponse = new CertificateResponse();
|
|
|
|
isCertificateDeviceIdentifierProvided = true;
|
|
|
|
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
|
|
|
|
}
|
|
|
|
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
|
|
|
|
|
|
|
|
certificateResponse.setCertificateId(resultSet.getString("ID"));
|
|
|
|
if (StringUtils.isNotEmpty(username)) {
|
|
|
|
certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER"));
|
|
|
|
query += "AND USERNAME LIKE ? ";
|
|
|
|
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
|
|
|
|
isCertificateUsernameProvided = true;
|
|
|
|
certificateResponse.setUsername(resultSet.getString("USERNAME"));
|
|
|
|
}
|
|
|
|
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
|
|
|
|
|
|
|
|
certificates.add(certificateResponse);
|
|
|
|
query += "ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
|
|
|
|
resultCount++;
|
|
|
|
|
|
|
|
|
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(query)) {
|
|
|
|
|
|
|
|
int paramIdx = 1;
|
|
|
|
|
|
|
|
stmt.setInt(paramIdx++, tenantId);
|
|
|
|
|
|
|
|
if (isCertificateSerialNumberProvided) {
|
|
|
|
|
|
|
|
stmt.setString(paramIdx++, serialNumber);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCertificateDeviceIdentifierProvided) {
|
|
|
|
|
|
|
|
stmt.setString(paramIdx++, deviceIdentifier);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCertificateUsernameProvided) {
|
|
|
|
|
|
|
|
stmt.setString(paramIdx++, "%" + username + "%");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt.setInt(paramIdx++, request.getStartIndex());
|
|
|
|
|
|
|
|
stmt.setInt(paramIdx++, request.getRowCount());
|
|
|
|
|
|
|
|
try (ResultSet resultSet = stmt.executeQuery()) {
|
|
|
|
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
|
|
|
certificateResponse = new CertificateResponse();
|
|
|
|
|
|
|
|
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
|
|
|
|
|
|
|
|
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
|
|
|
|
|
|
|
|
certificateResponse.setCertificateId(resultSet.getString("ID"));
|
|
|
|
|
|
|
|
certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER"));
|
|
|
|
|
|
|
|
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
|
|
|
|
|
|
|
|
certificateResponse.setUsername(resultSet.getString("USERNAME"));
|
|
|
|
|
|
|
|
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
|
|
|
|
|
|
|
|
certificates.add(certificateResponse);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
paginationResult = new PaginationResult();
|
|
|
|
|
|
|
|
paginationResult.setData(certificates);
|
|
|
|
|
|
|
|
paginationResult.setRecordsTotal(this.getCertificateCount(request));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
paginationResult = new PaginationResult();
|
|
|
|
|
|
|
|
paginationResult.setData(certificates);
|
|
|
|
|
|
|
|
paginationResult.setRecordsTotal(this.getCertificateCount(tenantId));
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
} catch (SQLException e) {
|
|
|
|
String errorMsg = "SQL error occurred while retrieving the certificates.";
|
|
|
|
String errorMsg = "SQL error occurred while retrieving the certificates.";
|
|
|
|
log.error(errorMsg, e);
|
|
|
|
log.error(errorMsg, e);
|
|
|
|
throw new CertificateManagementDAOException(errorMsg, e);
|
|
|
|
throw new CertificateManagementDAOException(errorMsg, e);
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paginationResult;
|
|
|
|
return paginationResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -91,14 +122,47 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
|
|
|
|
return CertificateManagementDAOFactory.getConnection();
|
|
|
|
return CertificateManagementDAOFactory.getConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private int getCertificateCount(int tenantId) throws CertificateManagementDAOException, SQLException {
|
|
|
|
private int getCertificateCount(CertificatePaginationRequest request) throws CertificateManagementDAOException {
|
|
|
|
int certificateCount = 0;
|
|
|
|
int certificateCount = 0;
|
|
|
|
|
|
|
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
|
|
|
|
String serialNumber = request.getSerialNumber();
|
|
|
|
|
|
|
|
String deviceIdentifier = request.getDeviceIdentifier();
|
|
|
|
|
|
|
|
String username = request.getUsername();
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Connection conn = this.getConnection();
|
|
|
|
Connection conn = this.getConnection();
|
|
|
|
String sql =
|
|
|
|
String sql = "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT " +
|
|
|
|
"SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?";
|
|
|
|
"FROM DM_DEVICE_CERTIFICATE " +
|
|
|
|
|
|
|
|
"WHERE TENANT_ID = ?";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotEmpty(serialNumber)) {
|
|
|
|
|
|
|
|
sql += " AND SERIAL_NUMBER = ?";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotEmpty(deviceIdentifier)) {
|
|
|
|
|
|
|
|
sql += " AND DEVICE_IDENTIFIER = ?";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotEmpty(username)) {
|
|
|
|
|
|
|
|
sql += " AND USERNAME LIKE ?";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
|
|
|
stmt.setInt(1, tenantId);
|
|
|
|
stmt.setInt(1, tenantId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int paramIdx = 2;
|
|
|
|
|
|
|
|
if (StringUtils.isNotEmpty(serialNumber)) {
|
|
|
|
|
|
|
|
stmt.setString(paramIdx++, serialNumber);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotEmpty(deviceIdentifier)) {
|
|
|
|
|
|
|
|
stmt.setString(paramIdx++, deviceIdentifier);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotEmpty(username)) {
|
|
|
|
|
|
|
|
stmt.setString(paramIdx, "%" + username + "%");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try (ResultSet rs = stmt.executeQuery()) {
|
|
|
|
try (ResultSet rs = stmt.executeQuery()) {
|
|
|
|
if (rs.next()) {
|
|
|
|
if (rs.next()) {
|
|
|
|
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
|
|
|
|
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
|
|
|
@ -106,10 +170,11 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
} catch (SQLException e) {
|
|
|
|
String errorMsg = "SQL error occurred while retrieving the certificates.";
|
|
|
|
String errorMsg = "SQL error occurred while retrieving the certificate count.";
|
|
|
|
log.error(errorMsg, e);
|
|
|
|
log.error(errorMsg, e);
|
|
|
|
throw new CertificateManagementDAOException(errorMsg, e);
|
|
|
|
throw new CertificateManagementDAOException(errorMsg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return certificateCount;
|
|
|
|
return certificateCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|