Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ReportBranding.java
Go to the documentation of this file.
1  /*
2  *
3  * Autopsy Forensic Browser
4  *
5  * Copyright 2013-2021 Basis Technology Corp.
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 
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.logging.Level;
28 import org.openide.util.NbBundle;
31 
39 public final class ReportBranding implements ReportBrandingProviderI {
40 
41  //property names
42  private static final String AGENCY_LOGO_PATH_PROP = "AgencyLogoPath"; //NON-NLS
43  private static final String REPORT_TITLE_PROP = "ReportTitle"; //NON-NLS
44  private static final String REPORT_FOOTER_PROP = "ReportFooter"; //NON-NLS
45  //default settings
46  private static final String DEFAULT_GENERATOR_LOGO = "/org/sleuthkit/autopsy/report/images/default_generator_logo.png"; //NON-NLS
47  private static final String DEFAULT_REPORT_TITLE = NbBundle
48  .getMessage(ReportBranding.class, "ReportBranding.defaultReportTitle.text");
49  private static final String DEFAULT_REPORT_FOOTER = NbBundle
50  .getMessage(ReportBranding.class, "ReportBranding.defaultReportFooter.text");
51  private final String reportsBrandingDir; //dir with extracted reports branding resources
52  private final Path userConfigDir = Paths.get(PlatformUtil.getUserDirectory().getAbsolutePath());
53  private static final String MODULE_NAME = ReportBranding.class.getSimpleName();
54  private static final Logger logger = Logger.getLogger(ReportBranding.class.getName());
55 
56  // this is static so that it can be set by another object
57  // before the report is actually made. Entire class should
58  // probably become singleton. Is set to null until setPath
59  // is called to specify something other than default.
60  private static String generatorLogoPath = null;
61 
62  private String defaultGeneratorLogoPath;
63 
64  public ReportBranding() {
65 
66  //initialize with extracting of resource files if needed, ensure 1 writer at a time
67  synchronized (ReportBranding.class) {
68 
69  reportsBrandingDir = PlatformUtil.getUserConfigDirectory() + File.separator + ReportGenerator.getReportsDirectory() + File.separator
70  + "branding"; //NON-NLS
71  File brandingDir = new File(reportsBrandingDir);
72  if (!brandingDir.exists()) {
73  if (!brandingDir.mkdirs()) {
74  logger.log(Level.SEVERE, "Error creating report branding dir for the case, will use defaults"); //NON-NLS
75  //TODO use defaults
76  }
77  }
81  }
82  }
83 
84  public String getReportsBrandingDir() {
85  return reportsBrandingDir;
86  }
87 
91  private void extractDefaultGeneratorLogo() {
92  try {
93  PlatformUtil.extractResourceToUserConfigDir(getClass(), DEFAULT_GENERATOR_LOGO, true);
94  } catch (IOException ex) {
95  logger.log(Level.SEVERE, "Error extracting report branding resource for generator logo ", ex); //NON-NLS
96  }
97  defaultGeneratorLogoPath = PlatformUtil.getUserConfigDirectory() + File.separator + DEFAULT_GENERATOR_LOGO;
98  }
99 
100  @Override
101  public String getGeneratorLogoPath() {
102  // if no one called to change the path, use default
103  if (generatorLogoPath == null) {
104  generatorLogoPath = defaultGeneratorLogoPath;
105  }
106 
107  return generatorLogoPath;
108  }
109 
110  @Override
111  public void setGeneratorLogoPath(String path) {
112  generatorLogoPath = path;
113  }
114 
123  @Override
124  public String getAgencyLogoPath() {
125 
126  /*
127  * The agency logo code uses these properties to persist changes in the
128  * logo (within the same process). This is different from the generator
129  * logo that uses a static variable.
130  */
131  String curPath = ModuleSettings.getConfigSetting(MODULE_NAME, AGENCY_LOGO_PATH_PROP);
132 
133 
134  //if has been set, validate it's correct, if not set, return null
135  if (curPath != null && !curPath.isEmpty()) {
136 
137  // check if the path is an absolute path (starts with either drive letter or "/")
138  Path driveLetterOrNetwork = Paths.get(curPath).getRoot();
139  if (driveLetterOrNetwork != null) {
140  // absolute path
141  return curPath;
142  }
143 
144  // Path is a relative path. Reverse path relativization performed in setAgencyLogoPath()
145  Path absolutePath = userConfigDir.resolve(curPath);
146  curPath = absolutePath.toString();
147  if (new File(curPath).canRead() == false) {
148  //use default
149  logger.log(Level.INFO, "Custom report branding for agency logo is not valid: {0}", curPath); //NON-NLS
150  curPath = null;
151  }
152  }
153 
154  return curPath;
155  }
156 
165  @Override
166  public void setAgencyLogoPath(String fullPath) {
167 
168  Path relativePath = Paths.get(fullPath);
169  // check if the path is within user directory
170  if (Paths.get(fullPath).startsWith(userConfigDir)) {
171  // relativize the path
172  relativePath = userConfigDir.relativize(relativePath);
173  }
174  // Use properties to persist the logo to use.
175  ModuleSettings.setConfigSetting(MODULE_NAME, AGENCY_LOGO_PATH_PROP, relativePath.toString());
176  }
177 
178  @Override
179  public String getReportTitle() {
180 
181  String curTitle = ModuleSettings.getConfigSetting(MODULE_NAME, REPORT_TITLE_PROP);
182  if (curTitle == null || curTitle.isEmpty()) {
183  //use default
184  logger.log(Level.INFO, "Using default report branding for report title"); //NON-NLS
185  curTitle = DEFAULT_REPORT_TITLE;
186  ModuleSettings.setConfigSetting(MODULE_NAME, REPORT_TITLE_PROP, curTitle);
187  }
188 
189  return curTitle;
190  }
191 
192  @Override
193  public void setReportTitle(String title) {
194  ModuleSettings.setConfigSetting(MODULE_NAME, REPORT_TITLE_PROP, title);
195  }
196 
197  @Override
198  public String getReportFooter() {
199 
200  String curFooter = ModuleSettings.getConfigSetting(MODULE_NAME, REPORT_FOOTER_PROP);
201  if (curFooter == null) {
202  //use default
203  logger.log(Level.INFO, "Using default report branding for report footer"); //NON-NLS
204  curFooter = DEFAULT_REPORT_FOOTER;
205  ModuleSettings.setConfigSetting(MODULE_NAME, REPORT_FOOTER_PROP, curFooter);
206  }
207 
208  return curFooter;
209  }
210 
211  @Override
212  public void setReportFooter(String footer) {
213  ModuleSettings.setConfigSetting(MODULE_NAME, REPORT_FOOTER_PROP, footer);
214  }
215 }
static synchronized String getConfigSetting(String moduleName, String settingName)
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static< T > boolean extractResourceToUserConfigDir(final Class< T > resourceClass, final String resourceFileName, boolean overWrite)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.