Merge branch 'switchToJdkSax'

This commit is contained in:
Robert von Burg 2013-01-20 22:28:57 +01:00
commit 663a0b5995
16 changed files with 1580 additions and 757 deletions

View File

@ -71,6 +71,13 @@ along with Privilege. If not, see <http://www.gnu.org/licenses/>.
<AllAllowed>true</AllAllowed>
</Privilege>
</Role>
<Role name="restrictedRole">
<Privilege name="ch.eitchnet.privilege.test.model.TestSystemUserAction" policy="DefaultPrivilege">
<Allow>hello</Allow>
<Deny>goodbye</Deny>
</Privilege>
</Role>
</Roles>
</UsersAndRoles>

View File

@ -81,11 +81,6 @@
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>dom4j</artifactId>
<version>1.7-20060614</version>
</dependency>
<dependency>
<groupId>ch.eitchnet</groupId>
<artifactId>ch.eitchnet.utils</artifactId>

View File

@ -20,28 +20,22 @@
package ch.eitchnet.privilege.handler;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.eitchnet.privilege.base.PrivilegeException;
import ch.eitchnet.privilege.helper.XmlConstants;
import ch.eitchnet.privilege.helper.XmlHelper;
import ch.eitchnet.privilege.model.UserState;
import ch.eitchnet.privilege.model.internal.Privilege;
import ch.eitchnet.privilege.model.internal.Role;
import ch.eitchnet.privilege.model.internal.User;
import ch.eitchnet.privilege.xml.PrivilegeModelDomWriter;
import ch.eitchnet.privilege.xml.PrivilegeModelSaxReader;
/**
* {@link PersistenceHandler} implementation which reads the configuration from XML files. These configuration is passed
@ -138,120 +132,6 @@ public class XmlPersistenceHandler implements PersistenceHandler {
this.roleMapDirty = true;
}
/**
* @see ch.eitchnet.privilege.handler.PersistenceHandler#persist()
*/
@Override
public boolean persist() {
// get models file name
String modelFileName = this.parameterMap.get(XmlConstants.XML_PARAM_MODEL_FILE);
if (modelFileName == null || modelFileName.isEmpty()) {
throw new PrivilegeException("[" + PersistenceHandler.class.getName() + "] Defined parameter "
+ XmlConstants.XML_PARAM_MODEL_FILE + " is invalid");
}
// get model file
File modelFile = new File(this.modelPath);
boolean modelFileUnchanged = modelFile.exists() && modelFile.lastModified() == this.modelsFileDate;
if (modelFileUnchanged && !this.roleMapDirty && !this.userMapDirty) {
XmlPersistenceHandler.logger
.warn("Not persisting as current file is unchanged and model data is not dirty");
return false;
}
DocumentFactory docFactory = DocumentFactory.getInstance();
// create root element
Element rootElement = docFactory.createElement(XmlConstants.XML_ROOT_PRIVILEGE_USERS_AND_ROLES);
// USERS
// build XML DOM of users
List<Element> users = XmlPersistenceHandler.toDomUsers(this.userMap);
Element usersElement = docFactory.createElement(XmlConstants.XML_USERS);
for (Element userElement : users) {
usersElement.add(userElement);
}
rootElement.add(usersElement);
// ROLES
// build XML DOM of roles
List<Element> roles = XmlPersistenceHandler.toDomRoles(this.roleMap);
Element rolesElement = docFactory.createElement(XmlConstants.XML_ROLES);
for (Element roleElement : roles) {
rolesElement.add(roleElement);
}
rootElement.add(rolesElement);
// now write the file
XmlHelper.writeElement(rootElement, modelFile);
// reset dirty states
this.userMapDirty = false;
this.roleMapDirty = false;
return true;
}
/**
* @see ch.eitchnet.privilege.handler.PersistenceHandler#reload()
*/
@Override
public boolean reload() {
// validate file exists
File modelsFile = new File(this.modelPath);
if (!modelsFile.exists()) {
throw new PrivilegeException("[" + PersistenceHandler.class.getName() + "] Defined parameter "
+ XmlConstants.XML_PARAM_MODEL_FILE + " is invalid as models file does not exist at path "
+ modelsFile.getAbsolutePath());
}
this.roleMap = Collections.synchronizedMap(new HashMap<String, Role>());
this.userMap = Collections.synchronizedMap(new HashMap<String, User>());
// parse models xml file to XML document
Element modelsRootElement = XmlHelper.parseDocument(modelsFile).getRootElement();
this.modelsFileDate = modelsFile.lastModified();
// ROLES
// get roles element
Element rolesElement = modelsRootElement.element(XmlConstants.XML_ROLES);
// read roles
Map<String, Role> roles = readRoles(rolesElement);
this.roleMap = roles;
// USERS
// get users element
Element usersElement = modelsRootElement.element(XmlConstants.XML_USERS);
// read users
Map<String, User> users = readUsers(usersElement);
this.userMap = users;
this.userMapDirty = false;
this.roleMapDirty = false;
XmlPersistenceHandler.logger.info("Read " + this.userMap.size() + " Users");
XmlPersistenceHandler.logger.info("Read " + this.roleMap.size() + " Roles");
// validate we have a user with PrivilegeAdmin access
boolean privilegeAdminExists = false;
for (String username : this.userMap.keySet()) {
User user = this.userMap.get(username);
if (user.hasRole(PrivilegeHandler.PRIVILEGE_ADMIN_ROLE)) {
privilegeAdminExists = true;
break;
}
}
if (!privilegeAdminExists) {
XmlPersistenceHandler.logger.warn("No User with role '" + PrivilegeHandler.PRIVILEGE_ADMIN_ROLE
+ "' exists. Privilege modifications will not be possible!");
}
return true;
}
/**
* @see ch.eitchnet.privilege.handler.PersistenceHandler#initialize(java.util.Map)
*/
@ -284,317 +164,94 @@ public class XmlPersistenceHandler implements PersistenceHandler {
}
/**
* Parses {@link User} objects from their XML representations
*
* @param usersRootElement
* the element containing suer elements
*
* @return the map of converted {@link User} objects
* @see ch.eitchnet.privilege.handler.PersistenceHandler#reload()
*/
protected Map<String, User> readUsers(Element usersRootElement) {
@Override
public boolean reload() {
Map<String, User> userMap = new HashMap<String, User>();
@SuppressWarnings("unchecked")
List<Element> userElements = usersRootElement.elements(XmlConstants.XML_USER);
for (Element userElement : userElements) {
String userId = userElement.attributeValue(XmlConstants.XML_ATTR_USER_ID);
String username = userElement.attributeValue(XmlConstants.XML_ATTR_USERNAME);
String password = userElement.attributeValue(XmlConstants.XML_ATTR_PASSWORD);
String firstname = userElement.element(XmlConstants.XML_FIRSTNAME).getTextTrim();
String surname = userElement.element(XmlConstants.XML_SURNAME).getTextTrim();
UserState userState = UserState.valueOf(userElement.element(XmlConstants.XML_STATE).getTextTrim());
// TODO better parsing needed
String localeName = userElement.element(XmlConstants.XML_LOCALE).getTextTrim();
Locale locale = new Locale(localeName);
// read roles
Element rolesElement = userElement.element(XmlConstants.XML_ROLES);
@SuppressWarnings("unchecked")
List<Element> rolesElementList = rolesElement.elements(XmlConstants.XML_ROLE);
Set<String> roles = new HashSet<String>();
for (Element roleElement : rolesElementList) {
String roleName = roleElement.getTextTrim();
if (roleName.isEmpty()) {
XmlPersistenceHandler.logger.error("User " + username
+ " has a role defined with no name, Skipped.");
} else if (!this.roleMap.containsKey(roleName)) {
XmlPersistenceHandler.logger.error("User " + username + " has a inexistant role " + roleName
+ ", Skipped.");
} else {
roles.add(roleName);
}
}
// read properties
Element propertiesElement = userElement.element(XmlConstants.XML_PROPERTIES);
Map<String, String> propertyMap = XmlPersistenceHandler.convertToPropertyMap(propertiesElement);
// create user
User user = new User(userId, username, password, firstname, surname, userState, roles, locale, propertyMap);
// put user in map
userMap.put(username, user);
XmlPersistenceHandler.logger.info("Loaded user " + user);
// validate file exists
File modelsFile = new File(this.modelPath);
if (!modelsFile.exists()) {
throw new PrivilegeException("[" + PersistenceHandler.class.getName() + "] Defined parameter "
+ XmlConstants.XML_PARAM_MODEL_FILE + " is invalid as models file does not exist at path "
+ modelsFile.getAbsolutePath());
}
return userMap;
this.roleMap = Collections.synchronizedMap(new HashMap<String, Role>());
this.userMap = Collections.synchronizedMap(new HashMap<String, User>());
// parse models xml file to XML document
PrivilegeModelSaxReader xmlHandler = new PrivilegeModelSaxReader();
XmlHelper.parseDocument(modelsFile, xmlHandler);
this.modelsFileDate = modelsFile.lastModified();
// ROLES
List<Role> roles = xmlHandler.getRoles();
for (Role role : roles) {
this.roleMap.put(role.getName(), role);
}
// USERS
List<User> users = xmlHandler.getUsers();
for (User user : users) {
this.userMap.put(user.getUsername(), user);
}
this.userMapDirty = false;
this.roleMapDirty = false;
XmlPersistenceHandler.logger.info("Read " + this.userMap.size() + " Users");
XmlPersistenceHandler.logger.info("Read " + this.roleMap.size() + " Roles");
// validate we have a user with PrivilegeAdmin access
boolean privilegeAdminExists = false;
for (String username : this.userMap.keySet()) {
User user = this.userMap.get(username);
if (user.hasRole(PrivilegeHandler.PRIVILEGE_ADMIN_ROLE)) {
privilegeAdminExists = true;
break;
}
}
if (!privilegeAdminExists) {
XmlPersistenceHandler.logger.warn("No User with role '" + PrivilegeHandler.PRIVILEGE_ADMIN_ROLE
+ "' exists. Privilege modifications will not be possible!");
}
return true;
}
/**
* Parses {@link Role} objects from their XML representations
*
* @param rolesRootElement
* the element containing role elements
*
* @return the map of converted {@link Role} objects
* @see ch.eitchnet.privilege.handler.PersistenceHandler#persist()
*/
protected Map<String, Role> readRoles(Element rolesRootElement) {
@Override
public boolean persist() {
Map<String, Role> roleMap = new HashMap<String, Role>();
@SuppressWarnings("unchecked")
List<Element> roleElements = rolesRootElement.elements(XmlConstants.XML_ROLE);
for (Element roleElement : roleElements) {
String roleName = roleElement.attributeValue(XmlConstants.XML_ATTR_NAME);
Map<String, Privilege> privilegeMap = readPrivileges(roleElement);
Role role = new Role(roleName, privilegeMap);
roleMap.put(roleName, role);
// get models file name
String modelFileName = this.parameterMap.get(XmlConstants.XML_PARAM_MODEL_FILE);
if (modelFileName == null || modelFileName.isEmpty()) {
throw new PrivilegeException("[" + PersistenceHandler.class.getName() + "] Defined parameter "
+ XmlConstants.XML_PARAM_MODEL_FILE + " is invalid");
}
return roleMap;
}
/**
* Parses {@link Privilege} objects from their XML representation to their objects
*
* @param roleParentElement
* the parent on which the Privilege XML elements are
*
* @return the map of {@link Privilege} objects
*/
protected Map<String, Privilege> readPrivileges(Element roleParentElement) {
Map<String, Privilege> privilegeMap = new HashMap<String, Privilege>();
@SuppressWarnings("unchecked")
List<Element> privilegeElements = roleParentElement.elements(XmlConstants.XML_PRIVILEGE);
for (Element privilegeElement : privilegeElements) {
String privilegeName = privilegeElement.attributeValue(XmlConstants.XML_ATTR_NAME);
String privilegePolicy = privilegeElement.attributeValue(XmlConstants.XML_ATTR_POLICY);
Element allAllowedE = privilegeElement.element(XmlConstants.XML_ALL_ALLOWED);
boolean allAllowed = false;
if (allAllowedE != null) {
allAllowed = Boolean.valueOf(allAllowedE.getTextTrim()).booleanValue();
}
@SuppressWarnings("unchecked")
List<Element> denyElements = privilegeElement.elements(XmlConstants.XML_DENY);
Set<String> denyList = new HashSet<String>(denyElements.size());
for (Element denyElement : denyElements) {
String denyValue = denyElement.getTextTrim();
if (!denyValue.isEmpty())
denyList.add(denyValue);
}
@SuppressWarnings("unchecked")
List<Element> allowElements = privilegeElement.elements(XmlConstants.XML_ALLOW);
Set<String> allowList = new HashSet<String>(allowElements.size());
for (Element allowElement : allowElements) {
String allowValue = allowElement.getTextTrim();
if (!allowValue.isEmpty())
allowList.add(allowValue);
}
Privilege privilege = new Privilege(privilegeName, privilegePolicy, allAllowed, denyList, allowList);
privilegeMap.put(privilegeName, privilege);
// get model file
File modelFile = new File(this.modelPath);
boolean modelFileUnchanged = modelFile.exists() && modelFile.lastModified() == this.modelsFileDate;
if (modelFileUnchanged && !this.roleMapDirty && !this.userMapDirty) {
XmlPersistenceHandler.logger
.warn("Not persisting as current file is unchanged and model data is not dirty");
return false;
}
return privilegeMap;
}
// delegate writing
PrivilegeModelDomWriter modelWriter = new PrivilegeModelDomWriter(getAllUsers(), getAllRoles(), modelFile);
modelWriter.write();
/**
* Converts {@link User} objects to their XML representations
*
* @param userMap
* the map of users to convert
*
* @return the list of XML User elements
*/
protected static List<Element> toDomUsers(Map<String, User> userMap) {
// reset dirty states
this.userMapDirty = false;
this.roleMapDirty = false;
List<Element> usersAsElements = new ArrayList<Element>(userMap.size());
DocumentFactory documentFactory = DocumentFactory.getInstance();
synchronized (userMap) {
for (String userName : userMap.keySet()) {
// get the user object
User user = userMap.get(userName);
// create the user element
Element userElement = documentFactory.createElement(XmlConstants.XML_USER);
userElement.addAttribute(XmlConstants.XML_ATTR_USER_ID, user.getUserId());
userElement.addAttribute(XmlConstants.XML_ATTR_USERNAME, user.getUsername());
userElement.addAttribute(XmlConstants.XML_ATTR_PASSWORD, user.getPassword());
// add first name element
Element firstnameElement = documentFactory.createElement(XmlConstants.XML_FIRSTNAME);
firstnameElement.setText(user.getFirstname());
userElement.add(firstnameElement);
// add surname element
Element surnameElement = documentFactory.createElement(XmlConstants.XML_SURNAME);
surnameElement.setText(user.getSurname());
userElement.add(surnameElement);
// add state element
Element stateElement = documentFactory.createElement(XmlConstants.XML_STATE);
stateElement.setText(user.getUserState().toString());
userElement.add(stateElement);
// add locale element
Element localeElement = documentFactory.createElement(XmlConstants.XML_LOCALE);
localeElement.setText(user.getLocale().toString());
userElement.add(localeElement);
// add all the role elements
Element rolesElement = documentFactory.createElement(XmlConstants.XML_ROLES);
userElement.add(rolesElement);
for (String roleName : user.getRoles()) {
Element roleElement = documentFactory.createElement(XmlConstants.XML_ROLE);
roleElement.setText(roleName);
rolesElement.add(roleElement);
}
// add element to return list
usersAsElements.add(userElement);
}
}
return usersAsElements;
}
/**
* Converts {@link Role} objects to their XML representations
*
* @param roleMap
* the roles to convert
*
* @return the list of XML Role elements
*/
protected static List<Element> toDomRoles(Map<String, Role> roleMap) {
List<Element> rolesAsElements = new ArrayList<Element>(roleMap.size());
DocumentFactory documentFactory = DocumentFactory.getInstance();
synchronized (roleMap) {
for (String roleName : roleMap.keySet()) {
// get the role object
Role role = roleMap.get(roleName);
// create the role element
Element roleElement = documentFactory.createElement(XmlConstants.XML_ROLE);
roleElement.addAttribute(XmlConstants.XML_ATTR_NAME, role.getName());
// add all the privileges
XmlPersistenceHandler.toDomPrivileges(roleElement, role.getPrivilegeMap());
// add element to return list
rolesAsElements.add(roleElement);
}
}
return rolesAsElements;
}
/**
* Converts {@link Privilege} objects to their XML representation
*
* @param roleParentElement
* the XML element of the parent {@link Role}
* @param privilegeMap
* the map of {@link Privilege}s to convert
*/
protected static void toDomPrivileges(Element roleParentElement, Map<String, Privilege> privilegeMap) {
DocumentFactory documentFactory = DocumentFactory.getInstance();
for (Privilege privilege : privilegeMap.values()) {
// create the privilege element
Element privilegeElement = documentFactory.createElement(XmlConstants.XML_PRIVILEGE);
privilegeElement.addAttribute(XmlConstants.XML_ATTR_NAME, privilege.getName());
privilegeElement.addAttribute(XmlConstants.XML_ATTR_POLICY, privilege.getPolicy());
// add the all allowed element
Element allAllowedElement = documentFactory.createElement(XmlConstants.XML_ALL_ALLOWED);
allAllowedElement.setText(Boolean.toString(privilege.isAllAllowed()));
privilegeElement.add(allAllowedElement);
// add all the deny values
for (String denyValue : privilege.getDenyList()) {
Element denyValueElement = documentFactory.createElement(XmlConstants.XML_DENY);
denyValueElement.setText(denyValue);
privilegeElement.add(denyValueElement);
}
// add all the allow values
for (String allowValue : privilege.getAllowList()) {
Element allowValueElement = documentFactory.createElement(XmlConstants.XML_ALLOW);
allowValueElement.setText(allowValue);
privilegeElement.add(allowValueElement);
}
// add element to parent
roleParentElement.add(privilegeElement);
}
}
/**
* Converts an {@link XmlConstants#XML_PROPERTIES} element containing {@link XmlConstants#XML_PROPERTY} elements to
* a {@link Map} of String key/value pairs
*
* @param element
* the XML {@link Element} with name {@link XmlConstants#XML_PROPERTIES} containing
* {@link XmlConstants#XML_PROPERTY} elements
*
* @return the {@link Map} of the property name/value combinations from the given {@link Element}
*/
@SuppressWarnings("unchecked")
protected static Map<String, String> convertToPropertyMap(Element element) {
// if element is null then there are no properties, so return empty map
if (element == null)
return Collections.emptyMap();
List<Element> elements = element.elements(XmlConstants.XML_PROPERTY);
// if elements is null or empty then there are no properties, so return empty map
if (elements == null || elements.isEmpty())
return Collections.emptyMap();
Map<String, String> propertyMap = new HashMap<String, String>();
for (Element property : elements) {
String name = property.attributeValue(XmlConstants.XML_ATTR_NAME);
String value = property.attributeValue(XmlConstants.XML_ATTR_VALUE);
propertyMap.put(name, value);
}
return propertyMap;
return true;
}
}

View File

@ -20,12 +20,12 @@
package ch.eitchnet.privilege.helper;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import java.util.HashMap;
import java.util.Map;
import ch.eitchnet.privilege.handler.PrivilegeHandler;
import ch.eitchnet.privilege.model.internal.PrivilegeContainerModel;
import ch.eitchnet.privilege.xml.PrivilegeConfigDomWriter;
/**
* <p>
@ -46,7 +46,7 @@ import ch.eitchnet.privilege.handler.PrivilegeHandler;
*/
public class BootstrapConfigurationHelper {
// private static final Logger logger = LoggerFactory.getLogger(BootstrapConfigurationHelper.class);
// private static final Logger logger = Loggerdoc.getLogger(BootstrapConfigurationHelper.class);
private static String path;
@ -81,81 +81,29 @@ public class BootstrapConfigurationHelper {
throw new RuntimeException("Could not create path " + pathF.getAbsolutePath());
}
Map<String, String> parameterMap = new HashMap<String, String>();
Map<String, String> encryptionHandlerParameterMap = new HashMap<String, String>();
Map<String, String> persistenceHandlerParameterMap = new HashMap<String, String>();
// TODO ask other questions...
parameterMap.put("autoPersistOnPasswordChange", "true");
encryptionHandlerParameterMap.put("hashAlgorithm", "SHA-256");
persistenceHandlerParameterMap.put("basePath", "./target/test");
persistenceHandlerParameterMap.put("modelXmlFile", "PrivilegeModel.xml");
PrivilegeContainerModel containerModel = new PrivilegeContainerModel();
containerModel.setParameterMap(parameterMap);
containerModel.setEncryptionHandlerClassName(defaultEncryptionHandler);
containerModel.setEncryptionHandlerParameterMap(encryptionHandlerParameterMap);
containerModel.setPersistenceHandlerClassName(defaultPersistenceHandler);
containerModel.setPersistenceHandlerParameterMap(persistenceHandlerParameterMap);
containerModel.addPolicy("DefaultPrivilege", "ch.eitchnet.privilege.policy.DefaultPrivilege");
// now perform work:
BootstrapConfigurationHelper.createXmlPrivilegeContainer();
BootstrapConfigurationHelper.createPolicyConfiguration();
BootstrapConfigurationHelper.createModel();
}
/**
*
*/
private static void createModel() {
// TODO Auto-generated method stub
}
/**
*
*/
private static void createPolicyConfiguration() {
// TODO Auto-generated method stub
}
/**
*
*/
private static void createXmlPrivilegeContainer() {
// create document root
DocumentFactory factory = DocumentFactory.getInstance();
Document doc = factory.createDocument(XmlHelper.DEFAULT_ENCODING);
doc.setName(XmlConstants.XML_ROOT_PRIVILEGE);
Element rootElement = factory.createElement(XmlConstants.XML_ROOT_PRIVILEGE);
doc.setRootElement(rootElement);
Element containerElement = factory.createElement(XmlConstants.XML_CONTAINER);
Element parameterElement;
Element parametersElement;
// create PersistenceHandler
Element persistenceHandlerElem = factory.createElement(XmlConstants.XML_HANDLER_PERSISTENCE);
containerElement.add(persistenceHandlerElem);
persistenceHandlerElem.addAttribute(XmlConstants.XML_ATTR_CLASS,
BootstrapConfigurationHelper.defaultPersistenceHandler);
parametersElement = factory.createElement(XmlConstants.XML_PARAMETERS);
persistenceHandlerElem.add(parametersElement);
// Parameter basePath
parameterElement = factory.createElement(XmlConstants.XML_PARAMETER);
parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_BASE_PATH);
parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, BootstrapConfigurationHelper.basePath);
parametersElement.add(parameterElement);
// Parameter modelXmlFile
parameterElement = factory.createElement(XmlConstants.XML_PARAMETER);
parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_MODEL_FILE);
parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, BootstrapConfigurationHelper.modelFileName);
parametersElement.add(parameterElement);
// create EncryptionHandler
Element encryptionHandlerElem = factory.createElement(XmlConstants.XML_HANDLER_ENCRYPTION);
containerElement.add(encryptionHandlerElem);
encryptionHandlerElem.addAttribute(XmlConstants.XML_ATTR_CLASS,
BootstrapConfigurationHelper.defaultEncryptionHandler);
parametersElement = factory.createElement(XmlConstants.XML_PARAMETERS);
encryptionHandlerElem.add(parametersElement);
// Parameter hashAlgorithm
parameterElement = factory.createElement(XmlConstants.XML_PARAMETER);
parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_HASH_ALGORITHM);
parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, BootstrapConfigurationHelper.hashAlgorithm);
parametersElement.add(parameterElement);
// write the container file to disk
File privilegeContainerFile = new File(BootstrapConfigurationHelper.path + "/"
File configFile = new File(BootstrapConfigurationHelper.path + "/"
+ BootstrapConfigurationHelper.defaultPrivilegeContainerXmlFile);
XmlHelper.writeDocument(doc, privilegeContainerFile);
PrivilegeConfigDomWriter configSaxWriter = new PrivilegeConfigDomWriter(containerModel, configFile);
configSaxWriter.write();
}
}

