[Minor] cleaned up all compiler warnings

This commit is contained in:
Robert von Burg 2013-12-25 14:37:22 +01:00
parent 15a245d94e
commit 3727d3545f
23 changed files with 275 additions and 406 deletions

View File

@ -19,14 +19,15 @@ import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.MessageFormat;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.eitchnet.privilege.base.PrivilegeException;
import ch.eitchnet.privilege.helper.HashHelper;
import ch.eitchnet.privilege.helper.XmlConstants;
import ch.eitchnet.utils.helper.StringHelper;
/**
* <p>
@ -60,27 +61,25 @@ public class DefaultEncryptionHandler implements EncryptionHandler {
@Override
public String convertToHash(String string) {
try {
return HashHelper.stringToHash(this.hashAlgorithm, string);
} catch (NoSuchAlgorithmException e) {
throw new PrivilegeException("Algorithm " + this.hashAlgorithm + " was not found!", e);
} catch (UnsupportedEncodingException e) {
throw new PrivilegeException("Charset ASCII is not supported!", e);
}
return convertToHash(string.getBytes());
}
@Override
public String convertToHash(byte[] bytes) {
try {
return HashHelper.stringToHash(this.hashAlgorithm, bytes);
return StringHelper.hashAsHex(this.hashAlgorithm, bytes);
} catch (NoSuchAlgorithmException e) {
throw new PrivilegeException("Algorithm " + this.hashAlgorithm + " was not found!", e);
} catch (UnsupportedEncodingException e) {
throw new PrivilegeException("Charset ASCII is not supported!", e);
} catch (RuntimeException e) {
if (e.getCause() == null)
throw e;
if (e.getCause().getClass().equals(NoSuchAlgorithmException.class))
throw new PrivilegeException(
MessageFormat.format("Algorithm {0} was not found!", this.hashAlgorithm), e.getCause()); //$NON-NLS-1$
if (e.getCause().getClass().equals(UnsupportedEncodingException.class))
throw new PrivilegeException("Charset ASCII is not supported!", e.getCause()); //$NON-NLS-1$
throw e;
}
}
@ -100,18 +99,21 @@ public class DefaultEncryptionHandler implements EncryptionHandler {
// get hash algorithm parameters
this.hashAlgorithm = parameterMap.get(XmlConstants.XML_PARAM_HASH_ALGORITHM);
if (this.hashAlgorithm == null || this.hashAlgorithm.isEmpty()) {
throw new PrivilegeException("[" + EncryptionHandler.class.getName() + "] Defined parameter "
+ XmlConstants.XML_PARAM_HASH_ALGORITHM + " is invalid");
String msg = "[{0}] Defined parameter {1} is invalid"; //$NON-NLS-1$
msg = MessageFormat.format(msg, EncryptionHandler.class.getName(), XmlConstants.XML_PARAM_HASH_ALGORITHM);
throw new PrivilegeException(msg);
}
// test hash algorithm
try {
convertToHash("test");
DefaultEncryptionHandler.logger.info("Using hashing algorithm " + this.hashAlgorithm);
convertToHash("test"); //$NON-NLS-1$
DefaultEncryptionHandler.logger.info(MessageFormat
.format("Using hashing algorithm {0}", this.hashAlgorithm)); //$NON-NLS-1$
} catch (Exception e) {
throw new PrivilegeException("[" + EncryptionHandler.class.getName() + "] Defined parameter "
+ XmlConstants.XML_PARAM_HASH_ALGORITHM + " is invalid because of underlying exception: "
+ e.getLocalizedMessage(), e);
String msg = "[{0}] Defined parameter {1} is invalid because of underlying exception: {2}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, EncryptionHandler.class.getName(), XmlConstants.XML_PARAM_HASH_ALGORITHM,
e.getLocalizedMessage());
throw new PrivilegeException(msg, e);
}
}
}

View File

@ -30,7 +30,6 @@ import org.slf4j.LoggerFactory;
import ch.eitchnet.privilege.base.AccessDeniedException;
import ch.eitchnet.privilege.base.PrivilegeException;
import ch.eitchnet.privilege.helper.ClassHelper;
import ch.eitchnet.privilege.model.Certificate;
import ch.eitchnet.privilege.model.IPrivilege;
import ch.eitchnet.privilege.model.PrivilegeContext;
@ -42,6 +41,7 @@ import ch.eitchnet.privilege.model.internal.PrivilegeImpl;
import ch.eitchnet.privilege.model.internal.Role;
import ch.eitchnet.privilege.model.internal.User;
import ch.eitchnet.privilege.policy.PrivilegePolicy;
import ch.eitchnet.utils.helper.ClassHelper;
/**
* <p>
@ -66,7 +66,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
/**
* configuration parameter to define automatic persisting on password change
*/
private static final String PARAM_AUTO_PERSIST_ON_PASSWORD_CHANGE = "autoPersistOnPasswordChange";
private static final String PARAM_AUTO_PERSIST_ON_PASSWORD_CHANGE = "autoPersistOnPasswordChange"; //$NON-NLS-1$
/**
* slf4j logger
@ -327,14 +327,16 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// get role
Role role = this.persistenceHandler.getRole(roleName);
if (role == null) {
throw new PrivilegeException("Role " + roleName + " does not exist!");
String msg = MessageFormat.format("Role {0} does not exist!", roleName); //$NON-NLS-1$
throw new PrivilegeException(msg);
}
// validate that policy exists if needed
String policy = privilegeRep.getPolicy();
if (policy != null && !this.policyMap.containsKey(policy)) {
throw new PrivilegeException("Policy " + policy + " for Privilege " + privilegeRep.getName()
+ " does not exist");
String msg = "Policy {0} for Privilege {1} does not exist"; //$NON-NLS-1$
msg = MessageFormat.format(msg, policy, privilegeRep.getName());
throw new PrivilegeException(msg);
}
// create new role with the additional privilege
@ -364,19 +366,21 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// get user
User user = this.persistenceHandler.getUser(username);
if (user == null) {
throw new PrivilegeException("User " + username + " does not exist!");
throw new PrivilegeException(MessageFormat.format("User {0} does not exist!", username)); //$NON-NLS-1$
}
// ignore if user already has role
Set<String> currentRoles = user.getRoles();
if (currentRoles.contains(roleName)) {
DefaultPrivilegeHandler.logger.error("User " + username + " already has role " + roleName);
String msg = MessageFormat.format("User {0} already has role {1}", username, roleName); //$NON-NLS-1$
DefaultPrivilegeHandler.logger.error(msg);
return;
}
// validate that role exists
if (getRole(roleName) == null) {
throw new PrivilegeException("Role " + roleName + " does not exist!");
String msg = MessageFormat.format("Role {0} does not exist!", roleName); //$NON-NLS-1$
throw new PrivilegeException(msg);
}
// create new user
@ -399,12 +403,14 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// get role
Role role = this.persistenceHandler.getRole(roleName);
if (role == null) {
throw new PrivilegeException("Role " + roleName + " does not exist!");
throw new PrivilegeException(MessageFormat.format("Role {0} does not exist!", roleName)); //$NON-NLS-1$
}
// ignore if role does not have privilege
if (!role.hasPrivilege(privilegeName))
throw new PrivilegeException("Role " + roleName + " does not have Privilege " + privilegeName);
if (!role.hasPrivilege(privilegeName)) {
String msg = MessageFormat.format("Role {0} does not have Privilege {1}", roleName, privilegeName); //$NON-NLS-1$
throw new PrivilegeException(msg);
}
// create new set of privileges with out the to removed privilege
Set<String> privilegeNames = role.getPrivilegeNames();
@ -447,13 +453,14 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// get User
User user = this.persistenceHandler.getUser(username);
if (user == null) {
throw new PrivilegeException("User " + username + " does not exist!");
throw new PrivilegeException(MessageFormat.format("User {0} does not exist!", username)); //$NON-NLS-1$
}
// ignore if user does not have role
Set<String> currentRoles = user.getRoles();
if (!currentRoles.contains(roleName)) {
DefaultPrivilegeHandler.logger.error("User " + user + " does not have role " + roleName);
String msg = MessageFormat.format("User {0} does not have role {1}", user, roleName); //$NON-NLS-1$
logger.error(msg);
return;
}
@ -494,7 +501,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// get User
User user = this.persistenceHandler.getUser(username);
if (user == null) {
throw new PrivilegeException("User " + username + " does not exist!");
throw new PrivilegeException(MessageFormat.format("User {0} does not exist!", username)); //$NON-NLS-1$
}
// create new user
@ -514,7 +521,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// get User
User user = this.persistenceHandler.getUser(username);
if (user == null) {
throw new PrivilegeException("User " + username + " does not exist!");
throw new PrivilegeException(MessageFormat.format("User {0} does not exist!", username)); //$NON-NLS-1$
}
// create new user
@ -548,7 +555,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// get User
User user = this.persistenceHandler.getUser(username);
if (user == null) {
throw new PrivilegeException("User " + username + " does not exist!");
throw new PrivilegeException(MessageFormat.format("User {0} does not exist!", username)); //$NON-NLS-1$
}
String passwordHash = null;
@ -587,7 +594,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// get User
User user = this.persistenceHandler.getUser(username);
if (user == null) {
throw new PrivilegeException("User " + username + " does not exist!");
throw new PrivilegeException(MessageFormat.format("User {0} does not exist!", username)); //$NON-NLS-1$
}
// create new user
@ -611,8 +618,10 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
Certificate certificate;
try {
// username must be at least 2 characters in length
if (username == null || username.length() < 2)
throw new PrivilegeException("The given username '" + username + "' is shorter than 2 characters");
if (username == null || username.length() < 2) {
String msg = MessageFormat.format("The given username ''{0}'' is shorter than 2 characters", username); //$NON-NLS-1$
throw new PrivilegeException(msg);
}
// and validate the password
validatePassword(password);
@ -623,26 +632,32 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// get user object
User user = this.persistenceHandler.getUser(username);
// no user means no authentication
if (user == null)
throw new AccessDeniedException("There is no user defined with the username " + username);
if (user == null) {
String msg = MessageFormat.format("There is no user defined with the username {0}", username); //$NON-NLS-1$
throw new AccessDeniedException(msg);
}
// validate password
String pwHash = user.getPassword();
if (pwHash == null)
throw new AccessDeniedException("User " + username + " has no password and may not login!");
throw new AccessDeniedException(MessageFormat.format(
"User {0} has no password and may not login!", username)); //$NON-NLS-1$
if (!pwHash.equals(passwordHash))
throw new AccessDeniedException("Password is incorrect for " + username);
throw new AccessDeniedException(MessageFormat.format("Password is incorrect for {0}", username)); //$NON-NLS-1$
// validate if user is allowed to login
// this also capture the trying to login of SYSTEM user
if (user.getUserState() != UserState.ENABLED)
throw new AccessDeniedException("User " + username + " does not have state " + UserState.ENABLED
+ " and can not login!");
if (user.getUserState() != UserState.ENABLED) {
String msg = "User {0} does not have state {1} and can not login!"; //$NON-NLS-1$
msg = MessageFormat.format(msg, username, UserState.ENABLED);
throw new AccessDeniedException(msg);
}
// validate user has at least one role
Set<String> userRoles = user.getRoles();
if (userRoles.isEmpty()) {
throw new PrivilegeException("User " + username + " does not have any roles defined!");
throw new PrivilegeException(
MessageFormat.format("User {0} does not have any roles defined!", username)); //$NON-NLS-1$
}
// get 2 auth tokens
@ -660,11 +675,13 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
this.privilegeContextMap.put(sessionId, privilegeContext);
// log
DefaultPrivilegeHandler.logger.info("User " + username + " authenticated: " + certificate);
DefaultPrivilegeHandler.logger.info(MessageFormat.format(
"User {0} authenticated: {1}", username, certificate)); //$NON-NLS-1$
} catch (RuntimeException e) {
DefaultPrivilegeHandler.logger.error("User " + username + " Failed to authenticate: "
+ e.getLocalizedMessage());
String msg = "User {0} Failed to authenticate: {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, username, e.getMessage());
DefaultPrivilegeHandler.logger.error(msg);
throw e;
} finally {
clearPassword(password);
@ -700,8 +717,9 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
IPrivilege privilege = role.getPrivilege(privilegeName);
if (privilege == null) {
throw new PrivilegeException(MessageFormat.format("The Privilege {0} does not exist for role {1}",
privilegeName, roleName));
String msg = "The Privilege {0} does not exist for role {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, privilegeName, roleName);
throw new PrivilegeException(msg);
}
privileges.put(privilegeName, privilege);
@ -712,8 +730,9 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
PrivilegePolicy policy = getPolicy(policyName);
if (policy == null) {
throw new PrivilegeException(MessageFormat.format(
"The Policy {0} does not exist for Privilege {1}", policyName, privilegeName));
String msg = "The Policy {0} does not exist for Privilege {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, policyName, privilegeName);
throw new PrivilegeException(msg);
}
policies.put(policyName, policy);
}
@ -736,9 +755,10 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// return true if object was really removed
boolean loggedOut = privilegeContext != null;
if (loggedOut)
DefaultPrivilegeHandler.logger.info("User " + certificate.getUsername() + " logged out.");
DefaultPrivilegeHandler.logger
.info(MessageFormat.format("User {0} logged out.", certificate.getUsername())); //$NON-NLS-1$
else
DefaultPrivilegeHandler.logger.warn("User already logged out!");
DefaultPrivilegeHandler.logger.warn("User already logged out!"); //$NON-NLS-1$
return loggedOut;
}
@ -747,25 +767,30 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// certificate must not be null
if (certificate == null)
throw new PrivilegeException("Certificate may not be null!");
throw new PrivilegeException("Certificate may not be null!"); //$NON-NLS-1$
// first see if a session exists for this certificate
PrivilegeContext privilegeContext = this.privilegeContextMap.get(certificate.getSessionId());
if (privilegeContext == null)
throw new AccessDeniedException("There is no session information for " + certificate.toString());
if (privilegeContext == null) {
String msg = MessageFormat.format("There is no session information for {0}", certificate); //$NON-NLS-1$
throw new AccessDeniedException(msg);
}
// validate certificate has not been tampered with
Certificate sessionCertificate = privilegeContext.getCertificate();
if (!sessionCertificate.equals(certificate))
throw new PrivilegeException("Received illegal certificate for session id " + certificate.getSessionId());
if (!sessionCertificate.equals(certificate)) {
String msg = "Received illegal certificate for session id {0}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, certificate.getSessionId());
throw new PrivilegeException(msg);
}
// get user object
User user = this.persistenceHandler.getUser(privilegeContext.getUsername());
// if user exists, then certificate is valid
if (user == null) {
throw new PrivilegeException(
"Oh boy, how did this happen: No User in user map although the certificate is valid!");
String msg = "Oh boy, how did this happen: No User in user map although the certificate is valid!"; //$NON-NLS-1$
throw new PrivilegeException(msg);
}
// everything is ok
@ -789,15 +814,16 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// get user object
User user = this.persistenceHandler.getUser(certificate.getUsername());
if (user == null) {
throw new PrivilegeException(
"Oh boy, how did this happen: No User in user map although the certificate is valid! Certificate: "
+ certificate);
String msg = "Oh boy, how did this happen: No User in user map although the certificate is valid! Certificate: {0}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, certificate);
throw new PrivilegeException(msg);
}
// validate user has PrivilegeAdmin role
if (!user.hasRole(PrivilegeHandler.PRIVILEGE_ADMIN_ROLE)) {
throw new AccessDeniedException("User does not have " + PrivilegeHandler.PRIVILEGE_ADMIN_ROLE
+ " role! Certificate: " + certificate);
String msg = "User does not have {0} role! Certificate: {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, PrivilegeHandler.PRIVILEGE_ADMIN_ROLE, certificate);
throw new AccessDeniedException(msg);
}
}
@ -810,11 +836,11 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
public void validatePassword(byte[] password) throws PrivilegeException {
if (password == null || password.length == 0) {
throw new PrivilegeException("A password may not be empty!");
throw new PrivilegeException("A password may not be empty!"); //$NON-NLS-1$
}
if (password.length < 3) {
throw new PrivilegeException("The given password is shorter than 3 characters");
throw new PrivilegeException("The given password is shorter than 3 characters"); //$NON-NLS-1$
}
}
@ -848,7 +874,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
PersistenceHandler persistenceHandler, Map<String, Class<PrivilegePolicy>> policyMap) {
if (this.initialized)
throw new PrivilegeException("Already initialized!");
throw new PrivilegeException("Already initialized!"); //$NON-NLS-1$
this.policyMap = policyMap;
this.encryptionHandler = encryptionHandler;
@ -859,10 +885,11 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
this.autoPersistOnPasswordChange = false;
} else if (autoPersistS.equals(Boolean.TRUE.toString())) {
this.autoPersistOnPasswordChange = true;
logger.info("Enabling automatic persistence on password change.");
logger.info("Enabling automatic persistence on password change."); //$NON-NLS-1$
} else {
logger.error("Parameter " + PARAM_AUTO_PERSIST_ON_PASSWORD_CHANGE + " has illegal value " + autoPersistS
+ ". Overriding with " + Boolean.FALSE.toString());
String msg = "Parameter {0} has illegal value {1}. Overriding with {2}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, PARAM_AUTO_PERSIST_ON_PASSWORD_CHANGE, autoPersistS, Boolean.FALSE);
logger.error(msg);
}
// validate policies on privileges of Roles
@ -886,8 +913,9 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
IPrivilege privilege = role.getPrivilege(privilegeName);
String policy = privilege.getPolicy();
if (policy != null && !this.policyMap.containsKey(policy)) {
throw new PrivilegeException("Policy " + policy + " for Privilege " + privilege.getName()
+ " does not exist on role " + role);
String msg = "Policy {0} for Privilege {1} does not exist on role {2}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, policy, privilege.getName(), role);
throw new PrivilegeException(msg);
}
}
}
@ -918,18 +946,18 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
public void runAsSystem(String systemUsername, SystemUserAction action) throws PrivilegeException {
if (systemUsername == null)
throw new PrivilegeException("systemUsername may not be null!");
throw new PrivilegeException("systemUsername may not be null!"); //$NON-NLS-1$
if (action == null)
throw new PrivilegeException("action may not be null!");
throw new PrivilegeException("action may not be null!"); //$NON-NLS-1$
// get the system user
User systemUser = this.persistenceHandler.getUser(systemUsername);
if (systemUser == null)
throw new PrivilegeException("System user " + systemUsername + " does not exist!");
throw new PrivilegeException(MessageFormat.format("System user {0} does not exist!", systemUsername)); //$NON-NLS-1$
// validate this is a system user
if (systemUser.getUserState() != UserState.SYSTEM)
throw new PrivilegeException("User " + systemUsername + " is not a System user!");
throw new PrivilegeException(MessageFormat.format("User {0} is not a System user!", systemUsername)); //$NON-NLS-1$
// validate this system user may perform the given action
String actionClassname = action.getClass().getName();
@ -966,7 +994,8 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
}
// default throw exception, as the user does not have the privilege
throw new PrivilegeException("User " + user.getUsername() + " does not have Privilege " + privilegeName);
String msg = MessageFormat.format("User {0} does not have Privilege {1}", user.getUsername(), privilegeName); //$NON-NLS-1$
throw new PrivilegeException(msg);
}
/**
@ -986,24 +1015,33 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// get user object
User user = this.persistenceHandler.getUser(systemUsername);
// no user means no authentication
if (user == null)
throw new AccessDeniedException("The system user with username " + systemUsername + " does not exist!");
if (user == null) {
String msg = MessageFormat.format("The system user with username {0} does not exist!", systemUsername); //$NON-NLS-1$
throw new AccessDeniedException(msg);
}
// validate password
String pwHash = user.getPassword();
if (pwHash == null)
throw new AccessDeniedException("System user " + systemUsername + " has no password and may not login!");
if (!pwHash.equals(passwordHash))
throw new AccessDeniedException("System user " + systemUsername + " has an incorrect password defined!");
if (pwHash == null) {
String msg = MessageFormat.format("System user {0} has no password and may not login!", systemUsername); //$NON-NLS-1$
throw new AccessDeniedException(msg);
}
if (!pwHash.equals(passwordHash)) {
String msg = MessageFormat.format("System user {0} has an incorrect password defined!", systemUsername); //$NON-NLS-1$
throw new AccessDeniedException(msg);
}
// validate user state is system
if (user.getUserState() != UserState.SYSTEM)
throw new PrivilegeException("The system " + systemUsername + " user does not have expected user state "
+ UserState.SYSTEM);
if (user.getUserState() != UserState.SYSTEM) {
String msg = "The system {0} user does not have expected user state {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, systemUsername, UserState.SYSTEM);
throw new PrivilegeException(msg);
}
// validate user has at least one role
if (user.getRoles().isEmpty()) {
throw new PrivilegeException("The system user " + systemUsername + " does not have any roles defined!");
String msg = MessageFormat.format("The system user {0} does not have any roles defined!", systemUsername); //$NON-NLS-1$
throw new PrivilegeException(msg);
}
// get 2 auth tokens
@ -1021,8 +1059,9 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
PrivilegeContext privilegeContext = buildPrivilegeContext(systemUserCertificate, user);
// log
DefaultPrivilegeHandler.logger.info("The system user " + systemUsername + " is logged in with session "
+ systemUserCertificate);
String msg = "The system user {0} is logged in with session {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, systemUsername, systemUserCertificate);
DefaultPrivilegeHandler.logger.info(msg);
return privilegeContext;
}
@ -1055,8 +1094,9 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
try {
policy = ClassHelper.instantiateClass(policyClazz);
} catch (Exception e) {
throw new PrivilegeException("The class for the policy with the name " + policyName + " does not exist!"
+ policyName, e);
String msg = "The class for the policy with the name {0} does not exist!{1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, policyName, policyName);
throw new PrivilegeException(msg, e);
}
return policy;

View File

@ -43,7 +43,7 @@ public interface PrivilegeHandler {
/**
* PRIVILEGE_ADMIN_ROLE = PrivilegeAdmin: This is the role users must have, if they are allowed to modify objects
*/
public static final String PRIVILEGE_ADMIN_ROLE = "PrivilegeAdmin";
public static final String PRIVILEGE_ADMIN_ROLE = "PrivilegeAdmin"; //$NON-NLS-1$
/**
* Returns a {@link UserRep} for the given username

View File

@ -40,6 +40,7 @@ import ch.eitchnet.privilege.xml.PrivilegeConfigDomWriter;
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@SuppressWarnings("nls")
public class BootstrapConfigurationHelper {
// private static final Logger logger = Loggerdoc.getLogger(BootstrapConfigurationHelper.class);

View File

@ -1,101 +0,0 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.eitchnet.privilege.helper;
import ch.eitchnet.privilege.base.PrivilegeException;
/**
* The {@link ClassHelper} class is a helper to instantiate classes using reflection
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ClassHelper {
/**
* Returns an instance of the class' name given by instantiating the class through an empty arguments constructor
*
* @param <T>
* the type of the class to return
* @param className
* the name of a class to instantiate through an empty arguments constructor
*
* @return the newly instantiated object from the given class name
*
* @throws PrivilegeException
* if the class could not be instantiated
*/
@SuppressWarnings("unchecked")
public static <T> T instantiateClass(String className) throws PrivilegeException {
try {
Class<T> clazz = (Class<T>) Class.forName(className);
return clazz.getConstructor().newInstance();
} catch (Exception e) {
throw new PrivilegeException("The class " + className + " could not be instantiated: ", e);
}
}
/**
* Instantiates an object for the given {@link Class} using an empty arguments constructor
*
* @param <T>
* the type of the class to return
* @param clazz
* the {@link Class} from which a new object is to be instantiated using an empty arguments constructor
*
* @return the newly instantiated object from the given {@link Class}
*
* @throws PrivilegeException
* if the {@link Class} could not be instantiated
*/
public static <T> T instantiateClass(Class<T> clazz) throws PrivilegeException {
try {
return clazz.getConstructor().newInstance();
} catch (Exception e) {
throw new PrivilegeException("The class " + clazz.getName() + " could not be instantiated: ", e);
}
}
/**
* Loads the {@link Class} object for the given class name
*
* @param <T>
* the type of {@link Class} to return
* @param className
* the name of the {@link Class} to load and return
*
* @return the {@link Class} object for the given class name
*
* @throws PrivilegeException
* if the class could not be instantiated
*/
@SuppressWarnings("unchecked")
public static <T> Class<T> loadClass(String className) throws PrivilegeException {
try {
Class<T> clazz = (Class<T>) Class.forName(className);
return clazz;
} catch (Exception e) {
throw new PrivilegeException("The class " + className + " could not be instantiated: ", e);
}
}
}

