Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
UiSnapShotReportWriter.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 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.autopsy.report.uisnapshot;
20 
21 import com.github.mustachejava.DefaultMustacheFactory;
22 import com.github.mustachejava.Mustache;
23 import com.github.mustachejava.MustacheFactory;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.Writer;
28 import java.nio.charset.Charset;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.text.SimpleDateFormat;
33 import java.util.Date;
34 import java.util.HashMap;
35 import org.apache.commons.lang3.StringUtils;
39 
43 public abstract class UiSnapShotReportWriter {
44 
48  private final static MustacheFactory mf = new DefaultMustacheFactory();
49 
50  private final Case currentCase;
51  private final Path reportFolderPath;
52  private final String reportName;
54 
55  private Date generationDate;
56 
66  protected UiSnapShotReportWriter(Case currentCase, Path reportFolderPath, String reportName, Date generationDate) {
67  this.currentCase = currentCase;
68  this.reportFolderPath = reportFolderPath;
69  this.reportName = reportName;
70  this.generationDate = generationDate;
71 
72  this.reportBranding = new ReportBranding();
73  }
74 
84  public Path writeReport() throws IOException {
85  //ensure directory exists
86  Files.createDirectories(reportFolderPath);
87 
88  copyResources();
89 
92  return writeIndexHTML();
93  }
94 
100  protected String getReportName() {
101  return reportName;
102  }
103 
109  protected Path getReportFolderPath() {
110  return reportFolderPath;
111  }
112 
118  protected Case getCurrentCase() {
119  return currentCase;
120  }
121 
128  protected abstract void writeSnapShotHTMLFile() throws IOException ;
129 
138  private Path writeIndexHTML() throws IOException {
139  //make a map of context objects to resolve template paramaters against
140  HashMap<String, Object> indexContext = new HashMap<>();
141  indexContext.put("reportBranding", reportBranding); //NON-NLS
142  indexContext.put("reportName", reportName); //NON-NLS
143  Path reportIndexFile = reportFolderPath.resolve("index.html"); //NON-NLS
144 
145  fillTemplateAndWrite("/org/sleuthkit/autopsy/report/uisnapshot/index_template.html", "Index", indexContext, reportIndexFile); //NON-NLS
146  return reportIndexFile;
147  }
148 
154  private void writeSummaryHTML() throws IOException {
155  //make a map of context objects to resolve template paramaters against
156  HashMap<String, Object> summaryContext = new HashMap<>();
157  summaryContext.put("reportName", reportName); //NON-NLS
158  summaryContext.put("reportBranding", reportBranding); //NON-NLS
159  summaryContext.put("generationDateTime", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(generationDate)); //NON-NLS
160  summaryContext.put("ingestRunning", IngestManager.getInstance().isIngestRunning()); //NON-NLS
161  summaryContext.put("currentCase", currentCase); //NON-NLS
162  String agencyLogo = "agency_logo.png"; //default name for agency logo.
163  if (StringUtils.isNotBlank(reportBranding.getAgencyLogoPath())){
164  agencyLogo = Paths.get(reportBranding.getAgencyLogoPath()).getFileName().toString();
165  }
166  summaryContext.put("agencyLogoFileName", agencyLogo);
167  fillTemplateAndWrite("/org/sleuthkit/autopsy/report/uisnapshot/summary_template.html", "Summary", summaryContext, reportFolderPath.resolve("summary.html")); //NON-NLS
168  }
169 
186  protected void fillTemplateAndWrite(final String templateLocation, final String templateName, Object context, final Path outPutFile) throws IOException {
187 
188  Mustache summaryMustache = mf.compile(new InputStreamReader(UiSnapShotReportWriter.class.getResourceAsStream(templateLocation)), templateName);
189  try (Writer writer = Files.newBufferedWriter(outPutFile, Charset.forName("UTF-8"))) { //NON-NLS
190  summaryMustache.execute(writer, context);
191  }
192  }
193 
200  private void copyResources() throws IOException {
201 
202  //pull generator and agency logos from branding
203  String generatorLogoPath = reportBranding.getGeneratorLogoPath();
204  if (StringUtils.isNotBlank(generatorLogoPath)) {
205  Files.copy(Files.newInputStream(Paths.get(generatorLogoPath)), reportFolderPath.resolve("generator_logo.png")); //NON-NLS
206  }
207  String agencyLogoPath = reportBranding.getAgencyLogoPath();
208  if (StringUtils.isNotBlank(agencyLogoPath)) {
209  Files.copy(Files.newInputStream(Paths.get(agencyLogoPath)), reportFolderPath.resolve(Paths.get(reportBranding.getAgencyLogoPath()).getFileName())); //NON-NLS
210  }
211 
212  //copy favicon
213  if (StringUtils.isBlank(agencyLogoPath)) {
214  copyInternalResource("/org/sleuthkit/autopsy/report/images/favicon.ico", "favicon.ico");
215  } else {
216  Files.copy(Files.newInputStream(Paths.get(agencyLogoPath)), reportFolderPath.resolve("favicon.ico")); //NON-NLS
217  }
218 
219  copyInternalResource("/org/sleuthkit/autopsy/report/uisnapshot/navigation.html", "nav.html");
220  copyInternalResource("/org/sleuthkit/autopsy/report/images/summary.png", "summary.png");
221  copyInternalResource("/org/sleuthkit/autopsy/report/images/image.png", "snapshot_icon.png");
222  copyInternalResource("/org/sleuthkit/autopsy/report/uisnapshot/index.css", "index.css");
223  copyInternalResource("/org/sleuthkit/autopsy/report/uisnapshot/summary.css", "summary.css");
224  }
225 
234  private void copyInternalResource(String internalPath, String fileName) throws IOException{
235  try (InputStream resource = UiSnapShotReportWriter.class.getResourceAsStream(internalPath)) { //NON-NLS
236  Files.copy(resource, reportFolderPath.resolve(fileName)); //NON-NLS
237  }
238  }
239 }
static synchronized IngestManager getInstance()
UiSnapShotReportWriter(Case currentCase, Path reportFolderPath, String reportName, Date generationDate)
void fillTemplateAndWrite(final String templateLocation, final String templateName, Object context, final Path outPutFile)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.