View File

@ -1,204 +0,0 @@
/*
* Copyright (c) 2010 - 2012
*
* This file is part of Privilege.
*
* Privilege is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Privilege is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Privilege. If not, see <http://www.gnu.org/licenses/>.
*
*/
package ch.eitchnet.privilege.helper;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.eitchnet.privilege.base.PrivilegeException;
import ch.eitchnet.privilege.handler.DefaultPrivilegeHandler;
import ch.eitchnet.privilege.handler.EncryptionHandler;
import ch.eitchnet.privilege.handler.PersistenceHandler;
import ch.eitchnet.privilege.handler.PrivilegeHandler;
import ch.eitchnet.privilege.policy.PrivilegePolicy;
import ch.eitchnet.utils.helper.StringHelper;
/**
* This class implements the initializing of the {@link PrivilegeHandler} by loading an XML file containing the
* configuration
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class InitializationHelper {
private static final Logger logger = LoggerFactory.getLogger(InitializationHelper.class);
/**
* Initializes the {@link DefaultPrivilegeHandler} from the configuration file
*
* @param privilegeXmlFile
* a {@link File} reference to the XML file containing the configuration for Privilege
*
* @return the {@link PrivilegeHandler} instance loaded from the configuration file
*/
public static PrivilegeHandler initializeFromXml(File privilegeXmlFile) {
// make sure file exists
if (!privilegeXmlFile.exists()) {
throw new PrivilegeException("Privilege file does not exist at path " + privilegeXmlFile.getAbsolutePath());
}
// parse container xml file to XML document
Element rootElement = XmlHelper.parseDocument(privilegeXmlFile).getRootElement();
Element containerElement = rootElement.element(XmlConstants.XML_CONTAINER);
// instantiate encryption handler
Element encryptionHandlerElement = containerElement.element(XmlConstants.XML_HANDLER_ENCRYPTION);
String encryptionHandlerClassName = encryptionHandlerElement.attributeValue(XmlConstants.XML_ATTR_CLASS);
EncryptionHandler encryptionHandler = ClassHelper.instantiateClass(encryptionHandlerClassName);
// instantiate persistence handler
Element persistenceHandlerElement = containerElement.element(XmlConstants.XML_HANDLER_PERSISTENCE);
String persistenceHandlerClassName = persistenceHandlerElement.attributeValue(XmlConstants.XML_ATTR_CLASS);
PersistenceHandler persistenceHandler = ClassHelper.instantiateClass(persistenceHandlerClassName);
// instantiate privilege handler
DefaultPrivilegeHandler privilegeHandler = new DefaultPrivilegeHandler();
// get policies
Element policiesElement = rootElement.element(XmlConstants.XML_POLICIES);
Map<String, Class<PrivilegePolicy>> policyMap = InitializationHelper.convertToPolicyMap(policiesElement);
try {
// get parameters
Element parameterElement = encryptionHandlerElement.element(XmlConstants.XML_PARAMETERS);
Map<String, String> parameterMap = InitializationHelper.convertToParameterMap(parameterElement);
// initialize encryption handler
encryptionHandler.initialize(parameterMap);
} catch (Exception e) {
InitializationHelper.logger.error(e.getMessage(), e);
throw new PrivilegeException("EncryptionHandler " + encryptionHandlerClassName
+ " could not be initialized");
}
try {
// get parameters
Element parameterElement = persistenceHandlerElement.element(XmlConstants.XML_PARAMETERS);
Map<String, String> parameterMap = InitializationHelper.convertToParameterMap(parameterElement);
// initialize persistence handler
persistenceHandler.initialize(parameterMap);
} catch (Exception e) {
InitializationHelper.logger.error(e.getMessage(), e);
throw new PrivilegeException("PersistenceHandler " + persistenceHandlerElement
+ " could not be initialized");
}
try {
// get parameters
Element parameterElement = containerElement.element(XmlConstants.XML_PARAMETERS);
Map<String, String> parameterMap = InitializationHelper.convertToParameterMap(parameterElement);
// initialize privilege handler
privilegeHandler.initialize(parameterMap, encryptionHandler, persistenceHandler, policyMap);
} catch (Exception e) {
InitializationHelper.logger.error(e.getMessage(), e);
throw new PrivilegeException("PrivilegeHandler " + privilegeHandler.getClass().getName()
+ " could not be initialized");
}
return privilegeHandler;
}
/**
* Converts an {@link XmlConstants#XML_PARAMETERS} element containing {@link XmlConstants#XML_PARAMETER} elements to
* a {@link Map} of String key/value pairs
*
* @param element
* the XML {@link Element} with name {@link XmlConstants#XML_PARAMETERS} containing
* {@link XmlConstants#XML_PARAMETER} elements
*
* @return the {@link Map} of the parameter name/value combinations from the given {@link Element}
*/
@SuppressWarnings("unchecked")
public static Map<String, String> convertToParameterMap(Element element) {
// if element is null then there are no parameters, so return empty map
if (element == null)
return Collections.emptyMap();
List<Element> elements = element.elements(XmlConstants.XML_PARAMETER);
// if elements is null or empty then there are no parameters, so return empty map
if (elements == null || elements.isEmpty())
return Collections.emptyMap();
Map<String, String> parameterMap = new HashMap<String, String>();
for (Element parameter : elements) {
String name = parameter.attributeValue(XmlConstants.XML_ATTR_NAME);
String value = parameter.attributeValue(XmlConstants.XML_ATTR_VALUE);
// replace any defined system properties
value = StringHelper.replaceSystemPropertiesIn(value);
parameterMap.put(name, value);
}
return parameterMap;
}
/**
* Converts an {@link XmlConstants#XML_POLICIES} element containing {@link XmlConstants#XML_POLICY} elements to a
* {@link Map} of String/Class pairs
*
* @param element
* the XML {@link Element} with name {@link XmlConstants#XML_POLICIES} containing
* {@link XmlConstants#XML_POLICY} elements
*
* @return the {@link Map} of the policy name/class combinations from the given {@link Element}
*/
@SuppressWarnings("unchecked")
public static Map<String, Class<PrivilegePolicy>> convertToPolicyMap(Element element) {
Map<String, Class<PrivilegePolicy>> policyMap = new HashMap<String, Class<PrivilegePolicy>>();
List<Element> policyElements = element.elements(XmlConstants.XML_POLICY);
for (Element policyElement : policyElements) {
String policyName = policyElement.attributeValue(XmlConstants.XML_ATTR_NAME);
String policyClass = policyElement.attributeValue(XmlConstants.XML_ATTR_CLASS);
Class<PrivilegePolicy> clazz;
try {
clazz = ClassHelper.loadClass(policyClass);
} catch (PrivilegeException e) {
throw new PrivilegeException("The Policy with name " + policyName + " does not exist", e);
}
policyMap.put(policyName, clazz);
}
return policyMap;
}
}

