Autopsy  4.15.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
CentralRepoAccount.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.centralrepository.datamodel;
20 
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Objects;
27 import org.sleuthkit.datamodel.Account;
28 import org.sleuthkit.datamodel.CommunicationsUtils;
29 import static org.sleuthkit.datamodel.CommunicationsUtils.normalizeEmailAddress;
30 import org.sleuthkit.datamodel.TskCoreException;
31 
35 public final class CentralRepoAccount {
36 
37  // primary key in the Accounts table in CR database
38  private final long accountId;
39 
41 
42  // type specific unique account identifier
43  private final String typeSpecificIdentifier;
44 
49  public static final class CentralRepoAccountType {
50 
51  // id is the primary key in the account_types table
52  private final int accountTypeId;
53  private final Account.Type acctType;
54  private final int correlationTypeId;
55 
56  CentralRepoAccountType(int acctTypeID, Account.Type acctType, int correlationTypeId) {
57  this.acctType = acctType;
58  this.correlationTypeId = correlationTypeId;
59  this.accountTypeId = acctTypeID;
60  }
61 
65  public Account.Type getAcctType() {
66  return acctType;
67  }
68 
69  public int getCorrelationTypeId() {
70  return this.correlationTypeId;
71  }
72 
73  public int getAccountTypeId() {
74  return this.accountTypeId;
75  }
76 
77  @Override
78  public int hashCode() {
79  int hash = 5;
80  hash = 29 * hash + this.accountTypeId;
81  hash = 29 * hash + Objects.hashCode(this.acctType);
82  hash = 29 * hash + this.correlationTypeId;
83  return hash;
84  }
85 
86  @Override
87  public boolean equals(Object obj) {
88  if (this == obj) {
89  return true;
90  }
91  if (obj == null) {
92  return false;
93  }
94  if (getClass() != obj.getClass()) {
95  return false;
96  }
98  if (this.accountTypeId != other.getAccountTypeId()) {
99  return false;
100  }
101  if (this.correlationTypeId != other.getCorrelationTypeId()) {
102  return false;
103  }
104  return Objects.equals(this.acctType, other.getAcctType());
105  }
106 
107  }
108 
109  public CentralRepoAccount(long accountId, CentralRepoAccountType accountType, String typeSpecificIdentifier) {
110  this.accountId = accountId;
111  this.accountType = accountType;
112  this.typeSpecificIdentifier = typeSpecificIdentifier;
113  }
114 
121  public String getIdentifier() {
122  return this.typeSpecificIdentifier;
123  }
124 
131  return this.accountType;
132  }
133 
139  public long getId() {
140  return this.accountId;
141  }
142 
143  @Override
144  public int hashCode() {
145  int hash = 5;
146  hash = 43 * hash + (int) (this.accountId ^ (this.accountId >>> 32));
147  hash = 43 * hash + (this.accountType != null ? this.accountType.hashCode() : 0);
148  hash = 43 * hash + (this.typeSpecificIdentifier != null ? this.typeSpecificIdentifier.hashCode() : 0);
149  return hash;
150  }
151 
152  @Override
153  public boolean equals(Object obj) {
154  if (this == obj) {
155  return true;
156  }
157  if (obj == null) {
158  return false;
159  }
160  if (getClass() != obj.getClass()) {
161  return false;
162  }
163  final CentralRepoAccount other = (CentralRepoAccount) obj;
164  if (this.accountId != other.getId()) {
165  return false;
166  }
167  if (!Objects.equals(this.typeSpecificIdentifier, other.getIdentifier())) {
168  return false;
169  }
170  return Objects.equals(this.accountType, other.getAccountType());
171  }
172 
176  private static class AccountsQueryCallback implements CentralRepositoryDbQueryCallback {
177 
178  Collection<CentralRepoAccount> accountsList = new ArrayList<>();
179 
180  @Override
181  public void process(ResultSet rs) throws CentralRepoException, SQLException {
182 
183  while (rs.next()) {
184 
185  // create account
186  Account.Type acctType = new Account.Type(rs.getString("type_name"), rs.getString("display_name"));
187  CentralRepoAccountType crAccountType = new CentralRepoAccountType(rs.getInt("account_type_id"), acctType, rs.getInt("correlation_type_id"));
188 
190  rs.getInt("account_id"),
191  crAccountType,
192  rs.getString("account_unique_identifier"));
193 
194  accountsList.add(account);
195  }
196  }
197 
198  Collection<CentralRepoAccount> getAccountsList() {
199  return Collections.unmodifiableCollection(accountsList);
200  }
201  };
202 
203  private static final String ACCOUNTS_QUERY_CLAUSE
204  = "SELECT accounts.id as account_id, "
205  + " accounts.account_type_id as account_type_id, accounts.account_unique_identifier as account_unique_identifier,"
206  + " account_types.id as account_type_id, "
207  + " account_types.type_name as type_name, account_types.display_name as display_name, account_types.correlation_type_id as correlation_type_id "
208  + " FROM accounts "
209  + " JOIN account_types as account_types on accounts.account_type_id = account_types.id ";
210 
223  public static Collection<CentralRepoAccount> getAccountsWithIdentifierLike(String accountIdentifierSubstring) throws CentralRepoException {
224 
225  String queryClause = ACCOUNTS_QUERY_CLAUSE
226  + " WHERE LOWER(accounts.account_unique_identifier) LIKE LOWER('%" + accountIdentifierSubstring + "%')";
227 
228  AccountsQueryCallback queryCallback = new AccountsQueryCallback();
229  CentralRepository.getInstance().executeSelectSQL(queryClause, queryCallback);
230 
231  return queryCallback.getAccountsList();
232  }
233 
245  public static Collection<CentralRepoAccount> getAccountsWithIdentifier(String accountIdentifier) throws CentralRepoException {
246 
247  String normalizedAccountIdentifier;
248 
249  try {
250  normalizedAccountIdentifier = normalizeAccountIdentifier(accountIdentifier);
251  } catch (TskCoreException ex) {
252  throw new CentralRepoException("Failed to normalize account identifier.", ex);
253  }
254 
255  String queryClause = ACCOUNTS_QUERY_CLAUSE
256  + " WHERE LOWER(accounts.account_unique_identifier) = LOWER('" + normalizedAccountIdentifier + "')";
257 
258  AccountsQueryCallback queryCallback = new AccountsQueryCallback();
259  CentralRepository.getInstance().executeSelectSQL(queryClause, queryCallback);
260 
261  return queryCallback.getAccountsList();
262  }
263 
273  public static Collection<CentralRepoAccount> getAllAccounts() throws CentralRepoException {
274 
275  String queryClause = ACCOUNTS_QUERY_CLAUSE;
276 
277  AccountsQueryCallback queryCallback = new AccountsQueryCallback();
278  CentralRepository.getInstance().executeSelectSQL(queryClause, queryCallback);
279 
280  return queryCallback.getAccountsList();
281  }
282 
292  private static String normalizeAccountIdentifier(String accountIdentifier) throws TskCoreException {
293  String normalizedAccountIdentifier = accountIdentifier;
294  if (CommunicationsUtils.isValidPhoneNumber(accountIdentifier)) {
295  normalizedAccountIdentifier = CommunicationsUtils.normalizePhoneNum(accountIdentifier);
296  } else if (CommunicationsUtils.isValidEmailAddress(accountIdentifier)) {
297  normalizedAccountIdentifier = normalizeEmailAddress(accountIdentifier);
298  }
299  return normalizedAccountIdentifier;
300  }
301 }
void executeSelectSQL(String sql, CentralRepositoryDbQueryCallback queryCallback)
CentralRepoAccount(long accountId, CentralRepoAccountType accountType, String typeSpecificIdentifier)
static Collection< CentralRepoAccount > getAccountsWithIdentifier(String accountIdentifier)
static Collection< CentralRepoAccount > getAccountsWithIdentifierLike(String accountIdentifierSubstring)

Copyright © 2012-2020 Basis Technology. Generated on: Mon Jul 6 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.