View File

@ -1,88 +0,0 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.eitchnet.privilege.helper;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Helper class to hash a String for a certain hash algorithm, using the Java {@link MessageDigest} classes
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class HashHelper {
/**
* Hex char table for fast calculating of hex values
*/
private static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4',
(byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
(byte) 'e', (byte) 'f' };
/**
* Creates the hash of the given string using {@link MessageDigest} and the defined hash algorithm
*
* @param hashAlgorithm
* the algorithm to use for hashing
* @param string
* the string to hash
*
* @return a new string encrypted by the defined algorithm
*
* @throws NoSuchAlgorithmException
* if the algorithm is not found
* @throws UnsupportedEncodingException
* if something is wrong with the given string to hash
*/
public static String stringToHash(String hashAlgorithm, String string) throws NoSuchAlgorithmException,
UnsupportedEncodingException {
return HashHelper.stringToHash(hashAlgorithm, string.getBytes());
}
/**
* Creates the hash of the given string using {@link MessageDigest} and the defined hash algorithm
*
* @param hashAlgorithm
* the algorithm to use for hashing
* @param bytes
* the bytes to hash
*
* @return a new string encrypted by the defined algorithm
*
* @throws NoSuchAlgorithmException
* if the algorithm is not found
* @throws UnsupportedEncodingException
* if something is wrong with the given string to hash
*/
public static String stringToHash(String hashAlgorithm, byte[] bytes) throws NoSuchAlgorithmException,
UnsupportedEncodingException {
MessageDigest digest = MessageDigest.getInstance(hashAlgorithm);
byte[] hashArray = digest.digest(bytes);
byte[] hex = new byte[2 * hashArray.length];
int index = 0;
for (byte b : hashArray) {
int v = b & 0xFF;
hex[index++] = HashHelper.HEX_CHAR_TABLE[v >>> 4];
hex[index++] = HashHelper.HEX_CHAR_TABLE[v & 0xF];
}
return new String(hex, "ASCII");
}
}