View File

@ -20,24 +20,30 @@
package ch.eitchnet.privilege.helper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import ch.eitchnet.privilege.base.PrivilegeException;
import ch.eitchnet.utils.exceptions.XmlException;
/**
* Helper class for performing XML based tasks using Dom4J
@ -61,22 +67,22 @@ public class XmlHelper {
*
* @return a {@link Document} object containing the dom4j {@link Element}s of the XML file
*/
public static Document parseDocument(File xmlFile) {
public static void parseDocument(File xmlFile, DefaultHandler xmlHandler) {
try {
InputStream inStream = new FileInputStream(xmlFile);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXReader reader = new SAXReader();
Document document = reader.read(inStream);
SAXParser sp = spf.newSAXParser();
XmlHelper.logger.info("Parsing XML document " + xmlFile.getAbsolutePath());
sp.parse(xmlFile, xmlHandler);
XmlHelper.logger.info("Read XML document " + document.getRootElement().getName());
return document;
} catch (FileNotFoundException e) {
throw new PrivilegeException("The XML file does not exist or is not readable: " + xmlFile.getAbsolutePath());
} catch (DocumentException e) {
throw new PrivilegeException("the XML file " + xmlFile.getAbsolutePath() + " is not parseable:", e);
} catch (ParserConfigurationException e) {
throw new PrivilegeException("Failed to initialize a SAX Parser: " + e.getLocalizedMessage(), e);
} catch (SAXException e) {
throw new PrivilegeException("The XML file " + xmlFile.getAbsolutePath() + " is not parseable:", e);
} catch (IOException e) {
throw new PrivilegeException("The XML could not be read: " + xmlFile.getAbsolutePath());
}
}
@ -87,40 +93,40 @@ public class XmlHelper {
* the {@link Document} to write to the file system
* @param file
* the {@link File} describing the path on the file system where the XML file should be written to
*
* @throws RuntimeException
* if something went wrong while creating the XML configuration, or writing the element
*/
public static void writeDocument(Document document, File file) {
public static void writeDocument(Document document, File file) throws RuntimeException {
XmlHelper.logger.info("Exporting document element " + document.getName() + " to " + file.getAbsolutePath());
OutputStream fileOutputStream = null;
XmlHelper.logger.info("Exporting document element " + document.getNodeName() + " to " + file.getAbsolutePath());
try {
fileOutputStream = new FileOutputStream(file);
String aEncodingScheme = document.getXMLEncoding();
if (aEncodingScheme == null || aEncodingScheme.isEmpty()) {
aEncodingScheme = XmlHelper.DEFAULT_ENCODING;
String encoding = document.getInputEncoding();
if (encoding == null || encoding.isEmpty()) {
encoding = XmlHelper.DEFAULT_ENCODING;
}
OutputFormat outformat = OutputFormat.createPrettyPrint();
outformat.setEncoding(aEncodingScheme);
XMLWriter writer = new XMLWriter(fileOutputStream, outformat);
writer.write(document);
writer.flush();
// Set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer transformer = transfac.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");
//transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\t");
// Transform to file
StreamResult result = new StreamResult(file);
Source xmlSource = new DOMSource(document);
transformer.transform(xmlSource, result);
} catch (Exception e) {
throw new PrivilegeException("Exception while exporting to file: " + e, e);
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
XmlHelper.logger.error("Could not close file output stream: " + e, e);
}
}
}
}
@ -131,13 +137,40 @@ public class XmlHelper {
* the {@link Element} to write to the file system
* @param file
* the {@link File} describing the path on the file system where the XML file should be written to
* @param encoding
* encoding to use to write the file
*
* @throws RuntimeException
* if something went wrong while creating the XML configuration, or writing the element
*/
public static void writeElement(Element rootElement, File file) {
Document document = DocumentFactory.getInstance().createDocument(XmlHelper.DEFAULT_ENCODING);
document.setRootElement(rootElement);
document.setName(rootElement.getName());
public static void writeElement(Element rootElement, File file, String encoding) throws RuntimeException {
Document document = createDocument();
document.appendChild(rootElement);
XmlHelper.writeDocument(document, file);
}
/**
* Returns a new document instance
*
* @return a new document instance
*
* @throws RuntimeException
* if something went wrong while creating the XML configuration
*/
public static Document createDocument() throws RuntimeException {
try {
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document document = docBuilder.newDocument();
return document;
} catch (DOMException e) {
throw new XmlException("Failed to create Document: " + e.getLocalizedMessage(), e);
} catch (ParserConfigurationException e) {
throw new XmlException("Failed to create Document: " + e.getLocalizedMessage(), e);
}
}
}

View File

@ -0,0 +1,167 @@
/*
* Copyright (c) 2012, Robert von Burg
*
* All rights reserved.
*
* This file is part of the XXX.
*
* XXX is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* XXX is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XXX. If not, see
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.privilege.model.internal;
import java.util.HashMap;
import java.util.Map;
import ch.eitchnet.privilege.base.PrivilegeException;
import ch.eitchnet.privilege.policy.PrivilegePolicy;
public class PrivilegeContainerModel {
String encryptionHandlerClassName;
Map<String, String> encryptionHandlerParameterMap;
String persistenceHandlerClassName;
Map<String, String> persistenceHandlerParameterMap;
Map<String, String> parameterMap;
private Map<String, Class<PrivilegePolicy>> policies = new HashMap<String, Class<PrivilegePolicy>>();
/**
* @return the parameterMap
*/
public Map<String, String> getParameterMap() {
return this.parameterMap;
}
/**
* @param parameterMap
* the parameterMap to set
*/
public void setParameterMap(Map<String, String> parameterMap) {
this.parameterMap = parameterMap;
}
/**
* @return the encryptionHandlerClassName
*/
public String getEncryptionHandlerClassName() {
return this.encryptionHandlerClassName;
}
/**
* @param encryptionHandlerClassName
* the encryptionHandlerClassName to set
*/
public void setEncryptionHandlerClassName(String encryptionHandlerClassName) {
this.encryptionHandlerClassName = encryptionHandlerClassName;
}
/**
* @return the encryptionHandlerParameterMap
*/
public Map<String, String> getEncryptionHandlerParameterMap() {
return this.encryptionHandlerParameterMap;
}
/**
* @param encryptionHandlerParameterMap
* the encryptionHandlerParameterMap to set
*/
public void setEncryptionHandlerParameterMap(Map<String, String> encryptionHandlerParameterMap) {
this.encryptionHandlerParameterMap = encryptionHandlerParameterMap;
}
/**
* @return the persistenceHandlerClassName
*/
public String getPersistenceHandlerClassName() {
return this.persistenceHandlerClassName;
}
/**
* @param persistenceHandlerClassName
* the persistenceHandlerClassName to set
*/
public void setPersistenceHandlerClassName(String persistenceHandlerClassName) {
this.persistenceHandlerClassName = persistenceHandlerClassName;
}
/**
* @return the persistenceHandlerParameterMap
*/
public Map<String, String> getPersistenceHandlerParameterMap() {
return this.persistenceHandlerParameterMap;
}
/**
* @param persistenceHandlerParameterMap
* the persistenceHandlerParameterMap to set
*/
public void setPersistenceHandlerParameterMap(Map<String, String> persistenceHandlerParameterMap) {
this.persistenceHandlerParameterMap = persistenceHandlerParameterMap;
}
/**
* @param name
* @param policyClass
*/
public void addPolicy(String privilegeName, String policyClassName) {
try {
// load class and try to create a new instance
@SuppressWarnings("unchecked")
Class<PrivilegePolicy> clazz = (Class<PrivilegePolicy>) Class.forName(policyClassName);
clazz.newInstance();
this.policies.put(privilegeName, clazz);
} catch (InstantiationException e) {
throw new PrivilegeException("Configured Privilege Policy " + privilegeName + " with class "
+ policyClassName + " could not be instantiated.", e);
} catch (IllegalAccessException e) {
throw new PrivilegeException("Configured Privilege Policy " + privilegeName + " with class "
+ policyClassName + " can not be accessed.", e);
} catch (ClassNotFoundException e) {
throw new PrivilegeException("Configured Privilege Policy " + privilegeName + " with class "
+ policyClassName + " does not exist.", e);
}
}
/**
* @return the policies
*/
public Map<String, Class<PrivilegePolicy>> getPolicies() {
return this.policies;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("PrivilegeContainerModel [encryptionHandlerClassName=");
builder.append(this.encryptionHandlerClassName);
builder.append(", encryptionHandlerParameterMap=");
builder.append(this.encryptionHandlerParameterMap.size());
builder.append(", persistenceHandlerClassName=");
builder.append(this.persistenceHandlerClassName);
builder.append(", persistenceHandlerParameterMap=");
builder.append(this.persistenceHandlerParameterMap.size());
builder.append(", parameterMap=");
builder.append(this.parameterMap.size());
builder.append(", policies=");
builder.append(this.policies.size());
builder.append("]");
return builder.toString();
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2012, Robert von Burg
*
* All rights reserved.
*
* This file is part of the XXX.
*
* XXX is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* XXX is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XXX. If not, see
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.privilege.xml;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
public interface ElementParser {
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException;
public void characters(char[] ch, int start, int length) throws SAXException;
public void endElement(String uri, String localName, String qName) throws SAXException;
public void notifyChild(ElementParser child);
}

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2012, Robert von Burg
*
* All rights reserved.
*
* This file is part of the XXX.
*
* XXX is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* XXX is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XXX. If not, see
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.privilege.xml;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
public abstract class ElementParserAdapter implements ElementParser {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// empty implementation
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
// empty implementation
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
// empty implementation
}
@Override
public void notifyChild(ElementParser child) {
// empty implementation
}
}

View File

@ -0,0 +1,106 @@
/*
* Copyright (c) 2010 - 2012
*
* This file is part of Privilege.
*
* Privilege is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Privilege is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Privilege. If not, see <http://www.gnu.org/licenses/>.
*
*/
package ch.eitchnet.privilege.xml;
import java.io.File;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.eitchnet.privilege.base.PrivilegeException;
import ch.eitchnet.privilege.handler.DefaultPrivilegeHandler;
import ch.eitchnet.privilege.handler.EncryptionHandler;
import ch.eitchnet.privilege.handler.PersistenceHandler;
import ch.eitchnet.privilege.handler.PrivilegeHandler;
import ch.eitchnet.privilege.helper.ClassHelper;
import ch.eitchnet.privilege.helper.XmlHelper;
import ch.eitchnet.privilege.model.internal.PrivilegeContainerModel;
import ch.eitchnet.privilege.policy.PrivilegePolicy;
/**
* This class implements the initializing of the {@link PrivilegeHandler} by loading an XML file containing the
* configuration
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class InitializationHelper {
private static final Logger logger = LoggerFactory.getLogger(InitializationHelper.class);
/**
* Initializes the {@link DefaultPrivilegeHandler} from the configuration file
*
* @param privilegeXmlFile
* a {@link File} reference to the XML file containing the configuration for Privilege
*
* @return the {@link PrivilegeHandler} instance loaded from the configuration file
*/
public static PrivilegeHandler initializeFromXml(File privilegeXmlFile) {
// make sure file exists
if (!privilegeXmlFile.exists()) {
throw new PrivilegeException("Privilege file does not exist at path " + privilegeXmlFile.getAbsolutePath());
}
// parse configuration file
PrivilegeContainerModel containerModel = new PrivilegeContainerModel();
PrivilegeConfigSaxReader xmlHandler = new PrivilegeConfigSaxReader(containerModel);
XmlHelper.parseDocument(privilegeXmlFile, xmlHandler);
// initialize encryption handler
String encryptionHandlerClassName = containerModel.getEncryptionHandlerClassName();
EncryptionHandler encryptionHandler = ClassHelper.instantiateClass(encryptionHandlerClassName);
Map<String, String> parameterMap = containerModel.getEncryptionHandlerParameterMap();
try {
encryptionHandler.initialize(parameterMap);
} catch (Exception e) {
InitializationHelper.logger.error(e.getMessage(), e);
throw new PrivilegeException("EncryptionHandler " + encryptionHandlerClassName
+ " could not be initialized");
}
// initialize persistence handler
String persistenceHandlerClassName = containerModel.getPersistenceHandlerClassName();
PersistenceHandler persistenceHandler = ClassHelper.instantiateClass(persistenceHandlerClassName);
parameterMap = containerModel.getPersistenceHandlerParameterMap();
try {
persistenceHandler.initialize(parameterMap);
} catch (Exception e) {
InitializationHelper.logger.error(e.getMessage(), e);
throw new PrivilegeException("PersistenceHandler " + persistenceHandlerClassName
+ " could not be initialized");
}
// initialize privilege handler
DefaultPrivilegeHandler privilegeHandler = new DefaultPrivilegeHandler();
parameterMap = containerModel.getParameterMap();
Map<String, Class<PrivilegePolicy>> policyMap = containerModel.getPolicies();
try {
privilegeHandler.initialize(parameterMap, encryptionHandler, persistenceHandler, policyMap);
} catch (Exception e) {
InitializationHelper.logger.error(e.getMessage(), e);
throw new PrivilegeException("PrivilegeHandler " + privilegeHandler.getClass().getName()
+ " could not be initialized");
}
return privilegeHandler;
}
}

View File

@ -0,0 +1,121 @@
/*
* Copyright (c) 2012, Robert von Burg
*
* All rights reserved.
*
* This file is part of the XXX.
*
* XXX is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* XXX is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XXX. If not, see
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.privilege.xml;
import java.io.File;
import java.util.Map.Entry;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.privilege.helper.XmlConstants;
import ch.eitchnet.privilege.helper.XmlHelper;
import ch.eitchnet.privilege.model.internal.PrivilegeContainerModel;
import ch.eitchnet.privilege.policy.PrivilegePolicy;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class PrivilegeConfigDomWriter {
private final PrivilegeContainerModel containerModel;
private final File configFile;
/**
*
*/
public PrivilegeConfigDomWriter(PrivilegeContainerModel containerModel, File configFile) {
this.containerModel = containerModel;
this.configFile = configFile;
}
/**
*
*/
public void write() {
// create document root
Document doc = XmlHelper.createDocument();
Element rootElement = doc.createElement(XmlConstants.XML_ROOT_PRIVILEGE);
doc.appendChild(rootElement);
Element containerElement = doc.createElement(XmlConstants.XML_CONTAINER);
rootElement.appendChild(containerElement);
Element parameterElement;
Element parametersElement;
// Parameters
parametersElement = doc.createElement(XmlConstants.XML_PARAMETERS);
containerElement.appendChild(parametersElement);
for (Entry<String, String> entry : this.containerModel.getParameterMap().entrySet()) {
parameterElement = doc.createElement(XmlConstants.XML_PARAMETER);
parameterElement.setAttribute(XmlConstants.XML_ATTR_NAME, entry.getKey());
parameterElement.setAttribute(XmlConstants.XML_ATTR_VALUE, entry.getValue());
parametersElement.appendChild(parameterElement);
}
// create EncryptionHandler
Element encryptionHandlerElem = doc.createElement(XmlConstants.XML_HANDLER_ENCRYPTION);
containerElement.appendChild(encryptionHandlerElem);
encryptionHandlerElem.setAttribute(XmlConstants.XML_ATTR_CLASS,
this.containerModel.getEncryptionHandlerClassName());
// Parameters
parametersElement = doc.createElement(XmlConstants.XML_PARAMETERS);
encryptionHandlerElem.appendChild(parametersElement);
for (Entry<String, String> entry : this.containerModel.getEncryptionHandlerParameterMap().entrySet()) {
parameterElement = doc.createElement(XmlConstants.XML_PARAMETER);
parameterElement.setAttribute(XmlConstants.XML_ATTR_NAME, entry.getKey());
parameterElement.setAttribute(XmlConstants.XML_ATTR_VALUE, entry.getValue());
parametersElement.appendChild(parameterElement);
}
// create PersistenceHandler
Element persistenceHandlerElem = doc.createElement(XmlConstants.XML_HANDLER_PERSISTENCE);
containerElement.appendChild(persistenceHandlerElem);
persistenceHandlerElem.setAttribute(XmlConstants.XML_ATTR_CLASS,
this.containerModel.getPersistenceHandlerClassName());
// Parameters
parametersElement = doc.createElement(XmlConstants.XML_PARAMETERS);
persistenceHandlerElem.appendChild(parametersElement);
for (Entry<String, String> entry : this.containerModel.getPersistenceHandlerParameterMap().entrySet()) {
parameterElement = doc.createElement(XmlConstants.XML_PARAMETER);
parameterElement.setAttribute(XmlConstants.XML_ATTR_NAME, entry.getKey());
parameterElement.setAttribute(XmlConstants.XML_ATTR_VALUE, entry.getValue());
parametersElement.appendChild(parameterElement);
}
// Policies
Element policiesElem = doc.createElement(XmlConstants.XML_POLICIES);
rootElement.appendChild(policiesElem);
for (Entry<String, Class<PrivilegePolicy>> entry : this.containerModel.getPolicies().entrySet()) {
Element policyElem = doc.createElement(XmlConstants.XML_POLICY);
policyElem.setAttribute(XmlConstants.XML_ATTR_NAME, entry.getKey());
policyElem.setAttribute(XmlConstants.XML_ATTR_CLASS, entry.getValue().getName());
policiesElem.appendChild(policyElem);
}
// write the container file to disk
XmlHelper.writeDocument(doc, this.configFile);
}
}

View File

@ -0,0 +1,183 @@
/*
* Copyright (c) 2012, Robert von Burg
*
* All rights reserved.
*
* This file is part of the XXX.
*
* XXX is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* XXX is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XXX. If not, see
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.privilege.xml;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import ch.eitchnet.privilege.model.internal.PrivilegeContainerModel;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class PrivilegeConfigSaxReader extends DefaultHandler {
// private static final Logger logger = LoggerFactory.getLogger(PrivilegeConfigSaxReader.class);
private Stack<ElementParser> buildersStack = new Stack<ElementParser>();
private PrivilegeContainerModel containerModel;
public PrivilegeConfigSaxReader(PrivilegeContainerModel containerModel) {
this.containerModel = containerModel;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("Container")) {
this.buildersStack.add(new ContainerParser());
} else if (qName.equals("Parameters")) {
this.buildersStack.add(new ParametersParser());
} else if (qName.equals("Policies")) {
this.buildersStack.add(new PoliciesParser());
}
if (!this.buildersStack.isEmpty())
this.buildersStack.peek().startElement(uri, localName, qName, attributes);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (!this.buildersStack.isEmpty())
this.buildersStack.peek().characters(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (!this.buildersStack.isEmpty())
this.buildersStack.peek().endElement(uri, localName, qName);
ElementParser elementParser = null;
if (qName.equals("Container")) {
elementParser = this.buildersStack.pop();
} else if (qName.equals("Parameters")) {
elementParser = this.buildersStack.pop();
} else if (qName.equals("Policies")) {
elementParser = this.buildersStack.pop();
}
if (!this.buildersStack.isEmpty() && elementParser != null)
this.buildersStack.peek().notifyChild(elementParser);
}
public class ContainerParser extends ElementParserAdapter {
// <Container>
// <Parameters>
// <!-- parameters for the container itself -->
// <Parameter name="autoPersistOnPasswordChange" value="true" />
// </Parameters>
// <EncryptionHandler class="ch.eitchnet.privilege.handler.DefaultEncryptionHandler">
// <Parameters>
// <Parameter name="hashAlgorithm" value="SHA-256" />
// </Parameters>
// </EncryptionHandler>
// <PersistenceHandler class="ch.eitchnet.privilege.handler.XmlPersistenceHandler">
// <Parameters>
// <Parameter name="basePath" value="./target/test" />
// <Parameter name="modelXmlFile" value="PrivilegeModel.xml" />
// </Parameters>
// </PersistenceHandler>
// </Container>
private String currentElement;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("Container")) {
this.currentElement = qName;
} else if (qName.equals("EncryptionHandler")) {
this.currentElement = qName;
PrivilegeConfigSaxReader.this.containerModel
.setEncryptionHandlerClassName(attributes.getValue("class"));
} else if (qName.equals("PersistenceHandler")) {
this.currentElement = qName;
PrivilegeConfigSaxReader.this.containerModel.setPersistenceHandlerClassName(attributes
.getValue("class"));
}
}
@Override
public void notifyChild(ElementParser child) {
if (!(child instanceof ParametersParser))
return;
ParametersParser parametersChild = (ParametersParser) child;
if (this.currentElement.equals("Container")) {
PrivilegeConfigSaxReader.this.containerModel.setParameterMap(parametersChild.getParameterMap());
} else if (this.currentElement.equals("EncryptionHandler")) {
PrivilegeConfigSaxReader.this.containerModel.setEncryptionHandlerParameterMap(parametersChild
.getParameterMap());
} else if (this.currentElement.equals("PersistenceHandler")) {
PrivilegeConfigSaxReader.this.containerModel.setPersistenceHandlerParameterMap(parametersChild
.getParameterMap());
}
}
}
class ParametersParser extends ElementParserAdapter {
// <Parameter name="autoPersistOnPasswordChange" value="true" />
private Map<String, String> parameterMap = new HashMap<String, String>();
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("Parameter")) {
String key = attributes.getValue("name");
String value = attributes.getValue("value");
this.parameterMap.put(key, value);
}
}
/**
* @return the parameterMap
*/
public Map<String, String> getParameterMap() {
return this.parameterMap;
}
}
class PoliciesParser extends ElementParserAdapter {
// <Policy name="DefaultPrivilege" class="ch.eitchnet.privilege.policy.DefaultPrivilege" />
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("Policy")) {
String policyName = attributes.getValue("name");
String policyClassName = attributes.getValue("class");
PrivilegeConfigSaxReader.this.containerModel.addPolicy(policyName, policyClassName);
}
}
}
}

