Autopsy  4.19.3
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.modules.file;
20 
24 import java.io.BufferedWriter;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStreamWriter;
29 import java.io.Writer;
30 import java.nio.charset.StandardCharsets;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.logging.Level;
35 import javax.swing.JPanel;
37 import org.openide.util.NbBundle;
41 import org.sleuthkit.datamodel.AbstractFile;
42 import org.sleuthkit.datamodel.TskCoreException;
43 
49 class FileReportText implements FileReportModule {
50 
51  private static final Logger logger = Logger.getLogger(FileReportText.class.getName());
52  private static final String FILE_NAME = "file-report.txt"; //NON-NLS
53  private static FileReportText instance;
54  private String reportPath;
55  private Writer out;
56  private ReportFileTextConfigurationPanel configPanel;
57 
58  // Get the default implementation of this report
59  public static synchronized FileReportText getDefault() {
60  if (instance == null) {
61  instance = new FileReportText();
62  }
63  return instance;
64  }
65 
71  @Override
72  public ReportModuleSettings getDefaultConfiguration() {
73  return new FileReportModuleSettings();
74  }
75 
81  @Override
82  public ReportModuleSettings getConfiguration() {
83  initializePanel();
84  return configPanel.getConfiguration();
85  }
86 
92  @Override
93  public void setConfiguration(ReportModuleSettings settings) {
94  initializePanel();
95  if (settings == null || settings instanceof NoReportModuleSettings) {
96  configPanel.setConfiguration((FileReportModuleSettings) getDefaultConfiguration());
97  return;
98  }
99 
100  if (settings instanceof FileReportModuleSettings) {
101  configPanel.setConfiguration((FileReportModuleSettings) settings);
102  return;
103  }
104 
105  throw new IllegalArgumentException("Expected settings argument to be an instance of FileReportModuleSettings");
106  }
107 
108  @Override
109  public void startReport(String baseReportDir) {
110  this.reportPath = baseReportDir + FILE_NAME;
111  try {
112  out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.reportPath), StandardCharsets.UTF_8));
113  out.write('\ufeff');
114  } catch (FileNotFoundException ex) {
115  logger.log(Level.WARNING, "Failed to create report text file", ex); //NON-NLS
116  } catch (IOException ex) {
117  logger.log(Level.WARNING, "Failed to write BOM to report text file", ex); //NON-NLS
118  }
119  }
120 
121  @Override
122  public void endReport() {
123  if (out != null) {
124  try {
125  out.close();
126  Case.getCurrentCaseThrows().addReport(reportPath, NbBundle.getMessage(this.getClass(),
127  "FileReportText.getName.text"), "");
128  } catch (IOException ex) {
129  logger.log(Level.WARNING, "Could not close output writer when ending report.", ex); //NON-NLS
130  } catch (TskCoreException ex) {
131  String errorMessage = String.format("Error adding %s to case as a report", reportPath); //NON-NLS
132  logger.log(Level.SEVERE, errorMessage, ex);
133  } catch (NoCurrentCaseException ex) {
134  logger.log(Level.SEVERE, "Exception while getting open case.", ex);
135  }
136  }
137  }
138 
139  private String getDelimitedList(List<String> list, String delimiter) {
140  StringBuilder output;
141  output = new StringBuilder();
142  Iterator<String> it = list.iterator();
143  while (it.hasNext()) {
144  output.append('"').append(it.next()).append('"').append((it.hasNext() ? delimiter : System.lineSeparator()));
145  }
146  return output.toString();
147  }
148 
149  @Override
150  public void startTable(List<FileReportDataTypes> headers) {
151  List<String> titles = new ArrayList<>();
152  for (FileReportDataTypes col : headers) {
153  titles.add(col.getName());
154  }
155  try {
156  out.write(getDelimitedList(titles, configPanel.getDelimiter()));
157  } catch (IOException ex) {
158  logger.log(Level.WARNING, "Error when writing headers to report file: {0}", ex); //NON-NLS
159  }
160  }
161 
162  @Override
163  public void addRow(AbstractFile toAdd, List<FileReportDataTypes> columns) {
164  List<String> cells = new ArrayList<>();
165  for (FileReportDataTypes type : columns) {
166  cells.add(type.getValue(toAdd));
167  }
168  try {
169  out.write(getDelimitedList(cells, configPanel.getDelimiter()));
170  } catch (IOException ex) {
171  logger.log(Level.WARNING, "Error when writing row to report file: {0}", ex); //NON-NLS
172  }
173  }
174 
175  @Override
176  public void endTable() {
177  try {
178  out.write(System.lineSeparator());
179  } catch (IOException ex) {
180  logger.log(Level.WARNING, "Error when closing table: {0}", ex); //NON-NLS
181  }
182  }
183 
184  @Override
185  public String getName() {
186  return NbBundle.getMessage(this.getClass(), "FileReportText.getName.text");
187  }
188 
189  @Override
190  public String getDescription() {
191  return NbBundle.getMessage(this.getClass(), "FileReportText.getDesc.text");
192  }
193 
194  @Override
195  public String getRelativeFilePath() {
196  return FILE_NAME;
197  }
198 
199  @Override
200  public JPanel getConfigurationPanel() {
201  initializePanel();
202  return configPanel;
203  }
204 
205  private void initializePanel() {
206  if (configPanel == null) {
207  configPanel = new ReportFileTextConfigurationPanel();
208  }
209  }
210 }

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