Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
HtmlViewer.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 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.contentviewers;
20 
21 import java.awt.Component;
22 import java.awt.Cursor;
23 import java.io.UnsupportedEncodingException;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.logging.Level;
27 import org.apache.tika.parser.txt.CharsetDetector;
28 import org.apache.tika.parser.txt.CharsetMatch;
29 import org.openide.util.NbBundle;
30 import org.openide.windows.WindowManager;
32 import org.sleuthkit.datamodel.AbstractFile;
33 import org.sleuthkit.datamodel.TskCoreException;
34 
38 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
39 final class HtmlViewer extends javax.swing.JPanel implements FileTypeViewer {
40 
41  private static final long serialVersionUID = 1L;
42  private static final Logger logger = Logger.getLogger(HtmlViewer.class.getName());
43  private static final String[] SUPPORTED_MIMETYPES = new String[]{
44  "text/html",
45  "application/xhtml+xml"
46  };
48 
52  HtmlViewer() {
53  initComponents();
54  this.add(htmlPanel);
55  }
56 
64  @NbBundle.Messages({
65  "HtmlViewer_file_error=This file is missing or unreadable.",
66  "HtmlViewer_encoding_error=This file has unsupported encoding"})
67  private String getHtmlText(AbstractFile abstractFile) {
68  try {
69  int fileSize = (int) abstractFile.getSize();
70  byte[] buffer = new byte[fileSize];
71  abstractFile.read(buffer, 0, fileSize);
72  CharsetMatch match = new CharsetDetector().setText(buffer).detect();
73  if (match != null) {
74  return new String(buffer, match.getName());
75  } else {
76  return new String(buffer);
77  }
78  } catch (TskCoreException ex) {
79  logger.log(Level.SEVERE, String.format("Unable to read from file '%s' (id=%d).",
80  abstractFile.getName(), abstractFile.getId()), ex);
81  return String.format("<p>%s</p>", Bundle.HtmlViewer_file_error());
82  } catch (UnsupportedEncodingException ex) {
83  logger.log(Level.SEVERE, String.format("Unsupported encoding for file '%s' (id=%d).",
84  abstractFile.getName(), abstractFile.getId()), ex);
85  return String.format("<p>%s</p>", Bundle.HtmlViewer_encoding_error());
86  }
87  }
88 
94  @SuppressWarnings("unchecked")
95  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
96  private void initComponents() {
97 
98  setLayout(new java.awt.BorderLayout());
99  }// </editor-fold>//GEN-END:initComponents
100 
101 
102  // Variables declaration - do not modify//GEN-BEGIN:variables
103  // End of variables declaration//GEN-END:variables
104  @Override
105  public List<String> getSupportedMIMETypes() {
106  return Arrays.asList(SUPPORTED_MIMETYPES);
107  }
108 
109  @Override
110  public void setFile(AbstractFile file) {
111  WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
112  htmlPanel.setHtmlText(getHtmlText(file));
113  WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
114  }
115 
116  @Override
117  public Component getComponent() {
118  return this;
119  }
120 
121  @Override
122  public void resetComponent() {
123  htmlPanel.reset();
124  }
125 
126  @Override
127  public boolean isSupported(AbstractFile file) {
128  return true;
129  }
130 }

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.