View File

@ -31,11 +31,14 @@ import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import ch.eitchnet.utils.helper.StringHelper;
/**
* Simple Swing UI to create passwords
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@SuppressWarnings("nls")
public class PasswordCreaterUI {
/**
@ -91,7 +94,7 @@ public class PasswordCreaterUI {
String digest = (String) digestCombo.getSelectedItem();
char[] passwordChar = passwordField.getPassword();
String password = new String(passwordChar);
String hash = HashHelper.stringToHash(digest, password);
String hash = StringHelper.hashAsHex(digest, password);
hashField.setText(hash);
} catch (Exception e1) {
e1.printStackTrace();

View File

@ -19,6 +19,8 @@ import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import ch.eitchnet.utils.helper.StringHelper;
/**
* <p>
* Simple main class which can be used to create a hash from a password which the user must type in at the command line
@ -38,6 +40,7 @@ public class PasswordCreator {
* @throws Exception
* thrown if anything goes wrong
*/
@SuppressWarnings("nls")
public static void main(String[] args) throws Exception {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
@ -63,7 +66,7 @@ public class PasswordCreator {
System.out.print("Password: ");
String password = r.readLine().trim();
System.out.print("Hash is: " + HashHelper.stringToHash(hashAlgorithm, password));
System.out.print("Hash is: " + StringHelper.hashAsHex(hashAlgorithm, password));
}
}

