Autopsy  4.12.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileReportText.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013 - 2019 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;
20 
21 import java.io.BufferedWriter;
22 import java.io.FileNotFoundException;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStreamWriter;
26 import java.io.Writer;
27 import java.nio.charset.StandardCharsets;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.logging.Level;
32 import javax.swing.JPanel;
34 
35 import org.openide.util.NbBundle;
38 import org.sleuthkit.datamodel.AbstractFile;
39 import org.sleuthkit.datamodel.TskCoreException;
40 
46 class FileReportText implements FileReportModule {
47 
48  private static final Logger logger = Logger.getLogger(FileReportText.class.getName());
49  private static final String FILE_NAME = "file-report.txt"; //NON-NLS
50  private static FileReportText instance;
51  private String reportPath;
52  private Writer out;
53  private ReportFileTextConfigurationPanel configPanel;
54 
55  // Get the default implementation of this report
56  public static synchronized FileReportText getDefault() {
57  if (instance == null) {
58  instance = new FileReportText();
59  }
60  return instance;
61  }
62 
63  @Override
64  public void startReport(String baseReportDir) {
65  this.reportPath = baseReportDir + FILE_NAME;
66  try {
67  out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.reportPath), StandardCharsets.UTF_8));
68  out.write('\ufeff');
69  } catch (FileNotFoundException ex) {
70  logger.log(Level.WARNING, "Failed to create report text file", ex); //NON-NLS
71  } catch (IOException ex) {
72  logger.log(Level.WARNING, "Failed to write BOM to report text file", ex); //NON-NLS
73  }
74  }
75 
76  @Override
77  public void endReport() {
78  if (out != null) {
79  try {
80  out.close();
81  Case.getCurrentCaseThrows().addReport(reportPath, NbBundle.getMessage(this.getClass(),
82  "FileReportText.getName.text"), "");
83  } catch (IOException ex) {
84  logger.log(Level.WARNING, "Could not close output writer when ending report.", ex); //NON-NLS
85  } catch (TskCoreException ex) {
86  String errorMessage = String.format("Error adding %s to case as a report", reportPath); //NON-NLS
87  logger.log(Level.SEVERE, errorMessage, ex);
88  } catch (NoCurrentCaseException ex) {
89  logger.log(Level.SEVERE, "Exception while getting open case.", ex);
90  }
91  }
92  }
93 
94  private String getDelimitedList(List<String> list, String delimiter) {
95  StringBuilder output;
96  output = new StringBuilder();
97  Iterator<String> it = list.iterator();
98  while (it.hasNext()) {
99  output.append('"').append(it.next()).append('"').append((it.hasNext() ? delimiter : System.lineSeparator()));
100  }
101  return output.toString();
102  }
103 
104  @Override
105  public void startTable(List<FileReportDataTypes> headers) {
106  List<String> titles = new ArrayList<>();
107  for (FileReportDataTypes col : headers) {
108  titles.add(col.getName());
109  }
110  try {
111  out.write(getDelimitedList(titles, configPanel.getDelimiter()));
112  } catch (IOException ex) {
113  logger.log(Level.WARNING, "Error when writing headers to report file: {0}", ex); //NON-NLS
114  }
115  }
116 
117  @Override
118  public void addRow(AbstractFile toAdd, List<FileReportDataTypes> columns) {
119  List<String> cells = new ArrayList<>();
120  for (FileReportDataTypes type : columns) {
121  cells.add(type.getValue(toAdd));
122  }
123  try {
124  out.write(getDelimitedList(cells, configPanel.getDelimiter()));
125  } catch (IOException ex) {
126  logger.log(Level.WARNING, "Error when writing row to report file: {0}", ex); //NON-NLS
127  }
128  }
129 
130  @Override
131  public void endTable() {
132  try {
133  out.write(System.lineSeparator());
134  } catch (IOException ex) {
135  logger.log(Level.WARNING, "Error when closing table: {0}", ex); //NON-NLS
136  }
137  }
138 
139  @Override
140  public String getName() {
141  return NbBundle.getMessage(this.getClass(), "FileReportText.getName.text");
142  }
143 
144  @Override
145  public String getDescription() {
146  return NbBundle.getMessage(this.getClass(), "FileReportText.getDesc.text");
147  }
148 
149  @Override
150  public String getRelativeFilePath() {
151  return FILE_NAME;
152  }
153 
154  @Override
155  public JPanel getConfigurationPanel() {
156  if (configPanel == null) {
157  configPanel = new ReportFileTextConfigurationPanel();
158  }
159  return configPanel;
160  }
161 }

Copyright © 2012-2018 Basis Technology. Generated on: Wed Sep 18 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.