From e130bc4341fac8b5d9f2b9ab830b0727e4baf54e Mon Sep 17 00:00:00 2001 From: Cheryl King Date: Wed, 31 Jan 2024 12:38:58 -0600 Subject: [PATCH] Fix xml file processing --- .../common/arquillian/util/HttpPortUtil.java | 15 ++- .../plugins/config/ServerConfigDocument.java | 2 + .../common/plugins/config/XmlDocument.java | 6 +- .../tools/common/plugins/util/DevUtil.java | 4 +- .../plugins/util/PrepareFeatureUtil.java | 102 +++++++++--------- .../plugins/util/ServerFeatureUtil.java | 5 +- 6 files changed, 76 insertions(+), 58 deletions(-) diff --git a/src/main/java/io/openliberty/tools/common/arquillian/util/HttpPortUtil.java b/src/main/java/io/openliberty/tools/common/arquillian/util/HttpPortUtil.java index 01199d383..375f9cd0d 100644 --- a/src/main/java/io/openliberty/tools/common/arquillian/util/HttpPortUtil.java +++ b/src/main/java/io/openliberty/tools/common/arquillian/util/HttpPortUtil.java @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corporation 2017, 2020 + * (C) Copyright IBM Corporation 2017, 2024 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,9 +47,15 @@ public class HttpPortUtil { private static final XPath XPATH = XPathFactory.newInstance().newXPath(); private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - static { - factory.setNamespaceAware(true); - } + private static boolean factoryInitialized = false; + + public static void initDocumentBuilderFactory() throws ParserConfigurationException { + if (!factoryInitialized) { + factory.setNamespaceAware(true); + factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); + factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + } + } public static Integer getHttpPort(File serverXML, File bootstrapProperties) throws FileNotFoundException, IOException, ParserConfigurationException, SAXException, @@ -83,6 +89,7 @@ public static Integer getHttpPort(File serverXML, File bootstrapProperties, File protected static Integer getHttpPortForServerXML(String serverXML, Properties bootstrapProperties, String configVariableXML) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, ArquillianConfigurationException { + initDocumentBuilderFactory(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(serverXML.getBytes())); diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java index bd2dbcb9c..d2b0e7563 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java @@ -130,6 +130,8 @@ private DocumentBuilder getDocumentBuilder() { docBuilderFactory.setIgnoringElementContentWhitespace(true); docBuilderFactory.setValidating(false); try { + docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); + docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); docBuilder = docBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { // fail catastrophically if we can't create a document builder diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java index ba7de16e9..f0f454434 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corporation 2017, 2021. + * (C) Copyright IBM Corporation 2017, 2024. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,7 +55,9 @@ public void createDocument(File xmlFile) throws ParserConfigurationException, SA builderFactory.setCoalescing(true); builderFactory.setIgnoringElementContentWhitespace(true); builderFactory.setValidating(false); - DocumentBuilder builder = builderFactory.newDocumentBuilder(); + builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); + builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + DocumentBuilder builder = builderFactory.newDocumentBuilder(); doc = builder.parse(xmlFile); } diff --git a/src/main/java/io/openliberty/tools/common/plugins/util/DevUtil.java b/src/main/java/io/openliberty/tools/common/plugins/util/DevUtil.java index 428fd62bc..77b990507 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/util/DevUtil.java +++ b/src/main/java/io/openliberty/tools/common/plugins/util/DevUtil.java @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corporation 2019, 2023. + * (C) Copyright IBM Corporation 2019, 2024. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -3446,6 +3446,8 @@ protected Collection getOmitFilesList(File looseAppFile, String srcDirecto try { if (looseAppFile != null && looseAppFile.exists()) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); + dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(looseAppFile); NodeList archiveList = document.getElementsByTagName("archive"); diff --git a/src/main/java/io/openliberty/tools/common/plugins/util/PrepareFeatureUtil.java b/src/main/java/io/openliberty/tools/common/plugins/util/PrepareFeatureUtil.java index af51788e2..17759d004 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/util/PrepareFeatureUtil.java +++ b/src/main/java/io/openliberty/tools/common/plugins/util/PrepareFeatureUtil.java @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corporation 2021, 2023. + * (C) Copyright IBM Corporation 2021, 2024. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -114,20 +114,20 @@ private Map populateESAMap(File additionalBOM) { */ private void prepareFeature(String groupId, String artifactId, String version, File additionalBOM, Map esaMap) { try { - String repoLocation = parseRepositoryLocation(additionalBOM, groupId, artifactId, "pom", version); - String targetJsonFile = createArtifactFilePath(repoLocation, groupId, FEATURES_JSON_ARTIFACT_ID, "json", - version); - File generatedJson = generateJson(targetJsonFile, esaMap); - if (generatedJson.exists()) { - jsonFile = generatedJson; - provideJsonFileDependency(generatedJson, groupId, version); - info("The features.json has been generated at the following location: " + generatedJson); - }else { - warn("The features.json could not be generated at the following location: " + generatedJson); - } + String repoLocation = parseRepositoryLocation(additionalBOM, groupId, artifactId, "pom", version); + String targetJsonFile = createArtifactFilePath(repoLocation, groupId, FEATURES_JSON_ARTIFACT_ID, "json", + version); + File generatedJson = generateJson(targetJsonFile, esaMap); + if (generatedJson.exists()) { + jsonFile = generatedJson; + provideJsonFileDependency(generatedJson, groupId, version); + info("The features.json has been generated at the following location: " + generatedJson); + }else { + warn("The features.json could not be generated at the following location: " + generatedJson); + } } catch (PluginExecutionException e) { - warn("Error: The features.json could not be generated."); - warn(e.getMessage()); + warn("Error: The features.json could not be generated."); + warn(e.getMessage()); } } @@ -144,42 +144,44 @@ private Map downloadArtifactsFromBOM(File additionalBOM) throws Pl Map result = new HashMap(); ArrayList missing_tags = new ArrayList<>(); try { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = dbf.newDocumentBuilder(); - Document doc = db.parse(additionalBOM); - doc.getDocumentElement().normalize(); - NodeList dependencyList = doc.getElementsByTagName("dependency"); - for (int itr = 0; itr < dependencyList.getLength(); itr++) { - Node node = dependencyList.item(itr); - if (node.getNodeType() == Node.ELEMENT_NODE) { - Element eElement = (Element) node; - - if(eElement.getElementsByTagName("groupId").item(0) == null ) { - missing_tags.add("groupId"); - } - if(eElement.getElementsByTagName("artifactId").item(0) == null ) { - missing_tags.add("artifactId "); - } - if(eElement.getElementsByTagName("type").item(0) == null ) { - missing_tags.add("type"); - } - if(eElement.getElementsByTagName("version").item(0) == null ) { - missing_tags.add("version"); - } - - if(!missing_tags.isEmpty()) { - throw new PluginExecutionException("Error: "+ missing_tags.toString() + " tag(s) not found in features-bom file " + additionalBOM); - } - - String groupId = eElement.getElementsByTagName("groupId").item(0).getTextContent(); - String artifactId = eElement.getElementsByTagName("artifactId").item(0).getTextContent(); - String type = eElement.getElementsByTagName("type").item(0).getTextContent(); - String version = eElement.getElementsByTagName("version").item(0).getTextContent(); - - File artifactFile = downloadArtifact(groupId, artifactId, type, version); - result.put(artifactFile, groupId); - } - } + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); + dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = db.parse(additionalBOM); + doc.getDocumentElement().normalize(); + NodeList dependencyList = doc.getElementsByTagName("dependency"); + for (int itr = 0; itr < dependencyList.getLength(); itr++) { + Node node = dependencyList.item(itr); + if (node.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element) node; + + if(eElement.getElementsByTagName("groupId").item(0) == null ) { + missing_tags.add("groupId"); + } + if(eElement.getElementsByTagName("artifactId").item(0) == null ) { + missing_tags.add("artifactId "); + } + if(eElement.getElementsByTagName("type").item(0) == null ) { + missing_tags.add("type"); + } + if(eElement.getElementsByTagName("version").item(0) == null ) { + missing_tags.add("version"); + } + + if(!missing_tags.isEmpty()) { + throw new PluginExecutionException("Error: "+ missing_tags.toString() + " tag(s) not found in features-bom file " + additionalBOM); + } + + String groupId = eElement.getElementsByTagName("groupId").item(0).getTextContent(); + String artifactId = eElement.getElementsByTagName("artifactId").item(0).getTextContent(); + String type = eElement.getElementsByTagName("type").item(0).getTextContent(); + String version = eElement.getElementsByTagName("version").item(0).getTextContent(); + + File artifactFile = downloadArtifact(groupId, artifactId, type, version); + result.put(artifactFile, groupId); + } + } } catch (ParserConfigurationException | SAXException | IOException e) { throw new PluginExecutionException("Cannot read the features-bom file " + additionalBOM.getAbsolutePath() + ". " + e.getMessage()); diff --git a/src/main/java/io/openliberty/tools/common/plugins/util/ServerFeatureUtil.java b/src/main/java/io/openliberty/tools/common/plugins/util/ServerFeatureUtil.java index 4386bed0e..c9849f964 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/util/ServerFeatureUtil.java +++ b/src/main/java/io/openliberty/tools/common/plugins/util/ServerFeatureUtil.java @@ -310,7 +310,10 @@ public Set getServerXmlFeatures(Set origResult, File serverDirec debug("The server file " + canonicalServerFile + " is empty."); } else { try { - DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); + dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + DocumentBuilder db = dbf.newDocumentBuilder(); db.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException e) throws SAXException {