package at.gv.egovernment.moa.id.auth.validator.parep.config; import java.io.File; import java.io.FileInputStream; import java.util.HashMap; import org.apache.xpath.XPathAPI; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import at.gv.egovernment.moa.id.auth.validator.parep.ParepUtils; import at.gv.egovernment.moa.id.auth.validator.parep.ParepValidator; import at.gv.egovernment.moa.id.auth.validator.parep.PartyRepresentative; import at.gv.egovernment.moa.id.auth.validator.parep.client.szrgw.SZRGWConstants; import at.gv.egovernment.moa.id.config.ConfigurationException; import at.gv.egovernment.moa.id.config.ConfigurationProvider; import at.gv.egovernment.moa.id.config.ConnectionParameter; import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.util.BoolUtils; import at.gv.egovernment.moa.util.Constants; /** * This class implements the Configuration. * * @author Peter Danner */ public class ParepConfiguration { /** * System property for config file. */ public final static String PAREP_VALIDATOR_CONFIG = "parep.validator.config"; /** * SZR-GW connection parameters. */ private ConnectionParameter standardConnectionParameters; /** * Input field processor. */ private String standardInputProcessorClass; /** * Input field processor template. */ private String standardInputProcessorTemplate; /** * Configured party representatives. */ private HashMap partyRepresentatives; /** * The configuration element. */ private Element configElement = null; /** * Defines whether the user input form must be shown on each * request or not (also predefined mandates) */ private boolean alwaysShowForm = false; /** * The configuration base directory. */ private String baseDir_; /** * Gets the SZR-GW connection parameters. * * @return the connection parameters. */ public ConnectionParameter getConnectionParameters(String representationID) { if (partyRepresentatives == null || "*".equals(representationID)) return standardConnectionParameters; PartyRepresentative pr = (PartyRepresentative) partyRepresentatives.get(representationID); ConnectionParameter connectionParameters = pr.getConnectionParameters(); if (connectionParameters==null) connectionParameters = standardConnectionParameters; return connectionParameters; } /** * Sets the SZR-GW connection parameters for standard connection. * * @param connectionParameters * the connection parameters. */ public void setStandardConnectionParameters(ConnectionParameter connectionParameters) { this.standardConnectionParameters = connectionParameters; } /* * */ public String getFullDirectoryName(String fileString) { return makeAbsoluteURL(fileString, baseDir_); } /* * */ private static String makeAbsoluteURL(String url, String root) { // if url is relative to rootConfigFileDirName make it absolute File keyFile; String newURL = url; if (null == url) return null; if (url.startsWith("http:/") || url.startsWith("https:/") || url.startsWith("ftp:/") || url.startsWith("file:/") || url.startsWith("file:\\")) { return url; } else { // check if absolute - if not make it absolute keyFile = new File(url); if (!keyFile.isAbsolute()) { keyFile = new File(root, url); newURL = keyFile.getPath(); } return newURL; } } /** * Initializes the configuration with a given XML configuration element found * in the MOA-ID configuration. * * @param configElem * the configuration element. * @throws ConfigurationException * if an error occurs initializing the configuration. */ public ParepConfiguration(Element configElem) throws ConfigurationException { partyRepresentatives = new HashMap(); partyRepresentatives.put("*", new PartyRepresentative(true, true)); String fileName = System.getProperty(ConfigurationProvider.CONFIG_PROPERTY_NAME); try { baseDir_ = (new File(fileName)).getParentFile().toURL().toString(); Logger.trace("Config base directory: " + baseDir_); // check for configuration in system properties if (System.getProperty(PAREP_VALIDATOR_CONFIG) != null) { Document doc = ParepUtils.readDocFromIs(new FileInputStream(System.getProperty(PAREP_VALIDATOR_CONFIG))); this.configElement = doc.getDocumentElement(); } else { this.configElement = configElem; } } catch (Exception e) { throw new ConfigurationException("Allgemeiner Fehler beim Einlesen der ParepValidatorConfiguration", null, e); } load(); } /* * */ private void load() throws ConfigurationException { Logger.debug("Parse ParepValidator Konfiguration"); try { Element nameSpaceNode = configElement.getOwnerDocument().createElement("NameSpaceNode"); nameSpaceNode.setAttribute("xmlns:" + Constants.MOA_ID_CONFIG_PREFIX, Constants.MOA_ID_CONFIG_NS_URI); // nameSpaceNode.setAttribute("xmlns:sgw", // SZRGWConstants.SZRGW_PROFILE_NS); Node inputProcessorNode = XPathAPI.selectSingleNode(configElement, Constants.MOA_ID_CONFIG_PREFIX + ":PartyRepresentation/" + Constants.MOA_ID_CONFIG_PREFIX + ":InputProcessor", nameSpaceNode); if (inputProcessorNode != null) { this.standardInputProcessorTemplate = ((Element) inputProcessorNode).getAttribute("template"); Node inputProcessorClassNode = XPathAPI.selectSingleNode(configElement, Constants.MOA_ID_CONFIG_PREFIX + ":PartyRepresentation/" + Constants.MOA_ID_CONFIG_PREFIX + ":InputProcessor/text()", nameSpaceNode); if (inputProcessorClassNode != null) { this.standardInputProcessorClass = inputProcessorClassNode.getNodeValue(); } } Node alwaysShowFormNode = XPathAPI.selectSingleNode(configElement, Constants.MOA_ID_CONFIG_PREFIX + ":PartyRepresentation/" + Constants.MOA_ID_CONFIG_PREFIX + ":AlwaysShowForm/text()", nameSpaceNode); if (alwaysShowFormNode != null) { this.setAlwaysShowForm(alwaysShowFormNode.getNodeValue()); } // load connection parameters Logger.debug("Lade SZR-Gateway Standard Verbindungsparameter"); Element connectionParamElement = (Element) XPathAPI.selectSingleNode(configElement, Constants.MOA_ID_CONFIG_PREFIX + ":PartyRepresentation/" + Constants.MOA_ID_CONFIG_PREFIX + ":ConnectionParameter", nameSpaceNode); if (connectionParamElement != null) { // parse connection parameters // ParepUtils.serializeElement(connectionParamElement, System.out); this.standardConnectionParameters = buildConnectionParameter(connectionParamElement, nameSpaceNode); } Logger.debug("Lade Konfiguration der Parteienvertreter"); NodeList partyRepresentativeNodeList = XPathAPI.selectNodeList(configElement, Constants.MOA_ID_CONFIG_PREFIX + ":PartyRepresentation/" + Constants.MOA_ID_CONFIG_PREFIX + ":PartyRepresentative", nameSpaceNode); for (int i = 0; i < partyRepresentativeNodeList.getLength(); i++) { PartyRepresentative partyRepresentative = new PartyRepresentative(); Element partyRepresentativeElement = (Element) partyRepresentativeNodeList.item(i); boolean representPhysicalParty = partyRepresentativeElement.getAttribute("representPhysicalParty").equalsIgnoreCase("true") ? true : false; boolean representCorporateParty = partyRepresentativeElement.getAttribute("representCorporateParty").equalsIgnoreCase("true") ? true : false; partyRepresentative.setOid(partyRepresentativeElement.getAttribute("oid")); partyRepresentative.setRepresentingPhysicalParty(representPhysicalParty); partyRepresentative.setRepresentingCorporateParty(representCorporateParty); partyRepresentative.setRepresentationText(partyRepresentativeElement.getAttribute("representationText")); Node inputProcessorSubNode = XPathAPI.selectSingleNode(partyRepresentativeElement, Constants.MOA_ID_CONFIG_PREFIX + ":InputProcessor", nameSpaceNode); if (inputProcessorSubNode != null) { partyRepresentative.setInputProcessorTemplate(((Element) inputProcessorSubNode).getAttribute("template")); Node inputProcessorClassSubNode = XPathAPI.selectSingleNode(partyRepresentativeElement, Constants.MOA_ID_CONFIG_PREFIX + ":InputProcessor/text()", nameSpaceNode); if (inputProcessorClassSubNode != null) { partyRepresentative.setInputProcessorClass(inputProcessorClassSubNode.getNodeValue()); } } Element connectionParamSubElement = (Element) XPathAPI.selectSingleNode(partyRepresentativeElement, Constants.MOA_ID_CONFIG_PREFIX + ":ConnectionParameter", nameSpaceNode); if (connectionParamSubElement == null) { if (this.standardConnectionParameters == null) { throw new ConfigurationException("Fehler beim Parsen der ParepValidatorConfiguration: SZR-GW Verbindungsparameter zu Parteienvetreter " + partyRepresentative.getOid() + " fehlen.", null, null); } } else { // parse connection parameters // ParepUtils.serializeElement(connectionParamSubElement, System.out); partyRepresentative.setConnectionParameters(buildConnectionParameter(connectionParamSubElement, nameSpaceNode)); } partyRepresentatives.put(partyRepresentative.getOid(), partyRepresentative); Logger.info("Parteienvertreter: " + partyRepresentative.getOid() + " erfolgreich konfiguriert (representPhysicalParty=" + partyRepresentative.isRepresentingPhysicalParty() + ", representCorporateParty=" + partyRepresentative.isRepresentingCorporateParty() + ", representationText=" + partyRepresentative.getRepresentationText() + ")"); } Logger.debug("ParepValidator Konfiguration erfolgreich geparst."); } catch (Exception e) { throw new ConfigurationException("Allgemeiner Fehler beim Parsen der MandateValidatorConfiguration", null, e); } } /* * */ private ConnectionParameter buildConnectionParameter(Element connParamElement, Element nameSpaceNode) throws ConfigurationException { try { ConnectionParameter connectionParameter = new ConnectionParameter(); // parse connection url String URL = connParamElement.getAttribute("URL"); connectionParameter.setUrl(URL); // accepted server certificates Node accServerCertsNode = XPathAPI.selectSingleNode(connParamElement, Constants.MOA_ID_CONFIG_PREFIX + ":AcceptedServerCertificates/text()", nameSpaceNode); if (accServerCertsNode != null) { String serverCertsDir = getFullDirectoryName(accServerCertsNode.getNodeValue()); Logger.debug("Full directory name of accepted server certificates: " + serverCertsDir); connectionParameter.setAcceptedServerCertificates(serverCertsDir); } // client key store Node clientKeyStoreNode = XPathAPI.selectSingleNode(connParamElement, Constants.MOA_ID_CONFIG_PREFIX + ":ClientKeyStore/text()", nameSpaceNode); if (clientKeyStoreNode != null) { String clientKeystore = getFullDirectoryName(clientKeyStoreNode.getNodeValue()); connectionParameter.setClientKeyStore(clientKeystore); } // client key store password Node clientKeyStorePasswordNode = XPathAPI.selectSingleNode(connParamElement, Constants.MOA_ID_CONFIG_PREFIX + ":ClientKeyStore/@password", nameSpaceNode); if (clientKeyStorePasswordNode != null) { connectionParameter.setClientKeyStorePassword(clientKeyStorePasswordNode.getNodeValue()); } return connectionParameter; } catch (Exception e) { throw new ConfigurationException("Allgemeiner Fehler beim Parsen der ParepValidator ConnectionParameter.", null, e); } } public boolean isPartyRepresentative(String representationID) { if (partyRepresentatives == null) return false; PartyRepresentative pr = (PartyRepresentative) partyRepresentatives.get(representationID); return pr != null; } public boolean isRepresentingCorporateParty(String representationID) { if (partyRepresentatives == null) return false; PartyRepresentative pr = (PartyRepresentative) partyRepresentatives.get(representationID); if (pr == null) return false; return pr.isRepresentingCorporateParty(); } public boolean isRepresentingPhysicalParty(String representationID) { if (partyRepresentatives == null) return false; PartyRepresentative pr = (PartyRepresentative) partyRepresentatives.get(representationID); if (pr == null) return false; return pr.isRepresentingPhysicalParty(); } public String getRepresentationText(String representationID) { String result = ParepValidator.STANDARD_REPRESENTATION_TEXT; if (partyRepresentatives != null) { PartyRepresentative pr = (PartyRepresentative) partyRepresentatives.get(representationID); if (pr != null) { if (!ParepUtils.isEmpty(pr.getRepresentationText())) result = pr.getRepresentationText(); } } return result; } /** * @return the input processor classname corresponding to representationID * @param representationID * the representation ID. */ public String getInputProcessorClass(String representationID) { String inputProcessorClass = standardInputProcessorClass; if (ParepUtils.isEmpty(inputProcessorClass)) inputProcessorClass = ParepValidator.PAREP_INPUT_PROCESSOR; if (!(partyRepresentatives == null || "*".equals(representationID))) { PartyRepresentative pr = (PartyRepresentative) partyRepresentatives.get(representationID); if (pr!=null) { String prInputProcessorClass = pr.getInputProcessorClass(); if (!ParepUtils.isEmpty(prInputProcessorClass)) inputProcessorClass = prInputProcessorClass; } } return inputProcessorClass; } /** * @param standardInputProcessorClass the standardInputProcessorClass to set */ public void setStandardInputProcessorClass(String standardInputProcessorClass) { this.standardInputProcessorClass = standardInputProcessorClass; } /** * @return the InputProcessorTemplate */ public String getInputProcessorTemplate(String representationID) { String inputProcessorTemplate = standardInputProcessorTemplate; if (ParepUtils.isEmpty(inputProcessorTemplate)) inputProcessorTemplate = ParepValidator.PAREP_INPUT_TEMPLATE; if (!(partyRepresentatives == null || "*".equals(representationID))) { PartyRepresentative pr = (PartyRepresentative) partyRepresentatives.get(representationID); if (pr!=null) { String prInputProcessorTemplate = pr.getInputProcessorTemplate(); if (!ParepUtils.isEmpty(prInputProcessorTemplate)) inputProcessorTemplate = prInputProcessorTemplate; } } return inputProcessorTemplate; } /** * @param standardInputProcessorTemplate the standardInputProcessorTemplate to set */ public void setStandardInputProcessorTemplate(String standardInputProcessorTemplate) { this.standardInputProcessorTemplate = standardInputProcessorTemplate; } /** * @return the alwaysShowForm */ public boolean isAlwaysShowForm() { return alwaysShowForm; } /** * @param alwaysShowForm the alwaysShowForm to set */ public void setAlwaysShowForm(String alwaysShowForm) { if (ParepUtils.isEmpty(alwaysShowForm)) { this.alwaysShowForm = false; } else { this.alwaysShowForm = alwaysShowForm.equalsIgnoreCase("true"); } } public static boolean isMandateCompatibilityMode(Element configElement) throws ConfigurationException { try { Element nameSpaceNode = configElement.getOwnerDocument().createElement("NameSpaceNode"); nameSpaceNode.setAttribute("xmlns:" + Constants.MOA_ID_CONFIG_PREFIX, Constants.MOA_ID_CONFIG_NS_URI); Node mandateCompatibilityNode = XPathAPI.selectSingleNode(configElement, Constants.MOA_ID_CONFIG_PREFIX + ":CompatibilityMode/text()", nameSpaceNode); if (mandateCompatibilityNode != null && !ParepUtils.isEmpty(mandateCompatibilityNode.getNodeValue())) { return mandateCompatibilityNode.getNodeValue().equalsIgnoreCase("true"); } return false; } catch (Exception e) { throw new ConfigurationException("Allgemeiner Fehler beim Parsen der ParepValidator ConnectionParameter.", null, e); } } // public static void main(String[] args) throws Exception { // System.setProperty(PAREP_VALIDATOR_CONFIG, "c:/Doku/work/Organwalter/ConfigurationSnippetAppSpecific.xml"); // System.setProperty("moa.id.configuration", "c:/workspace33moa/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf/moa-id/SampleMOAWIDConfiguration_withTestBKsProxy.xml"); // System.setProperty("log4j.configuration", "file:c:/workspace33moa/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf/moa-id/log4j.properties"); // Configuration cfg = new Configuration(null); // System.out.println(cfg.getInputProcessorClass("1.2.40.0.10.3.110")); //} }