From 715e4718fd958d99fbf3037411a06ce76e216980 Mon Sep 17 00:00:00 2001 From: Kelly Kelly Date: Fri, 19 Jun 2020 11:21:25 -0400 Subject: [PATCH] CentralRepository getInstance will throw exception when CR is disabled --- .../datamodel/CentralRepository.java | 2 +- .../datamodel/PersonaAccount.java | 116 ++++++------------ 2 files changed, 39 insertions(+), 79 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/CentralRepository.java b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/CentralRepository.java index a6af74ad3c..9b4f610721 100755 --- a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/CentralRepository.java +++ b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/CentralRepository.java @@ -53,7 +53,7 @@ public interface CentralRepository { case SQLITE: return SqliteCentralRepo.getInstance(); default: - return null; + throw new CentralRepoException("Failed to get CentralRepository instance, Central Repositiory is not enabled."); } } diff --git a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/PersonaAccount.java b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/PersonaAccount.java index efd8b76152..e0dcf11361 100644 --- a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/PersonaAccount.java +++ b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/PersonaAccount.java @@ -121,22 +121,18 @@ public class PersonaAccount { /** * Creates an account for the specified Persona. * - * @param persona Persona for which the account is being added. - * @param account Account. + * @param persona Persona for which the account is being added. + * @param account Account. * @param justification Reason for assigning the alias, may be null. - * @param confidence Confidence level. + * @param confidence Confidence level. * * @return PersonaAccount + * * @throws CentralRepoException If there is an error in creating the - * account. + * account. */ static PersonaAccount addPersonaAccount(Persona persona, CentralRepoAccount account, String justification, Persona.Confidence confidence) throws CentralRepoException { CentralRepository cr = CentralRepository.getInstance(); - - if(cr == null) { - throw new CentralRepoException("Failed to add Persona, Central Repository is not enable"); - } - CentralRepoExaminer currentExaminer = cr.getOrInsertExaminer(System.getProperty("user.name")); Instant instant = Instant.now(); @@ -151,7 +147,7 @@ public class PersonaAccount { + currentExaminer.getId() + ")"; - CentralRepository.getInstance().executeInsertSQL(insertClause); + cr.executeInsertSQL(insertClause); String queryClause = PERSONA_ACCOUNTS_QUERY_CLAUSE + "WHERE persona_id = " + persona.getId() @@ -249,19 +245,13 @@ public class PersonaAccount { * persona_account. */ static Collection getPersonaAccountsForPersona(long personaId) throws CentralRepoException { - CentralRepository cr = CentralRepository.getInstance(); + String queryClause = PERSONA_ACCOUNTS_QUERY_CLAUSE + + " WHERE persona_accounts.persona_id = " + personaId; - if (cr != null) { - String queryClause = PERSONA_ACCOUNTS_QUERY_CLAUSE - + " WHERE persona_accounts.persona_id = " + personaId; + PersonaAccountsQueryCallback queryCallback = new PersonaAccountsQueryCallback(); + CentralRepository.getInstance().executeSelectSQL(queryClause, queryCallback); - PersonaAccountsQueryCallback queryCallback = new PersonaAccountsQueryCallback(); - cr.executeSelectSQL(queryClause, queryCallback); - - return queryCallback.getPersonaAccountsList(); - } - - return new ArrayList<>(); + return queryCallback.getPersonaAccountsList(); } /** @@ -279,16 +269,10 @@ public class PersonaAccount { + " WHERE persona_accounts.account_id = " + accountId + " AND personas.status_id != " + Persona.PersonaStatus.DELETED.getStatusId(); - CentralRepository cr = CentralRepository.getInstance(); + PersonaAccountsQueryCallback queryCallback = new PersonaAccountsQueryCallback(); + CentralRepository.getInstance().executeSelectSQL(queryClause, queryCallback); - if (cr != null) { - PersonaAccountsQueryCallback queryCallback = new PersonaAccountsQueryCallback(); - cr.executeSelectSQL(queryClause, queryCallback); - - return queryCallback.getPersonaAccountsList(); - } - - return new ArrayList<>(); + return queryCallback.getPersonaAccountsList(); } /** @@ -308,15 +292,10 @@ public class PersonaAccount { + " WHERE LOWER(accounts.account_unique_identifier) LIKE LOWER('%" + accountIdentifierSubstring + "%')" + " AND personas.status_id != " + Persona.PersonaStatus.DELETED.getStatusId(); - CentralRepository cr = CentralRepository.getInstance(); - if (cr != null) { - PersonaAccountsQueryCallback queryCallback = new PersonaAccountsQueryCallback(); - cr.executeSelectSQL(queryClause, queryCallback); + PersonaAccountsQueryCallback queryCallback = new PersonaAccountsQueryCallback(); + CentralRepository.getInstance().executeSelectSQL(queryClause, queryCallback); - return queryCallback.getPersonaAccountsList(); - } - - return new ArrayList<>(); + return queryCallback.getPersonaAccountsList(); } /** @@ -335,14 +314,10 @@ public class PersonaAccount { + " AND type_name = '" + account.getAccountType().getTypeName() + "' " + " AND personas.status_id != " + Persona.PersonaStatus.DELETED.getStatusId(); - CentralRepository cr = CentralRepository.getInstance(); - if (cr != null) { - PersonaAccountsQueryCallback queryCallback = new PersonaAccountsQueryCallback(); - cr.executeSelectSQL(queryClause, queryCallback); - return queryCallback.getPersonaAccountsList(); - } + PersonaAccountsQueryCallback queryCallback = new PersonaAccountsQueryCallback(); + CentralRepository.getInstance().executeSelectSQL(queryClause, queryCallback); + return queryCallback.getPersonaAccountsList(); - return new ArrayList<>(); } /** @@ -351,36 +326,24 @@ public class PersonaAccount { * @param id row id for the account to be removed * * @throws CentralRepoException If there is an error in removing the - * account. + * account. */ static void removePersonaAccount(long id) throws CentralRepoException { - CentralRepository cr = CentralRepository.getInstance(); - - if(cr == null) { - throw new CentralRepoException("Failed to remove persona account, Central Repo is not enabled"); - } - String deleteClause = " DELETE FROM persona_accounts WHERE id = " + id; - cr.executeDeleteSQL(deleteClause); + CentralRepository.getInstance().executeDeleteSQL(deleteClause); } - + /** * Modifies the PersonaAccount row by the given id * * @param id row id for the account to be removed * * @throws CentralRepoException If there is an error in removing the - * account. + * account. */ static void modifyPersonaAccount(long id, Persona.Confidence confidence, String justification) throws CentralRepoException { - CentralRepository cr = CentralRepository.getInstance(); - - if (cr == null) { - throw new CentralRepoException("Failed to modify persona account, Central Repo is not enabled"); - } - String updateClause = "UPDATE persona_accounts SET confidence_id = " + confidence.getLevelId() + ", justification = \"" + justification + "\" WHERE id = " + id; - cr.executeUpdateSQL(updateClause); + CentralRepository.getInstance().executeUpdateSQL(updateClause); } /** @@ -418,28 +381,25 @@ public class PersonaAccount { * @param personaId Id of the persona to look for. * * @return Collection of all accounts associated with the given persona, may - * be empty. + * be empty. + * * @throws CentralRepoException If there is an error in getting the - * accounts. + * accounts. */ static Collection getAccountsForPersona(long personaId) throws CentralRepoException { - CentralRepository cr = CentralRepository.getInstance(); - if (cr != null) { - String queryClause = "SELECT account_id, " - + " accounts.account_type_id as account_type_id, accounts.account_unique_identifier as account_unique_identifier," - + " account_types.type_name as type_name " - + " FROM persona_accounts " - + " JOIN accounts as accounts on persona_accounts.account_id = accounts.id " - + " JOIN account_types as account_types on accounts.account_type_id = account_types.id " - + " WHERE persona_accounts.persona_id = " + personaId; + String queryClause = "SELECT account_id, " + + " accounts.account_type_id as account_type_id, accounts.account_unique_identifier as account_unique_identifier," + + " account_types.type_name as type_name " + + " FROM persona_accounts " + + " JOIN accounts as accounts on persona_accounts.account_id = accounts.id " + + " JOIN account_types as account_types on accounts.account_type_id = account_types.id " + + " WHERE persona_accounts.persona_id = " + personaId; - AccountsForPersonaQueryCallback queryCallback = new AccountsForPersonaQueryCallback(); - cr.executeSelectSQL(queryClause, queryCallback); + AccountsForPersonaQueryCallback queryCallback = new AccountsForPersonaQueryCallback(); + CentralRepository.getInstance().executeSelectSQL(queryClause, queryCallback); - return queryCallback.getAccountsList(); - } + return queryCallback.getAccountsList(); - return new ArrayList<>(); } }