Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
ContactCache.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
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.guiutils;
20 
21 import com.google.common.cache.CacheBuilder;
22 import com.google.common.cache.CacheLoader;
23 import com.google.common.cache.LoadingCache;
24 import java.beans.PropertyChangeListener;
25 import java.sql.SQLException;
26 import java.util.ArrayList;
27 import java.util.EnumSet;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.TimeUnit;
33 import java.util.logging.Level;
39 import org.sleuthkit.datamodel.Account;
40 import org.sleuthkit.datamodel.BlackboardArtifact;
41 import org.sleuthkit.datamodel.BlackboardAttribute;
42 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_NAME;
43 import org.sleuthkit.datamodel.TskCoreException;
44 
51 public final class ContactCache {
52 
53  private static final Logger logger = Logger.getLogger(ContactCache.class.getName());
54 
55  private static ContactCache instance;
56 
57  private final LoadingCache<String, Map<String, List<BlackboardArtifact>>> accountMap;
58 
69  public static synchronized List<BlackboardArtifact> getContacts(Account account) throws ExecutionException {
70  return getInstance().accountMap.get("realMap").get(account.getTypeSpecificID());
71  }
72 
83  public static synchronized List<String> getContactNameList(String accountTypeSpecificID) throws TskCoreException {
84  List<BlackboardArtifact> contactList;
85  try {
86  contactList = getInstance().accountMap.get("realMap").get(accountTypeSpecificID);
87  } catch (ExecutionException ex) {
88  throw new TskCoreException("Unable to get contact list from cache", ex);
89  }
90  List<String> contactNameList = new ArrayList<>();
91 
92  if (contactList != null) {
93  for (BlackboardArtifact artifact : contactList) {
94  BlackboardAttribute attribute = artifact.getAttribute(new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.fromID(TSK_NAME.getTypeID())));
95  if (attribute != null && !contactNameList.contains(attribute.getValueString())) {
96  contactNameList.add(attribute.getValueString());
97  }
98  }
99  }
100 
101  return contactNameList;
102  }
103 
107  static synchronized void invalidateCache() {
108  getInstance().accountMap.invalidateAll();
109  }
110 
114  private ContactCache() {
115 
116  accountMap = CacheBuilder.newBuilder().expireAfterAccess(10, TimeUnit.MINUTES).build(
117  new CacheLoader<String, Map<String, List<BlackboardArtifact>>>() {
118  @Override
119  public Map<String, List<BlackboardArtifact>> load(String key) {
120  try {
121  return buildMap();
122  } catch (SQLException | TskCoreException ex) {
123  logger.log(Level.WARNING, "Failed to build account to contact map", ex);
124  }
125  return new HashMap<>(); // Return an empty map if there is an exception to avoid NPE and continual trying.
126  }
127  });
128 
129  PropertyChangeListener ingestListener = pce -> {
130  String eventType = pce.getPropertyName();
131  if (eventType.equals(DATA_ADDED.toString())) {
132  ModuleDataEvent eventData = (ModuleDataEvent) pce.getOldValue();
133  if (eventData.getBlackboardArtifactType().getTypeID() == BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT.getTypeID()) {
134  invalidateCache();
135  }
136  }
137  };
138 
139  IngestManager.getInstance().addIngestModuleEventListener(EnumSet.of(DATA_ADDED), ingestListener);
140  }
141 
147  private static synchronized ContactCache getInstance() {
148  if (instance == null) {
149  instance = new ContactCache();
150  }
151 
152  return instance;
153  }
154 
163  private Map<String, List<BlackboardArtifact>> buildMap() throws TskCoreException, SQLException {
164  Map<String, List<BlackboardArtifact>> acctMap = new HashMap<>();
165  List<BlackboardArtifact> contactList = Case.getCurrentCase().getSleuthkitCase().getBlackboardArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT);
166 
167  for (BlackboardArtifact contactArtifact : contactList) {
168  List<BlackboardAttribute> contactAttributes = contactArtifact.getAttributes();
169  for (BlackboardAttribute attribute : contactAttributes) {
170  String typeName = attribute.getAttributeType().getTypeName();
171 
172  if (typeName.startsWith("TSK_EMAIL")
173  || typeName.startsWith("TSK_PHONE")
174  || typeName.startsWith("TSK_NAME")
175  || typeName.startsWith("TSK_ID")) {
176  String accountID = attribute.getValueString();
177  List<BlackboardArtifact> artifactList = acctMap.get(accountID);
178  if (artifactList == null) {
179  artifactList = new ArrayList<>();
180  acctMap.put(accountID, artifactList);
181  }
182  if (!artifactList.contains(contactArtifact)) {
183  artifactList.add(contactArtifact);
184  }
185  }
186  }
187 
188  }
189 
190  return acctMap;
191  }
192 }
BlackboardArtifact.Type getBlackboardArtifactType()
static synchronized List< String > getContactNameList(String accountTypeSpecificID)
static synchronized IngestManager getInstance()
final LoadingCache< String, Map< String, List< BlackboardArtifact > > > accountMap
Map< String, List< BlackboardArtifact > > buildMap()
static synchronized List< BlackboardArtifact > getContacts(Account account)
static synchronized ContactCache getInstance()
void addIngestModuleEventListener(final PropertyChangeListener listener)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Jun 27 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.