Autopsy  4.10.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 
68  protected UiSnapShotReportWriter(Case currentCase, Path reportFolderPath, String reportName, Date generationDate) {
69  this.currentCase = currentCase;
70  this.reportFolderPath = reportFolderPath;
71  this.reportName = reportName;
72  this.generationDate = generationDate;
73 
74  this.reportBranding = new ReportBranding();
75  }
76 
86  public Path writeReport() throws IOException {
87  //ensure directory exists
88  Files.createDirectories(reportFolderPath);
89 
90  copyResources();
91 
94  return writeIndexHTML();
95  }
96 
102  protected String getReportName() {
103  return reportName;
104  }
105 
111  protected Path getReportFolderPath() {
112  return reportFolderPath;
113  }
114 
120  protected Case getCurrentCase() {
121  return currentCase;
122  }
123 
130  protected abstract void writeSnapShotHTMLFile() throws IOException ;
131 
140  private Path writeIndexHTML() throws IOException {
141  //make a map of context objects to resolve template paramaters against
142  HashMap<String, Object> indexContext = new HashMap<>();
143  indexContext.put("reportBranding", reportBranding); //NON-NLS
144  indexContext.put("reportName", reportName); //NON-NLS
145  Path reportIndexFile = reportFolderPath.resolve("index.html"); //NON-NLS
146 
147  fillTemplateAndWrite("/org/sleuthkit/autopsy/report/uisnapshot/index_template.html", "Index", indexContext, reportIndexFile); //NON-NLS
148  return reportIndexFile;
149  }
150 
156  private void writeSummaryHTML() throws IOException {
157  //make a map of context objects to resolve template paramaters against
158  HashMap<String, Object> summaryContext = new HashMap<>();
159  summaryContext.put("reportName", reportName); //NON-NLS
160  summaryContext.put("reportBranding", reportBranding); //NON-NLS
161  summaryContext.put("generationDateTime", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(generationDate)); //NON-NLS
162  summaryContext.put("ingestRunning", IngestManager.getInstance().isIngestRunning()); //NON-NLS
163  summaryContext.put("currentCase", currentCase); //NON-NLS
164  String agencyLogo = "agency_logo.png"; //default name for agency logo.
165  if (StringUtils.isNotBlank(reportBranding.getAgencyLogoPath())){
166  agencyLogo = Paths.get(reportBranding.getAgencyLogoPath()).getFileName().toString();
167  }
168  summaryContext.put("agencyLogoFileName", agencyLogo);
169  fillTemplateAndWrite("/org/sleuthkit/autopsy/report/uisnapshot/summary_template.html", "Summary", summaryContext, reportFolderPath.resolve("summary.html")); //NON-NLS
170  }
171 
188  protected void fillTemplateAndWrite(final String templateLocation, final String templateName, Object context, final Path outPutFile) throws IOException {
189 
190  Mustache summaryMustache = mf.compile(new InputStreamReader(UiSnapShotReportWriter.class.getResourceAsStream(templateLocation)), templateName);
191  try (Writer writer = Files.newBufferedWriter(outPutFile, Charset.forName("UTF-8"))) { //NON-NLS
192  summaryMustache.execute(writer, context);
193  }
194  }
195 
202  private void copyResources() throws IOException {
203 
204  //pull generator and agency logos from branding
205  String generatorLogoPath = reportBranding.getGeneratorLogoPath();
206  if (StringUtils.isNotBlank(generatorLogoPath)) {
207  Files.copy(Files.newInputStream(Paths.get(generatorLogoPath)), reportFolderPath.resolve("generator_logo.png")); //NON-NLS
208  }
209  String agencyLogoPath = reportBranding.getAgencyLogoPath();
210  if (StringUtils.isNotBlank(agencyLogoPath)) {
211  Files.copy(Files.newInputStream(Paths.get(agencyLogoPath)), reportFolderPath.resolve(Paths.get(reportBranding.getAgencyLogoPath()).getFileName())); //NON-NLS
212  }
213 
214  //copy favicon
215  if (StringUtils.isBlank(agencyLogoPath)) {
216  copyInternalResource("/org/sleuthkit/autopsy/report/images/favicon.ico", "favicon.ico");
217  } else {
218  Files.copy(Files.newInputStream(Paths.get(agencyLogoPath)), reportFolderPath.resolve("favicon.ico")); //NON-NLS
219  }
220 
221  copyInternalResource("/org/sleuthkit/autopsy/report/uisnapshot/navigation.html", "nav.html");
222  copyInternalResource("/org/sleuthkit/autopsy/report/images/summary.png", "summary.png");
223  copyInternalResource("/org/sleuthkit/autopsy/report/images/image.png", "snapshot_icon.png");
224  copyInternalResource("/org/sleuthkit/autopsy/report/uisnapshot/index.css", "index.css");
225  copyInternalResource("/org/sleuthkit/autopsy/report/uisnapshot/summary.css", "summary.css");
226  }
227 
236  private void copyInternalResource(String internalPath, String fileName) throws IOException{
237  try (InputStream resource = UiSnapShotReportWriter.class.getResourceAsStream(internalPath)) { //NON-NLS
238  Files.copy(resource, reportFolderPath.resolve(fileName)); //NON-NLS
239  }
240  }
241 }
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-2018 Basis Technology. Generated on: Fri Mar 22 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.