Skip to content

Commit 339d5ba

Browse files
authored
Merge pull request #6411 from dannysmyda/6949-Stix-Report-Module-Errors-Release
6949 Stix Report Module Errors (RELEASE)
2 parents f0b12be + 083ca48 commit 339d5ba

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

Core/src/org/sleuthkit/autopsy/coreutils/XMLUtil.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Autopsy Forensic Browser
33
*
4-
* Copyright 2012-2014 Basis Technology Corp.
4+
* Copyright 2012-2020 Basis Technology Corp.
55
* Contact: carrier <at> sleuthkit <dot> org
66
*
77
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -54,6 +54,40 @@
5454
* -Loading documents from disk
5555
*/
5656
public class XMLUtil {
57+
58+
private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
59+
// See JIRA-6958 for details about class loading and jaxb.
60+
ClassLoader original = Thread.currentThread().getContextClassLoader();
61+
try {
62+
Thread.currentThread().setContextClassLoader(XMLUtil.class.getClassLoader());
63+
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
64+
return builderFactory.newDocumentBuilder();
65+
} finally {
66+
Thread.currentThread().setContextClassLoader(original);
67+
}
68+
}
69+
70+
private static SchemaFactory getSchemaFactory(String schemaLanguage) {
71+
// See JIRA-6958 for details about class loading and jaxb.
72+
ClassLoader original = Thread.currentThread().getContextClassLoader();
73+
try {
74+
Thread.currentThread().setContextClassLoader(XMLUtil.class.getClassLoader());
75+
return SchemaFactory.newInstance(schemaLanguage);
76+
} finally {
77+
Thread.currentThread().setContextClassLoader(original);
78+
}
79+
}
80+
81+
private static TransformerFactory getTransformerFactory() {
82+
// See JIRA-6958 for details about class loading and jaxb.
83+
ClassLoader original = Thread.currentThread().getContextClassLoader();
84+
try {
85+
Thread.currentThread().setContextClassLoader(XMLUtil.class.getClassLoader());
86+
return TransformerFactory.newInstance();
87+
} finally {
88+
Thread.currentThread().setContextClassLoader(original);
89+
}
90+
}
5791

5892
/**
5993
* Creates a W3C DOM.
@@ -63,9 +97,7 @@ public class XMLUtil {
6397
* @throws ParserConfigurationException
6498
*/
6599
public static Document createDocument() throws ParserConfigurationException {
66-
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
67-
DocumentBuilder builder = builderFactory.newDocumentBuilder();
68-
return builder.newDocument();
100+
return getDocumentBuilder().newDocument();
69101
}
70102

