Autopsy 4.23.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ContentProviderUtils.java
Go to the documentation of this file.
1/*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2023 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.casemodule;
20
21import java.util.Collection;
22import java.util.Optional;
23import java.util.logging.Level;
24import java.util.logging.Logger;
25import org.apache.commons.lang3.StringUtils;
26import org.openide.util.Lookup;
27import org.sleuthkit.datamodel.ContentStreamProvider;
28
38class ContentProviderUtils {
39
40 private static final Logger logger = Logger.getLogger(ContentProviderUtils.class.getName());
41
42 private ContentProviderUtils() {
43 }
44
53 static Optional<int[]> parseProviderVersion(String name) {
54 if (name == null) {
55 return Optional.empty();
56 }
57 int lastUnderscore = name.lastIndexOf('_');
58 if (lastUnderscore < 0 || lastUnderscore == name.length() - 1) {
59 return Optional.empty();
60 }
61 String[] parts = name.substring(lastUnderscore + 1).split("\\.");
62 if (parts.length != 3) {
63 return Optional.empty();
64 }
65 try {
66 return Optional.of(new int[]{
67 Integer.parseInt(parts[0]),
68 Integer.parseInt(parts[1]),
69 Integer.parseInt(parts[2])
70 });
71 } catch (NumberFormatException e) {
72 return Optional.empty();
73 }
74 }
75
83 static Optional<String> parseProviderBaseName(String name) {
84 if (!parseProviderVersion(name).isPresent()) {
85 return Optional.empty();
86 }
87 return Optional.of(name.substring(0, name.lastIndexOf('_')));
88 }
89
100 static boolean isVersionCompatible(String createdName, String moduleName) {
101 Optional<int[]> created = parseProviderVersion(createdName);
102 Optional<int[]> module = parseProviderVersion(moduleName);
103 if (!created.isPresent() || !module.isPresent()) {
104 return false;
105 }
106 Optional<String> createdBase = parseProviderBaseName(createdName);
107 Optional<String> moduleBase = parseProviderBaseName(moduleName);
108 if (!createdBase.isPresent() || !moduleBase.isPresent()
109 || !StringUtils.equalsIgnoreCase(createdBase.get(), moduleBase.get())) {
110 return false;
111 }
112 int[] c = created.get();
113 int[] m = module.get();
114 return c[0] == m[0] && c[1] == m[1] && m[2] >= c[2];
115 }
116
127 static Optional<AutopsyContentProvider> findInstalledProvider(String createdName) {
128 Optional<String> baseName = parseProviderBaseName(createdName);
129 if (!baseName.isPresent()) {
130 return Optional.empty();
131 }
132 Collection<? extends AutopsyContentProvider> providers
133 = Lookup.getDefault().lookupAll(AutopsyContentProvider.class);
134 for (AutopsyContentProvider provider : providers) {
135 Optional<String> providerBase = parseProviderBaseName(
136 provider != null ? provider.getName() : null);
137 if (providerBase.isPresent()
138 && StringUtils.equalsIgnoreCase(baseName.get(), providerBase.get())) {
139 return Optional.of(provider);
140 }
141 }
142 return Optional.empty();
143 }
144
154 static ContentStreamProvider getContentProvider(String createdName) {
155 Collection<? extends AutopsyContentProvider> providers
156 = Lookup.getDefault().lookupAll(AutopsyContentProvider.class);
157 for (AutopsyContentProvider provider : providers) {
158 if (provider != null
159 && (StringUtils.equalsIgnoreCase(createdName, provider.getName())
160 || isVersionCompatible(createdName, provider.getName()))) {
161 try {
162 ContentStreamProvider contentProvider = provider.load();
163 if (contentProvider != null) {
164 return contentProvider;
165 }
166 } catch (Exception ex) {
167 logger.log(Level.WARNING, "Content provider " + provider.getName() + " threw an exception during load(); skipping.", ex);
168 }
169 }
170 }
171 return null;
172 }
173}

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