Autopsy 4.22.1
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 */
19package org.sleuthkit.autopsy.guiutils;
20
21import com.google.common.cache.CacheBuilder;
22import com.google.common.cache.CacheLoader;
23import com.google.common.cache.LoadingCache;
24import java.beans.PropertyChangeListener;
25import java.sql.SQLException;
26import java.util.ArrayList;
27import java.util.EnumSet;
28import java.util.HashMap;
29import java.util.List;
30import java.util.Map;
31import java.util.concurrent.ExecutionException;
32import java.util.concurrent.TimeUnit;
33import java.util.logging.Level;
34import org.sleuthkit.autopsy.casemodule.Case;
35import org.sleuthkit.autopsy.coreutils.Logger;
36import org.sleuthkit.autopsy.ingest.IngestManager;
37import static org.sleuthkit.autopsy.ingest.IngestManager.IngestModuleEvent.DATA_ADDED;
38import org.sleuthkit.autopsy.ingest.ModuleDataEvent;
39import org.sleuthkit.datamodel.Account;
40import org.sleuthkit.datamodel.BlackboardArtifact;
41import org.sleuthkit.datamodel.BlackboardAttribute;
42import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_NAME;
43import org.sleuthkit.datamodel.TskCoreException;
44
51public 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}
synchronized static Logger getLogger(String name)
Definition Logger.java:124
static synchronized List< BlackboardArtifact > getContacts(Account account)
static synchronized List< String > getContactNameList(String accountTypeSpecificID)
final LoadingCache< String, Map< String, List< BlackboardArtifact > > > accountMap
static synchronized ContactCache getInstance()
Map< String, List< BlackboardArtifact > > buildMap()
static synchronized IngestManager getInstance()
void addIngestModuleEventListener(final PropertyChangeListener listener)

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.