|
11 | 11 | package de.rub.nds.tlsattacker.core.config; |
12 | 12 |
|
13 | 13 | import de.rub.nds.tlsattacker.core.config.filter.ConfigDisplayFilter; |
| 14 | +import de.rub.nds.tlsattacker.core.workflow.WorkflowTrace; |
| 15 | +import de.rub.nds.tlsattacker.core.workflow.WorkflowTraceSerializer; |
14 | 16 | import java.io.ByteArrayInputStream; |
15 | 17 | import java.io.ByteArrayOutputStream; |
16 | 18 | import java.io.File; |
| 19 | +import java.io.FileInputStream; |
17 | 20 | import java.io.FileNotFoundException; |
18 | 21 | import java.io.FileOutputStream; |
19 | 22 | import java.io.IOException; |
20 | 23 | import java.io.InputStream; |
21 | 24 | import java.io.OutputStream; |
| 25 | +import javax.xml.XMLConstants; |
| 26 | +import javax.xml.bind.DataBindingException; |
22 | 27 | import javax.xml.bind.JAXB; |
23 | | -import javax.xml.parsers.ParserConfigurationException; |
24 | | -import javax.xml.transform.TransformerException; |
25 | | -import javax.xml.xpath.XPathExpressionException; |
| 28 | +import javax.xml.bind.JAXBContext; |
| 29 | +import javax.xml.bind.JAXBException; |
| 30 | +import javax.xml.bind.Unmarshaller; |
| 31 | +import javax.xml.bind.ValidationEvent; |
| 32 | +import javax.xml.bind.ValidationEventHandler; |
| 33 | +import javax.xml.stream.XMLInputFactory; |
| 34 | +import javax.xml.stream.XMLStreamException; |
| 35 | +import javax.xml.stream.XMLStreamReader; |
| 36 | +import javax.xml.transform.stream.StreamSource; |
| 37 | +import javax.xml.validation.Schema; |
| 38 | +import javax.xml.validation.SchemaFactory; |
| 39 | +import org.apache.logging.log4j.LogManager; |
| 40 | +import org.apache.logging.log4j.Logger; |
26 | 41 | import org.xml.sax.SAXException; |
27 | 42 |
|
28 | 43 | public class ConfigIO { |
29 | 44 |
|
| 45 | + private static final Logger LOGGER = LogManager.getLogger(); |
| 46 | + |
| 47 | + /** |
| 48 | + * context initialization is expensive, we need to do that only once |
| 49 | + */ |
| 50 | + private static JAXBContext context; |
| 51 | + |
| 52 | + static synchronized JAXBContext getJAXBContext() throws JAXBException { |
| 53 | + if (context == null) { |
| 54 | + context = JAXBContext.newInstance(Config.class); |
| 55 | + } |
| 56 | + return context; |
| 57 | + } |
| 58 | + |
30 | 59 | public static void write(Config config, File f) { |
31 | 60 | try { |
32 | 61 | write(config, new FileOutputStream(f)); |
@@ -59,13 +88,70 @@ public static void write(Config config, OutputStream os, ConfigDisplayFilter fil |
59 | 88 | } |
60 | 89 |
|
61 | 90 | public static Config read(File f) { |
62 | | - Config config = JAXB.unmarshal(f, Config.class); |
63 | | - return config; |
| 91 | + try { |
| 92 | + Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller(); |
| 93 | + // output any anomalies in the given config file |
| 94 | + unmarshaller.setEventHandler(new ValidationEventHandler() { |
| 95 | + @Override |
| 96 | + public boolean handleEvent(ValidationEvent event) { |
| 97 | + // Raise an exception also on warnings |
| 98 | + return false; |
| 99 | + } |
| 100 | + }); |
| 101 | + return read(new FileInputStream(f), unmarshaller); |
| 102 | + } catch (JAXBException e) { |
| 103 | + throw new RuntimeException(e); |
| 104 | + } catch (FileNotFoundException e) { |
| 105 | + throw new IllegalArgumentException("File cannot be found"); |
| 106 | + } |
64 | 107 | } |
65 | 108 |
|
66 | 109 | public static Config read(InputStream stream) { |
67 | | - Config config = JAXB.unmarshal(stream, Config.class); |
68 | | - return config; |
| 110 | + try { |
| 111 | + Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller(); |
| 112 | + // output any anomalies in the given config file |
| 113 | + unmarshaller.setEventHandler(new ValidationEventHandler() { |
| 114 | + @Override |
| 115 | + public boolean handleEvent(ValidationEvent event) { |
| 116 | + // Raise an exception also on warnings |
| 117 | + return false; |
| 118 | + } |
| 119 | + }); |
| 120 | + return read(stream, unmarshaller); |
| 121 | + } catch (JAXBException e) { |
| 122 | + throw new RuntimeException(e); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Reads the XML from the given inputStream with the provided unmarshaller into a new Config |
| 128 | + * |
| 129 | + * @param stream |
| 130 | + * The stream that provides the XML structure |
| 131 | + * @param unmarshaller |
| 132 | + * The unmarshaller that will be used during the parsing |
| 133 | + * @return Config a new Config that contains the parsed values from the inputStream |
| 134 | + */ |
| 135 | + private static Config read(InputStream stream, Unmarshaller unmarshaller) { |
| 136 | + if (stream == null) { |
| 137 | + throw new IllegalArgumentException("Stream cannot be null"); |
| 138 | + } |
| 139 | + try { |
| 140 | + String xsd_source = ConfigSchemaGenerator.AccumulatingSchemaOutputResolver.mapSystemIds(); |
| 141 | + XMLInputFactory xif = XMLInputFactory.newFactory(); |
| 142 | + xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); |
| 143 | + xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); |
| 144 | + XMLStreamReader xsr = xif.createXMLStreamReader(stream); |
| 145 | + SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
| 146 | + Schema configSchema = |
| 147 | + sf.newSchema(new StreamSource(WorkflowTraceSerializer.class.getResourceAsStream("/" + xsd_source))); |
| 148 | + configSchema.newValidator(); |
| 149 | + unmarshaller.setSchema(configSchema); |
| 150 | + Config config = (Config) unmarshaller.unmarshal(xsr); |
| 151 | + return config; |
| 152 | + } catch (XMLStreamException | SAXException | JAXBException e) { |
| 153 | + throw new RuntimeException(e); |
| 154 | + } |
69 | 155 | } |
70 | 156 |
|
71 | 157 | public static Config copy(Config config) { |
|
0 commit comments