From 9d627a11daa1d680a10b95996ab6bb6d1ad95e14 Mon Sep 17 00:00:00 2001 From: gkolokythas Date: Tue, 14 Jan 2020 12:26:36 +0200 Subject: [PATCH] Adds functionality to create a map of keys and their respected display values of every external endpoint used in the configuration xml. --- .../config/configloaders/ConfigLoader.java | 2 + .../configloaders/DevelConfigLoader.java | 97 +++++++++++++++++-- .../configloaders/ProductionConfigLoader.java | 96 +++++++++++++++++- 3 files changed, 185 insertions(+), 10 deletions(-) diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/ConfigLoader.java b/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/ConfigLoader.java index 8a935aa53..37a166027 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/ConfigLoader.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/ConfigLoader.java @@ -5,10 +5,12 @@ import eu.eudat.logic.security.customproviders.ConfigurableProvider.entities.Con import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.util.List; +import java.util.Map; public interface ConfigLoader { ExternalUrls getExternalUrls(); List getRdaProperties(); XWPFDocument getDocument(); ConfigurableProviders getConfigurableProviders(); + Map getKeyToSourceMap(); } diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/DevelConfigLoader.java b/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/DevelConfigLoader.java index fa4885bae..6f613f64f 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/DevelConfigLoader.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/DevelConfigLoader.java @@ -9,16 +9,27 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import java.io.*; import java.net.URL; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; @Service("configLoader") @Profile("devel") @@ -28,6 +39,7 @@ public class DevelConfigLoader implements ConfigLoader { private List rdaProperties; private XWPFDocument document; private ConfigurableProviders configurableProviders; + private Map keyToSourceMap; @Autowired private Environment environment; @@ -58,7 +70,7 @@ public class DevelConfigLoader implements ConfigLoader { private void setRdaProperties() { String filePath = environment.getProperty("configuration.rda"); - String current = null; + String current; BufferedReader reader; List rdaList = new LinkedList<>(); try { @@ -79,7 +91,7 @@ public class DevelConfigLoader implements ConfigLoader { private void setDocument() { String filePath = environment.getProperty("configuration.h2020template"); - String current = null; + String current; InputStream is = null; try { current = new java.io.File(".").getCanonicalPath(); @@ -121,6 +133,30 @@ public class DevelConfigLoader implements ConfigLoader { } } + private void setKeyToSourceMap() { + String filePath = this.environment.getProperty("configuration.externalUrls"); + System.out.println("Loaded also config file: " + filePath); + Document doc = getXmlDocumentFromFilePath(filePath); + if (doc == null) { + this.keyToSourceMap = null; + return; + } + String xpathExpression = "//key"; + Map keysToSourceMap = new HashMap<>(); + List keys = getXmlValuesFromXPath(doc, xpathExpression); + keys = keys.stream().distinct().collect(Collectors.toList()); + for (String key : keys) { + String sourceExpression = String.format("//urlConfig[key='%s']/label", key); + List sources = getXmlValuesFromXPath(doc, sourceExpression); + if (sources.size() != 0) { + keysToSourceMap.put(key, sources.get(0)); + } + } + this.keyToSourceMap = keysToSourceMap; + } + + + public ExternalUrls getExternalUrls() { this.setExternalUrls(); return externalUrls; @@ -140,4 +176,53 @@ public class DevelConfigLoader implements ConfigLoader { this.setConfigurableProviders(); return configurableProviders; } + + public Map getKeyToSourceMap() { + this.setKeyToSourceMap(); + return keyToSourceMap; + } + + private Document getXmlDocumentFromFilePath(String filePath) { + InputStream is = null; + Document doc; + try { + String current = new java.io.File(".").getCanonicalPath(); + is = new URL("file:///" + current + filePath).openStream(); + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + doc = documentBuilder.parse(is); + return doc; + } catch (IOException | ParserConfigurationException | SAXException e) { + e.printStackTrace(); + } finally { + try { + if (is != null) { + is.close(); + } + } catch (IOException e) { + System.out.println("Warning: Could not close a stream after reading from file: " + filePath); + } + } + return null; + } + + private List getXmlValuesFromXPath(Document doc, String expression) { + XPath xPath = XPathFactory.newInstance().newXPath(); + NodeList nodeList = null; + List values = new LinkedList<>(); + try { + nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET); + } catch (XPathExpressionException e) { + e.printStackTrace(); + } + if (nodeList != null) { + for (int i = 0; i < nodeList.getLength(); i++) { + Node node = nodeList.item(i); + if (node.hasChildNodes()) { + values.add(nodeList.item(i).getChildNodes().item(0).getNodeValue()); + } + } + } + return values; + } } diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/ProductionConfigLoader.java b/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/ProductionConfigLoader.java index 366ab1874..3d16a4950 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/ProductionConfigLoader.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/ProductionConfigLoader.java @@ -8,14 +8,28 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; import java.io.*; import java.net.URL; import java.nio.file.Paths; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; @Service("configLoader") @Profile({ "production", "staging" }) @@ -25,6 +39,7 @@ public class ProductionConfigLoader implements ConfigLoader { private List rdaProperties; private XWPFDocument document; private ConfigurableProviders configurableProviders; + private Map keyToSourceMap; @Autowired private Environment environment; @@ -36,7 +51,6 @@ public class ProductionConfigLoader implements ConfigLoader { InputStream is = null; try { current = new java.io.File(".").getCanonicalPath(); - JAXBContext jaxbContext = JAXBContext.newInstance(ExternalUrls.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); is = new URL(Paths.get(fileUrl).toUri().toURL().toString()).openStream(); @@ -75,13 +89,18 @@ public class ProductionConfigLoader implements ConfigLoader { private void setDocument() { String filePath = environment.getProperty("configuration.h2020template"); - String current = null; + InputStream is = null; try { - current = new java.io.File(".").getCanonicalPath(); - InputStream is = new URL(Paths.get(filePath).toUri().toURL().toString()).openStream(); + is = new URL(Paths.get(filePath).toUri().toURL().toString()).openStream(); this.document = new XWPFDocument(is); } catch (IOException e) { e.printStackTrace(); + } finally { + try { + if (is != null) is.close(); + } catch (IOException e) { + System.out.println("Warning: Could not close a stream after reading from file: " + filePath); + } } } @@ -107,6 +126,27 @@ public class ProductionConfigLoader implements ConfigLoader { } } } + private void setKeyToSourceMap() { + String filePath = this.environment.getProperty("configuration.externalUrls"); + System.out.println("Loaded also config file: " + filePath); + Document doc = getXmlDocumentFromFilePath(filePath); + if (doc == null) { + this.keyToSourceMap = null; + return; + } + String xpathExpression = "//key"; + Map keysToSourceMap = new HashMap<>(); + List keys = getXmlValuesFromXPath(doc, xpathExpression); + keys = keys.stream().distinct().collect(Collectors.toList()); + for (String key : keys) { + String sourceExpression = String.format("//urlConfig[key='%s']/label", key); + List sources = getXmlValuesFromXPath(doc, sourceExpression); + if (sources.size() != 0) { + keysToSourceMap.put(key, sources.get(0)); + } + } + this.keyToSourceMap = keysToSourceMap; + } public ExternalUrls getExternalUrls() { this.setExternalUrls(); @@ -127,4 +167,52 @@ public class ProductionConfigLoader implements ConfigLoader { this.setConfigurableProviders(); return configurableProviders; } + + public Map getKeyToSourceMap() { + this.setKeyToSourceMap(); + return keyToSourceMap; + } + + private Document getXmlDocumentFromFilePath(String filePath) { + InputStream is = null; + Document doc; + try { + is = new URL(Paths.get(filePath).toUri().toURL().toString()).openStream(); + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + doc = documentBuilder.parse(is); + return doc; + } catch (IOException | ParserConfigurationException | SAXException e) { + e.printStackTrace(); + } finally { + try { + if (is != null) { + is.close(); + } + } catch (IOException e) { + System.out.println("Warning: Could not close a stream after reading from file: " + filePath); + } + } + return null; + } + + private List getXmlValuesFromXPath(Document doc, String expression) { + XPath xPath = XPathFactory.newInstance().newXPath(); + NodeList nodeList = null; + List values = new LinkedList<>(); + try { + nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET); + } catch (XPathExpressionException e) { + e.printStackTrace(); + } + if (nodeList != null) { + for (int i = 0; i < nodeList.getLength(); i++) { + Node node = nodeList.item(i); + if (node.hasChildNodes()) { + values.add(nodeList.item(i).getChildNodes().item(0).getNodeValue()); + } + } + } + return values; + } } \ No newline at end of file