From f71531346c6be197957311712ba093e024545e37 Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Fri, 7 Aug 2015 13:24:09 +0200 Subject: preparing the old code for the merge --- .../peps/configuration/ConfigurationReader.java | 319 ++++++++++----------- 1 file changed, 151 insertions(+), 168 deletions(-) (limited to 'id/server/stork2-saml-engine/src/main/java/eu/stork/peps/configuration/ConfigurationReader.java') diff --git a/id/server/stork2-saml-engine/src/main/java/eu/stork/peps/configuration/ConfigurationReader.java b/id/server/stork2-saml-engine/src/main/java/eu/stork/peps/configuration/ConfigurationReader.java index 7968c77ff..f68060154 100644 --- a/id/server/stork2-saml-engine/src/main/java/eu/stork/peps/configuration/ConfigurationReader.java +++ b/id/server/stork2-saml-engine/src/main/java/eu/stork/peps/configuration/ConfigurationReader.java @@ -46,189 +46,172 @@ import eu.stork.peps.exceptions.STORKSAMLEngineRuntimeException; */ public final class ConfigurationReader { - /** The Constant SAML_ENGINE_CONFIGURATION_FILE. */ - private static final String ENGINE_CONF_FILE = "SamlEngine.xml"; - - /** The Constant LOGGER. */ - private static final Logger LOGGER = LoggerFactory - .getLogger(ConfigurationReader.class.getName()); - - /** The Constant NODE_CONFIGURATION. */ - private static final String NODE_CONF = "configuration"; - - /** The Constant NODE_CONFIGURATION_NAME. */ - private static final String NODE_CONF_NAME = "name"; - - /** The Constant NODE_INSTANCE_NAME. */ - private static final String NODE_INST_NAME = "name"; - - /** The Constant NODE_INSTANCE. */ - private static final String NODE_INSTANCE = "instance"; - - /** The Constant NODE_CONFIGURATION_NAME. */ - private static final String NODE_PARAM_NAME = "name"; - - /** The Constant NODE_CONFIGURATION_NAME. */ - private static final String NODE_PARAM_VALUE = "value"; - - /** The Constant NODE_CONFIGURATION_NAME. */ - private static final String NODE_PARAMETER = "parameter"; - - /** - * Generate parameters. - * - * @param configurationNode the configuration node - * - * @return the map< string, string> - */ - private static Map generateParam( - final Element configurationNode) { - - final HashMap parameters = new HashMap(); - - final NodeList parameterNodes = configurationNode - .getElementsByTagName(NODE_PARAMETER); - - String parameterName; - String parameterValue; - - for (int k = 0; k < parameterNodes.getLength(); ++k) { - // for every parameter find, process. - final Element parameterNode = (Element) parameterNodes.item(k); - parameterName = parameterNode.getAttribute(NODE_PARAM_NAME); - parameterValue = parameterNode.getAttribute(NODE_PARAM_VALUE); - - // verified the content. - if (StringUtils.isBlank(parameterName) - || StringUtils.isBlank(parameterValue)) { - throw new STORKSAMLEngineRuntimeException( - "Error reader parameters (name - value)."); - } else { - parameters.put(parameterName.trim(), parameterValue.trim()); - } - } - return parameters; - } - - /** - * Read configuration. - * - * @return the map< string, instance engine> - * - * @throws SAMLEngineException the STORKSAML engine runtime - * exception - */ - public static Map readConfiguration() - throws SAMLEngineException { - - // fetch base from system properties, give a default if there is nothing configured - String base = System.getProperty("eu.stork.samlengine.config.location"); - if(null != base) - if(!base.endsWith("/")) - base += "/"; - - - LOGGER.info("Init reader: " + base + ENGINE_CONF_FILE); - final Map instanceConfs = - new HashMap(); - - Document document = null; - // Load configuration file - final DocumentBuilderFactory factory = DocumentBuilderFactory - .newInstance(); - DocumentBuilder builder; - - InputStream engineConf = null; - try { - - factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); - - builder = factory.newDocumentBuilder(); - - if (null != base) - engineConf = new FileInputStream(base + ENGINE_CONF_FILE); - else - engineConf = ConfigurationReader.class.getResourceAsStream("/" + ENGINE_CONF_FILE); - - document = builder.parse(engineConf); - - // Read instance - final NodeList list = document.getElementsByTagName(NODE_INSTANCE); - - for (int indexElem = 0; indexElem < list.getLength(); ++indexElem) { - final Element element = (Element) list.item(indexElem); - - final InstanceEngine instanceConf = new InstanceEngine(); - - // read every configuration. - final String instanceName = element - .getAttribute(NODE_INST_NAME); - - if (StringUtils.isBlank(instanceName)) { - throw new STORKSAMLEngineRuntimeException( - "Error reader instance name."); + /** The Constant SAML_ENGINE_CONFIGURATION_FILE. */ + private static final String ENGINE_CONF_FILE = "SamlEngine.xml"; + + /** The Constant LOGGER. */ + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationReader.class.getName()); + + /** The Constant NODE_CONFIGURATION. */ + private static final String NODE_CONF = "configuration"; + + /** The Constant NODE_CONFIGURATION_NAME. */ + private static final String NODE_CONF_NAME = "name"; + + /** The Constant NODE_INSTANCE_NAME. */ + private static final String NODE_INST_NAME = "name"; + + /** The Constant NODE_INSTANCE. */ + private static final String NODE_INSTANCE = "instance"; + + /** The Constant NODE_CONFIGURATION_NAME. */ + private static final String NODE_PARAM_NAME = "name"; + + /** The Constant NODE_CONFIGURATION_NAME. */ + private static final String NODE_PARAM_VALUE = "value"; + + /** The Constant NODE_CONFIGURATION_NAME. */ + private static final String NODE_PARAMETER = "parameter"; + + /** + * Generate parameters. + * + * @param configurationNode + * the configuration node + * + * @return the map< string, string> + */ + private static Map generateParam(final Element configurationNode) { + + final HashMap parameters = new HashMap(); + + final NodeList parameterNodes = configurationNode.getElementsByTagName(NODE_PARAMETER); + + String parameterName; + String parameterValue; + + for (int k = 0; k < parameterNodes.getLength(); ++k) { + // for every parameter find, process. + final Element parameterNode = (Element) parameterNodes.item(k); + parameterName = parameterNode.getAttribute(NODE_PARAM_NAME); + parameterValue = parameterNode.getAttribute(NODE_PARAM_VALUE); + + // verified the content. + if (StringUtils.isBlank(parameterName) || StringUtils.isBlank(parameterValue)) { + throw new STORKSAMLEngineRuntimeException("Error reader parameters (name - value)."); + } else { + parameters.put(parameterName.trim(), parameterValue.trim()); + } } - instanceConf.setName(instanceName.trim()); + return parameters; + } + + /** + * Read configuration. + * + * @return the map< string, instance engine> + * + * @throws SAMLEngineException + * the STORKSAML engine runtime exception + */ + public static Map readConfiguration() throws SAMLEngineException { - final NodeList confNodes = element - .getElementsByTagName(NODE_CONF); + // fetch base from system properties, give a default if there is nothing configured + String base = System.getProperty("eu.stork.samlengine.config.location"); + if (null != base) + if (!base.endsWith("/")) + base += "/"; - for (int indexNode = 0; indexNode < confNodes.getLength(); ++indexNode) { + LOGGER.info("Init reader: " + base + ENGINE_CONF_FILE); + final Map instanceConfs = new HashMap(); - final Element configurationNode = (Element) confNodes - .item(indexNode); + Document document = null; + // Load configuration file + final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder; - final String configurationName = configurationNode - .getAttribute(NODE_CONF_NAME); + InputStream engineConf = null; + try { - if (StringUtils.isBlank(configurationName)) { - throw new STORKSAMLEngineRuntimeException( - "Error reader configuration name."); - } + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); - final ConfigurationEngine confSamlEngine = - new ConfigurationEngine(); + builder = factory.newDocumentBuilder(); - // Set configuration name. - confSamlEngine.setName(configurationName.trim()); + if (null != base) + engineConf = new FileInputStream(base + ENGINE_CONF_FILE); + else + engineConf = ConfigurationReader.class.getResourceAsStream("/" + ENGINE_CONF_FILE); - // Read every parameter for this configuration. - final Map parameters = - generateParam(configurationNode); + document = builder.parse(engineConf); - // Set parameters - confSamlEngine.setParameters(parameters); + // Read instance + final NodeList list = document.getElementsByTagName(NODE_INSTANCE); - // Add parameters to the configuration. - instanceConf.getConfiguration().add(confSamlEngine); + for (int indexElem = 0; indexElem < list.getLength(); ++indexElem) { + final Element element = (Element) list.item(indexElem); + + final InstanceEngine instanceConf = new InstanceEngine(); + + // read every configuration. + final String instanceName = element.getAttribute(NODE_INST_NAME); + + if (StringUtils.isBlank(instanceName)) { + throw new STORKSAMLEngineRuntimeException("Error reader instance name."); + } + instanceConf.setName(instanceName.trim()); + + final NodeList confNodes = element.getElementsByTagName(NODE_CONF); + + for (int indexNode = 0; indexNode < confNodes.getLength(); ++indexNode) { + + final Element configurationNode = (Element) confNodes.item(indexNode); + + final String configurationName = configurationNode.getAttribute(NODE_CONF_NAME); + + if (StringUtils.isBlank(configurationName)) { + throw new STORKSAMLEngineRuntimeException("Error reader configuration name."); + } + + final ConfigurationEngine confSamlEngine = new ConfigurationEngine(); + + // Set configuration name. + confSamlEngine.setName(configurationName.trim()); + + // Read every parameter for this configuration. + final Map parameters = generateParam(configurationNode); + + // Set parameters + confSamlEngine.setParameters(parameters); + + // Add parameters to the configuration. + instanceConf.getConfiguration().add(confSamlEngine); + } + + // Add to the list of configurations. + instanceConfs.put(element.getAttribute(NODE_INST_NAME), instanceConf); + } + + } catch (SAXException e) { + LOGGER.error("Error: init library parser."); + throw new SAMLEngineException(e); + } catch (ParserConfigurationException e) { + LOGGER.error("Error: parser configuration file xml."); + throw new SAMLEngineException(e); + } catch (IOException e) { + LOGGER.error("Error: read configuration file."); + throw new SAMLEngineException(e); + } finally { + IOUtils.closeQuietly(engineConf); } - // Add to the list of configurations. - instanceConfs.put(element.getAttribute(NODE_INST_NAME), - instanceConf); - } - - } catch (SAXException e) { - LOGGER.error("Error: init library parser."); - throw new SAMLEngineException(e); - } catch (ParserConfigurationException e) { - LOGGER.error("Error: parser configuration file xml."); - throw new SAMLEngineException(e); - } catch (IOException e) { - LOGGER.error("Error: read configuration file."); - throw new SAMLEngineException(e); - } finally { - IOUtils.closeQuietly(engineConf); + return instanceConfs; } - return instanceConfs; - } + /** + * Instantiates a new configuration reader. + */ + private ConfigurationReader() { - /** - * Instantiates a new configuration reader. - */ - private ConfigurationReader() { - - } + } } -- cgit v1.2.3