Sleuth Kit Java Bindings (JNI)  4.4.1
Java bindings for using The Sleuth Kit
AbstractContent.java
Go to the documentation of this file.
1 /*
2  * Sleuth Kit Data Model
3  *
4  * Copyright 2011-2016 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.datamodel;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
31 
36 public abstract class AbstractContent implements Content {
37 
38  public final static long UNKNOWN_ID = -1;
39  private final SleuthkitCase db;
40  private final long objId;
41  private final String name;
42  private Content parent;
43  private String uniquePath;
44  protected long parentId;
45  private volatile boolean hasChildren;
46  private volatile boolean checkedHasChildren;
47  private volatile int childrenCount;
49 
50  protected AbstractContent(SleuthkitCase db, long obj_id, String name) {
51  this.db = db;
52  this.objId = obj_id;
53  this.name = name;
54  this.parentId = UNKNOWN_ID;
55 
56  checkedHasChildren = false;
57  hasChildren = false;
58  childrenCount = -1;
59  }
60 
61  @Override
62  public String getName() {
63  return this.name;
64  }
65 
66  /*
67  * This base implementation simply walks the hierarchy appending its own
68  * name to the result of calling its parent's getUniquePath() method (with
69  * interleaving forward slashes).
70  */
71  @Override
72  public synchronized String getUniquePath() throws TskCoreException {
73  if (uniquePath == null) {
74  uniquePath = "";
75  if (!name.isEmpty()) {
76  uniquePath = "/" + getName();
77  }
78 
79  Content myParent = getParent();
80  if (myParent != null) {
81  uniquePath = myParent.getUniquePath() + uniquePath;
82  }
83  }
84  return uniquePath;
85  }
86 
87  @Override
88  public boolean hasChildren() throws TskCoreException {
89  if (checkedHasChildren == true) {
90  return hasChildren;
91  }
92 
93  childrenCount = this.getSleuthkitCase().getContentChildrenCount(this);
94 
95  hasChildren = childrenCount > 0;
96  checkedHasChildren = true;
97 
98  return hasChildren;
99  }
100 
101  @Override
102  public int getChildrenCount() throws TskCoreException {
103  if (childrenCount != -1) {
104  return childrenCount;
105  }
106 
107  childrenCount = this.getSleuthkitCase().getContentChildrenCount(this);
108 
109  hasChildren = childrenCount > 0;
110  checkedHasChildren = true;
111 
112  return childrenCount;
113  }
114 
115  @Override
116  public synchronized Content getParent() throws TskCoreException {
117  if (parent == null) {
118  ObjectInfo parentInfo;
119  try {
120  parentInfo = db.getParentInfo(this);
121  } catch (TskCoreException ex) {
122  // there is not parent; not an error if we've got a data source
123  return null;
124  }
125  parent = db.getContentById(parentInfo.getId());
126  }
127  return parent;
128  }
129 
130  void setParent(Content parent) {
131  this.parent = parent;
132  }
133 
141  void setParentId(long parentId) {
142  this.parentId = parentId;
143  }
144 
145  @Override
146  public long getId() {
147  return this.objId;
148  }
149 
158  @Override
159  public List<Content> getChildren() throws TskCoreException {
160  List<Content> children = new ArrayList<Content>();
161 
162  children.addAll(getSleuthkitCase().getAbstractFileChildren(this));
163  children.addAll(getSleuthkitCase().getBlackboardArtifactChildren(this));
164 
165  return children;
166 
167  }
168 
178  @Override
179  public List<Long> getChildrenIds() throws TskCoreException {
180 
181  List<Long> childrenIDs = new ArrayList<Long>();
182 
183  childrenIDs.addAll(getSleuthkitCase().getAbstractFileChildrenIds(this));
184  childrenIDs.addAll(getSleuthkitCase().getBlackboardArtifactChildrenIds(this));
185 
186  return childrenIDs;
187  }
188 
189  // classes should override this if they can be a data source
190  @Override
192  Content myParent = getParent();
193  if (myParent == null) {
194  return null;
195  }
196 
197  return myParent.getDataSource();
198  }
199 
206  return db;
207  }
208 
209  @Override
210  public boolean equals(Object obj) {
211  if (obj == null) {
212  return false;
213  }
214  if (getClass() != obj.getClass()) {
215  return false;
216  }
217  final AbstractContent other = (AbstractContent) obj;
218  if (this.objId != other.objId) {
219  return false;
220  }
221 
222  try {
223  // New children may have been added to an existing content
224  // object in which case they are not equal.
225  if (this.getChildrenCount() != other.getChildrenCount()) {
226  return false;
227  }
228  } catch (TskCoreException ex) {
229  Logger.getLogger(AbstractContent.class.getName()).log(Level.SEVERE, null, ex);
230  }
231 
232  return true;
233  }
234 
235  @Override
236  public int hashCode() {
237  int hash = 7 + (int) (this.objId ^ (this.objId >>> 32));
238  try {
239  hash = 41 * hash + this.getChildrenCount();
240  } catch (TskCoreException ex) {
241  Logger.getLogger(AbstractContent.class.getName()).log(Level.SEVERE, null, ex);
242  }
243  return hash;
244  }
245 
246  @Override
247  public BlackboardArtifact newArtifact(int artifactTypeID) throws TskCoreException {
248  // don't let them make more than 1 GEN_INFO
249  if (artifactTypeID == ARTIFACT_TYPE.TSK_GEN_INFO.getTypeID()) {
250  return getGenInfoArtifact(true);
251  }
252  return db.newBlackboardArtifact(artifactTypeID, objId);
253  }
254 
255  @Override
257  return newArtifact(type.getTypeID());
258  }
259 
260  @Override
261  public ArrayList<BlackboardArtifact> getArtifacts(String artifactTypeName) throws TskCoreException {
262  return getArtifacts(db.getArtifactType(artifactTypeName).getTypeID());
263  }
264 
265  @Override
266  public ArrayList<BlackboardArtifact> getArtifacts(int artifactTypeID) throws TskCoreException {
267  if (artifactTypeID == ARTIFACT_TYPE.TSK_GEN_INFO.getTypeID()) {
268  if (genInfoArtifact == null) // don't make one if it doesn't already exist
269  {
270  getGenInfoArtifact(false);
271  }
272 
273  ArrayList<BlackboardArtifact> list = new ArrayList<BlackboardArtifact>();
274  // genInfoArtifact coudl still be null if there isn't an artifact
275  if (genInfoArtifact != null) {
276  list.add(genInfoArtifact);
277  }
278  return list;
279  }
280  return db.getBlackboardArtifacts(artifactTypeID, objId);
281  }
282 
283  @Override
284  public ArrayList<BlackboardArtifact> getArtifacts(BlackboardArtifact.ARTIFACT_TYPE type) throws TskCoreException {
285  return getArtifacts(type.getTypeID());
286  }
287 
288  @Override
290  return getGenInfoArtifact(true);
291  }
292 
293  @Override
295  if (genInfoArtifact != null) {
296  return genInfoArtifact;
297  }
298 
299  // go to db directly to avoid infinite loop
300  ArrayList<BlackboardArtifact> arts = db.getBlackboardArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_GEN_INFO, objId);
301  BlackboardArtifact retArt;
302  if (arts.isEmpty()) {
303  if (create) {
305  } else {
306  return null;
307  }
308  } else {
309  retArt = arts.get(0);
310  }
311  genInfoArtifact = retArt;
312  return retArt;
313  }
314 
315  @Override
316  public ArrayList<BlackboardAttribute> getGenInfoAttributes(ATTRIBUTE_TYPE attr_type) throws TskCoreException {
317  ArrayList<BlackboardAttribute> returnList = new ArrayList<BlackboardAttribute>();
318 
319  if (genInfoArtifact == null) {
320  getGenInfoArtifact(false);
321  if (genInfoArtifact == null) {
322  return returnList;
323  }
324  }
325 
326  for (BlackboardAttribute attribute : genInfoArtifact.getAttributes()) {
327  if (attribute.getAttributeType().getTypeID() == attr_type.getTypeID()) {
328  returnList.add(attribute);
329  }
330  }
331 
332  return returnList;
333  }
334 
335  @Override
336  public ArrayList<BlackboardArtifact> getAllArtifacts() throws TskCoreException {
337  return db.getMatchingArtifacts("WHERE obj_id = " + objId); //NON-NLS
338  }
339 
340  @Override
341  public long getArtifactsCount(String artifactTypeName) throws TskCoreException {
342  return db.getBlackboardArtifactsCount(artifactTypeName, objId);
343  }
344 
345  @Override
346  public long getArtifactsCount(int artifactTypeID) throws TskCoreException {
347  return db.getBlackboardArtifactsCount(artifactTypeID, objId);
348  }
349 
350  @Override
352  return db.getBlackboardArtifactsCount(type, objId);
353  }
354 
355  @Override
356  public long getAllArtifactsCount() throws TskCoreException {
357  return db.getBlackboardArtifactsCount(objId);
358  }
359 
360  @Override
361  public Set<String> getHashSetNames() throws TskCoreException {
362  Set<String> hashNames = new HashSet<String>();
363  ArrayList<BlackboardArtifact> artifacts = getArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT);
364 
365  for (BlackboardArtifact a : artifacts) {
367  if (attribute != null) {
368  hashNames.add(attribute.getValueString());
369  }
370  }
371  return Collections.unmodifiableSet(hashNames);
372  }
373 
374  @Override
375  public String toString() {
376  return toString(true);
377  }
378 
379  public String toString(boolean preserveState) {
380  if (preserveState) {
381  return "AbstractContent [\t" + "objId " + String.format("%010d", objId) + "\t" //NON-NLS
382  + "name " + name + "\t" + "parentId " + parentId + "\t" //NON-NLS
383  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
384  + "\t" + "hasChildren " + hasChildren //NON-NLS
385  + "\t" + "childrenCount " + childrenCount //NON-NLS
386  + "uniquePath " + uniquePath + "]\t"; //NON-NLS
387  } else {
388  try {
389  if (getParent() != null) {
390  return "AbstractContent [\t" + "objId " + String.format("%010d", objId) //NON-NLS
391  + "\t" + "name " + name //NON-NLS
392  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
393  + "\t" + "hasChildren " + hasChildren //NON-NLS
394  + "\t" + "childrenCount " + childrenCount //NON-NLS
395  + "\t" + "getUniquePath " + getUniquePath() //NON-NLS
396  + "\t" + "getParent " + getParent().getId() + "]\t"; //NON-NLS
397  } else {
398  return "AbstractContent [\t" + "objId " //NON-NLS
399  + String.format("%010d", objId) + "\t" + "name " + name //NON-NLS
400  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
401  + "\t" + "hasChildren " + hasChildren //NON-NLS
402  + "\t" + "childrenCount " + childrenCount //NON-NLS
403  + "\t" + "uniquePath " + getUniquePath() //NON-NLS
404  + "\t" + "parentId " + parentId + "]\t"; //NON-NLS
405  }
406  } catch (TskCoreException ex) {
407  Logger.getLogger(AbstractContent.class.getName()).log(Level.SEVERE, "Could not find Parent", ex); //NON-NLS
408  return "AbstractContent [\t" + "objId " + String.format("%010d", objId) + "\t" //NON-NLS
409  + "name " + name + "\t" + "parentId " + parentId + "\t" //NON-NLS
410  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
411  + "\t" + "hasChildren " + hasChildren //NON-NLS
412  + "\t" + "childrenCount " + childrenCount //NON-NLS
413  + "uniquePath " + uniquePath + "]\t"; //NON-NLS
414  }
415  }
416  }
417 }
ArrayList< BlackboardArtifact > getBlackboardArtifacts(int artifactTypeID)
ArrayList< BlackboardArtifact > getArtifacts(int artifactTypeID)
ArrayList< BlackboardArtifact > getAllArtifacts()
String toString(boolean preserveState)
ArrayList< BlackboardArtifact > getMatchingArtifacts(String whereClause)
BlackboardArtifact newArtifact(BlackboardArtifact.ARTIFACT_TYPE type)
ArrayList< BlackboardArtifact > getArtifacts(BlackboardArtifact.ARTIFACT_TYPE type)
BlackboardArtifact newBlackboardArtifact(int artifactTypeID, long obj_id)
AbstractContent(SleuthkitCase db, long obj_id, String name)
BlackboardArtifact.Type getArtifactType(String artTypeName)
long getArtifactsCount(ARTIFACT_TYPE type)
BlackboardArtifact newArtifact(int artifactTypeID)
long getArtifactsCount(int artifactTypeID)
ArrayList< BlackboardArtifact > getArtifacts(String artifactTypeName)
BlackboardArtifact getGenInfoArtifact(boolean create)
ArrayList< BlackboardAttribute > getGenInfoAttributes(ATTRIBUTE_TYPE attr_type)
long getArtifactsCount(String artifactTypeName)

Copyright © 2011-2015 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.