71103
/**
@@ -100,8 +132,7 @@ public static <T> Document loadDocument(String docPath, Class<T> clazz, String s
100132
* @throws IOException
101133
*/
102134
public static Document loadDocument(String docPath) throws ParserConfigurationException, SAXException, IOException {
103-
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
104-
DocumentBuilder builder = builderFactory.newDocumentBuilder();
135+
DocumentBuilder builder = getDocumentBuilder();
105136
Document doc = builder.parse(new FileInputStream(docPath));
106137
return doc;
107138
}
@@ -119,7 +150,7 @@ public static Document loadDocument(String docPath) throws ParserConfigurationEx
119150
public static <T> void validateDocument(final Document doc, Class<T> clazz, String schemaResourceName) throws SAXException, IOException {
120151
PlatformUtil.extractResourceToUserConfigDir(clazz, schemaResourceName, false);
121152
File schemaFile = new File(Paths.get(PlatformUtil.getUserConfigDirectory(), schemaResourceName).toAbsolutePath().toString());
122-
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
153+
SchemaFactory schemaFactory = getSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI);
123154
Schema schema = schemaFactory.newSchema(schemaFile);
124155
Validator validator = schema.newValidator();
125156
validator.validate(new DOMSource(doc), new DOMResult());
@@ -140,7 +171,7 @@ public static <T> void validateDocument(final Document doc, Class<T> clazz, Stri
140171
* @throws IOException
141172
*/
142173
public static void saveDocument(final Document doc, String encoding, String docPath) throws TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException, TransformerException, IOException {
143-
TransformerFactory xf = TransformerFactory.newInstance();
174+
TransformerFactory xf = getTransformerFactory();
144175
xf.setAttribute("indent-number", 1); //NON-NLS
145176
Transformer xformer = xf.newTransformer();
146177
xformer.setOutputProperty(OutputKeys.METHOD, "xml"); //NON-NLS
@@ -178,7 +209,7 @@ public static <T> boolean xmlIsValid(DOMSource xmlfile, Class<T> clazz, String s
178209
try {
179210
PlatformUtil.extractResourceToUserConfigDir(clazz, schemaFile, false);
180211
File schemaLoc = new File(PlatformUtil.getUserConfigDirectory() + File.separator + schemaFile);
181-
SchemaFactory schm = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
212+
SchemaFactory schm = getSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI);
182213
try {
183214
Schema schema = schm.newSchema(schemaLoc);
184215
Validator validator = schema.newValidator();
@@ -226,10 +257,9 @@ public static <T> boolean xmlIsValid(Document doc, Class<T> clazz, String type)
226257
*/
227258
// TODO: Deprecate.
228259
public static <T> Document loadDoc(Class<T> clazz, String xmlPath) {
229-
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
230260
Document ret = null;
231261
try {
232-
DocumentBuilder builder = builderFactory.newDocumentBuilder();
262+
DocumentBuilder builder = getDocumentBuilder();
233263
ret = builder.parse(new FileInputStream(xmlPath));
234264
} catch (ParserConfigurationException e) {
235265
Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error loading XML file " + xmlPath + " : can't initialize parser.", e); //NON-NLS
@@ -268,7 +298,7 @@ public static <T> Document loadDoc(Class<T> clazz, String xmlPath, String xsdPat
268298
*/
269299
// TODO: Deprecate.
270300
public static <T> boolean saveDoc(Class<T> clazz, String xmlPath, String encoding, final Document doc) {
271-
TransformerFactory xf = TransformerFactory.newInstance();
301+
TransformerFactory xf = getTransformerFactory();
272302
xf.setAttribute("indent-number", 1); //NON-NLS
273303
boolean success = false;
274304
try {

Core/src/org/sleuthkit/autopsy/report/modules/stix/STIXReportModule.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,19 @@ private void processFile(String stixFile, ReportProgressPanel progressPanel, Buf
228228
*/
229229
private STIXPackage loadSTIXFile(String stixFileName) throws JAXBException {
230230
// Create STIXPackage object from xml.
231-
File file = new File(stixFileName);
232-
JAXBContext jaxbContext = JAXBContext.newInstance("org.mitre.stix.stix_1:org.mitre.stix.common_1:org.mitre.stix.indicator_2:" //NON-NLS
233-
+ "org.mitre.cybox.objects:org.mitre.cybox.cybox_2:org.mitre.cybox.common_2"); //NON-NLS
234-
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
235-
STIXPackage stix = (STIXPackage) jaxbUnmarshaller.unmarshal(file);
236-
return stix;
231+
// See JIRA-6958 for details about class loading and jaxb.
232+
ClassLoader original = Thread.currentThread().getContextClassLoader();
233+
try {
234+
Thread.currentThread().setContextClassLoader(STIXReportModule.class.getClassLoader());
235+
File file = new File(stixFileName);
236+
JAXBContext jaxbContext = JAXBContext.newInstance("org.mitre.stix.stix_1:org.mitre.stix.common_1:org.mitre.stix.indicator_2:" //NON-NLS
237+
+ "org.mitre.cybox.objects:org.mitre.cybox.cybox_2:org.mitre.cybox.common_2"); //NON-NLS
238+
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
239+
STIXPackage stix = (STIXPackage) jaxbUnmarshaller.unmarshal(file);
240+
return stix;
241+
} finally {
242+
Thread.currentThread().setContextClassLoader(original);
243+
}
237244
}
238245

239246
/**

0 commit comments

Comments
 (0)