From e1519fa2a88f12e798627457d54cab257892b51f Mon Sep 17 00:00:00 2001 From: Pahansith Date: Sun, 24 Mar 2024 14:56:22 +0530 Subject: [PATCH] Add policy content store as Json --- .../pom.xml | 4 ++ .../impl/policy/AbstractPolicyDAOImpl.java | 41 ++++--------------- .../mgt/core/util/PolicyManagerUtil.java | 11 ++++- 3 files changed, 22 insertions(+), 34 deletions(-) diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/pom.xml b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/pom.xml index 3c0fabec4b..8fd87c21b2 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/pom.xml +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/pom.xml @@ -180,6 +180,10 @@ io.entgra.device.mgt.core io.entgra.device.mgt.core.policy.mgt.common + + com.google.code.gson + gson + diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java index 85bd77d859..f69e672cf7 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.policy.mgt.core.dao.impl.policy; +import com.google.gson.Gson; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -54,6 +55,7 @@ import java.util.Properties; */ public abstract class AbstractPolicyDAOImpl implements PolicyDAO { + private static final Gson gson = new Gson(); private static final Log log = LogFactory.getLog(AbstractPolicyDAOImpl.class); @Override @@ -1187,13 +1189,13 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO { stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); stmt.setInt(2, policy.getId()); - stmt.setBytes(3, PolicyManagerUtil.getBytes(policy)); + stmt.setString(3, PolicyManagerUtil.convertToJson(policy)); stmt.setTimestamp(4, currentTimestamp); stmt.setTimestamp(5, currentTimestamp); stmt.setInt(6, tenantId); stmt.setInt(7, enrolmentId); stmt.executeUpdate(); - } catch (SQLException | IOException e) { + } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while adding the evaluated feature list to device", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); @@ -1240,7 +1242,7 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO { "APPLIED = ? WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policy.getId()); - stmt.setBytes(2, PolicyManagerUtil.getBytes(policy)); + stmt.setString(2, PolicyManagerUtil.convertToJson(policy)); stmt.setTimestamp(3, currentTimestamp); stmt.setBoolean(4, false); stmt.setInt(5, deviceId); @@ -1248,7 +1250,7 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO { stmt.setInt(7, enrolmentId); stmt.executeUpdate(); - } catch (SQLException | IOException e) { + } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while updating the evaluated feature list " + "to device", e); } finally { @@ -1699,39 +1701,12 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO { resultSet = stmt.executeQuery(); while (resultSet.next()) { - ByteArrayInputStream bais = null; - ObjectInputStream ois = null; - byte[] contentBytes; - - try { - contentBytes = resultSet.getBytes("POLICY_CONTENT"); - bais = new ByteArrayInputStream(contentBytes); - ois = new ObjectInputStream(bais); - policy = (Policy) ois.readObject(); - } finally { - if (bais != null) { - try { - bais.close(); - } catch (IOException e) { - log.warn("Error occurred while closing ByteArrayOutputStream", e); - } - } - if (ois != null) { - try { - ois.close(); - } catch (IOException e) { - log.warn("Error occurred while closing ObjectOutputStream", e); - } - } - } + String contentString = resultSet.getString("POLICY_CONTENT"); + policy = gson.fromJson(contentString, Policy.class); } } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while getting the applied policy", e); - } catch (IOException e) { - throw new PolicyManagerDAOException("Unable to read the byte stream for content", e); - } catch (ClassNotFoundException e) { - throw new PolicyManagerDAOException("Class not found while converting the object", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); } diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/util/PolicyManagerUtil.java index 0bd3c36396..1aa7947e8e 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/util/PolicyManagerUtil.java @@ -62,7 +62,7 @@ import java.io.ObjectOutputStream; import java.util.*; public class PolicyManagerUtil { - + private static final Gson gson = new Gson(); public static final String GENERAL_CONFIG_RESOURCE_PATH = "general"; public static final String MONITORING_FREQUENCY = "notifierFrequency"; private static final Log log = LogFactory.getLog(PolicyManagerUtil.class); @@ -355,6 +355,15 @@ public class PolicyManagerUtil { return data; } + /** + * Using for converting policy objects into Json strings + * @param obj + * @return + */ + public static String convertToJson(Object obj) { + return gson.toJson(obj); + } + public static boolean convertIntToBoolean(int x) { return x == 1;