View File

@ -0,0 +1,160 @@
/*
* Copyright (c) 2012, Robert von Burg
*
* All rights reserved.
*
* This file is part of the XXX.
*
* XXX is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* XXX is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XXX. If not, see
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.privilege.xml;
import java.io.File;
import java.util.List;
import java.util.Map.Entry;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.privilege.helper.XmlConstants;
import ch.eitchnet.privilege.helper.XmlHelper;
import ch.eitchnet.privilege.model.internal.Privilege;
import ch.eitchnet.privilege.model.internal.Role;
import ch.eitchnet.privilege.model.internal.User;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class PrivilegeModelDomWriter {
private List<User> users;
private List<Role> roles;
private File modelFile;
/**
*
*/
public PrivilegeModelDomWriter(List<User> users, List<Role> roles, File modelFile) {
this.users = users;
this.roles = roles;
this.modelFile = modelFile;
}
public void write() {
// create document root
Document doc = XmlHelper.createDocument();
Element rootElement = doc.createElement(XmlConstants.XML_ROOT_PRIVILEGE_USERS_AND_ROLES);
doc.appendChild(rootElement);
Element usersElement = doc.createElement(XmlConstants.XML_USERS);
rootElement.appendChild(usersElement);
for (User user : this.users) {
// create the user element
Element userElement = doc.createElement(XmlConstants.XML_USER);
usersElement.appendChild(userElement);
userElement.setAttribute(XmlConstants.XML_ATTR_USER_ID, user.getUserId());
userElement.setAttribute(XmlConstants.XML_ATTR_USERNAME, user.getUsername());
userElement.setAttribute(XmlConstants.XML_ATTR_PASSWORD, user.getPassword());
// add first name element
Element firstnameElement = doc.createElement(XmlConstants.XML_FIRSTNAME);
firstnameElement.setTextContent(user.getFirstname());
userElement.appendChild(firstnameElement);
// add surname element
Element surnameElement = doc.createElement(XmlConstants.XML_SURNAME);
surnameElement.setTextContent(user.getSurname());
userElement.appendChild(surnameElement);
// add state element
Element stateElement = doc.createElement(XmlConstants.XML_STATE);
stateElement.setTextContent(user.getUserState().toString());
userElement.appendChild(stateElement);
// add locale element
Element localeElement = doc.createElement(XmlConstants.XML_LOCALE);
localeElement.setTextContent(user.getLocale().toString());
userElement.appendChild(localeElement);
// add all the role elements
Element rolesElement = doc.createElement(XmlConstants.XML_ROLES);
userElement.appendChild(rolesElement);
for (String roleName : user.getRoles()) {
Element roleElement = doc.createElement(XmlConstants.XML_ROLE);
roleElement.setTextContent(roleName);
rolesElement.appendChild(roleElement);
}
// add the parameters
Element parametersElement = doc.createElement(XmlConstants.XML_PARAMETERS);
userElement.appendChild(parametersElement);
for (Entry<String, String> entry : user.getProperties().entrySet()) {
Element paramElement = doc.createElement(XmlConstants.XML_PARAMETER);
paramElement.setAttribute(XmlConstants.XML_ATTR_NAME, entry.getKey());
paramElement.setAttribute(XmlConstants.XML_ATTR_VALUE, entry.getValue());
parametersElement.appendChild(paramElement);
}
}
Element rolesElement = doc.createElement(XmlConstants.XML_ROLES);
rootElement.appendChild(rolesElement);
for (Role role : this.roles) {
// create the role element
Element roleElement = doc.createElement(XmlConstants.XML_ROLE);
rolesElement.appendChild(roleElement);
roleElement.setAttribute(XmlConstants.XML_ATTR_NAME, role.getName());
for (Privilege privilege : role.getPrivilegeMap().values()) {
// create the privilege element
Element privilegeElement = doc.createElement(XmlConstants.XML_PRIVILEGE);
roleElement.appendChild(privilegeElement);
privilegeElement.setAttribute(XmlConstants.XML_ATTR_NAME, privilege.getName());
privilegeElement.setAttribute(XmlConstants.XML_ATTR_POLICY, privilege.getPolicy());
// add the all allowed element
Element allAllowedElement = doc.createElement(XmlConstants.XML_ALL_ALLOWED);
allAllowedElement.setTextContent(Boolean.toString(privilege.isAllAllowed()));
privilegeElement.appendChild(allAllowedElement);
// add all the deny values
for (String denyValue : privilege.getDenyList()) {
Element denyValueElement = doc.createElement(XmlConstants.XML_DENY);
denyValueElement.setTextContent(denyValue);
privilegeElement.appendChild(denyValueElement);
}
// add all the allow values
for (String allowValue : privilege.getAllowList()) {
Element allowValueElement = doc.createElement(XmlConstants.XML_ALLOW);
allowValueElement.setTextContent(allowValue);
privilegeElement.appendChild(allowValueElement);
}
}
}
// write the container file to disk
XmlHelper.writeDocument(doc, this.modelFile);
}
}

