Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
SnapShotReportWriter.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.timeline.snapshot;
20 
21 import com.github.mustachejava.DefaultMustacheFactory;
22 import com.github.mustachejava.Mustache;
23 import com.github.mustachejava.MustacheFactory;
24 import java.awt.image.BufferedImage;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.Writer;
29 import java.nio.charset.Charset;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.text.SimpleDateFormat;
34 import java.util.Date;
35 import java.util.HashMap;
36 import javax.imageio.ImageIO;
37 import org.apache.commons.lang3.StringUtils;
38 import org.joda.time.format.DateTimeFormat;
43 
47 public class SnapShotReportWriter {
48 
52  private final static MustacheFactory mf = new DefaultMustacheFactory();
53 
54  private final Case currentCase;
55  private final Path reportFolderPath;
56  private final String reportName;
58 
59  private final ZoomParams zoomParams;
60  private final Date generationDate;
61  private final BufferedImage image;
62 
76  public SnapShotReportWriter(Case currentCase, Path reportFolderPath, String reportName, ZoomParams zoomParams, Date generationDate, BufferedImage snapshot) {
77  this.currentCase = currentCase;
78  this.reportFolderPath = reportFolderPath;
79  this.reportName = reportName;
80  this.zoomParams = zoomParams;
81  this.generationDate = generationDate;
82  this.image = snapshot;
83 
84  this.reportBranding = new ReportBranding();
85  }
86 
96  public Path writeReport() throws IOException {
97  //ensure directory exists
98  Files.createDirectories(reportFolderPath);
99 
100  //save the snapshot in the report directory
101  ImageIO.write(image, "png", reportFolderPath.resolve("snapshot.png").toFile()); //NON-NLS
102 
103  copyResources();
104 
107  return writeIndexHTML();
108  }
109 
116  private void writeSnapShotHTMLFile() throws IOException {
117  //make a map of context objects to resolve template paramaters against
118  HashMap<String, Object> snapShotContext = new HashMap<>();
119  snapShotContext.put("reportTitle", reportName); //NON-NLS
120  snapShotContext.put("startTime", zoomParams.getTimeRange().getStart().toString(DateTimeFormat.fullDateTime())); //NON-NLS
121  snapShotContext.put("endTime", zoomParams.getTimeRange().getEnd().toString(DateTimeFormat.fullDateTime())); //NON-NLS
122  snapShotContext.put("zoomParams", zoomParams); //NON-NLS
123 
124  fillTemplateAndWrite("/org/sleuthkit/autopsy/timeline/snapshot/snapshot_template.html", "Snapshot", snapShotContext, reportFolderPath.resolve("snapshot.html")); //NON-NLS
125  }
126 
135  private Path writeIndexHTML() throws IOException {
136  //make a map of context objects to resolve template paramaters against
137  HashMap<String, Object> indexContext = new HashMap<>();
138  indexContext.put("reportBranding", reportBranding); //NON-NLS
139  indexContext.put("reportName", reportName); //NON-NLS
140  Path reportIndexFile = reportFolderPath.resolve("index.html"); //NON-NLS
141 
142  fillTemplateAndWrite("/org/sleuthkit/autopsy/timeline/snapshot/index_template.html", "Index", indexContext, reportIndexFile); //NON-NLS
143  return reportIndexFile;
144  }
145 
151  private void writeSummaryHTML() throws IOException {
152  //make a map of context objects to resolve template paramaters against
153  HashMap<String, Object> summaryContext = new HashMap<>();
154  summaryContext.put("reportName", reportName); //NON-NLS
155  summaryContext.put("reportBranding", reportBranding); //NON-NLS
156  summaryContext.put("generationDateTime", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(generationDate)); //NON-NLS
157  summaryContext.put("ingestRunning", IngestManager.getInstance().isIngestRunning()); //NON-NLS
158  summaryContext.put("currentCase", currentCase); //NON-NLS
159 
160  fillTemplateAndWrite("/org/sleuthkit/autopsy/timeline/snapshot/summary_template.html", "Summary", summaryContext, reportFolderPath.resolve("summary.html")); //NON-NLS
161  }
162 
179  private void fillTemplateAndWrite(final String templateLocation, final String templateName, Object context, final Path outPutFile) throws IOException {
180 
181  Mustache summaryMustache = mf.compile(new InputStreamReader(SnapShotReportWriter.class.getResourceAsStream(templateLocation)), templateName);
182  try (Writer writer = Files.newBufferedWriter(outPutFile, Charset.forName("UTF-8"))) { //NON-NLS
183  summaryMustache.execute(writer, context);
184  }
185  }
186 
193  private void copyResources() throws IOException {
194 
195  //pull generator and agency logos from branding
196  String generatorLogoPath = reportBranding.getGeneratorLogoPath();
197  if (StringUtils.isNotBlank(generatorLogoPath)) {
198  Files.copy(Files.newInputStream(Paths.get(generatorLogoPath)), reportFolderPath.resolve("generator_logo.png")); //NON-NLS
199  }
200  String agencyLogoPath = reportBranding.getAgencyLogoPath();
201  if (StringUtils.isNotBlank(agencyLogoPath)) {
202  Files.copy(Files.newInputStream(Paths.get(agencyLogoPath)), reportFolderPath.resolve("agency_logo.png")); //NON-NLS
203  }
204 
205  //copy navigation html
206  try (InputStream navStream = SnapShotReportWriter.class.getResourceAsStream("/org/sleuthkit/autopsy/timeline/snapshot/navigation.html")) { //NON-NLS
207  Files.copy(navStream, reportFolderPath.resolve("nav.html")); //NON-NLS
208  }
209  //copy favicon
210  if (StringUtils.isBlank(agencyLogoPath)) {
211  // use default Autopsy icon if custom icon is not set
212  try (InputStream faviconStream = SnapShotReportWriter.class.getResourceAsStream("/org/sleuthkit/autopsy/report/images/favicon.ico")) { //NON-NLS
213  Files.copy(faviconStream, reportFolderPath.resolve("favicon.ico")); //NON-NLS
214  }
215  } else {
216  Files.copy(Files.newInputStream(Paths.get(agencyLogoPath)), reportFolderPath.resolve("favicon.ico")); //NON-NLS
217  }
218 
219  //copy report summary icon
220  try (InputStream summaryStream = SnapShotReportWriter.class.getResourceAsStream("/org/sleuthkit/autopsy/report/images/summary.png")) { //NON-NLS
221  Files.copy(summaryStream, reportFolderPath.resolve("summary.png")); //NON-NLS
222  }
223  //copy snapshot icon
224  try (InputStream snapshotIconStream = SnapShotReportWriter.class.getResourceAsStream("/org/sleuthkit/autopsy/timeline/images/image.png")) { //NON-NLS
225  Files.copy(snapshotIconStream, reportFolderPath.resolve("snapshot_icon.png")); //NON-NLS
226  }
227  //copy main report css
228  try (InputStream resource = SnapShotReportWriter.class.getResourceAsStream("/org/sleuthkit/autopsy/timeline/snapshot/index.css")) { //NON-NLS
229  Files.copy(resource, reportFolderPath.resolve("index.css")); //NON-NLS
230  }
231  //copy summary css
232  try (InputStream resource = SnapShotReportWriter.class.getResourceAsStream("/org/sleuthkit/autopsy/timeline/snapshot/summary.css")) { //NON-NLS
233  Files.copy(resource, reportFolderPath.resolve("summary.css")); //NON-NLS
234  }
235  }
236 }
static synchronized IngestManager getInstance()
void fillTemplateAndWrite(final String templateLocation, final String templateName, Object context, final Path outPutFile)
SnapShotReportWriter(Case currentCase, Path reportFolderPath, String reportName, ZoomParams zoomParams, Date generationDate, BufferedImage snapshot)

Copyright © 2012-2016 Basis Technology. Generated on: Tue Oct 25 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.