Sleuth Kit Java Bindings (JNI)  4.6
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;
48  private BlackboardArtifact genInfoArtifact = null;
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  hasChildren = this.getSleuthkitCase().getHasChildren(this);
94  checkedHasChildren = true;
95 
96  return hasChildren;
97  }
98 
99  @Override
100  public int getChildrenCount() throws TskCoreException {
101  if (childrenCount != -1) {
102  return childrenCount;
103  }
104 
105  childrenCount = this.getSleuthkitCase().getContentChildrenCount(this);
106 
107  hasChildren = childrenCount > 0;
108  checkedHasChildren = true;
109 
110  return childrenCount;
111  }
112 
113  @Override
114  public synchronized Content getParent() throws TskCoreException {
115  if (parent == null) {
116  ObjectInfo parentInfo;
117  parentInfo = db.getParentInfo(this);
118  if (parentInfo == null) {
119  parent = null;
120  } else {
121  parent = db.getContentById(parentInfo.getId());
122  }
123  }
124  return parent;
125  }
126 
127  void setParent(Content parent) {
128  this.parent = parent;
129  }
130 
138  void setParentId(long parentId) {
139  this.parentId = parentId;
140  }
141 
142  @Override
143  public long getId() {
144  return this.objId;
145  }
146 
155  @Override
156  public List<Content> getChildren() throws TskCoreException {
157  List<Content> children = new ArrayList<Content>();
158 
159  children.addAll(getSleuthkitCase().getAbstractFileChildren(this));
160  children.addAll(getSleuthkitCase().getBlackboardArtifactChildren(this));
161 
162  return children;
163 
164  }
165 
175  @Override
176  public List<Long> getChildrenIds() throws TskCoreException {
177 
178  List<Long> childrenIDs = new ArrayList<Long>();
179 
180  childrenIDs.addAll(getSleuthkitCase().getAbstractFileChildrenIds(this));
181  childrenIDs.addAll(getSleuthkitCase().getBlackboardArtifactChildrenIds(this));
182 
183  return childrenIDs;
184  }
185 
186  // classes should override this if they can be a data source
187  @Override
189  Content myParent = getParent();
190  if (myParent == null) {
191  return null;
192  }
193 
194  return myParent.getDataSource();
195  }
196 
204  boolean isPoolContent() throws TskCoreException {
205  return getPool() != null;
206  }
207 
215  Volume getPoolVolume() throws TskCoreException {
216  Content myParent = getParent();
217  if (myParent == null) {
218  return null;
219  }
220 
221  if (! (myParent instanceof AbstractContent)) {
222  return null;
223  }
224 
225  if (myParent instanceof Volume) {
226  // This is potentially it, but need to check that this is a volume under a pool
227  if (((Volume) myParent).isPoolContent()) {
228  return (Volume)myParent;
229  } else {
230  // There are no pools in the hierarchy, so we're done
231  return null;
232  }
233  }
234 
235  // Try one level higher
236  return ((AbstractContent)myParent).getPoolVolume();
237  }
238 
246  Pool getPool() throws TskCoreException {
247  Content myParent = getParent();
248  if (myParent == null) {
249  return null;
250  }
251 
252  if (! (myParent instanceof AbstractContent)) {
253  return null;
254  }
255 
256  if (myParent instanceof Pool) {
257  return (Pool)myParent;
258  }
259 
260  // Try one level higher
261  return ((AbstractContent)myParent).getPool();
262  }
263 
270  return db;
271  }
272 
273  @Override
274  public boolean equals(Object obj) {
275  if (obj == null) {
276  return false;
277  }
278  if (getClass() != obj.getClass()) {
279  return false;
280  }
281  final AbstractContent other = (AbstractContent) obj;
282  if (this.objId != other.objId) {
283  return false;
284  }
285 
286  try {
287  // New children may have been added to an existing content
288  // object in which case they are not equal.
289  if (this.getChildrenCount() != other.getChildrenCount()) {
290  return false;
291  }
292  } catch (TskCoreException ex) {
293  Logger.getLogger(AbstractContent.class.getName()).log(Level.SEVERE, null, ex);
294  }
295 
296  return true;
297  }
298 
299  @Override
300  public int hashCode() {
301  int hash = 7 + (int) (this.objId ^ (this.objId >>> 32));
302  try {
303  hash = 41 * hash + this.getChildrenCount();
304  } catch (TskCoreException ex) {
305  Logger.getLogger(AbstractContent.class.getName()).log(Level.SEVERE, null, ex);
306  }
307  return hash;
308  }
309 
310  @Override
311  public BlackboardArtifact newArtifact(int artifactTypeID) throws TskCoreException {
312  // don't let them make more than 1 GEN_INFO
313  if (artifactTypeID == ARTIFACT_TYPE.TSK_GEN_INFO.getTypeID()) {
314  return getGenInfoArtifact(true);
315  }
316  return db.newBlackboardArtifact(artifactTypeID, objId);
317  }
318 
319  @Override
320  public BlackboardArtifact newArtifact(BlackboardArtifact.ARTIFACT_TYPE type) throws TskCoreException {
321  return newArtifact(type.getTypeID());
322  }
323 
324  @Override
325  public ArrayList<BlackboardArtifact> getArtifacts(String artifactTypeName) throws TskCoreException {
326  return getArtifacts(db.getArtifactType(artifactTypeName).getTypeID());
327  }
328 
329  @Override
330  public ArrayList<BlackboardArtifact> getArtifacts(int artifactTypeID) throws TskCoreException {
331  if (artifactTypeID == ARTIFACT_TYPE.TSK_GEN_INFO.getTypeID()) {
332  if (genInfoArtifact == null) // don't make one if it doesn't already exist
333  {
334  getGenInfoArtifact(false);
335  }
336 
337  ArrayList<BlackboardArtifact> list = new ArrayList<BlackboardArtifact>();
338  // genInfoArtifact coudl still be null if there isn't an artifact
339  if (genInfoArtifact != null) {
340  list.add(genInfoArtifact);
341  }
342  return list;
343  }
344  return db.getBlackboardArtifacts(artifactTypeID, objId);
345  }
346 
347  @Override
348  public ArrayList<BlackboardArtifact> getArtifacts(BlackboardArtifact.ARTIFACT_TYPE type) throws TskCoreException {
349  return getArtifacts(type.getTypeID());
350  }
351 
352  @Override
353  public BlackboardArtifact getGenInfoArtifact() throws TskCoreException {
354  return getGenInfoArtifact(true);
355  }
356 
357  @Override
358  public BlackboardArtifact getGenInfoArtifact(boolean create) throws TskCoreException {
359  if (genInfoArtifact != null) {
360  return genInfoArtifact;
361  }
362 
363  // go to db directly to avoid infinite loop
364  ArrayList<BlackboardArtifact> arts = db.getBlackboardArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_GEN_INFO, objId);
365  BlackboardArtifact retArt;
366  if (arts.isEmpty()) {
367  if (create) {
369  } else {
370  return null;
371  }
372  } else {
373  retArt = arts.get(0);
374  }
375  genInfoArtifact = retArt;
376  return retArt;
377  }
378 
379  @Override
380  public ArrayList<BlackboardAttribute> getGenInfoAttributes(ATTRIBUTE_TYPE attr_type) throws TskCoreException {
381  ArrayList<BlackboardAttribute> returnList = new ArrayList<BlackboardAttribute>();
382 
383  if (genInfoArtifact == null) {
384  getGenInfoArtifact(false);
385  if (genInfoArtifact == null) {
386  return returnList;
387  }
388  }
389 
390  for (BlackboardAttribute attribute : genInfoArtifact.getAttributes()) {
391  if (attribute.getAttributeType().getTypeID() == attr_type.getTypeID()) {
392  returnList.add(attribute);
393  }
394  }
395 
396  return returnList;
397  }
398 
399  @Override
400  public ArrayList<BlackboardArtifact> getAllArtifacts() throws TskCoreException {
401  return db.getMatchingArtifacts("WHERE obj_id = " + objId); //NON-NLS
402  }
403 
404  @Override
405  public long getArtifactsCount(String artifactTypeName) throws TskCoreException {
406  return db.getBlackboardArtifactsCount(artifactTypeName, objId);
407  }
408 
409  @Override
410  public long getArtifactsCount(int artifactTypeID) throws TskCoreException {
411  return db.getBlackboardArtifactsCount(artifactTypeID, objId);
412  }
413 
414  @Override
415  public long getArtifactsCount(ARTIFACT_TYPE type) throws TskCoreException {
416  return db.getBlackboardArtifactsCount(type, objId);
417  }
418 
419  @Override
420  public long getAllArtifactsCount() throws TskCoreException {
421  return db.getBlackboardArtifactsCount(objId);
422  }
423 
424  @Override
425  public Set<String> getHashSetNames() throws TskCoreException {
426  Set<String> hashNames = new HashSet<String>();
427  ArrayList<BlackboardArtifact> artifacts = getArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT);
428 
429  for (BlackboardArtifact a : artifacts) {
431  if (attribute != null) {
432  hashNames.add(attribute.getValueString());
433  }
434  }
435  return Collections.unmodifiableSet(hashNames);
436  }
437 
438  @Override
439  public String toString() {
440  return toString(true);
441  }
442 
443  public String toString(boolean preserveState) {
444  if (preserveState) {
445  return "AbstractContent [\t" + "objId " + String.format("%010d", objId) + "\t" //NON-NLS
446  + "name " + name + "\t" + "parentId " + parentId + "\t" //NON-NLS
447  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
448  + "\t" + "hasChildren " + hasChildren //NON-NLS
449  + "\t" + "childrenCount " + childrenCount //NON-NLS
450  + "uniquePath " + uniquePath + "]\t"; //NON-NLS
451  } else {
452  try {
453  if (getParent() != null) {
454  return "AbstractContent [\t" + "objId " + String.format("%010d", objId) //NON-NLS
455  + "\t" + "name " + name //NON-NLS
456  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
457  + "\t" + "hasChildren " + hasChildren //NON-NLS
458  + "\t" + "childrenCount " + childrenCount //NON-NLS
459  + "\t" + "getUniquePath " + getUniquePath() //NON-NLS
460  + "\t" + "getParent " + getParent().getId() + "]\t"; //NON-NLS
461  } else {
462  return "AbstractContent [\t" + "objId " //NON-NLS
463  + String.format("%010d", objId) + "\t" + "name " + name //NON-NLS
464  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
465  + "\t" + "hasChildren " + hasChildren //NON-NLS
466  + "\t" + "childrenCount " + childrenCount //NON-NLS
467  + "\t" + "uniquePath " + getUniquePath() //NON-NLS
468  + "\t" + "parentId " + parentId + "]\t"; //NON-NLS
469  }
470  } catch (TskCoreException ex) {
471  Logger.getLogger(AbstractContent.class.getName()).log(Level.SEVERE, "Could not find Parent", ex); //NON-NLS
472  return "AbstractContent [\t" + "objId " + String.format("%010d", objId) + "\t" //NON-NLS
473  + "name " + name + "\t" + "parentId " + parentId + "\t" //NON-NLS
474  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
475  + "\t" + "hasChildren " + hasChildren //NON-NLS
476  + "\t" + "childrenCount " + childrenCount //NON-NLS
477  + "uniquePath " + uniquePath + "]\t"; //NON-NLS
478  }
479  }
480  }
481 }
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-2018 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.