diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/impl/CertificateManagementAdminServiceImpl.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/impl/CertificateManagementAdminServiceImpl.java index 15b0a548c75..6d98253f83b 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/impl/CertificateManagementAdminServiceImpl.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/impl/CertificateManagementAdminServiceImpl.java @@ -9,8 +9,8 @@ import org.wso2.carbon.certificate.mgt.cert.jaxrs.api.beans.EnrollmentCertificat import org.wso2.carbon.certificate.mgt.cert.jaxrs.api.beans.ErrorResponse; import org.wso2.carbon.certificate.mgt.cert.jaxrs.api.util.DeviceMgtAPIUtils; import org.wso2.carbon.certificate.mgt.cert.jaxrs.api.util.RequestValidationUtil; -import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOException; import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse; +import org.wso2.carbon.certificate.mgt.core.exception.CertificateManagementException; import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementService; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -76,7 +76,7 @@ public class CertificateManagementAdminServiceImpl implements CertificateManagem try { certificateResponse = certificateService.searchCertificates(serialNumber); return Response.status(Response.Status.OK).entity(certificateResponse).build(); - } catch (CertificateManagementDAOException e) { + } catch (CertificateManagementException e) { String msg = "Error occurred while converting PEM file to X509Certificate"; log.error(msg, e); throw new UnexpectedServerErrorException( @@ -106,7 +106,7 @@ public class CertificateManagementAdminServiceImpl implements CertificateManagem certificates.setCount(result.getRecordsTotal()); certificates.setList((List) result.getData()); return Response.status(Response.Status.OK).entity(certificates).build(); - } catch (CertificateManagementDAOException e) { + } catch (CertificateManagementException e) { String msg = "Error occurred while fetching all certificates."; log.error(msg, e); throw new UnexpectedServerErrorException( @@ -128,7 +128,7 @@ public class CertificateManagementAdminServiceImpl implements CertificateManagem } return Response.status(Response.Status.OK).entity("Certificate that carries the serial number '" + serialNumber + "' has been removed").build(); - } catch (CertificateManagementDAOException e) { + } catch (CertificateManagementException e) { String msg = "Error occurred while converting PEM file to X509Certificate"; log.error(msg, e); throw new UnexpectedServerErrorException( diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateDAO.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateDAO.java index 5916148c228..1895c14491f 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateDAO.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateDAO.java @@ -18,11 +18,11 @@ package org.wso2.carbon.certificate.mgt.core.dao; +import org.wso2.carbon.certificate.mgt.core.bean.Certificate; import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; -import java.io.ByteArrayInputStream; import java.util.List; /** @@ -37,8 +37,9 @@ public interface CertificateDAO { * * @param certificate Holds the certificate and relevant details. * @throws CertificateManagementDAOException + * */ - void addCertificate(List certificate) + void addCertificate(List certificate) throws CertificateManagementDAOException; /** @@ -47,31 +48,37 @@ public interface CertificateDAO { * @param serialNumber Serial number of the certificate. * @return representation of the certificate. * @throws CertificateManagementDAOException + * */ - org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse retrieveCertificate(String serialNumber - ) throws CertificateManagementDAOException; + CertificateResponse retrieveCertificate(String serialNumber) throws CertificateManagementDAOException; /** * Get all the certificates in a paginated manner. + * * @param request Request mentioning pagination details such as length and stating index. * @return Pagination result with data and the count of results. * @throws CertificateManagementDAOException + * */ PaginationResult getAllCertificates(PaginationRequest request) throws CertificateManagementDAOException; /** * Get all the certificates. + * * @return List of certificates * @throws CertificateManagementDAOException + * */ public List getAllCertificates() throws CertificateManagementDAOException; /** * Delete a certificate identified by a serial number() + * * @param serialNumber serial number * @return whether the certificate was removed or not. */ boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException; public List searchCertificate(String serialNumber) throws CertificateManagementDAOException; + } diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateManagementDAOFactory.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateManagementDAOFactory.java index cfe65cdb7b3..02345c127b0 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateManagementDAOFactory.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateManagementDAOFactory.java @@ -50,7 +50,7 @@ public class CertificateManagementDAOFactory { try { databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName(); } catch (SQLException e) { - log.error("Error occurred while retrieving config.datasource connection", e); + log.error( "Error occurred while retrieving config.datasource connection", e); } } @@ -59,7 +59,7 @@ public class CertificateManagementDAOFactory { try { databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName(); } catch (SQLException e) { - log.error("Error occurred while retrieving config.datasource connection", e); + log.error("Error occurred while retrieving a datasource connection", e); } } @@ -72,11 +72,22 @@ public class CertificateManagementDAOFactory { } try { conn = dataSource.getConnection(); + } catch (SQLException e) { + throw new TransactionManagementException("Error occurred while retrieving a data source connection", e); + } + + try { conn.setAutoCommit(false); - currentConnection.set(conn); } catch (SQLException e) { - throw new TransactionManagementException("Error occurred while retrieving config.datasource connection", e); + try { + conn.close(); + } catch (SQLException e1) { + log.warn("Error occurred while closing the borrowed connection. " + + "Transaction has ended pre-maturely", e1); + } + throw new TransactionManagementException("Error occurred while setting auto-commit to false", e); } + currentConnection.set(conn); } public static void openConnection() throws SQLException { @@ -111,6 +122,8 @@ public class CertificateManagementDAOFactory { conn.commit(); } catch (SQLException e) { log.error("Error occurred while committing the transaction", e); + } finally { + closeConnection(); } } @@ -125,6 +138,8 @@ public class CertificateManagementDAOFactory { conn.rollback(); } catch (SQLException e) { log.warn("Error occurred while roll-backing the transaction", e); + } finally { + closeConnection(); } } @@ -138,7 +153,7 @@ public class CertificateManagementDAOFactory { try { conn.close(); } catch (SQLException e) { - log.warn("Error occurred while close the connection"); + log.warn("Error occurred while close the connection", e); } currentConnection.remove(); } 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 f71162904c4..e4332c599f9 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 @@ -674,10 +674,7 @@ public class CertificateGenerator { } catch (TransactionManagementException e) { String errorMsg = "Error occurred when saving the generated certificate"; log.error(errorMsg, e); - CertificateManagementDAOFactory.rollbackTransaction(); throw new KeystoreException(errorMsg, e); - } finally { - CertificateManagementDAOFactory.closeConnection(); } } @@ -738,9 +735,8 @@ public class CertificateGenerator { } catch (IOException e) { throw new KeystoreException("CSR cannot be recovered.", e); } - X509Certificate signedCertificate = generateCertificateFromCSR(privateKeyCA, certificationRequest, - certCA.getIssuerX500Principal().getName()); - return signedCertificate; + return generateCertificateFromCSR(privateKeyCA, certificationRequest, + certCA.getIssuerX500Principal().getName()); } public static void extractCertificateDetails(byte[] certificateBytes, CertificateResponse certificateResponse) 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 c7d5978fa67..3faf94abb96 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 @@ -19,6 +19,7 @@ package org.wso2.carbon.certificate.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.certificate.mgt.core.dao.CertificateDAO; import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOException; import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory; import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse; @@ -41,17 +42,21 @@ public class KeyStoreReader { private static final Log log = LogFactory.getLog(KeyStoreReader.class); - private KeyStore loadKeyStore(String configEntryKeyStoreType, String configEntryKeyStorePath, - String configEntryKeyStorePassword) throws KeystoreException { + private CertificateDAO certDao; - InputStream inputStream = null; - KeyStore keystore; + public KeyStoreReader() { + this.certDao = CertificateManagementDAOFactory.getCertificateDAO(); + } + private KeyStore loadKeyStore( + String configEntryKeyStoreType, String configEntryKeyStorePath, + String configEntryKeyStorePassword) throws KeystoreException { + InputStream is = null; + KeyStore keystore; try { keystore = KeyStore.getInstance(ConfigurationUtil.getConfigEntry(configEntryKeyStoreType)); - inputStream = new FileInputStream(ConfigurationUtil.getConfigEntry(configEntryKeyStorePath)); - keystore.load(inputStream, ConfigurationUtil.getConfigEntry(configEntryKeyStorePassword).toCharArray()); - + is = new FileInputStream(ConfigurationUtil.getConfigEntry(configEntryKeyStorePath)); + keystore.load(is, ConfigurationUtil.getConfigEntry(configEntryKeyStorePassword).toCharArray()); } catch (KeyStoreException e) { String errorMsg = "KeyStore issue occurred when loading KeyStore"; log.error(errorMsg, e); @@ -74,8 +79,8 @@ public class KeyStoreReader { throw new KeystoreException(errorMsg, e); } finally { try { - if (inputStream != null) { - inputStream.close(); + if (is != null) { + is.close(); } } catch (IOException e) { log.error("Error closing KeyStore input stream", e); @@ -86,16 +91,12 @@ public class KeyStoreReader { } private synchronized void saveKeyStore(KeyStore keyStore, String configEntryKeyStorePath, - String configEntryKeyStorePassword) throws KeystoreException { - - FileOutputStream outputStream = null; - + String configEntryKeyStorePassword) throws KeystoreException { + FileOutputStream os = null; try { - outputStream = new FileOutputStream( + os = new FileOutputStream( ConfigurationUtil.getConfigEntry(configEntryKeyStorePath)); - keyStore.store(outputStream, ConfigurationUtil.getConfigEntry(configEntryKeyStorePassword).toCharArray()); - outputStream.close(); - + keyStore.store(os, ConfigurationUtil.getConfigEntry(configEntryKeyStorePassword).toCharArray()); } catch (KeyStoreException e) { String errorMsg = "KeyStore issue occurred when loading KeyStore"; log.error(errorMsg, e); @@ -118,8 +119,8 @@ public class KeyStoreReader { throw new KeystoreException(errorMsg, e); } finally { try { - if (outputStream != null) { - outputStream.close(); + if (os != null) { + os.close(); } } catch (IOException e) { log.error("Error closing KeyStore output stream", e); @@ -139,10 +140,8 @@ public class KeyStoreReader { } public Certificate getCACertificate() throws KeystoreException { - KeyStore keystore = loadCertificateKeyStore(); Certificate caCertificate; - try { caCertificate = keystore.getCertificate(ConfigurationUtil.getConfigEntry(ConfigurationUtil.CA_CERT_ALIAS)); } catch (KeyStoreException e) { @@ -188,7 +187,6 @@ public class KeyStoreReader { } public Certificate getRACertificate() throws KeystoreException { - KeyStore keystore = loadCertificateKeyStore(); Certificate raCertificate; try { @@ -207,13 +205,11 @@ public class KeyStoreReader { } public Certificate getCertificateByAlias(String alias) throws KeystoreException { - Certificate raCertificate = null; try { CertificateManagementDAOFactory.openConnection(); - CertificateResponse certificateResponse = CertificateManagementDAOFactory.getCertificateDAO(). - retrieveCertificate(alias); - if(certificateResponse != null) { + CertificateResponse certificateResponse = certDao.retrieveCertificate(alias); + if (certificateResponse != null) { raCertificate = (Certificate) Serializer.deserialize(certificateResponse.getCertificate()); } } catch (CertificateManagementDAOException e) { @@ -221,7 +217,7 @@ public class KeyStoreReader { log.error(errorMsg, e); throw new KeystoreException(errorMsg, e); } catch (ClassNotFoundException | IOException e) { - String errorMsg = "Error when deserializing saved certificate."; + String errorMsg = "Error when de-serializing saved certificate."; log.error(errorMsg, e); throw new KeystoreException(errorMsg, e); } catch (SQLException e) { @@ -234,8 +230,7 @@ public class KeyStoreReader { return raCertificate; } - PrivateKey getRAPrivateKey() throws KeystoreException { - + public PrivateKey getRAPrivateKey() throws KeystoreException { KeyStore keystore = loadCertificateKeyStore(); PrivateKey raPrivateKey; try { @@ -264,13 +259,11 @@ public class KeyStoreReader { } public CertificateResponse getCertificateBySerial(String serialNumber) throws KeystoreException { - CertificateResponse certificateResponse = null; try { CertificateManagementDAOFactory.openConnection(); - certificateResponse = CertificateManagementDAOFactory.getCertificateDAO(). - retrieveCertificate(serialNumber); - if(certificateResponse != null && certificateResponse.getCertificate() != null) { + certificateResponse = certDao.retrieveCertificate(serialNumber); + if (certificateResponse != null && certificateResponse.getCertificate() != null) { Certificate certificate = (Certificate) Serializer.deserialize(certificateResponse.getCertificate()); if (certificate instanceof X509Certificate) { X509Certificate x509cert = (X509Certificate) certificate; @@ -278,10 +271,9 @@ public class KeyStoreReader { certificateResponse.setCommonName(commonName); } } - } catch (CertificateManagementDAOException e) { String errorMsg = "Error when retrieving certificate from the the database for the serial number: " + - serialNumber; + serialNumber; log.error(errorMsg, e); throw new KeystoreException(errorMsg, e); } catch (SQLException e) { @@ -289,7 +281,7 @@ public class KeyStoreReader { log.error(errorMsg, e); throw new KeystoreException(errorMsg, e); } catch (ClassNotFoundException | IOException e) { - String errorMsg = "Error when deserializing saved certificate."; + String errorMsg = "Error when de-serializing saved certificate."; log.error(errorMsg, e); throw new KeystoreException(errorMsg, e); } finally { 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 d294dbc224d..04d4c8c35f1 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 @@ -18,7 +18,7 @@ package org.wso2.carbon.certificate.mgt.core.service; import org.bouncycastle.pkcs.PKCS10CertificationRequest; -import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOException; +import org.wso2.carbon.certificate.mgt.core.exception.CertificateManagementException; import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse; import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse; import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; @@ -47,8 +47,8 @@ public interface CertificateManagementService { byte[] getPKIMessageSCEP(InputStream inputStream) throws KeystoreException; - X509Certificate generateCertificateFromCSR(PrivateKey privateKey, PKCS10CertificationRequest request, - String issueSubject) throws KeystoreException; + X509Certificate generateCertificateFromCSR( + PrivateKey privateKey, PKCS10CertificationRequest request, String issueSubject) throws KeystoreException; Certificate getCertificateByAlias(String alias) throws KeystoreException; @@ -71,13 +71,14 @@ public interface CertificateManagementService { public X509Certificate pemToX509Certificate(String pem) throws KeystoreException; - public CertificateResponse retrieveCertificate(String serialNumber) throws CertificateManagementDAOException; + public CertificateResponse retrieveCertificate(String serialNumber) throws CertificateManagementException; - public PaginationResult getAllCertificates(PaginationRequest request) throws CertificateManagementDAOException; + public PaginationResult getAllCertificates(PaginationRequest request) throws CertificateManagementException; - boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException; + boolean removeCertificate(String serialNumber) throws CertificateManagementException; - public List getCertificates() throws CertificateManagementDAOException; + public List getCertificates() throws CertificateManagementException; + + public List searchCertificates(String serialNumber) throws CertificateManagementException; - public List searchCertificates(String serialNumber) throws CertificateManagementDAOException; } 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 875704784db..26902e7248f 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 @@ -25,6 +25,7 @@ import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOExceptio import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory; import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse; import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse; +import org.wso2.carbon.certificate.mgt.core.exception.CertificateManagementException; import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import org.wso2.carbon.certificate.mgt.core.impl.CertificateGenerator; import org.wso2.carbon.certificate.mgt.core.impl.KeyStoreReader; @@ -51,7 +52,6 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe } public static CertificateManagementServiceImpl getInstance() { - if (certificateManagementServiceImpl == null) { certificateManagementServiceImpl = new CertificateManagementServiceImpl(); keyStoreReader = new KeyStoreReader(); @@ -106,7 +106,8 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe return certificateGenerator.verifyPEMSignature(requestCertificate); } - @Override public CertificateResponse verifySubjectDN(String requestDN) throws KeystoreException { + @Override + public CertificateResponse verifySubjectDN(String requestDN) throws KeystoreException { return certificateGenerator.verifyCertificateDN(requestDN); } @@ -135,39 +136,47 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe return certificateGenerator.pemToX509Certificate(pem); } - public CertificateResponse retrieveCertificate(String serialNumber) - throws CertificateManagementDAOException { + public CertificateResponse retrieveCertificate(String serialNumber) throws CertificateManagementException { CertificateDAO certificateDAO; try { CertificateManagementDAOFactory.openConnection(); certificateDAO = CertificateManagementDAOFactory.getCertificateDAO(); return certificateDAO.retrieveCertificate(serialNumber); } catch (SQLException e) { - String errorMsg = "Error when opening connection"; - log.error(errorMsg, e); - throw new CertificateManagementDAOException(errorMsg, e); + String msg = "Error occurred while opening a connection to the underlying data source"; + log.error(msg, e); + throw new CertificateManagementException(msg, e); + } catch (CertificateManagementDAOException e) { + String msg = "Error occurred while looking up for the certificate carrying the serial number '" + + serialNumber + "' in the underlying certificate repository"; + log.error(msg, e); + throw new CertificateManagementException(msg, e); } finally { CertificateManagementDAOFactory.closeConnection(); } } - public PaginationResult getAllCertificates(PaginationRequest request) - throws CertificateManagementDAOException { + public PaginationResult getAllCertificates(PaginationRequest request) throws CertificateManagementException { try { CertificateManagementDAOFactory.openConnection(); CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO(); return certificateDAO.getAllCertificates(request); } catch (SQLException e) { - String errorMsg = "Error when opening connection"; - log.error(errorMsg, e); - throw new CertificateManagementDAOException(errorMsg, e); + String msg = "Error occurred while opening a connection to the underlying data source"; + log.error(msg, e); + throw new CertificateManagementException(msg, e); + } catch (CertificateManagementDAOException e) { + String msg = "Error occurred while looking up for the list of certificates managed in the underlying " + + "certificate repository"; + log.error(msg, e); + throw new CertificateManagementException(msg, e); } finally { CertificateManagementDAOFactory.closeConnection(); } } @Override - public boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException { + public boolean removeCertificate(String serialNumber) throws CertificateManagementException { try { CertificateManagementDAOFactory.beginTransaction(); CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO(); @@ -175,38 +184,53 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe CertificateManagementDAOFactory.commitTransaction(); return status; } catch (TransactionManagementException e) { - String errorMsg = "Error when deleting"; - log.error(errorMsg, e); - throw new CertificateManagementDAOException(errorMsg, e); - } finally { - CertificateManagementDAOFactory.closeConnection(); + String msg = "Error occurred while removing certificate carrying serial number '" + serialNumber + "'"; + log.error(msg, e); + throw new CertificateManagementException(msg, e); + } catch (CertificateManagementDAOException e) { + CertificateManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while removing the certificate carrying serial number '" + serialNumber + + "' from the certificate repository"; + log.error(msg, e); + throw new CertificateManagementException(msg, e); } } @Override - public List getCertificates() throws CertificateManagementDAOException { + public List getCertificates() throws CertificateManagementException { try { CertificateManagementDAOFactory.openConnection(); CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO(); return certificateDAO.getAllCertificates(); } catch (SQLException e) { - String errorMsg = "Error when opening connection"; - log.error(errorMsg, e); - throw new CertificateManagementDAOException(errorMsg, e); + String msg = "Error occurred while opening a connection to the underlying data source"; + log.error(msg, e); + throw new CertificateManagementException(msg, e); + } catch (CertificateManagementDAOException e) { + String msg = "Error occurred while looking up for the list of certificates managed in the " + + "underlying certificate repository"; + log.error(msg, e); + throw new CertificateManagementException(msg, e); } finally { CertificateManagementDAOFactory.closeConnection(); } } - @Override public List searchCertificates(String serialNumber) throws CertificateManagementDAOException { + @Override + public List searchCertificates(String serialNumber) throws CertificateManagementException { try { CertificateManagementDAOFactory.openConnection(); CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO(); return certificateDAO.searchCertificate(serialNumber); } catch (SQLException e) { - String errorMsg = "Error when opening connection"; - log.error(errorMsg, e); - throw new CertificateManagementDAOException(errorMsg, e); + String msg = "Error occurred while opening a connection to the underlying data source"; + log.error(msg, e); + throw new CertificateManagementException(msg, e); + } catch (CertificateManagementDAOException e) { + String msg = "Error occurred while searching for the list of certificates carrying the serial number '" + + serialNumber + "' in the underlying certificate repository"; + log.error(msg, e); + throw new CertificateManagementException(msg, e); } finally { CertificateManagementDAOFactory.closeConnection(); } 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 d60e60d0972..d7dd0e5a70d 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 @@ -121,8 +121,8 @@ public class OperationManagerImpl implements OperationManager { notificationStrategy.execute(new NotificationContext(deviceId, operation)); } catch (PushNotificationExecutionFailedException e) { log.error("Error occurred while sending push notifications to " + - deviceId.getType() + " device carrying id '" + - deviceId + "'", e); + deviceId.getType() + " device carrying id '" + + deviceId + "'", e); } } } @@ -188,7 +188,7 @@ public class OperationManagerImpl implements OperationManager { return deviceDAO.getDevice(deviceId, tenantId); } catch (SQLException e) { throw new OperationManagementException("Error occurred while opening a connection the data " + - "source", e); + "source", e); } catch (DeviceManagementDAOException e) { OperationManagementDAOFactory.rollbackTransaction(); throw new OperationManagementException( @@ -209,44 +209,49 @@ public class OperationManagerImpl implements OperationManager { throw new UnauthorizedDeviceAccessException("User '" + getUser() + "' is not authorized to " + "fetch operations on device '" + deviceId.getId() + "'"); } - try { - try { - DeviceManagementDAOFactory.openConnection(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - if (enrolmentId < 0) { - return null; - } - OperationManagementDAOFactory.openConnection(); - List operationList = - operationDAO.getOperationsForDevice(enrolmentId); - - operations = new ArrayList<>(); - for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { - Operation operation = OperationDAOUtil.convertOperation(dtoOperation); - operations.add(operation); - } - } catch (OperationManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); - } catch (DeviceManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving metadata of '" + - deviceId.getType() + "' device carrying the identifier '" + - deviceId.getId() + "'"); - } catch (SQLException e) { - throw new OperationManagementException( - "Error occurred while opening a connection to the data source", e); - } finally { - OperationManagementDAOFactory.closeConnection(); - } } catch (DeviceAccessAuthorizationException e) { throw new OperationManagementException("Error occurred while authorizing access to the devices for user : " + this.getUser(), e); } + + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); + } catch (DeviceManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving metadata of '" + + deviceId.getType() + "' device carrying the identifier '" + + deviceId.getId() + "'"); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + try { + if (enrolmentId < 0) { + return null; + } + OperationManagementDAOFactory.openConnection(); + List operationList = + operationDAO.getOperationsForDevice(enrolmentId); + + operations = new ArrayList<>(); + for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { + Operation operation = OperationDAOUtil.convertOperation(dtoOperation); + operations.add(operation); + } + } catch (OperationManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving the list of " + + "operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } return operations; } @@ -259,55 +264,59 @@ public class OperationManagerImpl implements OperationManager { try { boolean isUserAuthorized = DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService(). isUserAuthorized(deviceId, DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS); - if (isUserAuthorized) { - try { - try { - DeviceManagementDAOFactory.openConnection(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - OperationManagementDAOFactory.openConnection(); - if (enrolmentId < 0) { - throw new OperationManagementException("Device not found for given device " + - "Identifier:" + deviceId.getId() + " and given type" + - deviceId.getType()); - } - List operationList = - operationDAO.getOperationsForDevice(enrolmentId, request); - for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { - Operation operation = OperationDAOUtil.convertOperation(dtoOperation); - operations.add(operation); - } - paginationResult = new PaginationResult(); - int count = operationDAO.getOperationCountForDevice(enrolmentId); - paginationResult.setData(operations); - paginationResult.setRecordsTotal(count); - paginationResult.setRecordsFiltered(count); - } catch (OperationManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); - } catch (DeviceManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving metadata of '" + - deviceId.getType() + "' device carrying the identifier '" + - deviceId.getId() + "'"); - } catch (SQLException e) { - throw new OperationManagementException( - "Error occurred while opening a connection to the data source", e); - } finally { - OperationManagementDAOFactory.closeConnection(); - } - } else { - log.info("User : " + getUser() + " is not authorized to fetch operations on device : " + deviceId.getId()); + if (!isUserAuthorized) { + log.error("User : " + getUser() + " is not authorized to fetch operations on device : " + + deviceId.getId()); } } catch (DeviceAccessAuthorizationException e) { throw new OperationManagementException("Error occurred while authorizing access to the devices for user : " + this.getUser(), e); } + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } catch (DeviceManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving metadata of '" + + deviceId.getType() + "' device carrying the identifier '" + + deviceId.getId() + "'"); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + try { + OperationManagementDAOFactory.openConnection(); + if (enrolmentId < 0) { + throw new OperationManagementException("Device not found for given device " + + "Identifier:" + deviceId.getId() + " and given type" + + deviceId.getType()); + } + List operationList = + operationDAO.getOperationsForDevice(enrolmentId, request); + for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { + Operation operation = OperationDAOUtil.convertOperation(dtoOperation); + operations.add(operation); + } + paginationResult = new PaginationResult(); + int count = operationDAO.getOperationCountForDevice(enrolmentId); + paginationResult.setData(operations); + paginationResult.setRecordsTotal(count); + paginationResult.setRecordsFiltered(count); + } catch (OperationManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving the list of " + + "operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } + return paginationResult; } @@ -323,57 +332,61 @@ public class OperationManagerImpl implements OperationManager { try { boolean isUserAuthorized = DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService(). isUserAuthorized(deviceId, DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS); - if (isUserAuthorized) { - try { - try { - DeviceManagementDAOFactory.openConnection(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - OperationManagementDAOFactory.openConnection(); - if (enrolmentId < 0) { - throw new OperationManagementException("Device not found for the given device Identifier:" + - deviceId.getId() + " and given type:" + - deviceId.getType()); - } - dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus( - enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus( - enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus( - enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus( - enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - Operation operation; - for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) { - operation = OperationDAOUtil.convertOperation(dtoOperation); - operations.add(operation); - } - Collections.sort(operations, new OperationCreateTimeComparator()); - } catch (OperationManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving the list of " + - "pending operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); - } catch (DeviceManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving the device " + - "for device Identifier type -'" + deviceId.getType() + - "' and device Id '" + deviceId.getId() + "'", e); - } catch (SQLException e) { - throw new OperationManagementException( - "Error occurred while opening a connection to the data source", e); - } finally { - OperationManagementDAOFactory.closeConnection(); - } - } else { - log.info("User : " + getUser() + " is not authorized to fetch operations on device : " + if (!isUserAuthorized) { + log.error("User : " + getUser() + " is not authorized to fetch operations on device : " + deviceId.getId()); } } catch (DeviceAccessAuthorizationException e) { throw new OperationManagementException("Error occurred while authorizing access to the devices for user :" + this.getUser(), e); } + + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } catch (DeviceManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving the device " + + "for device Identifier type -'" + deviceId.getType() + + "' and device Id '" + deviceId.getId() + "'", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + try { + OperationManagementDAOFactory.openConnection(); + if (enrolmentId < 0) { + throw new OperationManagementException("Device not found for the given device Identifier:" + + deviceId.getId() + " and given type:" + + deviceId.getType()); + } + dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus( + enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus( + enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus( + enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus( + enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + Operation operation; + for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) { + operation = OperationDAOUtil.convertOperation(dtoOperation); + operations.add(operation); + } + Collections.sort(operations, new OperationCreateTimeComparator()); + } catch (OperationManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving the list of " + + "pending operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } return operations; } @@ -387,63 +400,67 @@ public class OperationManagerImpl implements OperationManager { try { boolean isUserAuthorized = DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService(). isUserAuthorized(deviceId, DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS); - if (isUserAuthorized) { - try { - try { - DeviceManagementDAOFactory.openConnection(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - OperationManagementDAOFactory.openConnection(); - if (enrolmentId < 0) { - throw new OperationManagementException("Device not found for given device " + - "Identifier:" + deviceId.getId() + " and given type" + - deviceId.getType()); - } - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO. - getNextOperation(enrolmentId); - if (dtoOperation != null) { - if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND. - equals(dtoOperation.getType())) { - org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; - commandOperation = - (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. - getOperation(dtoOperation.getId()); - dtoOperation.setEnabled(commandOperation.isEnabled()); - } else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG. - equals(dtoOperation.getType())) { - dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); - } else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.PROFILE. - equals(dtoOperation.getType())) { - dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); - } else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.POLICY. - equals(dtoOperation.getType())) { - dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); - } - operation = OperationDAOUtil.convertOperation(dtoOperation); - } - } catch (OperationManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving next pending operation", e); - } catch (DeviceManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving the device " + - "for device Identifier type -'" + deviceId.getType() + - "' and device Id '" + deviceId.getId(), e); - } catch (SQLException e) { - throw new OperationManagementException( - "Error occurred while opening a connection to the data source", e); - } finally { - OperationManagementDAOFactory.closeConnection(); - } - } else { - log.info("User : " + getUser() + " is not authorized to fetch operations on device : " + if (!isUserAuthorized) { + log.error("User : " + getUser() + " is not authorized to fetch operations on device : " + deviceId.getId()); } } catch (DeviceAccessAuthorizationException e) { throw new OperationManagementException("Error occurred while authorizing access to the devices for user : " + this.getUser(), e); } + + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); + } catch (DeviceManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving the device " + + "for device Identifier type -'" + deviceId.getType() + + "' and device Id '" + deviceId.getId(), e); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + try { + OperationManagementDAOFactory.openConnection(); + if (enrolmentId < 0) { + throw new OperationManagementException("Device not found for given device " + + "Identifier:" + deviceId.getId() + " and given type" + + deviceId.getType()); + } + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO. + getNextOperation(enrolmentId); + if (dtoOperation != null) { + if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND. + equals(dtoOperation.getType())) { + org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; + commandOperation = + (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. + getOperation(dtoOperation.getId()); + dtoOperation.setEnabled(commandOperation.isEnabled()); + } else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG. + equals(dtoOperation.getType())) { + dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); + } else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.PROFILE. + equals(dtoOperation.getType())) { + dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); + } else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.POLICY. + equals(dtoOperation.getType())) { + dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); + } + operation = OperationDAOUtil.convertOperation(dtoOperation); + } + } catch (OperationManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving next pending operation", e); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } return operation; } @@ -457,52 +474,53 @@ public class OperationManagerImpl implements OperationManager { try { boolean isUserAuthorized = DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService(). isUserAuthorized(deviceId, DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS); - if (isUserAuthorized) { - try { - try { - DeviceManagementDAOFactory.openConnection(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - } catch (SQLException e) { - throw new OperationManagementException("Error occurred while opening a connection to the" + - " data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - OperationManagementDAOFactory.beginTransaction(); - boolean isUpdated = false; - if (operation.getStatus() != null) { - isUpdated = operationDAO.updateOperationStatus(enrolmentId, operationId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status. - valueOf(operation.getStatus().toString())); - } - if (isUpdated && operation.getOperationResponse() != null) { - operationDAO.addOperationResponse(enrolmentId, operationId, operation.getOperationResponse()); - } - OperationManagementDAOFactory.commitTransaction(); - } catch (OperationManagementDAOException e) { - OperationManagementDAOFactory.rollbackTransaction(); - throw new OperationManagementException( - "Error occurred while updating the operation: " + operationId + " status:" + - operation.getStatus(), e); - } catch (DeviceManagementDAOException e) { - OperationManagementDAOFactory.rollbackTransaction(); - throw new OperationManagementException( - "Error occurred while fetching the device for device identifier: " + deviceId.getId() + - "type:" + deviceId.getType(), e); - } catch (TransactionManagementException e) { - throw new OperationManagementException("Error occurred while initiating a transaction", e); - } finally { - OperationManagementDAOFactory.closeConnection(); - } - } else { - log.info("User : " + getUser() + " is not authorized to update operations on device : " + if (!isUserAuthorized) { + log.error("User : " + getUser() + " is not authorized to update operations on device : " + deviceId.getId()); } } catch (DeviceAccessAuthorizationException e) { throw new OperationManagementException("Error occurred while authorizing access to the devices for user :" + this.getUser(), e); } + + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); + } catch (SQLException e) { + throw new OperationManagementException("Error occurred while opening a connection to the" + + " data source", e); + } catch (DeviceManagementDAOException e) { + OperationManagementDAOFactory.rollbackTransaction(); + throw new OperationManagementException( + "Error occurred while fetching the device for device identifier: " + deviceId.getId() + + "type:" + deviceId.getType(), e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + try { + OperationManagementDAOFactory.beginTransaction(); + boolean isUpdated = false; + if (operation.getStatus() != null) { + isUpdated = operationDAO.updateOperationStatus(enrolmentId, operationId, + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status. + valueOf(operation.getStatus().toString())); + } + if (isUpdated && operation.getOperationResponse() != null) { + operationDAO.addOperationResponse(enrolmentId, operationId, operation.getOperationResponse()); + } + OperationManagementDAOFactory.commitTransaction(); + } catch (OperationManagementDAOException e) { + OperationManagementDAOFactory.rollbackTransaction(); + throw new OperationManagementException( + "Error occurred while updating the operation: " + operationId + " status:" + + operation.getStatus(), e); + } catch (TransactionManagementException e) { + throw new OperationManagementException("Error occurred while initiating a transaction", e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } } @Override @@ -538,68 +556,72 @@ public class OperationManagerImpl implements OperationManager { try { boolean isUserAuthorized = DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService(). isUserAuthorized(deviceId, DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS); - if (isUserAuthorized) { - try { - try { - DeviceManagementDAOFactory.openConnection(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - OperationManagementDAOFactory.openConnection(); - if (enrolmentId < 0) { - throw new OperationManagementException("Device not found for given device identifier: " + - deviceId.getId() + " type: " + deviceId.getType()); - } - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO. - getOperationByDeviceAndId(enrolmentId, operationId); - if (dtoOperation.getType(). - equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { - org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; - commandOperation = - (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. - getOperation(dtoOperation.getId()); - dtoOperation.setEnabled(commandOperation.isEnabled()); - } else if (dtoOperation.getType(). - equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { - dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); - } else if (dtoOperation.getType().equals( - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.PROFILE)) { - dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); - } else if (dtoOperation.getType().equals( - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.POLICY)) { - dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); - } - - if (dtoOperation == null) { - throw new OperationManagementException("Operation not found for operation Id:" + operationId + - " device id:" + deviceId.getId()); - } - operation = OperationDAOUtil.convertOperation(dtoOperation); - } catch (OperationManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); - } catch (DeviceManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving the device " + - "for device Identifier type -'" + deviceId.getType() + - "' and device Id '" + deviceId.getId() + "'", e); - } catch (SQLException e) { - throw new OperationManagementException("Error occurred while opening connection to the data source", - e); - } finally { - OperationManagementDAOFactory.closeConnection(); - } - } else { - log.info("User : " + getUser() + " is not authorized to fetch operations on device : " + if (!isUserAuthorized) { + log.error("User : " + getUser() + " is not authorized to fetch operations on device : " + deviceId.getId()); } } catch (DeviceAccessAuthorizationException e) { throw new OperationManagementException("Error occurred while authorizing access to the devices for user :" + this.getUser(), e); } + + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); + } catch (DeviceManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving the device " + + "for device Identifier type -'" + deviceId.getType() + + "' and device Id '" + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new OperationManagementException("Error occurred while opening connection to the data source", + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + try { + OperationManagementDAOFactory.openConnection(); + if (enrolmentId < 0) { + throw new OperationManagementException("Device not found for given device identifier: " + + deviceId.getId() + " type: " + deviceId.getType()); + } + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO. + getOperationByDeviceAndId(enrolmentId, operationId); + if (dtoOperation.getType(). + equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { + org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; + commandOperation = + (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. + getOperation(dtoOperation.getId()); + dtoOperation.setEnabled(commandOperation.isEnabled()); + } else if (dtoOperation.getType(). + equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { + dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); + } else if (dtoOperation.getType().equals( + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.PROFILE)) { + dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); + } else if (dtoOperation.getType().equals( + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.POLICY)) { + dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); + } + + if (dtoOperation == null) { + throw new OperationManagementException("Operation not found for operation Id:" + operationId + + " device id:" + deviceId.getId()); + } + operation = OperationDAOUtil.convertOperation(dtoOperation); + } catch (OperationManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving the list of " + + "operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new OperationManagementException("Error occurred while opening connection to the data source", + e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } + return operation; } @@ -612,56 +634,7 @@ public class OperationManagerImpl implements OperationManager { try { boolean isUserAuthorized = DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService(). isUserAuthorized(deviceId, DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS); - if (isUserAuthorized) { - try { - try { - DeviceManagementDAOFactory.openConnection(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - OperationManagementDAOFactory.openConnection(); - - if (enrolmentId < 0) { - throw new OperationManagementException( - "Device not found for device id:" + deviceId.getId() + " " + "type:" + - deviceId.getType()); - } - - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status dtoOpStatus = - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.valueOf(status.toString()); - dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, dtoOpStatus)); - dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - - Operation operation; - - for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) { - operation = OperationDAOUtil.convertOperation(dtoOperation); - operations.add(operation); - } - - } catch (OperationManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + - deviceId.getId() + "' and status:" + status.toString(), e); - } catch (DeviceManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving the device " + - "for device Identifier type -'" + deviceId.getType() + - "' and device Id '" + deviceId.getId(), e); - } catch (SQLException e) { - throw new OperationManagementException( - "Error occurred while opening a connection to the data source", e); - } finally { - OperationManagementDAOFactory.closeConnection(); - } - } else { + if (!isUserAuthorized) { log.info("User : " + getUser() + " is not authorized to fetch operations on device : " + deviceId.getId()); } @@ -669,6 +642,59 @@ public class OperationManagerImpl implements OperationManager { throw new OperationManagementException("Error occurred while authorizing access to the devices for user :" + this.getUser(), e); } + + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); + } catch (DeviceManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving the device " + + "for device Identifier type -'" + deviceId.getType() + + "' and device Id '" + deviceId.getId(), e); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + try { + OperationManagementDAOFactory.openConnection(); + + if (enrolmentId < 0) { + throw new OperationManagementException( + "Device not found for device id:" + deviceId.getId() + " " + "type:" + + deviceId.getType()); + } + + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status dtoOpStatus = + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.valueOf(status.toString()); + dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, dtoOpStatus)); + dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + + Operation operation; + + for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) { + operation = OperationDAOUtil.convertOperation(dtoOperation); + operations.add(operation); + } + + } catch (OperationManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving the list of " + + "operations assigned for '" + deviceId.getType() + + "' device '" + + deviceId.getId() + "' and status:" + status.toString(), e); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } return operations; } @@ -809,7 +835,8 @@ public class OperationManagerImpl implements OperationManager { } @Override - public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException { + public List getActivitiesUpdatedAfter(long timestamp, int limit, + int offset) throws OperationManagementException { try { OperationManagementDAOFactory.openConnection(); return operationDAO.getActivitiesUpdatedAfter(timestamp, limit, offset); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/Search/util/Utils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/Search/util/Utils.java index 8cc205f210c..58c6f621ac8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/Search/util/Utils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/Search/util/Utils.java @@ -19,7 +19,6 @@ package org.wso2.carbon.device.mgt.core.search.util; -import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml index 50e79984474..daddc132a19 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml @@ -49,10 +49,10 @@ org.wso2.carbon.device.mgt.api.feature zip - - org.wso2.carbon.commons - org.wso2.carbon.email.verification - + + + + @@ -114,9 +114,9 @@ org.wso2.carbon.devicemgt:org.wso2.carbon.device.mgt.common:${carbon.device.mgt.version} - - org.wso2.carbon.commons:org.wso2.carbon.email.verification - + + + org.wso2.carbon.identity:org.wso2.carbon.identity.oauth.stub:${carbon.identity.version} diff --git a/pom.xml b/pom.xml index bc5f7311a22..d9afb815108 100644 --- a/pom.xml +++ b/pom.xml @@ -757,11 +757,11 @@ ${axiom.wso2.version} - - org.wso2.carbon.commons - org.wso2.carbon.email.verification - ${carbon.commons.version} - + + + + + @@ -1739,7 +1739,7 @@ 6.1.1 - 4.4.3 + 4.4.7 [4.4.0, 5.0.0) 1.5.4 1.3 @@ -1791,8 +1791,8 @@ 4.5.8 - 1.2.11-wso2v5 - [1.2.11.wso2v5, 1.3.0) + 1.2.11-wso2v11 + [1.2.11, 1.3.0) 1.2.11.wso2v5