Add improvements to certificate component

location-fix
Navod Zoysa 1 year ago
parent 017c70fa08
commit dbc73d18ed

@ -311,20 +311,17 @@ public interface CertificateManagementAdminService {
@ApiParam(
name = "serialNumber",
value = "The serial number of the certificates",
required = false,
defaultValue = "0")
required = false)
@QueryParam("serialNumber") String serialNumber,
@ApiParam(
name = "deviceIdentifier",
value = "The device identifier of the certificates",
required = false,
defaultValue = "0")
required = false)
@QueryParam("deviceIdentifier") String deviceIdentifier,
@ApiParam(
name = "username",
value = "User name of the certificate added user",
required = false,
defaultValue = "0")
required = false)
@QueryParam("username") String username,
@ApiParam(
name = "If-Modified-Since",

@ -27,6 +27,7 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import io.entgra.device.mgt.core.device.mgt.common.CertificatePaginationRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import io.entgra.device.mgt.core.certificate.mgt.cert.admin.api.CertificateManagementAdminService;
@ -138,20 +139,23 @@ public class CertificateManagementAdminServiceImpl implements CertificateManagem
*/
@GET
public Response getAllCertificates(
@QueryParam("serialNumber") String serialNumber, @QueryParam("deviceIdentifier") String deviceIdentifier,
@QueryParam("username") String username, @HeaderParam("If-Modified-Since") String ifModifiedSince, @QueryParam("offset") int offset,
@QueryParam("serialNumber") String serialNumber,
@QueryParam("deviceIdentifier") String deviceIdentifier,
@QueryParam("username") String username,
@HeaderParam("If-Modified-Since") String ifModifiedSince,
@QueryParam("offset") int offset,
@QueryParam("limit") int limit) {
RequestValidationUtil.validatePaginationInfo(offset, limit);
CertificateManagementService certificateService = CertificateMgtAPIUtils.getCertificateManagementService();
CertificatePaginationRequest request = new CertificatePaginationRequest(offset, limit);
if (serialNumber != null && !serialNumber.isEmpty()) {
if (StringUtils.isNotEmpty(serialNumber)) {
request.setSerialNumber(serialNumber);
}
if (deviceIdentifier != null){
if (StringUtils.isNotEmpty(deviceIdentifier)){
request.setDeviceIdentifier(deviceIdentifier);
}
if (username != null){
if (StringUtils.isNotEmpty(username)){
request.setUsername(username);
}
try {
@ -188,18 +192,19 @@ public class CertificateManagementAdminServiceImpl implements CertificateManagem
serialNumber + "' has been removed").build();
}
} catch (CertificateManagementException e) {
String msg = "Error occurred while converting PEM file to X509Certificate";
String msg = "Error occurred while removing certificate with the given " +
"serial number '" + serialNumber + "'";
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
} else {
return Response.status(Response.Status.NOT_FOUND).entity(
"User not have to access delete certificate " +
return Response.status(Response.Status.UNAUTHORIZED).entity(
"User unauthorized to delete certificate with " +
"serial number '" + serialNumber + "'").build();
}
} catch (CertificateManagementException e) {
String msg = "Error occurred while converting PEM file to X509Certificate";
String msg = "Error occurred while getting the metadata entry for certificate deletion.";
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();

@ -56,6 +56,7 @@
<Import-Package>
org.osgi.framework.*;version="${imp.package.version.osgi.framework}",
org.osgi.service.*;version="${imp.package.version.osgi.service}",
org.apache.commons.lang,
org.apache.commons.logging,
org.apache.commons.collections.map,
javax.security.auth.x500,

@ -21,11 +21,11 @@ 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.impl.CertificateGenerator;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
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.CertificateManagementDAOFactory;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOUtil;
import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult;
import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -55,66 +55,58 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
String serialNumber = request.getSerialNumber();
String deviceIdentifier = request.getDeviceIdentifier();
String username = request.getUsername();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Connection conn = this.getConnection();
StringBuilder queryBuilder = new StringBuilder("SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?");
String sql = "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT " +
"FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ?";
if (serialNumber != null && !serialNumber.isEmpty()) {
queryBuilder.append(" AND SERIAL_NUMBER = ?");
if (StringUtils.isNotEmpty(serialNumber)) {
sql += " AND SERIAL_NUMBER = ?";
}
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) {
queryBuilder.append(" AND DEVICE_IDENTIFIER = ?");
if (StringUtils.isNotEmpty(deviceIdentifier)) {
sql += " AND DEVICE_IDENTIFIER = ?";
}
if (username != null && !username.isEmpty()) {
queryBuilder.append(" AND USERNAME LIKE ?");
if (StringUtils.isNotEmpty(username)) {
sql += " AND USERNAME LIKE ?";
}
String sql = queryBuilder.toString();
stmt = conn.prepareStatement(sql);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
int paramIdx = 2;
if (serialNumber != null && !serialNumber.isEmpty()) {
if (StringUtils.isNotEmpty(serialNumber)) {
stmt.setString(paramIdx++, serialNumber);
}
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) {
if (StringUtils.isNotEmpty(deviceIdentifier)) {
stmt.setString(paramIdx++, deviceIdentifier);
}
if (username != null && !username.isEmpty()) {
if (StringUtils.isNotEmpty(username)) {
stmt.setString(paramIdx, "%" + username + "%");
}
rs = stmt.executeQuery();
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
}
}
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificate count.";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, rs);
}
return certificateCount;
}
@Override
public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
PreparedStatement stmt = null;
ResultSet resultSet = null;
CertificateResponse certificateResponse;
List<CertificateResponse> certificates = new ArrayList<>();
PaginationResult paginationResult;
@ -130,24 +122,25 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
String query = "SELECT * " +
"FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ? ";
if (serialNumber != null && !serialNumber.isEmpty()) {
if (StringUtils.isNotEmpty(serialNumber)) {
query += "AND SERIAL_NUMBER = ? ";
isCertificateSerialNumberProvided = true;
}
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) {
if (StringUtils.isNotEmpty(deviceIdentifier)) {
query += "AND DEVICE_IDENTIFIER = ? ";
isCertificateDeviceIdentifierProvided = true;
}
if (username != null && !username.isEmpty()) {
if (StringUtils.isNotEmpty(username)) {
query += "AND USERNAME LIKE ? ";
isCertificateUsernameProvided = true;
}
query += "ORDER BY ID LIMIT ?,?";
stmt = conn.prepareStatement(query);
try (PreparedStatement stmt = conn.prepareStatement(query)) {
int paramIdx = 1;
stmt.setInt(paramIdx++, tenantId);
if (isCertificateSerialNumberProvided) {
@ -161,7 +154,7 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
}
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount());
resultSet = stmt.executeQuery();
try (ResultSet resultSet = stmt.executeQuery()) {
while (resultSet.next()) {
certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
@ -176,13 +169,12 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
paginationResult = new PaginationResult();
paginationResult.setData(certificates);
paginationResult.setRecordsTotal(this.getCertificateCount(request));
}
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
}
return paginationResult;
}

@ -21,11 +21,11 @@ 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.impl.CertificateGenerator;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
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.CertificateManagementDAOFactory;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOUtil;
import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult;
import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -47,8 +47,6 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
@Override
public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
PreparedStatement stmt = null;
ResultSet resultSet = null;
CertificateResponse certificateResponse;
List<CertificateResponse> certificates = new ArrayList<>();
PaginationResult paginationResult;
@ -64,24 +62,24 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
String query = "SELECT * " +
"FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ? ";
if (serialNumber != null && !serialNumber.isEmpty()) {
if (StringUtils.isNotEmpty(serialNumber)) {
query += "AND SERIAL_NUMBER = ? ";
isCertificateSerialNumberProvided = true;
}
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) {
if (StringUtils.isNotEmpty(deviceIdentifier)) {
query += "AND DEVICE_IDENTIFIER = ? ";
isCertificateDeviceIdentifierProvided = true;
}
if (username != null && !username.isEmpty()) {
if (StringUtils.isNotEmpty(username)) {
query += "AND USERNAME LIKE ? ";
isCertificateUsernameProvided = true;
}
query += "ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(query);
try (PreparedStatement stmt = conn.prepareStatement(query)) {
int paramIdx = 1;
stmt.setInt(paramIdx++, tenantId);
if (isCertificateSerialNumberProvided) {
@ -95,7 +93,7 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
}
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount());
resultSet = stmt.executeQuery();
try (ResultSet resultSet = stmt.executeQuery()) {
while (resultSet.next()) {
certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
@ -110,12 +108,12 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
paginationResult = new PaginationResult();
paginationResult.setData(certificates);
paginationResult.setRecordsTotal(this.getCertificateCount(request));
}
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
}
return paginationResult;
}
@ -130,56 +128,52 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
String serialNumber = request.getSerialNumber();
String deviceIdentifier = request.getDeviceIdentifier();
String username = request.getUsername();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Connection conn = this.getConnection();
StringBuilder queryBuilder = new StringBuilder("SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?");
String sql = "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT " +
"FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ?";
if (serialNumber != null && !serialNumber.isEmpty()) {
queryBuilder.append(" AND SERIAL_NUMBER = ?");
if (StringUtils.isNotEmpty(serialNumber)) {
sql += " AND SERIAL_NUMBER = ?";
}
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) {
queryBuilder.append(" AND DEVICE_IDENTIFIER = ?");
if (StringUtils.isNotEmpty(deviceIdentifier)) {
sql += " AND DEVICE_IDENTIFIER = ?";
}
if (username != null && !username.isEmpty()) {
queryBuilder.append(" AND USERNAME LIKE ?");
if (StringUtils.isNotEmpty(username)) {
sql += " AND USERNAME LIKE ?";
}
String sql = queryBuilder.toString();
stmt = conn.prepareStatement(sql);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
int paramIdx = 2;
if (serialNumber != null && !serialNumber.isEmpty()) {
if (StringUtils.isNotEmpty(serialNumber)) {
stmt.setString(paramIdx++, serialNumber);
}
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) {
if (StringUtils.isNotEmpty(deviceIdentifier)) {
stmt.setString(paramIdx++, deviceIdentifier);
}
if (username != null && !username.isEmpty()) {
if (StringUtils.isNotEmpty(username)) {
stmt.setString(paramIdx, "%" + username + "%");
}
rs = stmt.executeQuery();
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
}
}
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificate count.";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, rs);
}
return certificateCount;
}

@ -21,11 +21,11 @@ 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.impl.CertificateGenerator;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
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.CertificateManagementDAOFactory;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOUtil;
import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult;
import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -47,8 +47,6 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
@Override
public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
PreparedStatement stmt = null;
ResultSet resultSet = null;
CertificateResponse certificateResponse;
List<CertificateResponse> certificates = new ArrayList<>();
PaginationResult paginationResult;
@ -64,24 +62,24 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
String query = "SELECT * " +
"FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ? ";
if (serialNumber != null && !serialNumber.isEmpty()) {
if (StringUtils.isNotEmpty(serialNumber)) {
query += "AND SERIAL_NUMBER = ? ";
isCertificateSerialNumberProvided = true;
}
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) {
if (StringUtils.isNotEmpty(deviceIdentifier)) {
query += "AND DEVICE_IDENTIFIER = ? ";
isCertificateDeviceIdentifierProvided = true;
}
if (username != null && !username.isEmpty()) {
if (StringUtils.isNotEmpty(username)) {
query += "AND USERNAME LIKE ? ";
isCertificateUsernameProvided = true;
}
query += "ORDER BY ID LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(query);
try (PreparedStatement stmt = conn.prepareStatement(query)) {
int paramIdx = 1;
stmt.setInt(paramIdx++, tenantId);
if (isCertificateSerialNumberProvided) {
@ -95,7 +93,7 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
}
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount());
resultSet = stmt.executeQuery();
try (ResultSet resultSet = stmt.executeQuery()) {
while (resultSet.next()) {
certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
@ -110,13 +108,12 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
paginationResult = new PaginationResult();
paginationResult.setData(certificates);
paginationResult.setRecordsTotal(this.getCertificateCount(request));
}
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
}
return paginationResult;
}
@ -131,56 +128,52 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
String serialNumber = request.getSerialNumber();
String deviceIdentifier = request.getDeviceIdentifier();
String username = request.getUsername();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Connection conn = this.getConnection();
StringBuilder queryBuilder = new StringBuilder("SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?");
String sql = "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT " +
"FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ?";
if (serialNumber != null && !serialNumber.isEmpty()) {
queryBuilder.append(" AND SERIAL_NUMBER = ?");
if (StringUtils.isNotEmpty(serialNumber)) {
sql += " AND SERIAL_NUMBER = ?";
}
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) {
queryBuilder.append(" AND DEVICE_IDENTIFIER = ?");
if (StringUtils.isNotEmpty(deviceIdentifier)) {
sql += " AND DEVICE_IDENTIFIER = ?";
}
if (username != null && !username.isEmpty()) {
queryBuilder.append(" AND USERNAME ILIKE ?");
if (StringUtils.isNotEmpty(username)) {
sql += " AND USERNAME ILIKE ?";
}
String sql = queryBuilder.toString();
stmt = conn.prepareStatement(sql);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
int paramIdx = 2;
if (serialNumber != null && !serialNumber.isEmpty()) {
if (StringUtils.isNotEmpty(serialNumber)) {
stmt.setString(paramIdx++, serialNumber);
}
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) {
if (StringUtils.isNotEmpty(deviceIdentifier)) {
stmt.setString(paramIdx++, deviceIdentifier);
}
if (username != null && !username.isEmpty()) {
if (StringUtils.isNotEmpty(username)) {
stmt.setString(paramIdx, "%" + username + "%");
}
rs = stmt.executeQuery();
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
}
}
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificate count.";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, rs);
}
return certificateCount;
}

@ -21,11 +21,11 @@ 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.impl.CertificateGenerator;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
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.CertificateManagementDAOFactory;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOUtil;
import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult;
import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -47,8 +47,6 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
@Override
public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
PreparedStatement stmt = null;
ResultSet resultSet = null;
CertificateResponse certificateResponse;
List<CertificateResponse> certificates = new ArrayList<>();
PaginationResult paginationResult;
@ -64,24 +62,24 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
String query = "SELECT * " +
"FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ? ";
if (serialNumber != null && !serialNumber.isEmpty()) {
if (StringUtils.isNotEmpty(serialNumber)) {
query += "AND SERIAL_NUMBER = ? ";
isCertificateSerialNumberProvided = true;
}
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) {
if (StringUtils.isNotEmpty(deviceIdentifier)) {
query += "AND DEVICE_IDENTIFIER = ? ";
isCertificateDeviceIdentifierProvided = true;
}
if (username != null && !username.isEmpty()) {
if (StringUtils.isNotEmpty(username)) {
query += "AND USERNAME LIKE ? ";
isCertificateUsernameProvided = true;
}
query += "ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(query);
try (PreparedStatement stmt = conn.prepareStatement(query)) {
int paramIdx = 1;
stmt.setInt(paramIdx++, tenantId);
if (isCertificateSerialNumberProvided) {
@ -95,7 +93,7 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
}
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount());
resultSet = stmt.executeQuery();
try (ResultSet resultSet = stmt.executeQuery()) {
while (resultSet.next()) {
certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
@ -110,13 +108,12 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
paginationResult = new PaginationResult();
paginationResult.setData(certificates);
paginationResult.setRecordsTotal(this.getCertificateCount(request));
}
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
}
return paginationResult;
}
@ -131,56 +128,52 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
String serialNumber = request.getSerialNumber();
String deviceIdentifier = request.getDeviceIdentifier();
String username = request.getUsername();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Connection conn = this.getConnection();
StringBuilder queryBuilder = new StringBuilder("SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?");
String sql = "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT " +
"FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ?";
if (serialNumber != null && !serialNumber.isEmpty()) {
queryBuilder.append(" AND SERIAL_NUMBER = ?");
if (StringUtils.isNotEmpty(serialNumber)) {
sql += " AND SERIAL_NUMBER = ?";
}
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) {
queryBuilder.append(" AND DEVICE_IDENTIFIER = ?");
if (StringUtils.isNotEmpty(deviceIdentifier)) {
sql += " AND DEVICE_IDENTIFIER = ?";
}
if (username != null && !username.isEmpty()) {
queryBuilder.append(" AND USERNAME LIKE ?");
if (StringUtils.isNotEmpty(username)) {
sql += " AND USERNAME LIKE ?";
}
String sql = queryBuilder.toString();
stmt = conn.prepareStatement(sql);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
int paramIdx = 2;
if (serialNumber != null && !serialNumber.isEmpty()) {
if (StringUtils.isNotEmpty(serialNumber)) {
stmt.setString(paramIdx++, serialNumber);
}
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) {
if (StringUtils.isNotEmpty(deviceIdentifier)) {
stmt.setString(paramIdx++, deviceIdentifier);
}
if (username != null && !username.isEmpty()) {
if (StringUtils.isNotEmpty(username)) {
stmt.setString(paramIdx, "%" + username + "%");
}
rs = stmt.executeQuery();
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
}
}
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificate count.";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, rs);
}
return certificateCount;
}

@ -17,8 +17,6 @@
*/
package io.entgra.device.mgt.core.certificate.mgt.core.service;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
@ -38,6 +36,7 @@ import io.entgra.device.mgt.core.certificate.mgt.core.impl.KeyStoreReader;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
@ -210,7 +209,7 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe
metadata = CertificateManagerUtil.getMetadataManagementService().retrieveMetadata(CertificateManagementConstants.CERTIFICATE_DELETE);
if (metadata != null) {
String metaValue = metadata.getMetaValue();
if (metaValue != null && !metaValue.isEmpty()) {
if (StringUtils.isNotEmpty(metaValue)) {
JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse(metaValue).getAsJsonObject();
return jsonObject.get(CertificateManagementConstants.IS_CERTIFICATE_DELETE_ENABLE).getAsBoolean();

Loading…
Cancel
Save