30 package org.sleuthkit.autopsy.examples;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.logging.Level;
38 import javax.xml.parsers.DocumentBuilder;
39 import javax.xml.parsers.DocumentBuilderFactory;
40 import javax.xml.parsers.ParserConfigurationException;
41 import javax.xml.transform.Transformer;
42 import javax.xml.transform.TransformerConfigurationException;
43 import javax.xml.transform.TransformerException;
44 import javax.xml.transform.TransformerFactory;
45 import javax.xml.transform.dom.DOMSource;
46 import javax.xml.transform.stream.StreamResult;
65 import org.w3c.dom.Document;
66 import org.w3c.dom.Element;
88 File outputDir =
new File(outputDirPath);
89 if (outputDir.exists() ==
false) {
112 String resultsFilePath = outputDirPath + File.separator + String.format(
"job_%d_results.xml", context.
getJobId());
113 boolean haveRealExecutable =
false;
114 if (haveRealExecutable) {
115 if (dataSource instanceof
Image) {
116 Image image = (Image)dataSource;
117 String dataSourcePath = image.
getPaths()[0];
118 List<String> commandLine =
new ArrayList<>();
119 commandLine.add(
"some.exe");
120 commandLine.add(dataSourcePath);
121 commandLine.add(resultsFilePath);
122 ProcessBuilder processBuilder =
new ProcessBuilder(commandLine);
144 }
catch (ParserConfigurationException | TransformerException | IOException ex) {
146 logger.log(Level.SEVERE,
"Failed to simulate analysis and results import", ex);
153 private void generateSimulatedResults(String resultsFilePath)
throws ParserConfigurationException, IOException, TransformerConfigurationException, TransformerException {
160 List<String> filePaths =
new ArrayList<>();
161 String fileContents =
"This is a simulated derived file.";
162 for (
int i = 0; i < 2; ++i) {
163 String fileName = String.format(
"job_%d_derived_file_%d.txt", context.
getJobId(), i);
164 filePaths.add(
generateFile(fileName, fileContents.getBytes()));
166 this.derivedFileInCaseDatabase = this.fileInCaseDatabase +
"/" + fileName;
173 List<String> filePaths =
new ArrayList<>();
174 String fileContents =
"This is a simulated report.";
175 for (
int i = 0; i < 2; ++i) {
176 String fileName = String.format(
"job_%d_report_%d.txt", context.
getJobId(), i);
177 filePaths.add(
generateFile(fileName, fileContents.getBytes()));
182 private String
generateFile(String fileName, byte[] fileContents)
throws IOException {
183 String filePath = outputDirPath + File.separator + fileName;
184 File file =
new File(filePath);
185 if (!file.exists()) {
186 file.createNewFile();
188 try (FileOutputStream fileStream =
new FileOutputStream(file)) {
189 fileStream.write(fileContents);
195 private void generateSimulatedResultsFile(List<String> derivedFilePaths, List<String> reportPaths, String resultsFilePath)
throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
248 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
249 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
250 Document doc = docBuilder.newDocument();
252 doc.appendChild(rootElement);
256 rootElement.appendChild(derivedFilesListElement);
265 for (
int i = 0; i < derivedFilePaths.size(); ++i) {
266 String filePath = derivedFilePaths.get(i);
268 derivedFilesListElement.appendChild(derivedFileElement);
270 localPathElement.setTextContent(filePath);
271 derivedFileElement.appendChild(localPathElement);
274 parentPathElement.setTextContent(this.fileInCaseDatabase);
276 parentPathElement.setTextContent(this.derivedFileInCaseDatabase);
278 derivedFileElement.appendChild(parentPathElement);
283 rootElement.appendChild(artifactsListElement);
290 artifactsListElement.appendChild(artifactElement);
296 fileElement.setTextContent(this.fileInCaseDatabase);
297 artifactElement.appendChild(fileElement);
304 artifactElement.appendChild(artifactAttrElement);
310 artifactAttributeValueElement.setTextContent(
"SampleInterestingFilesSet");
311 artifactAttrElement.appendChild(artifactAttributeValueElement);
316 artifactAttrSourceElement.setTextContent(moduleName);
317 artifactAttrElement.appendChild(artifactAttrSourceElement);
322 artifactsListElement.appendChild(artifactElement);
326 fileElement.setTextContent(this.derivedFileInCaseDatabase);
327 artifactElement.appendChild(fileElement);
331 for (
int i = 0; i < 4; ++i) {
334 artifactElement.appendChild(artifactAttrElement);
339 artifactAttributeValueElement.setTextContent(
"One");
343 artifactAttributeValueElement.setTextContent(
"2");
347 artifactAttributeValueElement.setTextContent(
"3");
351 artifactAttributeValueElement.setTextContent(
"4.0");
354 artifactAttrElement.appendChild(artifactAttributeValueElement);
359 rootElement.appendChild(reportsListElement);
368 for (
int i = 0; i < reportPaths.size(); ++i) {
369 String reportPath = reportPaths.get(i);
371 reportsListElement.appendChild(reportElement);
373 reportPathElement.setTextContent(reportPath);
374 reportElement.appendChild(reportPathElement);
376 reportSourceModuleElement.setTextContent(moduleName);
377 reportElement.appendChild(reportSourceModuleElement);
380 reportNameElement.setTextContent(
"Sample Report");
381 reportElement.appendChild(reportNameElement);
385 TransformerFactory transformerFactory = TransformerFactory.newInstance();
386 Transformer transformer = transformerFactory.newTransformer();
387 DOMSource source =
new DOMSource(doc);
388 StreamResult result =
new StreamResult(
new File(resultsFilePath));
389 transformer.transform(source, result);
synchronized long get(long jobId)
static int execute(ProcessBuilder processBuilder)
static IngestMessage createErrorMessage(String source, String subject, String detailsHtml)
List< ErrorInfo > getErrorInfo()
String derivedFileInCaseDatabase
static final String moduleName
void generateSimulatedResultsFile(List< String > derivedFilePaths, List< String > reportPaths, String resultsFilePath)
Logger getLogger(String moduleDisplayName)
synchronized long incrementAndGet(long jobId)
List< String > generateSimulatedDerivedFiles()
String getModulesOutputDirAbsPath()
static final IngestModuleReferenceCounter refCounter
void postMessage(final IngestMessage message)
String generateFile(String fileName, byte[] fileContents)
void generateSimulatedResults(String resultsFilePath)
void startUp(IngestJobContext context)
List< ErrorInfo > importResults(ExternalResults results)
void switchToDeterminate(int workUnits)
ProcessResult process(Content dataSource, DataSourceIngestModuleProgress progressBar)
static Case getCurrentCase()
List< String > generateSimulatedReports()
final String fileInCaseDatabase
void progress(int workUnits)
static synchronized IngestServices getInstance()