Author: chris.laprun(a)jboss.com
Date: 2012-02-01 13:54:13 -0500 (Wed, 01 Feb 2012)
New Revision: 8347
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UserConfigurableUsernameValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java
Log:
- GTNPORTAL-1673: Made UserConfigurableUsernameValidator more generic so that it can load
any number of named configurations from gatein.conf.dir/configuration.properties. A
validation is activated by creating a validator with the associated name. Documentation
upcoming.
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UserConfigurableUsernameValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UserConfigurableUsernameValidator.java 2012-02-01
15:28:25 UTC (rev 8346)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UserConfigurableUsernameValidator.java 2012-02-01
18:54:13 UTC (rev 8347)
@@ -33,31 +33,109 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
/** @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a> */
@Serialized
-public class UserConfigurableUsernameValidator extends UsernameValidator
+public class UserConfigurableUsernameValidator extends MultipleConditionsValidator
{
- private static ValidatorConfiguration configuration = new ValidatorConfiguration();
+ protected static Log log =
ExoLogger.getLogger(UserConfigurableUsernameValidator.class);
+ public static final String USERNAME = "username";
+ public static final String GROUPMEMBERSHIP = "groupmembership";
+ public static final String DEFAULT_LOCALIZATION_KEY =
"ExpressionValidator.msg.value-invalid";
+ public static final String GROUP_MEMBERSHIP_VALIDATION_REGEX =
"^\\p{L}[\\p{L}\\d._\\-\\s*,\\s*]+$";
+ public static final String GROUP_MEMBERSHIP_LOCALIZATION_KEY =
"UIGroupMembershipForm.msg.Invalid-char";
+
+ private static Map<String, ValidatorConfiguration> configurations = new
HashMap<String, ValidatorConfiguration>(3);
+
+ public static final String KEY_PREFIX = "gatein.validators.";
+
+ static
+ {
+ String gateinConfDir = System.getProperty("gatein.conf.dir");
+ File conf = new File(gateinConfDir, "configuration.properties");
+ if (conf.exists())
+ {
+ try
+ {
+ Properties properties = new Properties();
+ properties.load(new FileInputStream(conf));
+ int length = KEY_PREFIX.length();
+ for (Object objectKey : properties.keySet())
+ {
+ String key = (String)objectKey;
+ if (key.startsWith(KEY_PREFIX))
+ {
+ // extract property key
+ String propertyKey = key.substring(length, key.indexOf('.',
length));
+ if(!configurations.containsKey(propertyKey))
+ {
+ configurations.put(propertyKey, new
ValidatorConfiguration(propertyKey, properties));
+ }
+ }
+ }
+ }
+ catch (IOException e)
+ {
+ log.info(e.getLocalizedMessage());
+ log.debug(e);
+ }
+ }
+ }
+
+ private final String validatorName;
+ private final String localizationKey;
+
// needed by @Serialized
public UserConfigurableUsernameValidator()
{
+ this(USERNAME, DEFAULT_LOCALIZATION_KEY);
+ }
+
+ public UserConfigurableUsernameValidator(String validatorName, String
messageLocalizationKey)
+ {
this.exceptionOnMissingMandatory = true;
this.trimValue = true;
+ localizationKey = messageLocalizationKey != null ? messageLocalizationKey :
DEFAULT_LOCALIZATION_KEY;
+ this.validatorName = validatorName != null ? validatorName : USERNAME;
}
+ public UserConfigurableUsernameValidator(String validatorName)
+ {
+ this(validatorName, DEFAULT_LOCALIZATION_KEY);
+ }
+
@Override
protected void validate(String value, String label, CompoundApplicationMessage
messages, UIFormInput uiInput)
{
- if (configuration.defaultConfig)
+ ValidatorConfiguration configuration = configurations.get(validatorName);
+
+ if (configuration == null)
{
- super.validate(value, label, messages, uiInput);
+ // we don't have a user-configured validator for this validator name
+
+ if (USERNAME.equals(validatorName))
+ {
+ // if the validator name is USERNAME constant, we have a username to validate
with the original, non-configured behavior
+ UsernameValidator.validate(value, label, messages,
UsernameValidator.DEFAULT_MIN_LENGTH, UsernameValidator.DEFAULT_MAX_LENGTH);
+ }
+ else
+ {
+ // else, we assume that we need to validate a group membership, replicating
original behavior
+ if (!Pattern.matches(GROUP_MEMBERSHIP_VALIDATION_REGEX, value))
+ {
+ messages.addMessage(localizationKey, new Object[]{label});
+ }
+ }
}
else
{
+ // otherwise, use the user-provided configuration
+
if (value.length() < configuration.minLength || value.length() >
configuration.maxLength)
{
messages.addMessage("StringLengthValidator.msg.length-invalid", new
Object[]{label, configuration.minLength.toString(), configuration.maxLength.toString()});
@@ -65,48 +143,33 @@
if (!Pattern.matches(configuration.pattern, value))
{
- messages.addMessage("ExpressionValidator.msg.value-invalid", new
Object[]{label, configuration.formatMessage});
+ messages.addMessage(localizationKey, new Object[]{label,
configuration.formatMessage});
}
}
}
private static class ValidatorConfiguration
{
- protected static Log log = ExoLogger.getLogger("username-validator");
private Integer minLength;
private Integer maxLength;
private String pattern;
private String formatMessage;
- private boolean defaultConfig = true;
- private ValidatorConfiguration()
+ private ValidatorConfiguration(String propertyKey, Properties properties)
{
- String gateinConfDir = System.getProperty("gatein.conf.dir");
- File conf = new File(gateinConfDir, "username-validator.properties");
+ // used to assign backward compatible default values
+ boolean isUser = USERNAME.equals(propertyKey);
+ String prefixedKey = KEY_PREFIX + propertyKey;
- minLength = DEFAULT_MIN_LENGTH;
- maxLength = DEFAULT_MAX_LENGTH;
- pattern = Utils.USER_NAME_VALIDATOR_REGEX;
- formatMessage = pattern;
+ String property = properties.getProperty(prefixedKey +
".min.length");
+ minLength = property != null ? Integer.valueOf(property) : (isUser ?
UsernameValidator.DEFAULT_MIN_LENGTH : 0);
- if (conf.exists())
- {
- try
- {
- Properties properties = new Properties();
- properties.load(new FileInputStream(conf));
- minLength = Integer.valueOf(properties.getProperty("minLength",
String.valueOf(DEFAULT_MIN_LENGTH)));
- maxLength = Integer.valueOf(properties.getProperty("maxLength",
String.valueOf(DEFAULT_MAX_LENGTH)));
- pattern = properties.getProperty("regexp",
Utils.USER_NAME_VALIDATOR_REGEX);
- formatMessage = properties.getProperty("formatMessage",
formatMessage);
- defaultConfig = false;
- }
- catch (IOException e)
- {
- log.info(e.getLocalizedMessage());
- log.debug(e);
- }
- }
+ property = properties.getProperty(prefixedKey + ".max.length");
+ maxLength = property != null ? Integer.valueOf(property) : (isUser ?
UsernameValidator.DEFAULT_MAX_LENGTH : Integer.MAX_VALUE);
+
+ pattern = properties.getProperty(prefixedKey + ".regexp",
Utils.USER_NAME_VALIDATOR_REGEX);
+ formatMessage = properties.getProperty(prefixedKey +
".format.message", pattern);
}
+
}
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java 2012-02-01
15:28:25 UTC (rev 8346)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java 2012-02-01
18:54:13 UTC (rev 8347)
@@ -49,6 +49,11 @@
protected void validate(String value, String label, CompoundApplicationMessage
messages, UIFormInput uiInput)
{
+ validate(value, label, messages, min, max);
+ }
+
+ static void validate(String value, String label, CompoundApplicationMessage messages,
Integer min, Integer max)
+ {
char[] buff = value.toCharArray();
if (buff.length < min || buff.length > max)
{
@@ -93,7 +98,7 @@
}
}
- private boolean isSymbol(char c)
+ private static boolean isSymbol(char c)
{
return c == '_' || c == '.';
}
Modified:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java 2012-02-01
15:28:25 UTC (rev 8346)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java 2012-02-01
18:54:13 UTC (rev 8347)
@@ -31,6 +31,7 @@
import org.exoplatform.webui.form.validator.ResourceValidator;
import org.exoplatform.webui.form.validator.SpecialCharacterValidator;
import org.exoplatform.webui.form.validator.URLValidator;
+import org.exoplatform.webui.form.validator.UserConfigurableUsernameValidator;
import org.exoplatform.webui.form.validator.UsernameValidator;
import org.exoplatform.webui.form.validator.Validator;
@@ -93,6 +94,14 @@
public void testUsernameValidator()
{
Validator validator = new UsernameValidator(3, 30);
+ validateUsernames(validator);
+
+ validator = new
UserConfigurableUsernameValidator(UserConfigurableUsernameValidator.USERNAME);
+ validateUsernames(validator);
+ }
+
+ private void validateUsernames(Validator validator)
+ {
assertTrue(expected(validator, "root.gtn"));
assertTrue(expected(validator, "root_gtn"));
assertTrue(expected(validator, "root_gtn.01"));