218 lines
7.9 KiB
Java
218 lines
7.9 KiB
Java
package eu.eudat.logic.proxy.config.configloaders;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import eu.eudat.logic.proxy.config.ExternalUrls;
|
|
import eu.eudat.logic.security.customproviders.ConfigurableProvider.entities.ConfigurableProviders;
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
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" })
|
|
public class ProductionConfigLoader implements ConfigLoader {
|
|
|
|
private ExternalUrls externalUrls;
|
|
private List<String> rdaProperties;
|
|
private XWPFDocument document;
|
|
private ConfigurableProviders configurableProviders;
|
|
private Map<String, String> keyToSourceMap;
|
|
|
|
@Autowired
|
|
private Environment environment;
|
|
|
|
private void setExternalUrls() {
|
|
String fileUrl = this.environment.getProperty("configuration.externalUrls");
|
|
System.out.println("Loaded also config file: " + fileUrl);
|
|
String current = null;
|
|
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();
|
|
externalUrls = (ExternalUrls) jaxbUnmarshaller.unmarshal(is);
|
|
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
System.out.println("Cannot find in folder" + current);
|
|
} finally {
|
|
try {
|
|
if (is != null) is.close();
|
|
} catch (IOException e) {
|
|
System.out.println("Warning: Could not close a stream after reading from file: " + fileUrl);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void setRdaProperties() {
|
|
String filePath = environment.getProperty("configuration.rda");
|
|
BufferedReader reader;
|
|
List<String> rdaList = new LinkedList<>();
|
|
try {
|
|
reader = new BufferedReader(new FileReader(filePath));
|
|
String line = reader.readLine();
|
|
while (line != null) {
|
|
rdaList.add(line);
|
|
line = reader.readLine();
|
|
}
|
|
reader.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
rdaProperties = rdaList;
|
|
}
|
|
|
|
private void setDocument() {
|
|
String filePath = environment.getProperty("configuration.h2020template");
|
|
InputStream is = null;
|
|
try {
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void setConfigurableProviders() {
|
|
String filePath = environment.getProperty("configuration.configurable_login_providers");
|
|
InputStream is = null;
|
|
try {
|
|
File tempFile = new File(filePath);
|
|
if (tempFile.exists()) {
|
|
is = new URL(Paths.get(filePath).toUri().toURL().toString()).openStream();
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
this.configurableProviders = objectMapper.readValue(is, ConfigurableProviders.class);
|
|
} else {
|
|
this.configurableProviders = new ConfigurableProviders();
|
|
}
|
|
} 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);
|
|
}
|
|
}
|
|
}
|
|
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<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;
|
|
}
|
|
|
|
public ExternalUrls getExternalUrls() {
|
|
this.setExternalUrls();
|
|
return externalUrls;
|
|
}
|
|
|
|
public List<String> getRdaProperties() {
|
|
this.setRdaProperties();
|
|
return rdaProperties;
|
|
}
|
|
|
|
public XWPFDocument getDocument() {
|
|
this.setDocument();
|
|
return document;
|
|
}
|
|
|
|
public ConfigurableProviders getConfigurableProviders() {
|
|
this.setConfigurableProviders();
|
|
return configurableProviders;
|
|
}
|
|
|
|
public Map<String, String> 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<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) {
|
|
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;
|
|
}
|
|
} |