View File

@ -29,6 +29,7 @@ import ch.eitchnet.privilege.handler.PrivilegeHandler;
import ch.eitchnet.privilege.model.internal.PrivilegeContainerModel;
import ch.eitchnet.privilege.policy.PrivilegePolicy;
import ch.eitchnet.privilege.xml.PrivilegeConfigSaxReader;
import ch.eitchnet.utils.helper.ClassHelper;
import ch.eitchnet.utils.helper.XmlHelper;
/**

View File

@ -20,6 +20,7 @@ package ch.eitchnet.privilege.helper;
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@SuppressWarnings("nls")
public class XmlConstants {
/**

View File

@ -72,16 +72,16 @@ public final class Certificate implements Serializable {
// validate arguments are not null
if (StringHelper.isEmpty(sessionId)) {
throw new PrivilegeException("sessionId is null!");
throw new PrivilegeException("sessionId is null!"); //$NON-NLS-1$
}
if (StringHelper.isEmpty(username)) {
throw new PrivilegeException("username is null!");
throw new PrivilegeException("username is null!"); //$NON-NLS-1$
}
if (StringHelper.isEmpty(authToken)) {
throw new PrivilegeException("authToken is null!");
throw new PrivilegeException("authToken is null!"); //$NON-NLS-1$
}
if (StringHelper.isEmpty(authPassword)) {
throw new PrivilegeException("authPassword is null!");
throw new PrivilegeException("authPassword is null!"); //$NON-NLS-1$
}
this.sessionId = sessionId;
@ -162,6 +162,7 @@ public final class Certificate implements Serializable {
*
* @see java.lang.Object#toString()
*/
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder builder = new StringBuilder();

