From a51e7646e39b5ee3e1bad1d99d3344d275733e3d Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 26 Aug 2015 12:48:53 +0530 Subject: [PATCH 01/13] Fixing issues of previous PRs --- .../carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 7456d57a8c..f026f789d0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -746,12 +746,9 @@ public class PolicyManagerImpl implements PolicyManager { @Override public int getPolicyCount() throws PolicyManagementException { - - int policyCount; try { PolicyManagementDAOFactory.openConnection(); - policyCount = policyDAO.getPolicyCount(); - return policyCount; + return policyDAO.getPolicyCount(); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting policy count", e); } catch (SQLException e) { From 5e285071d801761bdd15cec554a1c3f112d5eb99 Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 26 Aug 2015 15:16:29 +0530 Subject: [PATCH 02/13] Removed unnecessary white spaces --- .../identity/oauth/extension/profile/RegistrationProfile.java | 2 -- .../carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/profile/RegistrationProfile.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/profile/RegistrationProfile.java index 2c1a42bae3..25a760ab25 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/profile/RegistrationProfile.java +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/profile/RegistrationProfile.java @@ -164,6 +164,4 @@ public class RegistrationProfile { public void setGrantType(String grantType) { this.grantType = grantType; } - - } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java index fde4b6d173..e10e1e3097 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java @@ -102,7 +102,7 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint { if (pipDevice.getRoles() != null) { policies = policyFilter.filterRolesBasedPolicies(pipDevice.getRoles(), policies); } - if(pipDevice.getUserId() != null && !pipDevice.getUserId().isEmpty()) { + if (pipDevice.getUserId() != null && !pipDevice.getUserId().isEmpty()) { policies = policyFilter.filterUserBasedPolicies(pipDevice.getUserId(), policies); } From 97c19c23b0cf275d0a6d26ced3e1d5a59607af37 Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 26 Aug 2015 17:14:12 +0530 Subject: [PATCH 03/13] Added method level comments and null checks in DCA --- .../extension/DynamicClientRegistrationUtil.java | 5 +++++ .../oauth/extension/RegistrationService.java | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/DynamicClientRegistrationUtil.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/DynamicClientRegistrationUtil.java index 0cdb0a097f..0a7698217a 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/DynamicClientRegistrationUtil.java +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/DynamicClientRegistrationUtil.java @@ -228,6 +228,11 @@ public class DynamicClientRegistrationUtil { oAuthAdminService.removeOAuthApplicationData(consumerKey); ApplicationManagementService appMgtService = ApplicationManagementService.getInstance(); + + if (appMgtService == null) { + throw new APIManagementException("Error occurred while retrieving Application Management" + + "Service"); + } ServiceProvider createdServiceProvider = appMgtService.getApplication(applicationName); if (createdServiceProvider == null) { diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationService.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationService.java index a31673a776..962b721091 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationService.java +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationService.java @@ -45,9 +45,24 @@ public interface RegistrationService { } } + /** + * This method is used to register an Oauth application. + * + * @param profile contains the necessary attributes that are + * needed in order to register an app. + * @return Status 200 if success including consumerKey and consumerSecret. + */ @POST Response register(RegistrationProfile profile); + /** + * This method is used to remove already registered Oauth application. + * + * @param applicationName name of the application. + * @param userId name of the application owner. + * @param consumerKey provided consumerKey for the registered application. + * @return Status 200 if success. + */ @DELETE public Response unregister(@QueryParam("applicationName") String applicationName, @QueryParam("userId") String userId, From 8c748b3dfded1339ca727dc4395a31423f6c914a Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 26 Aug 2015 17:45:21 +0530 Subject: [PATCH 04/13] Removed unnecessary comments --- .../DeviceManagementProviderService.java | 2 +- .../DeviceManagementProviderServiceImpl.java | 21 ++++++-------- .../pom.xml | 28 ------------------- 3 files changed, 9 insertions(+), 42 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 4aaf8bf06f..bd0223e962 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -93,7 +93,7 @@ public interface DeviceManagementProviderService extends OperationManager { void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status active) throws DeviceManagementException; /** - * This method is used to retrieve list of devices based on the device status + * This method is used to retrieve list of devices based on the device status. * * @param status Device status * @return List of devices diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 197a9321e3..3e05baf788 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -492,8 +492,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { Device device; try { + DeviceManagementDAOFactory.openConnection(); device = deviceDAO.getDevice(deviceId, this.getTenantId()); - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementDAOException | SQLException e) { throw new DeviceManagementException("Error occurred while obtaining the device for id " + "'" + deviceId.getId() + "'", e); } finally { @@ -731,10 +732,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } for (Device device : userDevices) { - Device dmsDevice = - this.getPluginRepository().getDeviceManagementService( - device.getType()).getDeviceManager().getDevice( - new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + Device dmsDevice = this.getDeviceManager(device.getType()). + getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); if (dmsDevice != null) { device.setFeatures(dmsDevice.getFeatures()); device.setProperties(dmsDevice.getProperties()); @@ -771,10 +770,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } for (Device device : allDevices) { - Device dmsDevice = - this.getPluginRepository().getDeviceManagementService( - device.getType()).getDeviceManager().getDevice( - new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + Device dmsDevice = this.getDeviceManager(device.getType()). + getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); if (dmsDevice != null) { device.setFeatures(dmsDevice.getFeatures()); device.setProperties(dmsDevice.getProperties()); @@ -835,10 +832,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } for (Device device : allDevices) { - Device dmsDevice = - this.getPluginRepository().getDeviceManagementService( - device.getType()).getDeviceManager().getDevice( - new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + Device dmsDevice = this.getDeviceManager(device.getType()). + getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); if (dmsDevice != null) { device.setFeatures(dmsDevice.getFeatures()); device.setProperties(dmsDevice.getProperties()); diff --git a/features/oauth-extensions/org.wso2.carbon.oauth.extensions.server.feature/pom.xml b/features/oauth-extensions/org.wso2.carbon.oauth.extensions.server.feature/pom.xml index a20ebb17c6..eae36b42e9 100644 --- a/features/oauth-extensions/org.wso2.carbon.oauth.extensions.server.feature/pom.xml +++ b/features/oauth-extensions/org.wso2.carbon.oauth.extensions.server.feature/pom.xml @@ -37,14 +37,6 @@ This feature contains oauth functionality - - - - - - - - @@ -101,8 +93,6 @@ - - org.wso2.maven carbon-p2-plugin @@ -123,20 +113,6 @@ org.eclipse.equinox.p2.type.group:false - - - - - - - - - - - - - - org.wso2.carbon.core.server:${carbon.kernel.version} @@ -145,10 +121,6 @@ - - - - From 7fed00a14db8dd7ab5324badddb45827c175392f Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 26 Aug 2015 18:10:10 +0530 Subject: [PATCH 05/13] Removed unnecessary logs and added null checks in MonitoringManager --- .../mgt/core/mgt/impl/MonitoringManagerImpl.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index 34a9ad9aaa..b4445af160 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -293,13 +293,16 @@ public class MonitoringManagerImpl implements MonitoringManager { } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyComplianceException("Error occurred reading the applied policies to devices.", e); + } catch (DeviceManagementException e) { + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyComplianceException("Error occurred while adding monitoring operation to DB."); } finally { PolicyManagementDAOFactory.closeConnection(); } } private void addMonitoringOperationsToDatabase(List devices) - throws PolicyComplianceException, OperationManagementException { + throws PolicyComplianceException, OperationManagementException, DeviceManagementException { List deviceIdentifiers = this.getDeviceIdentifiersFromDevices(devices); CommandOperation monitoringOperation = new CommandOperation(); @@ -308,9 +311,10 @@ public class MonitoringManagerImpl implements MonitoringManager { monitoringOperation.setCode(OPERATION_MONITOR); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); + if (service == null) { + throw new DeviceManagementException("Error occurred while retrieving Device Management Service"); + } service.addOperation(monitoringOperation, deviceIdentifiers); -// PolicyManagementDataHolder.getInstance().getDeviceManagementService(). -// addOperation(monitoringOperation, deviceIdentifiers); } private List getDeviceIdentifiersFromDevices(List devices) { From 94326323bcf53296d97517f6bf91d33eed695022 Mon Sep 17 00:00:00 2001 From: mharindu Date: Thu, 27 Aug 2015 01:00:59 +0530 Subject: [PATCH 06/13] Added class level and method level comments for Policy core --- .../policy/mgt/core/dao/FeatureDAO.java | 84 +++++++++-- .../policy/mgt/core/dao/ProfileDAO.java | 49 ++++++- .../mgt/core/dao/impl/FeatureDAOImpl.java | 134 ++---------------- .../mgt/core/dao/impl/ProfileDAOImpl.java | 20 ++- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 4 +- .../mgt/core/mgt/impl/ProfileManagerImpl.java | 2 +- 6 files changed, 148 insertions(+), 145 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java index 19c9c13715..3b8ecec20c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java @@ -25,34 +25,102 @@ import org.wso2.carbon.policy.mgt.common.ProfileFeature; import java.util.List; +/** + * This interface represents the key operations related to profile features of device policies. + */ public interface FeatureDAO { -/* Feature addFeature(Feature feature) throws FeatureManagerDAOException; - - List addFeatures(List feature) throws FeatureManagerDAOException; - - Feature updateFeature(Feature feature) throws FeatureManagerDAOException;*/ - + /** + * This method is used to add a feature related to given profile. + * + * @param feature consists of device specific configurations. + * @param profileId id of the profile. + * @return returns ProfileFeature object. + * @throws FeatureManagerDAOException + */ ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException; + /** + * This method is used to update a feature related to given profile. + * @param feature consists of device specific configurations. + * @param profileId id of the profile. + * @return returns updated ProfileFeature object. + * @throws FeatureManagerDAOException + */ ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException; + /** + * This method is used to add set of features to a given profile. + * + * @param features consists of device specific configurations. + * @param profileId id of the profile. + * @return returns list of ProfileFeature objects. + * @throws FeatureManagerDAOException + */ List addProfileFeatures(List features, int profileId) throws FeatureManagerDAOException; + /** + * This method is used to update set of features to a given profile. + * + * @param features consists of device specific configurations. + * @param profileId id of the profile. + * @return returns list of ProfileFeature objects. + * @throws FeatureManagerDAOException + */ List updateProfileFeatures(List features, int profileId) throws FeatureManagerDAOException; + /** + * This method is used to retrieve all the profile features. + * + * @return returns list of ProfileFeature objects. + * @throws FeatureManagerDAOException + */ List getAllProfileFeatures() throws FeatureManagerDAOException; + /** + * This method is used to retrieve all the profile features based on device type. + * + * @return returns list of ProfileFeature objects. + * @throws FeatureManagerDAOException + */ List getAllFeatures(String deviceType) throws FeatureManagerDAOException; - List getFeaturesForProfile(int ProfileId) throws FeatureManagerDAOException; - + /** + * This method is used to retrieve all the profile features of given profile. + * + * @param profileId id of the profile. + * @return returns list of ProfileFeature objects. + * @throws FeatureManagerDAOException + */ + List getFeaturesForProfile(int profileId) throws FeatureManagerDAOException; + + /** + * This method is used remove a feature. + * + * @param featureId id of the removing feature. + * @return returns true if success. + * @throws FeatureManagerDAOException + */ boolean deleteFeature(int featureId) throws FeatureManagerDAOException; + /** + * This method is used to remove set of features of given profile. + * + * @param profile that contains features to be removed. + * @return returns true if success. + * @throws FeatureManagerDAOException + */ boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException; + /** + * This method is used to remove set of features of given profile id. + * + * @param profileId id of the profile. + * @return returns true if success. + * @throws FeatureManagerDAOException + */ boolean deleteFeaturesOfProfile(int profileId) throws FeatureManagerDAOException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java index 174ca08c48..5d91650d7e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java @@ -24,20 +24,67 @@ import org.wso2.carbon.policy.mgt.common.Profile; import java.util.List; +/** + * This interface represents the key operations related to policy profile. + */ public interface ProfileDAO { + /** + * This method is used to add a profile. + * + * @param profile profile object. + * @return returns added profile object. + * @throws ProfileManagerDAOException + */ Profile addProfile(Profile profile) throws ProfileManagerDAOException; + /** + * This method is used to update a profile + * @param profile profile object. + * @return returns updated profile object. + * @throws ProfileManagerDAOException + */ Profile updateProfile(Profile profile) throws ProfileManagerDAOException; + /** + * This method is used to remove a profile. + * @param profile profile object + * @return returns true if success. + * @throws ProfileManagerDAOException + */ boolean deleteProfile(Profile profile) throws ProfileManagerDAOException; + /** + * This method is used to remove a profile of given policy id. + * @param policyId policy id. + * @return returns true if success. + * @throws ProfileManagerDAOException + */ boolean deleteProfile(int policyId) throws ProfileManagerDAOException; - Profile getProfiles(int profileId) throws ProfileManagerDAOException; + /** + * This method is used to retrieve a profile when id is given. + * @param profileId profile id. + * @return returns profile object. + * @throws ProfileManagerDAOException + */ + Profile getProfile(int profileId) throws ProfileManagerDAOException; + /** + * This method is used to retrieve all the profiles. + * + * @return returns a list of profile objects. + * @throws ProfileManagerDAOException + */ List getAllProfiles() throws ProfileManagerDAOException; + /** + * This method is used to retrieve all the profile of given device type. + * + * @param deviceType device type object. + * @return retruns list of profiles. + * @throws ProfileManagerDAOException + */ List getProfilesOfDeviceType(DeviceType deviceType) throws ProfileManagerDAOException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index 37dde34099..8670c35b7f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -43,115 +43,6 @@ public class FeatureDAOImpl implements FeatureDAO { private static final Log log = LogFactory.getLog(FeatureDAOImpl.class); - -/* @Override - public Feature addFeature(Feature feature) throws FeatureManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - ResultSet generatedKeys = null; - - try { - conn = this.getConnection(); - String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); - stmt.setString(1, feature.getName()); - stmt.setString(2, feature.getCode()); - stmt.setString(3, feature.getDescription()); - int affectedRows = stmt.executeUpdate(); - - if (log.isDebugEnabled()) { - log.debug(affectedRows + " feature is added."); - } - generatedKeys = stmt.getGeneratedKeys(); - - while (generatedKeys.next()) { - feature.setId(generatedKeys.getInt(1)); - } - - } catch (SQLException e) { - String msg = "Error occurred while adding feature to the database."; - log.error(msg, e); - throw new FeatureManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); - } - return feature; - }*/ - - /* @Override - public List addFeatures(List features) throws FeatureManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - ResultSet generatedKeys = null; - List featureList = new ArrayList(); - - try { - conn = this.getConnection(); - String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); - - for (Feature feature : features) { - stmt.setString(1, feature.getName()); - stmt.setString(2, feature.getCode()); - stmt.setString(3, feature.getDescription()); - stmt.addBatch(); - } - - int[] affectedRows = stmt.executeBatch(); - - generatedKeys = stmt.getGeneratedKeys(); - - if (log.isDebugEnabled()) { - log.debug(affectedRows.length + " features are added to the database."); - } - generatedKeys = stmt.getGeneratedKeys(); - int i = 0; - - while (generatedKeys.next()) { - features.get(i).setId(generatedKeys.getInt(1)); - i++; - } - } catch (SQLException e) { - String msg = "Error occurred while adding feature to the database."; - log.error(msg, e); - throw new FeatureManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); - } - return featureList; - }*/ - - - /* @Override - public Feature updateFeature(Feature feature) throws FeatureManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - - try { - conn = this.getConnection(); - String query = "UPDATE DM_FEATURES SET NAME = ?, CODE = ?, DESCRIPTION = ? WHERE ID = ?"; - stmt = conn.prepareStatement(query); - stmt.setString(1, feature.getName()); - stmt.setString(2, feature.getCode()); - stmt.setString(3, feature.getDescription()); - stmt.setInt(4, feature.getId()); - stmt.executeUpdate(); - - } catch (SQLException e) { - String msg = "Error occurred while updating feature " + feature.getName() + " (Feature Name) to the - database."; - log.error(msg, e); - throw new FeatureManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); - } - - return feature; - }*/ - @Override public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException { return null; @@ -247,7 +138,6 @@ public class FeatureDAOImpl implements FeatureDAO { @Override public boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException { - Connection conn; PreparedStatement stmt = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -258,9 +148,10 @@ public class FeatureDAOImpl implements FeatureDAO { stmt = conn.prepareStatement(query); stmt.setInt(1, profile.getProfileId()); stmt.setInt(2, tenantId); - stmt.executeUpdate(); - return true; - + if (stmt.executeUpdate() > 0) { + return true; + } + return false; } catch (SQLException e) { throw new FeatureManagerDAOException("Error occurred while deleting the feature related to a profile.", e); } finally { @@ -270,7 +161,6 @@ public class FeatureDAOImpl implements FeatureDAO { @Override public boolean deleteFeaturesOfProfile(int profileId) throws FeatureManagerDAOException { - Connection conn; PreparedStatement stmt = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -280,9 +170,10 @@ public class FeatureDAOImpl implements FeatureDAO { stmt = conn.prepareStatement(query); stmt.setInt(1, profileId); stmt.setInt(2, tenantId); - stmt.executeUpdate(); - return true; - + if (stmt.executeUpdate() > 0) { + return true; + } + return false; } catch (SQLException e) { throw new FeatureManagerDAOException("Error occurred while deleting the feature related to a profile.", e); } finally { @@ -448,7 +339,6 @@ public class FeatureDAOImpl implements FeatureDAO { @Override public boolean deleteFeature(int featureId) throws FeatureManagerDAOException { - Connection conn; PreparedStatement stmt = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -459,9 +349,10 @@ public class FeatureDAOImpl implements FeatureDAO { stmt = conn.prepareStatement(query); stmt.setInt(1, featureId); stmt.setInt(2, tenantId); - stmt.executeUpdate(); - return true; - + if(stmt.executeUpdate() > 0) { + return true; + } + return false; } catch (SQLException e) { throw new FeatureManagerDAOException("Unable to delete the feature " + featureId + " (Feature ID) " + "from database.", e); @@ -471,7 +362,6 @@ public class FeatureDAOImpl implements FeatureDAO { } private Connection getConnection() throws FeatureManagerDAOException { - try { return PolicyManagementDAOFactory.getConnection(); } catch (PolicyManagerDAOException e) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java index 58ceefba76..89d06d0a10 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -133,7 +133,6 @@ public class ProfileDAOImpl implements ProfileDAO { @Override public boolean deleteProfile(Profile profile) throws ProfileManagerDAOException { - Connection conn; PreparedStatement stmt = null; @@ -142,9 +141,10 @@ public class ProfileDAOImpl implements ProfileDAO { String query = "DELETE FROM DM_PROFILE WHERE ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, profile.getProfileId()); - stmt.executeUpdate(); - return true; - + if (stmt.executeUpdate() > 0) { + return true; + } + return false; } catch (SQLException e) { String msg = "Error occurred while deleting the profile from the data base."; log.error(msg); @@ -164,9 +164,10 @@ public class ProfileDAOImpl implements ProfileDAO { String query = "DELETE FROM DM_PROFILE WHERE ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, profileId); - stmt.executeUpdate(); - return true; - + if (stmt.executeUpdate() > 0) { + return true; + } + return false; } catch (SQLException e) { String msg = "Error occurred while deleting the profile from the data base."; log.error(msg); @@ -178,8 +179,7 @@ public class ProfileDAOImpl implements ProfileDAO { @Override - public Profile getProfiles(int profileId) throws ProfileManagerDAOException { - + public Profile getProfile(int profileId) throws ProfileManagerDAOException { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -217,7 +217,6 @@ public class ProfileDAOImpl implements ProfileDAO { @Override public List getAllProfiles() throws ProfileManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -259,7 +258,6 @@ public class ProfileDAOImpl implements ProfileDAO { @Override public List getProfilesOfDeviceType(DeviceType deviceType) throws ProfileManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index f026f789d0..1d31fd0738 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -397,7 +397,7 @@ public class PolicyManagerImpl implements PolicyManager { // policyDAO.getTimesOfPolicy(policy); // policyDAO.getLocationsOfPolicy(policy); - profile = profileDAO.getProfiles(profileId); + profile = profileDAO.getProfile(profileId); policy.setProfile(profile); policy.setRoles(roleNames); @@ -433,7 +433,7 @@ public class PolicyManagerImpl implements PolicyManager { // policyDAO.getTimesOfPolicy(policy); // policyDAO.getLocationsOfPolicy(policy); - Profile profile = profileDAO.getProfiles(policy.getProfileId()); + Profile profile = profileDAO.getProfile(policy.getProfileId()); policy.setProfile(profile); policy.setRoles(roleNames); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java index 2dc77739cd..197b0f933b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java @@ -147,7 +147,7 @@ public class ProfileManagerImpl implements ProfileManager { try { PolicyManagementDAOFactory.openConnection(); - profile = profileDAO.getProfiles(profileId); + profile = profileDAO.getProfile(profileId); featureList = featureDAO.getFeaturesForProfile(profileId); deviceType = deviceTypeDAO.getDeviceType(profile.getDeviceType().getId()); From 460fda1bb948c29d9b36fa4e6039095c6b37cf2d Mon Sep 17 00:00:00 2001 From: mharindu Date: Thu, 27 Aug 2015 10:43:52 +0530 Subject: [PATCH 07/13] Removed unnecessary new lines in DeviceDAOImpl --- .../mgt/core/dao/impl/DeviceDAOImpl.java | 112 +++++++++--------- 1 file changed, 53 insertions(+), 59 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 6d8d4107bf..df3ee197ba 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -111,13 +111,12 @@ public class DeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = - "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + - "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT " + - "FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + - "t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + - "AND TENANT_ID = ?"; + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + + "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + + "t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + + "AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getType()); stmt.setString(2, deviceId.getId()); @@ -144,12 +143,12 @@ public class DeviceDAOImpl implements DeviceDAO { List devices = null; try { conn = this.getConnection(); - String sql = - "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + - "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " + - "FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME," + - "d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t " + - "WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; + String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, " + + "d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setInt(2, tenantId); @@ -176,12 +175,12 @@ public class DeviceDAOImpl implements DeviceDAO { List devices = null; try { conn = this.getConnection(); - String sql = - "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + - "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " + - "FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.OWNER, t.NAME " + - "AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + - "AND d.TENANT_ID = ?) d1 WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " + + "d.NAME, d.DEVICE_IDENTIFICATION, d.OWNER, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + + "AND d.TENANT_ID = ?) d1 WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, type); stmt.setInt(2, tenantId); @@ -207,13 +206,12 @@ public class DeviceDAOImpl implements DeviceDAO { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = - "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT t.NAME AS DEVICE_TYPE, d.ID, d.DESCRIPTION, " + - "d.NAME, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t " + - "WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + - "WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? AND e.OWNER = ?"; + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT t.NAME AS DEVICE_TYPE, d.ID, d.DESCRIPTION, " + + "d.NAME, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t " + + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + + "WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? AND e.OWNER = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setInt(2, tenantId); @@ -281,12 +279,12 @@ public class DeviceDAOImpl implements DeviceDAO { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = - "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + - "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " + - "FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, d.DESCRIPTION, t.NAME AS DEVICE_TYPE, " + - "d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " + - "AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, " + + "d.DESCRIPTION, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " + + "WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceName + "%"); stmt.setInt(2, tenantId); @@ -314,9 +312,8 @@ public class DeviceDAOImpl implements DeviceDAO { int enrolmentId = -1; try { conn = this.getConnection(); - String sql = - "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS,DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, " + - "TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)"; + String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS,DATE_OF_ENROLMENT, " + + "DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setInt(1, device.getId()); stmt.setString(2, device.getEnrolmentInfo().getOwner()); @@ -346,10 +343,9 @@ public class DeviceDAOImpl implements DeviceDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String sql = - "UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + - "WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " + - "AND OWNER = ? AND TENANT_ID = ?"; + String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? " + + "AND t.NAME = ? AND d.TENANT_ID = ?) AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, status.toString()); stmt.setString(2, deviceId.getId()); @@ -375,10 +371,9 @@ public class DeviceDAOImpl implements DeviceDAO { Status status = null; try { conn = this.getConnection(); - String sql = - "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = " + - "(SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND " + - "d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) AND OWNER = ? AND TENANT_ID = ?"; + String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? " + + "AND t.NAME = ? AND d.TENANT_ID = ?) AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getId()); stmt.setString(2, deviceId.getType()); @@ -407,11 +402,11 @@ public class DeviceDAOImpl implements DeviceDAO { EnrolmentInfo enrolmentInfo = null; try { conn = this.getConnection(); - String sql = - "SELECT ID AS ENROLMENT_ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, " + - "TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + - "WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " + - "AND OWNER = ? AND TENANT_ID = ?"; + String sql = "SELECT ID AS ENROLMENT_ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, DATE_OF_ENROLMENT, " + + "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID " + + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " + + "AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " + + "AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getId()); stmt.setString(2, deviceId.getType()); @@ -438,10 +433,10 @@ public class DeviceDAOImpl implements DeviceDAO { ResultSet rs = null; try { conn = this.getConnection(); - String sql = - "SELECT ID AS ENROLMENT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? " + - "AND d.TENANT_ID = ?) AND STATUS = ? AND TENANT_ID = ?"; + String sql = "SELECT ID AS ENROLMENT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID " + + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " + + "AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " + + "AND STATUS = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getId()); stmt.setString(2, deviceId.getType()); @@ -490,13 +485,12 @@ public class DeviceDAOImpl implements DeviceDAO { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = - "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " + - "d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " + - "e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE FROM DM_ENROLMENT e WHERE TENANT_ID = ? " + - "AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_ID = e.DEVICE_ID " + - "AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " + + "d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " + + "e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE FROM DM_ENROLMENT e WHERE TENANT_ID = ? " + + "AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_ID = e.DEVICE_ID " + + "AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, status.toString()); From abc16557dcc83724c9f0ce6be1fe4e4b251e27d8 Mon Sep 17 00:00:00 2001 From: mharindu Date: Thu, 27 Aug 2015 10:45:10 +0530 Subject: [PATCH 08/13] Removed unnecessary new lines in DeviceDAOImpl --- .../carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index df3ee197ba..5244ce4abd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -42,9 +42,8 @@ public class DeviceDAOImpl implements DeviceDAO { int deviceId = -1; try { conn = this.getConnection(); - String sql = - "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, TENANT_ID) " + - "VALUES (?, ?, ?, ?, ?)"; + String sql = "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, TENANT_ID) " + + "VALUES (?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setString(1, device.getDescription()); stmt.setString(2, device.getName()); @@ -74,9 +73,8 @@ public class DeviceDAOImpl implements DeviceDAO { int deviceId = -1; try { conn = this.getConnection(); - String sql = - "UPDATE DM_DEVICE SET DESCRIPTION = ?, NAME = ? WHERE DEVICE_IDENTIFICATION = ? AND " + - "DEVICE_TYPE_ID = ? AND TENANT_ID = ?"; + String sql = "UPDATE DM_DEVICE SET DESCRIPTION = ?, NAME = ? WHERE DEVICE_IDENTIFICATION = ? AND " + + "DEVICE_TYPE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setString(1, device.getDescription()); stmt.setString(2, device.getName()); From 69cf3f8939d45cd3101527298de57d691cb2ae28 Mon Sep 17 00:00:00 2001 From: mharindu Date: Thu, 27 Aug 2015 17:15:37 +0530 Subject: [PATCH 09/13] Cleaned code and added comments --- .../device/mgt/common/EnrolmentInfo.java | 17 +-- .../carbon/device/mgt/core/dao/DeviceDAO.java | 124 +++++++++++++++++- .../operation/mgt/OperationManagerImpl.java | 4 +- .../mgt/dao/impl/CommandOperationDAOImpl.java | 8 +- .../DeviceManagementProviderServiceImpl.java | 8 +- 5 files changed, 140 insertions(+), 21 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java index 2c599474fc..1054075c34 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java @@ -96,19 +96,14 @@ public class EnrolmentInfo { public boolean equals(Object obj) { if (obj instanceof EnrolmentInfo) { EnrolmentInfo tempInfo = (EnrolmentInfo) obj; - if (owner != null && ownership != null - && tempInfo.getOwner() != null && tempInfo.getOwnership() != null) { - - if (owner.equals(tempInfo.getOwner()) && ownership.equals(tempInfo.getOwnership())) { - return true; - } else { - return false; + if (owner != null && ownership != null) { + if (tempInfo.getOwner() != null && tempInfo.getOwnership() != null) { + if (owner.equals(tempInfo.getOwner()) && ownership.equals(tempInfo.getOwnership())) { + return true; + } } - } else { - return false; } - } else { - return false; } + return false; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 5b5d0c66aa..3eb1f48ac4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -31,38 +31,160 @@ import java.util.List; */ public interface DeviceDAO { + /** + * This method is used to add a device. + * + * @param typeId device type id. + * @param device device object. + * @param tenantId tenant id. + * @return returns the id of the persisted device record. + * @throws DeviceManagementDAOException + */ int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to update a given device. + * + * @param typeId device type id. + * @param device device object. + * @param tenantId tenant id. + * @return returns the id of updated device. + * @throws DeviceManagementDAOException + */ int updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to remove a device. + * + * @param deviceId id of the device that should be removed. + * @param tenantId tenant id. + * @return returns the id of removed device. + * @throws DeviceManagementDAOException + */ int removeDevice(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve a device of a given id. + * + * @param deviceId device id. + * @param tenantId tenant id. + * @return returns the device object. + * @throws DeviceManagementDAOException + */ Device getDevice(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve all the devices of a given tenant. + * + * @param tenantId tenant id. + * @return returns a list of devices. + * @throws DeviceManagementDAOException + */ List getDevices(int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve all the devices of a given tenant and device type. + * + * @param type device type. + * @param tenantId tenant id. + * @return returns list of devices. + * @throws DeviceManagementDAOException + */ List getDevices(String type, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve devices of a given user. + * @param username user name. + * @param tenantId tenant id. + * @return returns list of devices. + * @throws DeviceManagementDAOException + */ List getDevicesOfUser(String username, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve the device count of a given tenant. + * + * @param tenantId tenant id. + * @return returns the device count. + * @throws DeviceManagementDAOException + */ int getDeviceCount(int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve devices of a given device name. + * @param deviceName device name. + * @param tenantId tenant id. + * @return returns list of devices. + * @throws DeviceManagementDAOException + */ List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to add an enrollment information of a given device. + * + * @param device device object. + * @param tenantId tenant id. + * @return returns the id of the enrollment. + * @throws DeviceManagementDAOException + */ int addEnrollment(Device device, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to set the current enrollment status of given device and user. + * + * @param deviceId device id. + * @param currentOwner current user name. + * @param status device status. + * @param tenantId tenant id. + * @return returns true if success. + * @throws DeviceManagementDAOException + */ boolean setEnrolmentStatus(DeviceIdentifier deviceId, String currentOwner, Status status, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to get the status of current enrollment of a given user and device. + * + * @param deviceId device id. + * @param currentOwner device owner. + * @param tenantId tenant id. + * @return returns current enrollment status. + * @throws DeviceManagementDAOException + */ Status getEnrolmentStatus(DeviceIdentifier deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve current enrollment of a given device and user. + * + * @param deviceId device id. + * @param currentUser user name. + * @param tenantId tenant id. + * @return returns EnrolmentInfo object. + * @throws DeviceManagementDAOException + */ EnrolmentInfo getEnrolment(DeviceIdentifier deviceId, String currentUser, int tenantId) throws DeviceManagementDAOException; - + /** + * This method is used to retrieve devices of a given enrollment status. + * + * @param status enrollment status. + * @param tenantId tenant id. + * @return returns list of devices. + * @throws DeviceManagementDAOException + */ List getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve the enrollment id of a given device and status. + * + * @param deviceId device id. + * @param status enrollment status. + * @param tenantId tenant id. + * @return returns the id of current enrollment. + * @throws DeviceManagementDAOException + */ int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status, int tenantId) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index b2e593291d..9bdcf595fa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -76,7 +76,7 @@ public class OperationManagerImpl implements OperationManager { log.debug("operation:[" + operation.toString() + "]"); for (DeviceIdentifier deviceIdentifier : deviceIds) { log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" + - deviceIdentifier.getType() + "]"); + deviceIdentifier.getType() + "]"); } } try { @@ -167,7 +167,7 @@ public class OperationManagerImpl implements OperationManager { if (enrolmentId < 0) { throw new OperationManagementException("Device not found for the given device Identifier:" + - deviceId.getId() + " and given type:" + deviceId.getType()); + deviceId.getId() + " and given type:" + deviceId.getType()); } dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java index fa3348789f..f485408d67 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java @@ -120,9 +120,11 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { List commandOperations = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT o.ID, co1.ENABLED, co1.STATUS, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.OPERATION_CODE FROM (SELECT co.OPERATION_ID, co.ENABLED, dm.STATUS FROM DM_COMMAND_OPERATION co " + - "INNER JOIN (SELECT ENROLMENT_ID, OPERATION_ID, STATUS FROM DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID = ? " + - "AND STATUS = ?) dm ON dm.OPERATION_ID = co.OPERATION_ID) co1 INNER JOIN DM_OPERATION o ON co1.OPERATION_ID = o.ID"; + String sql = "SELECT o.ID, co1.ENABLED, co1.STATUS, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, " + + "o.OPERATION_CODE FROM (SELECT co.OPERATION_ID, co.ENABLED, dm.STATUS " + + "FROM DM_COMMAND_OPERATION co INNER JOIN (SELECT ENROLMENT_ID, OPERATION_ID, STATUS " + + "FROM DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID = ? AND STATUS = ?) dm " + + "ON dm.OPERATION_ID = co.OPERATION_ID) co1 INNER JOIN DM_OPERATION o ON co1.OPERATION_ID = o.ID"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 3e05baf788..c44bf6bd27 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -402,7 +402,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBody = messageBody.trim() + System.getProperty("line.separator") + System.getProperty("line.separator") + url.replaceAll("\\{" - + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", + + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); @@ -458,8 +458,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBody = messageBody.trim().replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants - .USERNAME - + "\\}", + .USERNAME + + "\\}", URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants .ENCODED_SCHEME)); @@ -468,7 +468,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv .ENCODED_SCHEME)); messageBody = messageBody + System.getProperty("line.separator") + url.replaceAll("\\{" - + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", + + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); From 6385c130122b5a8c042a533aaa586b9408e69e27 Mon Sep 17 00:00:00 2001 From: mharindu Date: Tue, 1 Sep 2015 15:25:16 +0530 Subject: [PATCH 10/13] Fixed issues in DeviceDAO --- .../wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index f2e68732d4..fc6d79b133 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -176,9 +176,9 @@ public class DeviceDAOImpl implements DeviceDAO { String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " + - "d.NAME, d.DEVICE_IDENTIFICATION, d.OWNER, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + - "AND d.TENANT_ID = ?) d1 WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; + "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, type); stmt.setInt(2, tenantId); From 0140974487b85e51f865b33e98c9cf62a9568e5d Mon Sep 17 00:00:00 2001 From: Dilshan Edirisuriya Date: Wed, 2 Sep 2015 10:39:51 +0530 Subject: [PATCH 11/13] Certificate verification --- .../pom.xml | 1 - .../mgt/core/impl/CertificateGenerator.java | 48 +++++++++++++++++++ .../mgt/core/impl/KeyStoreReader.java | 19 ++++++++ .../service/CertificateManagementService.java | 17 ++++--- .../CertificateManagementServiceImpl.java | 8 ++++ 5 files changed, 85 insertions(+), 8 deletions(-) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml index 72647601ae..81d6be9ba4 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml @@ -27,7 +27,6 @@ 4.0.0 - org.wso2.carbon.devicemgt org.wso2.carbon.certificate.mgt.core 0.9.2-SNAPSHOT bundle diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java index a1ddb3c20e..e0c999ad07 100755 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java @@ -77,6 +77,7 @@ import java.security.PrivateKey; import java.security.SecureRandom; import java.security.Security; import java.security.SignatureException; +import java.security.cert.Certificate; import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.security.cert.CertificateExpiredException; @@ -283,6 +284,53 @@ public class CertificateGenerator { } } + public boolean verifySignature(String headerSignature) throws KeystoreException { + + if (headerSignature == null || headerSignature.isEmpty()) { + return false; + } + + try { + KeyStoreReader keyStoreReader = new KeyStoreReader(); + CMSSignedData signedData = new CMSSignedData(Base64.decodeBase64(headerSignature.getBytes())); + Store reqStore = signedData.getCertificates(); + @SuppressWarnings("unchecked") + Collection reqCerts = reqStore.getMatches(null); + + if (reqCerts != null && reqCerts.size() > 0) { + CertificateFactory certificateFactory = CertificateFactory.getInstance(ConfigurationUtil.X_509); + X509CertificateHolder holder = reqCerts.iterator().next(); + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(holder.getEncoded()); + X509Certificate reqCert = (X509Certificate) certificateFactory. + generateCertificate(byteArrayInputStream); + + if(reqCert != null && reqCert.getSerialNumber() != null) { + Certificate lookUpCertificate = keyStoreReader.getCertificateByAlias( + reqCert.getSerialNumber().toString()); + + if (lookUpCertificate != null) { + return true; + } + } + + } + } catch (CMSException e) { + String errorMsg = "CMSException when decoding certificate signature"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } catch (IOException e) { + String errorMsg = "IOException when decoding certificate signature"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } catch (CertificateException e) { + String errorMsg = "CertificateException when decoding certificate signature"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } + + return false; + } + public X509Certificate generateCertificateFromCSR(PrivateKey privateKey, PKCS10CertificationRequest request, String issueSubject) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/KeyStoreReader.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/KeyStoreReader.java index f714a4746b..1b82bb9683 100755 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/KeyStoreReader.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/KeyStoreReader.java @@ -204,6 +204,25 @@ public class KeyStoreReader { return raCertificate; } + public Certificate getCertificateByAlias(String alias) throws KeystoreException { + + KeyStore keystore = loadCertificateKeyStore(); + Certificate raCertificate; + try { + raCertificate = keystore.getCertificate(alias); + } catch (KeyStoreException e) { + String errorMsg = "KeyStore issue occurred when retrieving RA private key"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } + + if (raCertificate == null) { + throw new KeystoreException("RA certificate not found in KeyStore"); + } + + return raCertificate; + } + PrivateKey getRAPrivateKey() throws KeystoreException { KeyStore keystore = loadCertificateKeyStore(); diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java index c9b1ca5c96..67171a3f93 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java @@ -33,17 +33,20 @@ public interface CertificateManagementService { Certificate getRACertificate() throws KeystoreException; - public List getRootCertificates(byte[] ca, byte[] ra) throws KeystoreException; + List getRootCertificates(byte[] ca, byte[] ra) throws KeystoreException; - public X509Certificate generateX509Certificate() throws KeystoreException; + X509Certificate generateX509Certificate() throws KeystoreException; - public SCEPResponse getCACertSCEP() throws KeystoreException; + SCEPResponse getCACertSCEP() throws KeystoreException; - public byte[] getCACapsSCEP(); + byte[] getCACapsSCEP(); - public byte[] getPKIMessageSCEP(InputStream inputStream) throws KeystoreException; + byte[] getPKIMessageSCEP(InputStream inputStream) throws KeystoreException; - public X509Certificate generateCertificateFromCSR(PrivateKey privateKey, - PKCS10CertificationRequest request, + X509Certificate generateCertificateFromCSR(PrivateKey privateKey, PKCS10CertificationRequest request, String issueSubject) throws KeystoreException; + + Certificate getCertificateByAlias(String alias) throws KeystoreException; + + boolean verifySignature(String headerSignature) throws KeystoreException; } diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java index a294acbc16..014363e90d 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java @@ -84,4 +84,12 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe String issueSubject) throws KeystoreException { return certificateGenerator.generateCertificateFromCSR(privateKey, request, issueSubject); } + + public Certificate getCertificateByAlias(String alias) throws KeystoreException { + return keyStoreReader.getCertificateByAlias(alias); + } + + public boolean verifySignature(String headerSignature) throws KeystoreException { + return certificateGenerator.verifySignature(headerSignature); + } } From 0c43338f5db1923eae12c7ad373275024ee9fb2d Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 2 Sep 2015 16:16:18 +0530 Subject: [PATCH 12/13] Added runtime exception to DCA --- .../carbon/device/mgt/common/EnrolmentInfo.java | 13 ++++++------- .../extension/DynamicClientRegistrationUtil.java | 6 +++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java index a21aa44b91..7b3ff89d1f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java @@ -20,7 +20,7 @@ package org.wso2.carbon.device.mgt.common; import java.io.Serializable; -public class EnrolmentInfo implements Serializable{ +public class EnrolmentInfo implements Serializable { private static final long serialVersionUID = 1998101712L; @@ -40,7 +40,8 @@ public class EnrolmentInfo implements Serializable{ private Status status; private String owner; - public EnrolmentInfo() {} + public EnrolmentInfo() { + } public EnrolmentInfo(Device device, String owner, OwnerShip ownership, Status status) { this.device = device; @@ -109,11 +110,9 @@ public class EnrolmentInfo implements Serializable{ public boolean equals(Object obj) { if (obj instanceof EnrolmentInfo) { EnrolmentInfo tempInfo = (EnrolmentInfo) obj; - if (owner != null && ownership != null) { - if (tempInfo.getOwner() != null && tempInfo.getOwnership() != null) { - if (owner.equals(tempInfo.getOwner()) && ownership.equals(tempInfo.getOwnership())) { - return true; - } + if (this.owner != null && this.ownership != null) { + if (this.owner.equals(tempInfo.getOwner()) && this.ownership.equals(tempInfo.getOwnership())) { + return true; } } } diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/DynamicClientRegistrationUtil.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/DynamicClientRegistrationUtil.java index 0a7698217a..c823323364 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/DynamicClientRegistrationUtil.java +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/DynamicClientRegistrationUtil.java @@ -128,6 +128,10 @@ public class DynamicClientRegistrationUtil { serviceProvider.setDescription("Service Provider for application " + applicationName); ApplicationManagementService appMgtService = ApplicationManagementService.getInstance(); + if (appMgtService == null) { + throw new IllegalStateException("Error occurred while retrieving Application Management" + + "Service"); + } appMgtService.createApplication(serviceProvider); ServiceProvider createdServiceProvider = appMgtService.getApplication(applicationName); @@ -230,7 +234,7 @@ public class DynamicClientRegistrationUtil { ApplicationManagementService appMgtService = ApplicationManagementService.getInstance(); if (appMgtService == null) { - throw new APIManagementException("Error occurred while retrieving Application Management" + + throw new IllegalStateException("Error occurred while retrieving Application Management" + "Service"); } ServiceProvider createdServiceProvider = appMgtService.getApplication(applicationName); From 454c459172aa4092d1767bc5c8aa81f657a5fc99 Mon Sep 17 00:00:00 2001 From: Dilshan Edirisuriya Date: Wed, 2 Sep 2015 16:46:51 +0530 Subject: [PATCH 13/13] Extract certificate from signature --- .../mgt/core/impl/CertificateGenerator.java | 19 +++++++++++-------- .../service/CertificateManagementService.java | 2 ++ .../CertificateManagementServiceImpl.java | 4 ++++ .../certificate/mgt/core/util/CommonUtil.java | 5 +++++ .../pom.xml | 6 +++++- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java index e0c999ad07..7a2538af22 100755 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java @@ -64,7 +64,6 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.KeyPair; @@ -158,10 +157,9 @@ public class CertificateGenerator { keyPairGenerator.initialize(ConfigurationUtil.RSA_KEY_LENGTH, new SecureRandom()); KeyPair pair = keyPairGenerator.generateKeyPair(); X500Principal principal = new X500Principal(ConfigurationUtil.DEFAULT_PRINCIPAL); - BigInteger serial = BigInteger.valueOf(System.currentTimeMillis()); X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder( - principal, serial, validityBeginDate, validityEndDate, + principal, CommonUtil.generateSerialNumber(), validityBeginDate, validityEndDate, principal, pair.getPublic()); ContentSigner contentSigner = new JcaContentSignerBuilder(ConfigurationUtil.SHA256_RSA) .setProvider(ConfigurationUtil.PROVIDER).build( @@ -285,9 +283,14 @@ public class CertificateGenerator { } public boolean verifySignature(String headerSignature) throws KeystoreException { + Certificate certificate = extractCertificateFromSignature(headerSignature); + return (certificate != null); + } + + public X509Certificate extractCertificateFromSignature(String headerSignature) throws KeystoreException { if (headerSignature == null || headerSignature.isEmpty()) { - return false; + return null; } try { @@ -308,8 +311,8 @@ public class CertificateGenerator { Certificate lookUpCertificate = keyStoreReader.getCertificateByAlias( reqCert.getSerialNumber().toString()); - if (lookUpCertificate != null) { - return true; + if (lookUpCertificate != null && (lookUpCertificate instanceof X509Certificate)) { + return (X509Certificate)lookUpCertificate; } } @@ -328,7 +331,7 @@ public class CertificateGenerator { throw new KeystoreException(errorMsg, e); } - return false; + return null; } public X509Certificate generateCertificateFromCSR(PrivateKey privateKey, @@ -353,7 +356,7 @@ public class CertificateGenerator { } X509v3CertificateBuilder certificateBuilder = new X509v3CertificateBuilder( - new X500Name(issueSubject), BigInteger.valueOf(System.currentTimeMillis()), + new X500Name(issueSubject), CommonUtil.generateSerialNumber(), validityBeginDate, validityEndDate, certSubject, request.getSubjectPublicKeyInfo()); ContentSigner sigGen; diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java index 67171a3f93..0b47c43707 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java @@ -49,4 +49,6 @@ public interface CertificateManagementService { Certificate getCertificateByAlias(String alias) throws KeystoreException; boolean verifySignature(String headerSignature) throws KeystoreException; + + public X509Certificate extractCertificateFromSignature(String headerSignature) throws KeystoreException; } diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java index 014363e90d..c379df4264 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java @@ -92,4 +92,8 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe public boolean verifySignature(String headerSignature) throws KeystoreException { return certificateGenerator.verifySignature(headerSignature); } + + public X509Certificate extractCertificateFromSignature(String headerSignature) throws KeystoreException { + return certificateGenerator.extractCertificateFromSignature(headerSignature); + } } diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/util/CommonUtil.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/util/CommonUtil.java index a149c92569..6b9bc5897e 100755 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/util/CommonUtil.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/util/CommonUtil.java @@ -17,6 +17,7 @@ */ package org.wso2.carbon.certificate.mgt.core.util; +import java.math.BigInteger; import java.util.Calendar; import java.util.Date; @@ -40,4 +41,8 @@ public class CommonUtil { return calendar.getTime(); } + public static synchronized BigInteger generateSerialNumber() { + return BigInteger.valueOf(System.currentTimeMillis()); + } + } diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/pom.xml b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/pom.xml index 3405148639..edca5ac955 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/pom.xml +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/pom.xml @@ -88,7 +88,11 @@ org.wso2.carbon.user.core.tenant, org.wso2.carbon.utils, org.wso2.carbon.utils.multitenancy, - org.xml.sax + org.xml.sax, + javax.servlet.http, + javax.xml, + org.apache.axis2.transport.http, + org.wso2.carbon.apimgt.impl