Fix application search issue #341

Closed
pahansith wants to merge 20 commits from pahansith/device-mgt-core:search-filter-fix into master

@ -103,4 +103,7 @@ public interface CertificateDAO {
List<CertificateResponse> searchCertificate(String serialNumber) throws CertificateManagementDAOException; List<CertificateResponse> searchCertificate(String serialNumber) throws CertificateManagementDAOException;
List<CertificateResponse> retrieveEmptyDeviceIdCerts() throws CertificateManagementDAOException;
int updateDeviceIdentifier(CertificateResponse cert) throws CertificateManagementDAOException;
} }

@ -229,6 +229,60 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{
return certificates; return certificates;
} }
@Override
public List<CertificateResponse> retrieveEmptyDeviceIdCerts() 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 ID, CERTIFICATE FROM DM_DEVICE_CERTIFICATE "
+ "WHERE DEVICE_IDENTIFIER is NULL";
stmt = conn.prepareStatement(query);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setId(resultSet.getInt("ID"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
certificates.add(certificateResponse);
}
} catch (SQLException e) {
String errorMsg =
"Error while reading null device identifier certificates";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
}
return certificates;
}
@Override
public int updateDeviceIdentifier(CertificateResponse cert) throws CertificateManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
try {
conn = this.getConnection();
stmt = conn.prepareStatement(
"UPDATE DM_DEVICE_CERTIFICATE SET DEVICE_IDENTIFIER = ? WHERE ID = ?");
stmt.setString(1, cert.getOrganization());
stmt.setInt(2, cert.getId());
return stmt.executeUpdate();
} catch (SQLException e) {
throw new CertificateManagementDAOException("Error occurred while updating device identifier "
+ cert.getOrganization() + " of certificate id " + cert.getId()
, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override @Override
public List<CertificateResponse> getAllCertificates() throws CertificateManagementDAOException { public List<CertificateResponse> getAllCertificates() throws CertificateManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;

@ -26,6 +26,26 @@ import java.math.BigInteger;
@ApiModel(value = "CertificateResponse", description = "This class carries all information related to certificates") @ApiModel(value = "CertificateResponse", description = "This class carries all information related to certificates")
public class CertificateResponse { public class CertificateResponse {
int id;
String organization;
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ApiModelProperty(name = "certificate", value = "The certificate in bytes", required = true) @ApiModelProperty(name = "certificate", value = "The certificate in bytes", required = true)
byte[] certificate; byte[] certificate;

@ -77,6 +77,20 @@ public class CertificateManagementServiceComponent {
bundleContext.registerService(SCEPManager.class.getName(), bundleContext.registerService(SCEPManager.class.getName(),
new SCEPManagerImpl(), null); new SCEPManagerImpl(), null);
new Thread(new Runnable() {
@Override
public void run() {
log.info("=================Starting the certificate table device identifier updating worker===============");
CertificateManagementService certificateManagementService = CertificateManagementServiceImpl.getInstance();
try {
certificateManagementService.updateCertificateDeviceIdentifiers();
log.info("=================Completed the certificate table device identifier updating worker===============");
} catch (CertificateManagementException e) {
log.error("Failed while updating device identifiers of the certificates.");
}
}
}).start();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Certificate management core bundle has been successfully initialized"); log.debug("Certificate management core bundle has been successfully initialized");
} }

@ -84,4 +84,5 @@ public interface CertificateManagementService {
X509Certificate generateAlteredCertificateFromCSR(String csr) throws KeystoreException; X509Certificate generateAlteredCertificateFromCSR(String csr) throws KeystoreException;
void updateCertificateDeviceIdentifiers() throws CertificateManagementException;
} }

@ -272,4 +272,44 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe
return certificateGenerator.generateAlteredCertificateFromCSR(csr); return certificateGenerator.generateAlteredCertificateFromCSR(csr);
} }
@Override
public void updateCertificateDeviceIdentifiers() throws CertificateManagementException {
try {
CertificateManagementDAOFactory.beginTransaction();
CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO();
List<CertificateResponse> certificateResponses = certificateDAO.retrieveEmptyDeviceIdCerts();
for (CertificateResponse certificateResponse : certificateResponses) {
String subject = certificateResponse.getSubject();
String deviceId = null;
log.info("Extracting deviceId from certificate with subject : " + subject);
if (subject.contains("O=")) {
String[] dnParts = subject.split(",");
for (int i = 0; i < dnParts.length; i++) {
if (dnParts[i].contains("O=")) {
String[] orgParts = dnParts[i].split("=");
if (orgParts[1].matches("^[a-zA-Z0-9]+$")) { //check if the O is alphanumeric
deviceId = orgParts[1];
} else {
log.info("Certificate is not representing an Android device. " +
"Marking device identifier as null");
}
}
}
}
certificateResponse.setOrganization(deviceId);
certificateDAO.updateDeviceIdentifier(certificateResponse);
}
CertificateManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
String msg = "Failed while updating certificate device identifier";
log.error(msg, e);
throw new CertificateManagementException(e);
} catch (CertificateManagementDAOException e) {
CertificateManagementDAOFactory.rollbackTransaction();
String msg = "Failed while updating certificate device identifier";
log.error(msg, e);
throw new CertificateManagementException(e);
}
}
} }

