/*
* Copyright 2003 Federal Chancellery Austria
* MOA-ID has been developed in a cooperation between BRZ, the Federal
* Chancellery Austria - ICT staff unit, and Graz University of Technology.
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
* http://www.osor.eu/eupl/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*
* This product combines work with different licenses. See the "NOTICE" text
* file for details on the various modules and licenses.
* The "NOTICE" text file is part of the distribution. Any derivative works
* that you distribute must include a readable copy of the "NOTICE" text file.
*/
package at.gv.egovernment.moa.id.config.proxy;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.NodeIterator;
import at.gv.egovernment.moa.id.config.legacy.ConfigurationBuilder;
import at.gv.egovernment.moa.id.config.ConfigurationException;
import at.gv.egovernment.moa.id.config.legacy.ConnectionParameter;
import at.gv.egovernment.moa.logging.Logger;
import at.gv.egovernment.moa.util.DOMUtils;
import at.gv.egovernment.moa.util.FileUtils;
import at.gv.egovernment.moa.util.XPathUtils;
/**
* Builds the configuration for MOA-ID Proxy.
*/
public class ProxyConfigurationBuilder extends ConfigurationBuilder {
/**
* Default online application configuration file name
* (used when /OnlineApplication/ProxyComponent@configFileURL
is null
).
*/
public static final String DEFAULT_OA_CONFIG_FILENAME = "MOAConfig.xml";
/** an XPATH-Expression */
private static final String PROXY_AUTH_XPATH =
ROOT + CONF + "ProxyComponent/" + CONF + "AuthComponent";
/** an XPATH-Expression */
protected static final String ROOTOA = "/" + CONF + "Configuration/";
/** an XPATH-Expression */
private static final String OA_PROXY_COMPONENT_XPATH = CONF + "ProxyComponent";
/** an XPATH-Expression */
private static final String OA_PROXY_COMPONENT_ABSOLUTE_XPATH = ROOT + CONF + "OnlineApplication/" + CONF + "ProxyComponent";
/** an XPATH-Expression */
private static final String OA_PROXY_URL_XPATH = CONF + "ProxyComponent/@configFileURL";
/** an XPATH-Expression */
private static final String OA_PROXY_SESSION_TIMEOUT_XPATH = CONF + "ProxyComponent/@sessionTimeOut";
/** an XPATH-Expression */
private static final String OA_PROXY_LOGIN_PARA_XPATH = CONF + "ProxyComponent/@loginParameterResolverImpl";
/** an XPATH-Expression */
private static final String OA_PROXY_LOGIN_PARA_CONF_XPATH = CONF + "ProxyComponent/@loginParameterResolverConfiguration";
/** an XPATH-Expression */
private static final String OA_PROXY_CONNECTION_BUILDER_XPATH = CONF + "ProxyComponent/@connectionBuilderImpl";
/** an XPATH-Expression */
protected static final String OACONF_LOGIN_TYPE_XPATH =
ROOTOA + CONF + "LoginType";
/** an XPATH-Expression */
protected static final String OACONF_BINDING_TYPE_XPATH =
ROOTOA + CONF + "Binding";
/** an XPATH-Expression */
protected static final String OACONF_PARAM_AUTH_PARAMETER_XPATH =
ROOTOA + CONF + "ParamAuth/" + CONF + "Parameter";
/** an XPATH-Expression */
protected static final String OACONF_USER_ID_XPATH =
ROOTOA + CONF + "BasicAuth/" + CONF + "UserID";
/** an XPATH-Expression */
protected static final String OACONF_PASSWORD_XPATH =
ROOTOA + CONF + "BasicAuth/" + CONF + "Password";
/** an XPATH-Expression */
protected static final String OACONF_HEADER_AUTH_HEADER_XPATH =
ROOTOA + CONF + "HeaderAuth/" + CONF + "Header";
/**
* Creates a new MOAConfigurationProvider
.
*
* @param configElem The root element of the MOA-ID configuration.
*/
public ProxyConfigurationBuilder(Element configElem, String rootConfigDir) {
super(configElem, rootConfigDir);
}
/**
* Method buildOAConfiguration.
*
* Build an {@link OAConfiguration} Object from the given configuration DOM element
*
* @param root
* @return OAConfiguration
* @throws ConfigurationException
*/
public OAConfiguration buildOAConfiguration(Element root) throws ConfigurationException{
OAConfiguration oaConfiguration = new OAConfiguration();
//The LoginType hast to be "stateless" or "stateful" to be valid
oaConfiguration.setLoginType(
XPathUtils.getElementValue(root, OACONF_LOGIN_TYPE_XPATH, null));
oaConfiguration.setBinding(
XPathUtils.getElementValue(root, OACONF_BINDING_TYPE_XPATH, OAConfiguration.BINDUNG_FULL));
//Try to build the Parameter Auth Parameters
NodeIterator paramAuthIter =
XPathUtils.selectNodeIterator(
root,
OACONF_PARAM_AUTH_PARAMETER_XPATH);
Element paramAuthElem;
HashMap paramAuthMap = new HashMap();
while ((paramAuthElem = (Element) paramAuthIter.nextNode()) != null) {
String name = XPathUtils.getAttributeValue(paramAuthElem, "@Name", null);
String value = XPathUtils.getAttributeValue(paramAuthElem, "@Value", null);
if (paramAuthMap.containsKey(name))
throw new ConfigurationException("config.06", new Object[]{"Doppelter Wert für Parameter per HeaderAuthentication"});
paramAuthMap.put(name, value);
}
oaConfiguration.setParamAuthMapping(paramAuthMap);
// Try to build the BasicAuthParameters
oaConfiguration.setBasicAuthUserIDMapping(
XPathUtils.getElementValue(root, OACONF_USER_ID_XPATH, null));
oaConfiguration.setBasicAuthPasswordMapping(
XPathUtils.getElementValue(root, OACONF_PASSWORD_XPATH, null));
//Try to build the Parameter Auth Parameters
NodeIterator headerAuthIter = XPathUtils.selectNodeIterator(root,OACONF_HEADER_AUTH_HEADER_XPATH);
Element headerAuthElem;
HashMap headerAuthMap = new HashMap();
while ((headerAuthElem = (Element) headerAuthIter.nextNode()) != null) {
String name =
XPathUtils.getAttributeValue(headerAuthElem, "@Name", null);
String value =
XPathUtils.getAttributeValue(headerAuthElem, "@Value", null);
// Contains Key (Neue Config-Exception: doppelte werte)
if (headerAuthMap.containsKey(name))
throw new ConfigurationException("config.06", new Object[]{"Doppelter Wert für Parameter per HeaderAuthentication"});
headerAuthMap.put(name, value);
}
oaConfiguration.setHeaderAuthMapping(headerAuthMap);
if (paramAuthMap.size() == 0) {
if (oaConfiguration.getBasicAuthUserIDMapping() == null) {
oaConfiguration.setAuthType(OAConfiguration.HEADER_AUTH);
}
else
oaConfiguration.setAuthType(OAConfiguration.BASIC_AUTH);
}
else
oaConfiguration.setAuthType(OAConfiguration.PARAM_AUTH);
return oaConfiguration;
}
/**
* Build an array of OnlineApplication Parameter Beans containing information
* about the proxy component
* @return An OAProxyParameter array containing beans
* with all relevant information for the proxy component of the online
* application
*/
public OAProxyParameter[] buildOnlineApplicationProxyParameters() throws ConfigurationException{
List oA_list = new ArrayList();
NodeList OAIter = XPathUtils.selectNodeList(configElem_, OA_XPATH);
for (int i = 0; i < OAIter.getLength(); i++) {
Element oAElem = (Element) OAIter.item(i);
Element proxyComponentElem = (Element) XPathUtils.selectSingleNode(oAElem,OA_PROXY_COMPONENT_XPATH);
if (proxyComponentElem != null) {
OAProxyParameter oap = new OAProxyParameter();
oap.setPublicURLPrefix(oAElem.getAttribute("publicURLPrefix"));
oap.setOaType(oAElem.getAttribute("type"));
oap.setConfigFileURL(XPathUtils.getAttributeValue(oAElem, OA_PROXY_URL_XPATH, null));
oap.setConfigFileURL(FileUtils.makeAbsoluteURL(oap.getConfigFileURL(), rootConfigFileDir_));
// default session time out: 3600 sec = 1 h
oap.setSessionTimeOut(new Integer(XPathUtils.getAttributeValue(oAElem,OA_PROXY_SESSION_TIMEOUT_XPATH,"3600")).intValue());
oap.setLoginParameterResolverImpl(XPathUtils.getAttributeValue(oAElem, OA_PROXY_LOGIN_PARA_XPATH, null));
oap.setLoginParameterResolverConfiguration(XPathUtils.getAttributeValue(oAElem, OA_PROXY_LOGIN_PARA_CONF_XPATH, null));
oap.setLoginParameterResolverConfiguration(FileUtils.makeAbsoluteURL(oap.getLoginParameterResolverConfiguration(), rootConfigFileDir_));
oap.setConnectionBuilderImpl(XPathUtils.getAttributeValue(oAElem,OA_PROXY_CONNECTION_BUILDER_XPATH, null));
ConnectionParameter conPara = buildConnectionParameter(proxyComponentElem);
oap.setConnectionParameter(conPara);
OAConfiguration oaConf = buildOAConfiguration(getOAConfigElement(oap));
oap.setOaConfiguration(oaConf);
oA_list.add(oap);
}
}
OAProxyParameter[] result =
new OAProxyParameter[oA_list.size()];
oA_list.toArray(result);
return result;
}
/**
* Reads the configuration file of the online application, and creates a DOM tree from it.
* If /OnlineApplication/ProxyComponent@configFileURL
is not given,
* uses default configuration file location.
*
* @param oap configuration data of online application, meant for use by MOA-ID-PROXY
* @return Element DOM tree root element
* @throws ConfigurationException on any exception thrown
*/
private Element getOAConfigElement(OAProxyParameter oap) throws ConfigurationException
{
try {
String configFileURL = oap.getConfigFileURL();
if (configFileURL == null) {
// use default config file URL, when config file URL is not given
configFileURL = oap.getConnectionParameter().getUrl();
if (configFileURL.charAt(configFileURL.length() - 1) != '/')
configFileURL += "/";
configFileURL += DEFAULT_OA_CONFIG_FILENAME;
}
Logger.info("Loading MOA-OA configuration " + configFileURL);
Element configElem = DOMUtils.parseXmlValidating(
new ByteArrayInputStream(FileUtils.readURL(configFileURL)));
return configElem;
}
catch (Throwable t) {
throw new ConfigurationException("config.03", new Object[] {"OAConfiguration"} , t);
}
}
/**
* Build a bean containing all information about the ProxyComponent
* @return The ConnectionParameter for the Proxy Component
*/
public ConnectionParameter buildAuthComponentConnectionParameter()
{
Element connectionParameter = (Element) XPathUtils.selectSingleNode(configElem_, PROXY_AUTH_XPATH);
if (connectionParameter==null) return null;
return buildConnectionParameter(connectionParameter);
}
}