2018-06-27 12:29:21 +02:00
|
|
|
package eu.eudat.logic.proxy.config.configloaders;
|
2018-02-09 16:54:41 +01:00
|
|
|
|
2019-11-13 16:32:55 +01:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.logic.proxy.config.ExternalUrls;
|
2019-11-13 16:32:55 +01:00
|
|
|
import eu.eudat.logic.security.customproviders.ConfigurableProvider.entities.ConfigurableProviders;
|
2019-11-11 09:40:12 +01:00
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
2020-01-16 16:46:24 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-02-09 16:54:41 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.context.annotation.Profile;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.stereotype.Service;
|
2020-01-14 11:26:36 +01:00
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Node;
|
|
|
|
import org.w3c.dom.NodeList;
|
|
|
|
import org.xml.sax.SAXException;
|
2018-02-09 16:54:41 +01:00
|
|
|
|
2022-01-21 09:58:29 +01:00
|
|
|
import jakarta.xml.bind.JAXBContext;
|
|
|
|
import jakarta.xml.bind.Unmarshaller;
|
2020-01-14 11:26:36 +01:00
|
|
|
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;
|
2020-01-03 11:55:12 +01:00
|
|
|
import java.io.*;
|
2018-02-09 16:54:41 +01:00
|
|
|
import java.net.URL;
|
2018-07-11 15:47:36 +02:00
|
|
|
import java.nio.file.Paths;
|
2020-01-14 11:26:36 +01:00
|
|
|
import java.util.HashMap;
|
2019-11-08 16:49:33 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2020-01-14 11:26:36 +01:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.stream.Collectors;
|
2018-02-09 16:54:41 +01:00
|
|
|
|
2021-09-28 13:15:50 +02:00
|
|
|
/*
|
2018-02-09 16:54:41 +01:00
|
|
|
@Service("configLoader")
|
2018-11-27 15:13:56 +01:00
|
|
|
@Profile({ "production", "staging" })
|
2018-02-16 11:34:02 +01:00
|
|
|
public class ProductionConfigLoader implements ConfigLoader {
|
2020-01-16 16:46:24 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ProductionConfigLoader.class);
|
2018-02-09 16:54:41 +01:00
|
|
|
|
|
|
|
private ExternalUrls externalUrls;
|
2019-11-08 16:49:33 +01:00
|
|
|
private List<String> rdaProperties;
|
2019-11-11 09:40:12 +01:00
|
|
|
private XWPFDocument document;
|
2019-11-13 16:32:55 +01:00
|
|
|
private ConfigurableProviders configurableProviders;
|
2020-01-14 11:26:36 +01:00
|
|
|
private Map<String, String> keyToSourceMap;
|
2018-02-09 16:54:41 +01:00
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private Environment environment;
|
|
|
|
|
|
|
|
private void setExternalUrls() {
|
|
|
|
String fileUrl = this.environment.getProperty("configuration.externalUrls");
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.info("Loaded also config file: " + fileUrl);
|
2018-02-09 16:54:41 +01:00
|
|
|
String current = null;
|
|
|
|
InputStream is = null;
|
|
|
|
try {
|
|
|
|
current = new java.io.File(".").getCanonicalPath();
|
|
|
|
JAXBContext jaxbContext = JAXBContext.newInstance(ExternalUrls.class);
|
|
|
|
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
|
2018-07-11 15:47:36 +02:00
|
|
|
is = new URL(Paths.get(fileUrl).toUri().toURL().toString()).openStream();
|
2018-02-09 16:54:41 +01:00
|
|
|
externalUrls = (ExternalUrls) jaxbUnmarshaller.unmarshal(is);
|
|
|
|
|
|
|
|
} catch (Exception ex) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error("Cannot find in folder" + current, ex);
|
2018-02-09 16:54:41 +01:00
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (is != null) is.close();
|
|
|
|
} catch (IOException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.warn("Warning: Could not close a stream after reading from file: " + fileUrl, e);
|
2018-02-09 16:54:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 16:49:33 +01:00
|
|
|
private void setRdaProperties() {
|
|
|
|
String filePath = environment.getProperty("configuration.rda");
|
|
|
|
BufferedReader reader;
|
|
|
|
List<String> rdaList = new LinkedList<>();
|
2020-06-16 10:19:27 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
try {
|
|
|
|
if (i == 0) {
|
|
|
|
reader = new BufferedReader(new FileReader(filePath));
|
|
|
|
} else {
|
|
|
|
reader = new BufferedReader(new FileReader(getClass().getClassLoader().getResource(filePath).getFile()));
|
|
|
|
}
|
|
|
|
String line = reader.readLine();
|
|
|
|
while (line != null) {
|
|
|
|
rdaList.add(line);
|
|
|
|
line = reader.readLine();
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
break;
|
|
|
|
} catch (IOException e) {
|
|
|
|
if (i == 1) {
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
}
|
2019-11-08 16:49:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rdaProperties = rdaList;
|
|
|
|
}
|
2018-02-09 16:54:41 +01:00
|
|
|
|
2019-11-11 09:40:12 +01:00
|
|
|
private void setDocument() {
|
|
|
|
String filePath = environment.getProperty("configuration.h2020template");
|
2020-01-14 11:26:36 +01:00
|
|
|
InputStream is = null;
|
2020-06-16 10:19:27 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2020-01-14 11:26:36 +01:00
|
|
|
try {
|
2020-06-16 10:19:27 +02:00
|
|
|
if (i == 0) {
|
|
|
|
is = new URL(Paths.get(filePath).toUri().toURL().toString()).openStream();
|
|
|
|
} else {
|
|
|
|
is = getClass().getClassLoader().getResource(filePath).openStream();
|
|
|
|
}
|
|
|
|
this.document = new XWPFDocument(is);
|
|
|
|
is.close();
|
|
|
|
is = null;
|
|
|
|
break;
|
2020-01-14 11:26:36 +01:00
|
|
|
} catch (IOException e) {
|
2020-06-16 10:19:27 +02:00
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (is != null) is.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.warn("Warning: Could not close a stream after reading from file: " + filePath, e);
|
|
|
|
}
|
2020-01-14 11:26:36 +01:00
|
|
|
}
|
2019-11-11 09:40:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 11:55:12 +01:00
|
|
|
private void setConfigurableProviders() {
|
2019-11-13 16:32:55 +01:00
|
|
|
String filePath = environment.getProperty("configuration.configurable_login_providers");
|
2020-01-03 11:55:12 +01:00
|
|
|
InputStream is = null;
|
2019-11-13 16:32:55 +01:00
|
|
|
try {
|
2020-01-03 11:55:12 +01:00
|
|
|
File tempFile = new File(filePath);
|
|
|
|
if (tempFile.exists()) {
|
|
|
|
is = new URL(Paths.get(filePath).toUri().toURL().toString()).openStream();
|
|
|
|
} else {
|
2020-06-16 10:19:27 +02:00
|
|
|
is = getClass().getClassLoader().getResource(filePath).openStream();
|
2020-01-03 11:55:12 +01:00
|
|
|
}
|
2020-06-16 10:19:27 +02:00
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
this.configurableProviders = objectMapper.readValue(is, ConfigurableProviders.class);
|
2019-11-13 16:32:55 +01:00
|
|
|
} catch (IOException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2020-01-03 11:55:12 +01:00
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (is != null) is.close();
|
|
|
|
} catch (IOException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.warn("Warning: Could not close a stream after reading from file: " + filePath, e);
|
2020-01-03 11:55:12 +01:00
|
|
|
}
|
2019-11-13 16:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-14 11:26:36 +01:00
|
|
|
private void setKeyToSourceMap() {
|
|
|
|
String filePath = this.environment.getProperty("configuration.externalUrls");
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.info("Loaded also config file: " + filePath);
|
2020-01-14 11:26:36 +01:00
|
|
|
Document doc = getXmlDocumentFromFilePath(filePath);
|
|
|
|
if (doc == null) {
|
|
|
|
this.keyToSourceMap = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String xpathExpression = "//key";
|
|
|
|
Map<String, String> keysToSourceMap = new HashMap<>();
|
|
|
|
List<String> 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<String> sources = getXmlValuesFromXPath(doc, sourceExpression);
|
|
|
|
if (sources.size() != 0) {
|
|
|
|
keysToSourceMap.put(key, sources.get(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.keyToSourceMap = keysToSourceMap;
|
|
|
|
}
|
2019-11-13 16:32:55 +01:00
|
|
|
|
2018-02-09 16:54:41 +01:00
|
|
|
public ExternalUrls getExternalUrls() {
|
|
|
|
this.setExternalUrls();
|
|
|
|
return externalUrls;
|
|
|
|
}
|
|
|
|
|
2019-11-08 16:49:33 +01:00
|
|
|
public List<String> getRdaProperties() {
|
|
|
|
this.setRdaProperties();
|
|
|
|
return rdaProperties;
|
|
|
|
}
|
2019-11-11 09:40:12 +01:00
|
|
|
|
|
|
|
public XWPFDocument getDocument() {
|
|
|
|
this.setDocument();
|
|
|
|
return document;
|
|
|
|
}
|
2019-11-13 16:32:55 +01:00
|
|
|
|
|
|
|
public ConfigurableProviders getConfigurableProviders() {
|
|
|
|
this.setConfigurableProviders();
|
|
|
|
return configurableProviders;
|
|
|
|
}
|
2020-01-14 11:26:36 +01:00
|
|
|
|
|
|
|
public Map<String, String> getKeyToSourceMap() {
|
|
|
|
this.setKeyToSourceMap();
|
|
|
|
return keyToSourceMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Document getXmlDocumentFromFilePath(String filePath) {
|
|
|
|
InputStream is = null;
|
|
|
|
Document doc;
|
|
|
|
try {
|
2020-06-16 10:19:27 +02:00
|
|
|
System.out.println(filePath);
|
2020-01-14 11:26:36 +01:00
|
|
|
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) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2020-01-14 11:26:36 +01:00
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (is != null) {
|
|
|
|
is.close();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.warn("Warning: Could not close a stream after reading from file: " + filePath, e);
|
2020-01-14 11:26:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<String> getXmlValuesFromXPath(Document doc, String expression) {
|
|
|
|
XPath xPath = XPathFactory.newInstance().newXPath();
|
|
|
|
NodeList nodeList = null;
|
|
|
|
List<String> values = new LinkedList<>();
|
|
|
|
try {
|
|
|
|
nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
|
|
|
|
} catch (XPathExpressionException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2020-01-14 11:26:36 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2021-09-28 13:15:50 +02:00
|
|
|
}*/
|