View File

@ -0,0 +1,347 @@
/*
* Copyright (c) 2012, Robert von Burg
*
* All rights reserved.
*
* This file is part of the XXX.
*
* XXX is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* XXX is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XXX. If not, see
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.privilege.xml;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import ch.eitchnet.privilege.model.UserState;
import ch.eitchnet.privilege.model.internal.Privilege;
import ch.eitchnet.privilege.model.internal.Role;
import ch.eitchnet.privilege.model.internal.User;
import ch.eitchnet.utils.helper.StringHelper;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class PrivilegeModelSaxReader extends DefaultHandler {
private static final Logger logger = LoggerFactory.getLogger(PrivilegeModelSaxReader.class);
private Stack<ElementParser> buildersStack = new Stack<ElementParser>();
private List<User> users;
private List<Role> roles;
private boolean insideUser;
/**
*
*/
public PrivilegeModelSaxReader() {
this.users = new ArrayList<User>();
this.roles = new ArrayList<Role>();
}
/**
* @return the users
*/
public List<User> getUsers() {
return this.users;
}
/**
* @return the roles
*/
public List<Role> getRoles() {
return this.roles;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("Users")) {
this.buildersStack.add(new UserParser());
this.insideUser = true;
} else if (qName.equals("Properties")) {
this.buildersStack.add(new PropertyParser());
} else if (qName.equals("Roles") && !this.insideUser) {
this.buildersStack.add(new RoleParser());
}
if (!this.buildersStack.isEmpty())
this.buildersStack.peek().startElement(uri, localName, qName, attributes);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (!this.buildersStack.isEmpty())
this.buildersStack.peek().characters(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (!this.buildersStack.isEmpty())
this.buildersStack.peek().endElement(uri, localName, qName);
ElementParser elementParser = null;
if (qName.equals("Users")) {
elementParser = this.buildersStack.pop();
this.insideUser = false;
PrivilegeModelSaxReader.logger.info("Popping for Users");
} else if (qName.equals("Properties")) {
elementParser = this.buildersStack.pop();
PrivilegeModelSaxReader.logger.info("Popping for Properties");
} else if (qName.equals("Roles") && !this.insideUser) {
elementParser = this.buildersStack.pop();
PrivilegeModelSaxReader.logger.info("Popping for Roles");
}
if (!this.buildersStack.isEmpty() && elementParser != null)
this.buildersStack.peek().notifyChild(elementParser);
}
// <Role name="AppUser">
// <Privilege name="ch.eitchnet.privilege.test.model.TestRestrictable">
// <AllAllowed>true</AllAllowed>
// </Privilege>
// </Role>
// <Role name="system_admin_privileges">
// <Privilege name="ch.eitchnet.privilege.test.model.TestSystemUserAction">
// <AllAllowed>true</AllAllowed>
// </Privilege>
// <Privilege name="ch.eitchnet.privilege.test.model.TestSystemRestrictable">
// <AllAllowed>true</AllAllowed>
// </Privilege>
// </Role>
public class RoleParser extends ElementParserAdapter {
private StringBuilder text;
private String roleName;
private String privilegeName;
private String privilegePolicy;
private boolean allAllowed;
private Set<String> denyList;
private Set<String> allowList;
private Map<String, Privilege> privileges;
/**
*
*/
public RoleParser() {
init();
}
/**
*
*/
private void init() {
this.privileges = new HashMap<String, Privilege>();
this.text = null;
this.roleName = null;
this.privilegeName = null;
this.privilegePolicy = null;
this.allAllowed = false;
this.denyList = new HashSet<String>();
this.allowList = new HashSet<String>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
this.text = new StringBuilder();
if (qName.equals("Role")) {
this.roleName = attributes.getValue("name");
} else if (qName.equals("Privilege")) {
this.privilegeName = attributes.getValue("name");
this.privilegePolicy = attributes.getValue("policy");
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (this.text != null)
this.text.append(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("AllAllowed")) {
this.allAllowed = StringHelper.parseBoolean(this.text.toString().trim());
} else if (qName.equals("Allow")) {
this.allowList.add(this.text.toString().trim());
} else if (qName.equals("Deny")) {
this.denyList.add(this.text.toString().trim());
} else if (qName.equals("Privilege")) {
Privilege privilege = new Privilege(this.privilegeName, this.privilegePolicy, this.allAllowed,
this.denyList, this.allowList);
this.privileges.put(this.privilegeName, privilege);
} else if (qName.equals("Role")) {
Role role = new Role(this.roleName, this.privileges);
PrivilegeModelSaxReader.this.roles.add(role);
PrivilegeModelSaxReader.logger.info("New Role: " + role);
init();
}
}
}
// <User userId="1" username="admin" password="8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918">
// <Firstname>Application</Firstname>
// <Surname>Administrator</Surname>
// <State>ENABLED</State>
// <Locale>en_GB</Locale>
// <Roles>
// <Role>PrivilegeAdmin</Role>
// <Role>AppUser</Role>
// </Roles>
// <Properties>
// <Property name="organization" value="eitchnet.ch" />
// <Property name="organizationalUnit" value="Development" />
// </Properties>
// </User>
public class UserParser extends ElementParserAdapter {
StringBuilder text;
String userId;
String username;
String password;
String firstName;
String surname;
UserState userState;
Locale locale;
Set<String> userRoles;
Map<String, String> parameters;
public UserParser() {
this.userRoles = new HashSet<String>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
this.text = new StringBuilder();
if (qName.equals("User")) {
this.userId = attributes.getValue("userId");
this.username = attributes.getValue("username");
this.password = attributes.getValue("password");
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
this.text.append(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("Firstname")) {
this.firstName = this.text.toString().trim();
} else if (qName.equals("Surname")) {
this.surname = this.text.toString().trim();
} else if (qName.equals("State")) {
this.userState = UserState.valueOf(this.text.toString().trim());
} else if (qName.equals("Locale")) {
this.locale = Locale.forLanguageTag(this.text.toString().trim());
} else if (qName.equals("Role")) {
this.userRoles.add(this.text.toString().trim());
} else if (qName.equals("User")) {
User user = new User(this.userId, this.username, this.password, this.firstName, this.surname,
this.userState, this.userRoles, this.locale, this.parameters);
StringBuilder builder = new StringBuilder();
builder.append("UserParser [userId=");
builder.append(this.userId);
builder.append(", username=");
builder.append(this.username);
builder.append(", password=");
builder.append(this.password);
builder.append(", firstName=");
builder.append(this.firstName);
builder.append(", surname=");
builder.append(this.surname);
builder.append(", userState=");
builder.append(this.userState);
builder.append(", locale=");
builder.append(this.locale);
builder.append(", userRoles=");
builder.append(this.userRoles.size());
builder.append(", parameters=");
builder.append(this.parameters.size());
builder.append("]");
PrivilegeModelSaxReader.logger.info(builder.toString());
PrivilegeModelSaxReader.this.users.add(user);
}
}
@Override
public void notifyChild(ElementParser child) {
if (child instanceof PropertyParser) {
this.parameters = ((PropertyParser) child).parameterMap;
}
}
}
class PropertyParser extends ElementParserAdapter {
// <Property name="organizationalUnit" value="Development" />
private Map<String, String> parameterMap = new HashMap<String, String>();
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("Property")) {
String key = attributes.getValue("name");
String value = attributes.getValue("value");
this.parameterMap.put(key, value);
}
}
/**
* @return the parameterMap
*/
public Map<String, String> getParameterMap() {
return this.parameterMap;
}
}
}

