aboutsummaryrefslogtreecommitdiff
path: root/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/ConfigurationUtil.java
diff options
context:
space:
mode:
authorMartin Bonato <mbonato@datentechnik-innovation.com>2015-04-09 13:24:55 +0200
committerThomas Lenz <tlenz@iaik.tugraz.at>2015-06-19 11:09:55 +0200
commit0fb4c31f049d71e917dfbfdab96553a807195d0c (patch)
treeedeb1c502de674320928feb71bcd83c3056d135e /id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/ConfigurationUtil.java
parent6d1f99a50744de690afccc12568539c23d839f93 (diff)
downloadmoa-id-spss-0fb4c31f049d71e917dfbfdab96553a807195d0c.tar.gz
moa-id-spss-0fb4c31f049d71e917dfbfdab96553a807195d0c.tar.bz2
moa-id-spss-0fb4c31f049d71e917dfbfdab96553a807195d0c.zip
Rename java packages
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/ConfigurationUtil.java')
-rw-r--r--id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/ConfigurationUtil.java227
1 files changed, 227 insertions, 0 deletions
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/ConfigurationUtil.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/ConfigurationUtil.java
new file mode 100644
index 000000000..d8fde7eee
--- /dev/null
+++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/ConfigurationUtil.java
@@ -0,0 +1,227 @@
+package at.gv.egovernment.moa.id.commons.config;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Properties;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+import at.gv.egovernment.moa.id.commons.config.persistence.Configuration;
+import at.gv.egovernment.moa.id.commons.config.persistence.JsonMapper;
+import at.gv.egovernment.moa.id.commons.db.MOAIDConfigurationConstants;
+import at.gv.egovernment.moa.id.commons.db.dao.config.MOAIDConfiguration;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+
+public class ConfigurationUtil {
+
+ final boolean isOverwriteData;
+
+ public ConfigurationUtil(boolean isOverwriteData){
+ this.isOverwriteData = isOverwriteData;
+ }
+
+ /**
+ * Read an input MOAID 2 XML file, transfer it to properties and write the
+ * properties to a MOAID 3 property file.
+ *
+ * @param inStream
+ * the input stream to read from.
+ * @param outFile
+ * the output file to write to.
+ * @throws JAXBException
+ */
+ public void readFromXMLFileConvertToPropertyFile(FileInputStream inStream, File outFile) throws JAXBException {
+
+ try (FileOutputStream outStream = new FileOutputStream(outFile);) {
+
+ // get config from xml file
+ JAXBContext jc = JAXBContext.newInstance("at.gv.egovernment.moa.id.commons.db.dao.config");
+ Unmarshaller m = jc.createUnmarshaller();
+ MOAIDConfiguration config = (MOAIDConfiguration) m.unmarshal(inStream);
+
+ // serialize config to JSON properties
+ Properties result = moaIdConfigToJsonProperties(config);
+
+ // write to output stream
+ result.store(outStream, null);
+
+ } catch (FileNotFoundException e) {
+ System.out.println("Could not find the output file.");
+ System.exit(1);
+ } catch (IOException e) {
+ System.out.println("Could not write to the output file.");
+ System.exit(1);
+ }
+ }
+
+ /**
+ * Helper method to serialize a {@link MOAIDConfiguration} to Properties
+ * with JSON encoded values.
+ *
+ * @param config
+ * the MOAIDConfiguration to serialize
+ * @return {@link Properties} containing the database key and the serialized
+ * values
+ * @throws JsonProcessingException
+ * is thrown if problem occurred while serializing one of the
+ * database values
+ */
+ private Properties moaIdConfigToJsonProperties(MOAIDConfiguration config) throws JsonProcessingException {
+
+ Properties result = new Properties();
+ boolean prettyPrint = true;
+ JsonMapper mapper = new JsonMapper(prettyPrint);
+
+ // serialize config to JSON
+ String oaJson = mapper.serialize(config.getOnlineApplication());
+ String authCompGeneralJson = mapper.serialize(config.getAuthComponentGeneral());
+ String chainingModeJson = mapper.serialize(config.getChainingModes());
+ String defaultBKUJson = mapper.serialize(config.getDefaultBKUs());
+ String genericConfigJson = mapper.serialize(config.getGenericConfiguration());
+ String pvp2RefreshJson = mapper.serialize(config.getPvp2RefreshItem());
+ String slRequestTemplatesJson = mapper.serialize(config.getSLRequestTemplates());
+ String timestampJson = mapper.serialize(config.getTimestampItem());
+ String trustedCaCertJson = mapper.serialize(config.getTrustedCACertificates());
+
+ // add to properties
+ result.put(MOAIDConfigurationConstants.ONLINE_APPLICATIONS_KEY, oaJson);
+ result.put(MOAIDConfigurationConstants.AUTH_COMPONENT_GENERAL_KEY, authCompGeneralJson);
+ result.put(MOAIDConfigurationConstants.CHAINING_MODES_KEY, chainingModeJson);
+ result.put(MOAIDConfigurationConstants.DEFAULT_BKUS_KEY, defaultBKUJson);
+ result.put(MOAIDConfigurationConstants.GENERIC_CONFIGURATION_KEY, genericConfigJson);
+ result.put(MOAIDConfigurationConstants.PVP2REFRESH_ITEM_KEY, pvp2RefreshJson);
+ result.put(MOAIDConfigurationConstants.SLREQUEST_TEMPLATES_KEY, slRequestTemplatesJson);
+ result.put(MOAIDConfigurationConstants.TIMESTAMP_ITEM_KEY, timestampJson);
+ result.put(MOAIDConfigurationConstants.TRUSTED_CERTIFICATES_KEY, trustedCaCertJson);
+
+ return result;
+ }
+
+ /**
+ * Exports a key-value database to a property file, where keys are the same
+ * as in the database, and the values are serialized JSON objects.
+ *
+ * @param inputDBConfigFilePath
+ * the path to the database properties, for the db the data is
+ * read from.
+ * @param outFile
+ * the destination file for the exported data.
+ */
+ public void readFromDBWriteToFile(String inputDBConfigFilePath, File outFile) {
+
+ try (FileOutputStream outStream = new FileOutputStream(outFile);) {
+
+ Properties result = new Properties();
+
+ System.getProperties().setProperty("location", "file:" + inputDBConfigFilePath);
+ ApplicationContext context = new ClassPathXmlApplicationContext("configuration.beans.xml");
+ Configuration dbConfiguration = (Configuration) context.getBean("config");
+ boolean prettyPrint = true;
+ at.gv.egovernment.moa.id.commons.config.persistence.JsonMapper mapper = new JsonMapper(prettyPrint);
+
+ for (String key : MOAIDConfigurationConstants.getAllMOAIDConfigurationKeys()) {
+
+ // extract database value
+ Object value = dbConfiguration.get(key);
+
+ // serialize value to JSON
+ String json = mapper.serialize(value);
+
+ // add to properties
+ result.setProperty(key, json);
+ }
+
+ // write to output stream
+ result.store(outStream, null);
+
+ System.out.println("Property configuration written to:");
+ System.out.println(outFile.getAbsolutePath());
+
+ } catch (FileNotFoundException e) {
+ System.out.println("Could not find the output file.");
+ System.exit(1);
+ } catch (IOException e) {
+ System.out.println("Could not write to the output file.");
+ System.exit(1);
+ }
+ }
+
+ /**
+ * Read an input property file, deserialize it's values and write them to
+ * the given database.
+ *
+ * @param inStream
+ * the FileInputStream to read from.
+ * @param outputDBConfigFilePath
+ * the path to the database properties, for the db which is
+ * written.
+ * @throws IOException
+ * is thrown in case the properties could not be loaded from the
+ * stream
+ */
+ public void readFromFileWriteToDB(FileInputStream inStream, String outputDBConfigFilePath) throws IOException {
+
+ Properties inProperties = new Properties();
+ inProperties.load(inStream);
+
+ System.getProperties().setProperty("location", "file:" + outputDBConfigFilePath);
+ ApplicationContext context = new ClassPathXmlApplicationContext("configuration.beans.xml");
+ Configuration dbConfiguration = (Configuration) context.getBean("config");
+ boolean prettyPrint = true;
+ JsonMapper mapper = new JsonMapper(prettyPrint);
+
+ List<String> keys = dbConfiguration.getAllKeys();
+
+ if (keys == null) {
+ System.out.println("Database can not be read.");
+ System.exit(1);
+ }
+
+ if (!keys.isEmpty() && !isOverwriteData) {
+ System.out.println("The database already contains configuration data.");
+ System.out.println("Use force switch if you want to override data)");
+ System.exit(1);
+ }
+
+ if (isOverwriteData) {
+ // remove existing entries
+ for (String key : keys) {
+ dbConfiguration.set(key, null);
+ }
+ }
+
+ Enumeration<?> propertyNames = inProperties.propertyNames();
+
+ while (propertyNames.hasMoreElements()) {
+ String key = (String) propertyNames.nextElement();
+ // extract database value
+ String json = inProperties.getProperty(key);
+
+ // deserialize value to object
+ Object value = mapper.deserialize(json, null);
+
+ // add to database
+ boolean result = dbConfiguration.set(key, value);
+ if (!result) {
+ System.out.println("Could NOT persist the configuration file's information in the database.");
+ }
+ }
+ System.out.println("Data has been successfully written to the database.");
+ }
+
+ private static void readFromDBWriteToDB(String inputDBConfigFilePath, String outputDBConfigFilePath) {
+ //TODO: implement
+ }
+
+}