Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
IndexMetadata.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2017 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.keywordsearch;
20 
21 import java.io.BufferedWriter;
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStreamWriter;
26 import java.io.StringWriter;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.ArrayList;
30 import java.util.List;
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
34 import javax.xml.transform.OutputKeys;
35 import javax.xml.transform.Result;
36 import javax.xml.transform.Source;
37 import javax.xml.transform.Transformer;
38 import javax.xml.transform.TransformerException;
39 import javax.xml.transform.TransformerFactory;
40 import javax.xml.transform.dom.DOMSource;
41 import javax.xml.transform.stream.StreamResult;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46 import org.w3c.dom.NodeList;
47 import org.xml.sax.SAXException;
48 
52 class IndexMetadata {
53 
54  private final Path metadataFilePath;
55  private final Path caseDirectoryPath;
56  private final static String METADATA_FILE_NAME = "SolrCore.properties";
57  private final static String ROOT_ELEMENT_NAME = "SolrCores"; //NON-NLS
58  private final static String CORE_ELEMENT_NAME = "Core"; //NON-NLS
59  private final static String CORE_NAME_ELEMENT_NAME = "CoreName"; //NON-NLS
60  private final static String SCHEMA_VERSION_ELEMENT_NAME = "SchemaVersion"; //NON-NLS
61  private final static String SOLR_VERSION_ELEMENT_NAME = "SolrVersion"; //NON-NLS
62  private final static String TEXT_INDEX_PATH_ELEMENT_NAME = "TextIndexPath"; //NON-NLS
63  private List<Index> indexes = new ArrayList<>();
64  private final UNCPathUtilities uncPathUtilities = new UNCPathUtilities();
65 
66  IndexMetadata(String caseDirectory, Index index) throws TextIndexMetadataException {
67  this.metadataFilePath = Paths.get(caseDirectory, METADATA_FILE_NAME);
68  this.caseDirectoryPath = Paths.get(uncPathUtilities.convertPathToUNC(caseDirectory));
69  this.indexes.add(index);
70  writeToFile();
71  }
72 
73  IndexMetadata(String caseDirectory, List<Index> indexes) throws TextIndexMetadataException {
74 
75  this.metadataFilePath = Paths.get(caseDirectory, METADATA_FILE_NAME);
76  this.caseDirectoryPath = Paths.get(uncPathUtilities.convertPathToUNC(caseDirectory));
77  this.indexes = indexes;
78  writeToFile();
79  }
80 
90  IndexMetadata(String caseDirectory) throws TextIndexMetadataException {
91  this.caseDirectoryPath = Paths.get(caseDirectory);
92  this.metadataFilePath = Paths.get(caseDirectory, METADATA_FILE_NAME);
93  if (!this.metadataFilePath.toFile().exists()) {
94  throw new TextIndexMetadataException(String.format("Text index metadata file doesn't exist: %s", metadataFilePath));
95  }
96  readFromFile();
97  }
98 
99  List<Index> getIndexes() {
100  return indexes;
101  }
102 
110  static boolean isMetadataFilePresent(String caseDirectory) {
111  File file = Paths.get(caseDirectory, METADATA_FILE_NAME).toFile();
112  if (file.exists()) {
113  return true;
114  }
115  return false;
116  }
117 
124  private void writeToFile() throws TextIndexMetadataException {
125  try {
126  /*
127  * Create the XML DOM.
128  */
129  Document doc = XMLUtil.createDocument();
130  createXMLDOM(doc);
131  doc.normalize();
132 
133  /*
134  * Prepare the DOM for pretty printing to the metadata file.
135  */
136  Source source = new DOMSource(doc);
137  StringWriter stringWriter = new StringWriter();
138  Result streamResult = new StreamResult(stringWriter);
139  Transformer transformer = TransformerFactory.newInstance().newTransformer();
140  transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //NON-NLS
141  transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //NON-NLS
142  transformer.transform(source, streamResult);
143 
144  /*
145  * Write the DOM to the metadata file.
146  */
147  try (BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(metadataFilePath.toFile())))) {
148  fileWriter.write(stringWriter.toString());
149  fileWriter.flush();
150  }
151 
152  } catch (ParserConfigurationException | TransformerException | IOException ex) {
153  throw new TextIndexMetadataException(String.format("Error writing to text index metadata file %s", metadataFilePath), ex);
154  }
155  }
156 
157  /*
158  * Creates an XML DOM from the text index metadata.
159  */
160  private void createXMLDOM(Document doc) {
161  /*
162  * Create the root element and its children.
163  */
164  Element rootElement = doc.createElement(ROOT_ELEMENT_NAME);
165  doc.appendChild(rootElement);
166 
167  /*
168  * Create the children of the Solr cores element.
169  */
170  for (Index index : indexes) {
171  Element coreElement = doc.createElement(CORE_ELEMENT_NAME);
172  rootElement.appendChild(coreElement);
173  createChildElement(doc, coreElement, CORE_NAME_ELEMENT_NAME, index.getIndexName());
174  createChildElement(doc, coreElement, SOLR_VERSION_ELEMENT_NAME, index.getSolrVersion());
175  createChildElement(doc, coreElement, SCHEMA_VERSION_ELEMENT_NAME, index.getSchemaVersion());
176  Path relativePath = caseDirectoryPath.relativize(Paths.get(index.getIndexPath()));
177  createChildElement(doc, coreElement, TEXT_INDEX_PATH_ELEMENT_NAME, relativePath.toString());
178  }
179  }
180 
190  private void createChildElement(Document doc, Element parentElement, String elementName, String elementContent) {
191  Element element = doc.createElement(elementName);
192  element.appendChild(doc.createTextNode(elementContent));
193  parentElement.appendChild(element);
194  }
195 
196 
203  private void readFromFile() throws TextIndexMetadataException {
204  try {
205  /*
206  * Parse the file into an XML DOM and get the root element.
207  */
208  DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
209  Document doc = builder.parse(metadataFilePath.toFile());
210  doc.getDocumentElement().normalize();
211  Element rootElement = doc.getDocumentElement();
212  if (!rootElement.getNodeName().equals(ROOT_ELEMENT_NAME)) {
213  throw new TextIndexMetadataException("Text index metadata file corrupted");
214  }
215 
216  /*
217  * Get the content of the children of the core element.
218  */
219  NodeList coreElements = doc.getElementsByTagName(CORE_ELEMENT_NAME);
220  if (coreElements.getLength() == 0) {
221  throw new TextIndexMetadataException("Text index metadata file corrupted");
222  }
223  int coreIndx = 0;
224  while (coreIndx < coreElements.getLength()) {
225  Element coreElement = (Element) coreElements.item(coreIndx);
226  String coreName = getElementTextContent(coreElement, CORE_NAME_ELEMENT_NAME, true);
227  String solrVersion = getElementTextContent(coreElement, SOLR_VERSION_ELEMENT_NAME, true);
228  String schemaVersion = getElementTextContent(coreElement, SCHEMA_VERSION_ELEMENT_NAME, true);
229  String relativeTextIndexPath = getElementTextContent(coreElement, TEXT_INDEX_PATH_ELEMENT_NAME, true);
230  Path absoluteDatabasePath = caseDirectoryPath.resolve(relativeTextIndexPath);
231  Index index = new Index(absoluteDatabasePath.toString(), solrVersion, schemaVersion, coreName, "");
232  indexes.add(index);
233  coreIndx++;
234  }
235 
236  } catch (ParserConfigurationException | SAXException | IOException ex) {
237  throw new TextIndexMetadataException(String.format("Error reading from text index metadata file %s", metadataFilePath), ex);
238  }
239  }
240 
253  private String getElementTextContent(Element parentElement, String elementName, boolean contentIsRequired) throws TextIndexMetadataException {
254  NodeList elementsList = parentElement.getElementsByTagName(elementName);
255  if (elementsList.getLength() == 0) {
256  throw new TextIndexMetadataException(String.format("Missing %s element from text index metadata file %s", elementName, metadataFilePath));
257  }
258  String textContent = elementsList.item(0).getTextContent();
259  if (textContent.isEmpty() && contentIsRequired) {
260  throw new TextIndexMetadataException(String.format("Empty %s element in text index metadata file %s", elementName, metadataFilePath));
261  }
262  return textContent;
263  }
264 
269  public final static class TextIndexMetadataException extends Exception {
270 
271  private static final long serialVersionUID = 1L;
272 
273  private TextIndexMetadataException(String message) {
274  super(message);
275  }
276 
277  private TextIndexMetadataException(String message, Throwable cause) {
278  super(message, cause);
279  }
280  }
281 }

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.