View File

@ -37,7 +37,6 @@ import ch.eitchnet.privilege.base.AccessDeniedException;
import ch.eitchnet.privilege.base.PrivilegeException;
import ch.eitchnet.privilege.handler.PrivilegeHandler;
import ch.eitchnet.privilege.helper.CertificateThreadLocal;
import ch.eitchnet.privilege.helper.InitializationHelper;
import ch.eitchnet.privilege.model.Certificate;
import ch.eitchnet.privilege.model.PrivilegeRep;
import ch.eitchnet.privilege.model.Restrictable;
@ -47,6 +46,7 @@ import ch.eitchnet.privilege.model.UserState;
import ch.eitchnet.privilege.test.model.TestRestrictable;
import ch.eitchnet.privilege.test.model.TestSystemUserAction;
import ch.eitchnet.privilege.test.model.TestSystemUserActionDeny;
import ch.eitchnet.privilege.xml.InitializationHelper;
import ch.eitchnet.utils.helper.FileHelper;
/**

View File

@ -0,0 +1,219 @@
/*
* Copyright (c) 2012, Robert von Burg
*
* All rights reserved.
*
* This file is part of the XXX.
*
* XXX is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* XXX is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XXX. If not, see
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.privilege.test;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.eitchnet.privilege.handler.DefaultEncryptionHandler;
import ch.eitchnet.privilege.handler.XmlPersistenceHandler;
import ch.eitchnet.privilege.helper.XmlHelper;
import ch.eitchnet.privilege.model.UserState;
import ch.eitchnet.privilege.model.internal.Privilege;
import ch.eitchnet.privilege.model.internal.PrivilegeContainerModel;
import ch.eitchnet.privilege.model.internal.Role;
import ch.eitchnet.privilege.model.internal.User;
import ch.eitchnet.privilege.xml.PrivilegeConfigDomWriter;
import ch.eitchnet.privilege.xml.PrivilegeConfigSaxReader;
import ch.eitchnet.privilege.xml.PrivilegeModelDomWriter;
import ch.eitchnet.privilege.xml.PrivilegeModelSaxReader;
import ch.eitchnet.utils.helper.FileHelper;
import ch.eitchnet.utils.helper.StringHelper;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class XmlTest {
/**
*
*/
private static final String TARGET_TEST = "./target/test";
private static final Logger logger = LoggerFactory.getLogger(XmlTest.class);
/**
* @throws Exception
* if something goes wrong
*/
@BeforeClass
public static void init() throws Exception {
File tmpDir = new File("target/test");
if (tmpDir.exists())
FileHelper.deleteFile(tmpDir, false);
tmpDir.mkdirs();
}
@AfterClass
public static void destroy() throws Exception {
File tmpDir = new File("target/test");
if (!tmpDir.exists())
return;
File tmpFile = new File("target/test/PrivilegeTest.xml");
if (tmpFile.exists() && !tmpFile.delete()) {
throw new RuntimeException("Tmp still exists and can not be deleted at " + tmpFile.getAbsolutePath());
}
tmpFile = new File("target/test/PrivilegeModelTest.xml");
if (tmpFile.exists() && !tmpFile.delete()) {
throw new RuntimeException("Tmp still exists and can not be deleted at " + tmpFile.getAbsolutePath());
}
// and temporary parent
if (!tmpDir.delete()) {
throw new RuntimeException("Could not remove temporary parent for tmp " + tmpFile);
}
}
@Test
public void canReadConfig() {
PrivilegeContainerModel containerModel = new PrivilegeContainerModel();
PrivilegeConfigSaxReader saxReader = new PrivilegeConfigSaxReader(containerModel);
File xmlFile = new File("config/Privilege.xml");
XmlHelper.parseDocument(xmlFile, saxReader);
logger.info(containerModel.toString());
// assert all objects read
Assert.assertNotNull(containerModel.getParameterMap());
Assert.assertNotNull(containerModel.getPolicies());
Assert.assertNotNull(containerModel.getEncryptionHandlerClassName());
Assert.assertNotNull(containerModel.getEncryptionHandlerParameterMap());
Assert.assertNotNull(containerModel.getPersistenceHandlerClassName());
Assert.assertNotNull(containerModel.getPersistenceHandlerParameterMap());
Assert.assertEquals(1, containerModel.getParameterMap().size());
Assert.assertEquals(1, containerModel.getPolicies().size());
Assert.assertEquals(1, containerModel.getEncryptionHandlerParameterMap().size());
Assert.assertEquals(2, containerModel.getPersistenceHandlerParameterMap().size());
// TODO extend assertions to actual model
}
@Test
public void canWriteConfig() {
Map<String, String> parameterMap = new HashMap<String, String>();
Map<String, String> encryptionHandlerParameterMap = new HashMap<String, String>();
Map<String, String> persistenceHandlerParameterMap = new HashMap<String, String>();
// TODO ask other questions...
parameterMap.put("autoPersistOnPasswordChange", "true");
encryptionHandlerParameterMap.put("hashAlgorithm", "SHA-256");
persistenceHandlerParameterMap.put("basePath", TARGET_TEST);
persistenceHandlerParameterMap.put("modelXmlFile", "PrivilegeModel.xml");
PrivilegeContainerModel containerModel = new PrivilegeContainerModel();
containerModel.setParameterMap(parameterMap);
containerModel.setEncryptionHandlerClassName(DefaultEncryptionHandler.class.getName());
containerModel.setEncryptionHandlerParameterMap(encryptionHandlerParameterMap);
containerModel.setPersistenceHandlerClassName(XmlPersistenceHandler.class.getName());
containerModel.setPersistenceHandlerParameterMap(persistenceHandlerParameterMap);
containerModel.addPolicy("DefaultPrivilege", "ch.eitchnet.privilege.policy.DefaultPrivilege");
File configFile = new File("./target/test/PrivilegeTest.xml");
PrivilegeConfigDomWriter configSaxWriter = new PrivilegeConfigDomWriter(containerModel, configFile);
configSaxWriter.write();
String fileHash = StringHelper.getHexString(FileHelper.hashFileSha256(configFile));
Assert.assertEquals("22d4ba39605d49c758184d9bd63beae5ccf8786f3dabbab45cd9f59c2afbcbd0", fileHash);
}
@Test
public void canReadModel() {
PrivilegeModelSaxReader xmlHandler = new PrivilegeModelSaxReader();
File xmlFile = new File("config/PrivilegeModel.xml");
XmlHelper.parseDocument(xmlFile, xmlHandler);
List<User> users = xmlHandler.getUsers();
Assert.assertNotNull(users);
List<Role> roles = xmlHandler.getRoles();
Assert.assertNotNull(roles);
Assert.assertEquals(2, users.size());
Assert.assertEquals(4, roles.size());
// TODO extend assertions to actual model
}
@Test
public void canWriteModel() {
Map<String, String> propertyMap;
Set<String> userRoles;
Map<String, Privilege> privilegeMap;
List<User> users = new ArrayList<User>();
propertyMap = new HashMap<String, String>();
propertyMap.put("prop1", "value1");
userRoles = new HashSet<String>();
userRoles.add("role1");
users.add(new User("1", "user1", "blabla", "Bob", "White", UserState.DISABLED, userRoles, Locale.ENGLISH,
propertyMap));
propertyMap = new HashMap<String, String>();
propertyMap.put("prop2", "value2");
userRoles = new HashSet<String>();
userRoles.add("role2");
users.add(new User("2", "user2", "haha", "Leonard", "Sheldon", UserState.ENABLED, userRoles, Locale.ENGLISH,
propertyMap));
List<Role> roles = new ArrayList<Role>();
privilegeMap = new HashMap<String, Privilege>();
privilegeMap.put("priv1", new Privilege("priv1", "DefaultPrivilege", true, null, null));
roles.add(new Role("role1", privilegeMap));
privilegeMap = new HashMap<String, Privilege>();
Set<String> denyList = new HashSet<String>();
denyList.add("myself");
Set<String> allowList = new HashSet<String>();
allowList.add("other");
privilegeMap.put("priv2", new Privilege("priv2", "DefaultPrivilege", false, denyList, allowList));
roles.add(new Role("role2", privilegeMap));
File modelFile = new File("./target/test/PrivilegeModelTest.xml");
PrivilegeModelDomWriter configSaxWriter = new PrivilegeModelDomWriter(users, roles, modelFile);
configSaxWriter.write();
String fileHash = StringHelper.getHexString(FileHelper.hashFileSha256(modelFile));
Assert.assertEquals("8e1e82278162f21b1654c2e059570bbcb3cb63b053c1dd784bc8e225e8cfd04f", fileHash);
}
}