View File

@ -94,7 +94,7 @@ public class PrivilegeContext {
String privilegeName = restrictable.getPrivilegeName();
IPrivilege privilege = this.privileges.get(privilegeName);
if (privilege == null) {
String msg = MessageFormat.format(PrivilegeMessages.getString("Privilege.accessdenied.noprivilege"),
String msg = MessageFormat.format(PrivilegeMessages.getString("Privilege.accessdenied.noprivilege"), //$NON-NLS-1$
getUsername(), privilegeName, restrictable.getClass().getName());
throw new AccessDeniedException(msg);
}
@ -103,7 +103,7 @@ public class PrivilegeContext {
String policyName = privilege.getPolicy();
PrivilegePolicy policy = this.policies.get(policyName);
if (policy == null) {
String msg = "The PrivilegePolicy {0} does not exist on the PrivilegeContext!";
String msg = "The PrivilegePolicy {0} does not exist on the PrivilegeContext!"; //$NON-NLS-1$
throw new PrivilegeException(MessageFormat.format(msg, policyName));
}
@ -130,7 +130,7 @@ public class PrivilegeContext {
public static PrivilegeContext get() throws PrivilegeException {
PrivilegeContext privilegeContext = PrivilegeContext.threadLocal.get();
if (privilegeContext == null) {
throw new PrivilegeException("There is no PrivilegeContext currently bound to the ThreadLocal!");
throw new PrivilegeException("There is no PrivilegeContext currently bound to the ThreadLocal!"); //$NON-NLS-1$
}
return privilegeContext;
}
@ -148,7 +148,7 @@ public class PrivilegeContext {
public static void set(PrivilegeContext privilegeContext) throws PrivilegeException {
PrivilegeContext currentContext = PrivilegeContext.threadLocal.get();
if (privilegeContext != null && currentContext != null) {
throw new PrivilegeException("There already is a PrivilegeContext bound to the ThreadLocal!");
throw new PrivilegeException("There already is a PrivilegeContext bound to the ThreadLocal!"); //$NON-NLS-1$
}
PrivilegeContext.threadLocal.set(privilegeContext);
}

View File

@ -72,18 +72,18 @@ public class PrivilegeRep implements Serializable {
public void validate() {
if (StringHelper.isEmpty(this.name)) {
throw new PrivilegeException("No name defined!");
throw new PrivilegeException("No name defined!"); //$NON-NLS-1$
}
if (StringHelper.isEmpty(this.policy)) {
throw new PrivilegeException("policy is null!");
throw new PrivilegeException("policy is null!"); //$NON-NLS-1$
}
if (this.denyList == null) {
throw new PrivilegeException("denyList is null");
throw new PrivilegeException("denyList is null"); //$NON-NLS-1$
}
if (this.allowList == null) {
throw new PrivilegeException("allowList is null");
throw new PrivilegeException("allowList is null"); //$NON-NLS-1$
}
}
@ -167,6 +167,7 @@ public class PrivilegeRep implements Serializable {
*
* @see java.lang.Object#toString()
*/
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder builder = new StringBuilder();

View File

@ -57,7 +57,7 @@ public class RoleRep implements Serializable {
*/
public void validate() {
if (StringHelper.isEmpty(this.name))
throw new PrivilegeException("name is null");
throw new PrivilegeException("name is null"); //$NON-NLS-1$
}
/**
@ -87,6 +87,7 @@ public class RoleRep implements Serializable {
*
* @see java.lang.Object#toString()
*/
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder builder = new StringBuilder();

View File

@ -85,22 +85,22 @@ public class UserRep implements Serializable {
public void validate() {
if (StringHelper.isEmpty(this.userId))
throw new PrivilegeException("userId is null or empty");
throw new PrivilegeException("userId is null or empty"); //$NON-NLS-1$
if (StringHelper.isEmpty(this.username))
throw new PrivilegeException("username is null or empty");
throw new PrivilegeException("username is null or empty"); //$NON-NLS-1$
if (StringHelper.isEmpty(this.firstname))
throw new PrivilegeException("firstname is null or empty");
throw new PrivilegeException("firstname is null or empty"); //$NON-NLS-1$
if (StringHelper.isEmpty(this.surname))
throw new PrivilegeException("surname is null or empty");
throw new PrivilegeException("surname is null or empty"); //$NON-NLS-1$
if (this.userState == null)
throw new PrivilegeException("userState is null");
throw new PrivilegeException("userState is null"); //$NON-NLS-1$
if (this.roles == null)
throw new PrivilegeException("roles is null");
throw new PrivilegeException("roles is null"); //$NON-NLS-1$
}
/**
@ -247,6 +247,7 @@ public class UserRep implements Serializable {
*
* @see java.lang.Object#toString()
*/
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder builder = new StringBuilder();

View File

@ -15,6 +15,7 @@
*/
package ch.eitchnet.privilege.model.internal;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
@ -139,14 +140,17 @@ public class PrivilegeContainerModel {
this.policies.put(privilegeName, clazz);
} catch (InstantiationException e) {
throw new PrivilegeException("Configured Privilege Policy " + privilegeName + " with class "
+ policyClassName + " could not be instantiated.", e);
String msg = "Configured Privilege Policy {0} with class {1} could not be instantiated."; //$NON-NLS-1$
msg = MessageFormat.format(msg, privilegeName, policyClassName);
throw new PrivilegeException(msg, e);
} catch (IllegalAccessException e) {
throw new PrivilegeException("Configured Privilege Policy " + privilegeName + " with class "
+ policyClassName + " can not be accessed.", e);
String msg = "Configured Privilege Policy {0} with class {1} can not be accessed."; //$NON-NLS-1$
msg = MessageFormat.format(msg, privilegeName, policyClassName);
throw new PrivilegeException(msg, e);
} catch (ClassNotFoundException e) {
throw new PrivilegeException("Configured Privilege Policy " + privilegeName + " with class "
+ policyClassName + " does not exist.", e);
String msg = "Configured Privilege Policy {0} with class {1} does not exist."; //$NON-NLS-1$
msg = MessageFormat.format(msg, privilegeName, policyClassName);
throw new PrivilegeException(msg, e);
}
}
@ -162,6 +166,7 @@ public class PrivilegeContainerModel {
*
* @see java.lang.Object#toString()
*/
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder builder = new StringBuilder();

View File

@ -73,16 +73,16 @@ public final class PrivilegeImpl implements IPrivilege {
public PrivilegeImpl(String name, String policy, boolean allAllowed, Set<String> denyList, Set<String> allowList) {
if (StringHelper.isEmpty(name)) {
throw new PrivilegeException("No name defined!");
throw new PrivilegeException("No name defined!"); //$NON-NLS-1$
}
if (StringHelper.isEmpty(policy)) {
throw new PrivilegeException("Policy may not be empty!");
throw new PrivilegeException("Policy may not be empty!"); //$NON-NLS-1$
}
if (denyList == null) {
throw new PrivilegeException("denyList is null!");
throw new PrivilegeException("denyList is null!"); //$NON-NLS-1$
}
if (allowList == null) {
throw new PrivilegeException("allowList is null!");
throw new PrivilegeException("allowList is null!"); //$NON-NLS-1$
}
this.name = name;
@ -189,6 +189,7 @@ public final class PrivilegeImpl implements IPrivilege {
*
* @see java.lang.Object#toString()
*/
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder builder = new StringBuilder();

View File

@ -55,10 +55,10 @@ public final class Role {
public Role(String name, Map<String, IPrivilege> privilegeMap) {
if (StringHelper.isEmpty(name)) {
throw new PrivilegeException("No name defined!");
throw new PrivilegeException("No name defined!"); //$NON-NLS-1$
}
if (privilegeMap == null) {
throw new PrivilegeException("No privileges defined!");
throw new PrivilegeException("No privileges defined!"); //$NON-NLS-1$
}
this.name = name;
@ -75,11 +75,11 @@ public final class Role {
String name = roleRep.getName();
if (StringHelper.isEmpty(name)) {
throw new PrivilegeException("No name defined!");
throw new PrivilegeException("No name defined!"); //$NON-NLS-1$
}
if (roleRep.getPrivilegeMap() == null) {
throw new PrivilegeException("No privileges defined!");
throw new PrivilegeException("No privileges defined!"); //$NON-NLS-1$
}
// build privileges from reps
@ -145,6 +145,7 @@ public final class Role {
*
* @see java.lang.Object#toString()
*/
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder builder = new StringBuilder();

View File

@ -82,19 +82,19 @@ public final class User {
Set<String> roles, Locale locale, Map<String, String> propertyMap) {
if (StringHelper.isEmpty(userId)) {
throw new PrivilegeException("No UserId defined!");
throw new PrivilegeException("No UserId defined!"); //$NON-NLS-1$
}
if (StringHelper.isEmpty(username)) {
throw new PrivilegeException("No username defined!");
throw new PrivilegeException("No username defined!"); //$NON-NLS-1$
}
if (StringHelper.isEmpty(firstname)) {
throw new PrivilegeException("No firstname defined!");
throw new PrivilegeException("No firstname defined!"); //$NON-NLS-1$
}
if (StringHelper.isEmpty(surname)) {
throw new PrivilegeException("No surname defined!");
throw new PrivilegeException("No surname defined!"); //$NON-NLS-1$
}
if (userState == null) {
throw new PrivilegeException("No userState defined!");
throw new PrivilegeException("No userState defined!"); //$NON-NLS-1$
}
// password may be null, meaning not able to login
@ -240,6 +240,7 @@ public final class User {
*
* @see java.lang.Object#toString()
*/
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder builder = new StringBuilder();

View File

@ -81,7 +81,7 @@ public class DefaultPrivilege implements PrivilegePolicy {
// first check values not allowed
if (privilege.isDenied(privilegeValue)) {
// then throw access denied
String msg = MessageFormat.format(PrivilegeMessages.getString("Privilege.accessdenied.noprivilege"),
String msg = MessageFormat.format(PrivilegeMessages.getString("Privilege.accessdenied.noprivilege"), //$NON-NLS-1$
PrivilegeContext.get().getUsername(), privilegeName, restrictable.getClass().getName());
throw new AccessDeniedException(msg);
}
@ -91,7 +91,7 @@ public class DefaultPrivilege implements PrivilegePolicy {
return;
// default is not allowed
String msg = MessageFormat.format(PrivilegeMessages.getString("Privilege.accessdenied.noprivilege"),
String msg = MessageFormat.format(PrivilegeMessages.getString("Privilege.accessdenied.noprivilege"), //$NON-NLS-1$
PrivilegeContext.get().getUsername(), privilegeName, restrictable.getClass().getName());
throw new AccessDeniedException(msg);
}

View File

@ -23,11 +23,11 @@ import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import ch.eitchnet.privilege.helper.XmlConstants;
import ch.eitchnet.privilege.model.internal.PrivilegeContainerModel;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class PrivilegeConfigSaxReader extends DefaultHandler {
@ -41,14 +41,18 @@ public class PrivilegeConfigSaxReader extends DefaultHandler {
this.containerModel = containerModel;
}
public PrivilegeContainerModel getContainerModel() {
return this.containerModel;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("Container")) {
if (qName.equals(XmlConstants.XML_CONTAINER)) {
this.buildersStack.add(new ContainerParser());
} else if (qName.equals("Parameters")) {
} else if (qName.equals(XmlConstants.XML_PARAMETERS)) {
this.buildersStack.add(new ParametersParser());
} else if (qName.equals("Policies")) {
} else if (qName.equals(XmlConstants.XML_POLICIES)) {
this.buildersStack.add(new PoliciesParser());
}
@ -69,11 +73,11 @@ public class PrivilegeConfigSaxReader extends DefaultHandler {
this.buildersStack.peek().endElement(uri, localName, qName);
ElementParser elementParser = null;
if (qName.equals("Container")) {
if (qName.equals(XmlConstants.XML_CONTAINER)) {
elementParser = this.buildersStack.pop();
} else if (qName.equals("Parameters")) {
} else if (qName.equals(XmlConstants.XML_PARAMETERS)) {
elementParser = this.buildersStack.pop();
} else if (qName.equals("Policies")) {
} else if (qName.equals(XmlConstants.XML_POLICIES)) {
elementParser = this.buildersStack.pop();
}
@ -105,16 +109,16 @@ public class PrivilegeConfigSaxReader extends DefaultHandler {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("Container")) {
if (qName.equals(XmlConstants.XML_CONTAINER)) {
this.currentElement = qName;
} else if (qName.equals("EncryptionHandler")) {
} else if (qName.equals(XmlConstants.XML_HANDLER_ENCRYPTION)) {
this.currentElement = qName;
PrivilegeConfigSaxReader.this.containerModel
.setEncryptionHandlerClassName(attributes.getValue("class"));
} else if (qName.equals("PersistenceHandler")) {
String className = attributes.getValue(XmlConstants.XML_ATTR_CLASS);
getContainerModel().setEncryptionHandlerClassName(className);
} else if (qName.equals(XmlConstants.XML_HANDLER_PERSISTENCE)) {
this.currentElement = qName;
PrivilegeConfigSaxReader.this.containerModel.setPersistenceHandlerClassName(attributes
.getValue("class"));
String className = attributes.getValue(XmlConstants.XML_ATTR_CLASS);
getContainerModel().setPersistenceHandlerClassName(className);
}
}
@ -125,14 +129,12 @@ public class PrivilegeConfigSaxReader extends DefaultHandler {
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());
if (this.currentElement.equals(XmlConstants.XML_CONTAINER)) {
getContainerModel().setParameterMap(parametersChild.getParameterMap());
} else if (this.currentElement.equals(XmlConstants.XML_HANDLER_ENCRYPTION)) {
getContainerModel().setEncryptionHandlerParameterMap(parametersChild.getParameterMap());
} else if (this.currentElement.equals(XmlConstants.XML_HANDLER_PERSISTENCE)) {
getContainerModel().setPersistenceHandlerParameterMap(parametersChild.getParameterMap());
}
}
}
@ -145,9 +147,9 @@ public class PrivilegeConfigSaxReader extends DefaultHandler {
@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");
if (qName.equals(XmlConstants.XML_PARAMETER)) {
String key = attributes.getValue(XmlConstants.XML_ATTR_NAME);
String value = attributes.getValue(XmlConstants.XML_ATTR_VALUE);
this.parameterMap.put(key, value);
}
}
@ -166,11 +168,11 @@ public class PrivilegeConfigSaxReader extends DefaultHandler {
@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");
if (qName.equals(XmlConstants.XML_POLICY)) {
String policyName = attributes.getValue(XmlConstants.XML_ATTR_NAME);
String policyClassName = attributes.getValue(XmlConstants.XML_ATTR_CLASS);
PrivilegeConfigSaxReader.this.containerModel.addPolicy(policyName, policyClassName);
getContainerModel().addPolicy(policyName, policyClassName);
}
}
}

View File

@ -15,6 +15,7 @@
*/
package ch.eitchnet.privilege.xml;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@ -30,6 +31,7 @@ import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import ch.eitchnet.privilege.helper.XmlConstants;
import ch.eitchnet.privilege.model.IPrivilege;
import ch.eitchnet.privilege.model.UserState;
import ch.eitchnet.privilege.model.internal.PrivilegeImpl;
@ -42,7 +44,7 @@ import ch.eitchnet.utils.helper.StringHelper;
*/
public class PrivilegeModelSaxReader extends DefaultHandler {
private static final Logger logger = LoggerFactory.getLogger(PrivilegeModelSaxReader.class);
protected static final Logger logger = LoggerFactory.getLogger(PrivilegeModelSaxReader.class);
private Stack<ElementParser> buildersStack = new Stack<ElementParser>();
@ -73,12 +75,12 @@ public class PrivilegeModelSaxReader extends DefaultHandler {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("Users")) {
if (qName.equals(XmlConstants.XML_USERS)) {
this.buildersStack.add(new UserParser());
this.insideUser = true;
} else if (qName.equals("Properties")) {
} else if (qName.equals(XmlConstants.XML_PROPERTIES)) {
this.buildersStack.add(new PropertyParser());
} else if (qName.equals("Roles") && !this.insideUser) {
} else if (qName.equals(XmlConstants.XML_ROLES) && !this.insideUser) {
this.buildersStack.add(new RoleParser());
}
@ -99,16 +101,16 @@ public class PrivilegeModelSaxReader extends DefaultHandler {
this.buildersStack.peek().endElement(uri, localName, qName);
ElementParser elementParser = null;
if (qName.equals("Users")) {
if (qName.equals(XmlConstants.XML_USERS)) {
elementParser = this.buildersStack.pop();
this.insideUser = false;
PrivilegeModelSaxReader.logger.info("Popping for Users");
} else if (qName.equals("Properties")) {
logger.info("Popping for Users"); //$NON-NLS-1$
} else if (qName.equals(XmlConstants.XML_PROPERTIES)) {
elementParser = this.buildersStack.pop();
PrivilegeModelSaxReader.logger.info("Popping for Properties");
} else if (qName.equals("Roles") && !this.insideUser) {
logger.info("Popping for Properties"); //$NON-NLS-1$
} else if (qName.equals(XmlConstants.XML_ROLES) && !this.insideUser) {
elementParser = this.buildersStack.pop();
PrivilegeModelSaxReader.logger.info("Popping for Roles");
logger.info("Popping for Roles"); //$NON-NLS-1$
}
if (!this.buildersStack.isEmpty() && elementParser != null)
@ -142,16 +144,10 @@ public class PrivilegeModelSaxReader extends DefaultHandler {
private Map<String, IPrivilege> privileges;
/**
*
*/
public RoleParser() {
init();
}
/**
*
*/
private void init() {
this.privileges = new HashMap<String, IPrivilege>();
@ -170,11 +166,11 @@ public class PrivilegeModelSaxReader extends DefaultHandler {
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");
if (qName.equals(XmlConstants.XML_ROLE)) {
this.roleName = attributes.getValue(XmlConstants.XML_ATTR_NAME);
} else if (qName.equals(XmlConstants.XML_PRIVILEGE)) {
this.privilegeName = attributes.getValue(XmlConstants.XML_ATTR_NAME);
this.privilegePolicy = attributes.getValue(XmlConstants.XML_ATTR_POLICY);
}
}
@ -187,24 +183,24 @@ public class PrivilegeModelSaxReader extends DefaultHandler {
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("AllAllowed")) {
if (qName.equals(XmlConstants.XML_ALL_ALLOWED)) {
this.allAllowed = StringHelper.parseBoolean(this.text.toString().trim());
} else if (qName.equals("Allow")) {
} else if (qName.equals(XmlConstants.XML_ALLOW)) {
this.allowList.add(this.text.toString().trim());
} else if (qName.equals("Deny")) {
} else if (qName.equals(XmlConstants.XML_DENY)) {
this.denyList.add(this.text.toString().trim());
} else if (qName.equals("Privilege")) {
} else if (qName.equals(XmlConstants.XML_PRIVILEGE)) {
IPrivilege privilege = new PrivilegeImpl(this.privilegeName, this.privilegePolicy, this.allAllowed,
this.denyList, this.allowList);
this.privileges.put(this.privilegeName, privilege);
} else if (qName.equals("Role")) {
} else if (qName.equals(XmlConstants.XML_ROLE)) {
Role role = new Role(this.roleName, this.privileges);
PrivilegeModelSaxReader.this.roles.add(role);
PrivilegeModelSaxReader.logger.info("New Role: " + role);
getRoles().add(role);
logger.info(MessageFormat.format("New Role: {0}", role)); //$NON-NLS-1$
init();
}
}
@ -248,10 +244,10 @@ public class PrivilegeModelSaxReader extends DefaultHandler {
this.text = new StringBuilder();
if (qName.equals("User")) {
this.userId = attributes.getValue("userId");
this.username = attributes.getValue("username");
this.password = attributes.getValue("password");
if (qName.equals(XmlConstants.XML_USER)) {
this.userId = attributes.getValue(XmlConstants.XML_ATTR_USER_ID);
this.username = attributes.getValue(XmlConstants.XML_ATTR_USERNAME);
this.password = attributes.getValue(XmlConstants.XML_ATTR_PASSWORD);
}
}
@ -263,22 +259,22 @@ public class PrivilegeModelSaxReader extends DefaultHandler {
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("Firstname")) {
if (qName.equals(XmlConstants.XML_FIRSTNAME)) {
this.firstName = this.text.toString().trim();
} else if (qName.equals("Surname")) {
} else if (qName.equals(XmlConstants.XML_SURNAME)) {
this.surname = this.text.toString().trim();
} else if (qName.equals("State")) {
} else if (qName.equals(XmlConstants.XML_STATE)) {
this.userState = UserState.valueOf(this.text.toString().trim());
} else if (qName.equals("Locale")) {
} else if (qName.equals(XmlConstants.XML_LOCALE)) {
this.locale = Locale.forLanguageTag(this.text.toString().trim());
} else if (qName.equals("Role")) {
} else if (qName.equals(XmlConstants.XML_ROLE)) {
this.userRoles.add(this.text.toString().trim());
} else if (qName.equals("User")) {
} else if (qName.equals(XmlConstants.XML_USER)) {
User user = new User(this.userId, this.username, this.password, this.firstName, this.surname,
this.userState, this.userRoles, this.locale, this.parameters);
PrivilegeModelSaxReader.this.users.add(user);
getUsers().add(user);
}
}
@ -294,20 +290,17 @@ public class PrivilegeModelSaxReader extends DefaultHandler {
// <Property name="organizationalUnit" value="Development" />
private Map<String, String> parameterMap = new HashMap<String, String>();
public 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");
if (qName.equals(XmlConstants.XML_PROPERTY)) {
String key = attributes.getValue(XmlConstants.XML_ATTR_NAME);
String value = attributes.getValue(XmlConstants.XML_ATTR_VALUE);
this.parameterMap.put(key, value);
}
}
/**
* @return the parameterMap
*/
public Map<String, String> getParameterMap() {
return this.parameterMap;
}

View File

@ -148,7 +148,7 @@ public class XmlTest {
configSaxWriter.write();
String fileHash = StringHelper.getHexString(FileHelper.hashFileSha256(configFile));
assertEquals("2ABD3442EEC8BCEC5BEE365AAB6DB2FD4E1789325425CB1E017E900582525685", fileHash);
assertEquals("2abd3442eec8bcec5bee365aab6db2fd4e1789325425cb1e017e900582525685", fileHash);
}
@Test
@ -210,6 +210,6 @@ public class XmlTest {
configSaxWriter.write();
String fileHash = StringHelper.getHexString(FileHelper.hashFileSha256(modelFile));
assertEquals("A2127D20A61E00BCDBB61569CD2B200C4F0F111C972BAC3B1E54DF3B2FCDC8BE", fileHash);
assertEquals("a2127d20a61e00bcdbb61569cd2b200c4f0f111c972bac3b1e54df3b2fcdc8be", fileHash);
}
}