@ -21,6 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.core.common;
public class Constants { public class Constants {
public static final String SCHEME_SEPARATOR = "://"; public static final String SCHEME_SEPARATOR = "://";
public static final String COLON = ":"; public static final String COLON = ":";
public static final String QUERY_WILDCARD = "%";
public static final String URI_QUERY_SEPARATOR = "?"; public static final String URI_QUERY_SEPARATOR = "?";
public static final String URI_SEPARATOR = "/"; public static final String URI_SEPARATOR = "/";
public static final String BASIC_AUTH_HEADER_PREFIX = "Basic "; public static final String BASIC_AUTH_HEADER_PREFIX = "Basic ";

@ -17,6 +17,7 @@
*/ */
package io.entgra.device.mgt.core.device.mgt.core.dao.impl; package io.entgra.device.mgt.core.device.mgt.core.dao.impl;
import io.entgra.device.mgt.core.device.mgt.core.common.Constants;
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.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest;
@ -298,9 +299,13 @@ public class ApplicationDAOImpl implements ApplicationDAO {
String filter = request.getFilter(); String filter = request.getFilter();
if (filter != null) { if (filter != null) {
sql = sql + "AND NAME LIKE ? "; sql = sql + "AND NAME LIKE ? ";
filter = Constants.QUERY_WILDCARD.concat(filter).concat(Constants.QUERY_WILDCARD);
} }
boolean isLimitPresent = false;
if (request != null && request.getRowCount() != -1) { if (request != null && request.getRowCount() != -1) {
sql = sql + "LIMIT ? OFFSET ?"; sql = sql + "LIMIT ? OFFSET ?";
isLimitPresent = true;
} }
Connection conn = this.getConnection(); Connection conn = this.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
@ -312,7 +317,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
if (filter != null){ if (filter != null){
stmt.setString(paramIdx++, filter); stmt.setString(paramIdx++, filter);
} }
if (request != null && request.getRowCount() != -1) { if (isLimitPresent) {
stmt.setInt(paramIdx++, request.getRowCount()); stmt.setInt(paramIdx++, request.getRowCount());
stmt.setInt(paramIdx, request.getStartIndex()); stmt.setInt(paramIdx, request.getStartIndex());
} }

@ -463,6 +463,8 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION (
)ENGINE = InnoDB; )ENGINE = InnoDB;
CREATE INDEX IDX_DM_APPLICATION ON DM_APPLICATION(DEVICE_ID, ENROLMENT_ID, TENANT_ID); CREATE INDEX IDX_DM_APPLICATION ON DM_APPLICATION(DEVICE_ID, ENROLMENT_ID, TENANT_ID);
CREATE INDEX DM_APPLICATION_NAME ON DM_APPLICATION(NAME);
CREATE INDEX DM_APPLICATION_NAME_PLATFORM_TID ON DM_APPLICATION(NAME, PLATFORM, TENANT_ID);
-- END OF POLICY RELATED TABLES -- -- END OF POLICY RELATED TABLES --

Loading…
Cancel
Save