diff options
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.java | 452 |
1 files changed, 226 insertions, 226 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 index e794951d7..468e4a536 100644 --- 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 @@ -1,226 +1,226 @@ -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.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 - } - -} +//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.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 +// } +// +//} |