From do-not-reply at jboss.org Wed Feb 1 13:56:17 2012 Content-Type: multipart/mixed; boundary="===============5287649442531382368==" MIME-Version: 1.0 From: do-not-reply at jboss.org To: gatein-commits at lists.jboss.org Subject: [gatein-commits] gatein SVN: r8348 - in portal/trunk/webui/core/src: test/java/org/exoplatform/webui/test/validator and 1 other directory. Date: Wed, 01 Feb 2012 13:56:16 -0500 Message-ID: <201202011856.q11IuGDa029705@svn01.web.mwc.hst.phx2.redhat.com> --===============5287649442531382368== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: chris.laprun(a)jboss.com Date: 2012-02-01 13:56:16 -0500 (Wed, 01 Feb 2012) New Revision: 8348 Added: portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validat= or/UserConfigurableValidator.java Removed: portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validat= or/UserConfigurableUsernameValidator.java Modified: portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validat= or/TestWebuiValidator.java Log: - GTNPORTAL-1673: Renamed UserConfigurableUsernameValidator to UserConfigur= ableValidator since it makes more sense. Deleted: portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/v= alidator/UserConfigurableUsernameValidator.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/valida= tor/UserConfigurableUsernameValidator.java 2012-02-01 18:54:13 UTC (rev 834= 7) +++ portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/valida= tor/UserConfigurableUsernameValidator.java 2012-02-01 18:56:16 UTC (rev 834= 8) @@ -1,175 +0,0 @@ -/* - * JBoss, a division of Red Hat - * Copyright 2012, Red Hat Middleware, LLC, and individual - * contributors as indicated by the @authors tag. See the - * copyright.txt in the distribution for a full listing of - * individual contributors. - * - * This 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 2.1 of - * the License, or (at your option) any later version. - * - * This software 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 this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ - -package org.exoplatform.webui.form.validator; - -import org.exoplatform.commons.serialization.api.annotations.Serialized; -import org.exoplatform.portal.pom.config.Utils; -import org.exoplatform.services.log.ExoLogger; -import org.exoplatform.services.log.Log; -import org.exoplatform.web.application.CompoundApplicationMessage; -import org.exoplatform.webui.form.UIFormInput; - -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 Chris Laprun */ -(a)Serialized -public class UserConfigurableUsernameValidator extends MultipleConditionsV= alidator -{ - protected static Log log =3D ExoLogger.getLogger(UserConfigurableUserna= meValidator.class); - - public static final String USERNAME =3D "username"; - public static final String GROUPMEMBERSHIP =3D "groupmembership"; - public static final String DEFAULT_LOCALIZATION_KEY =3D "ExpressionVali= dator.msg.value-invalid"; - public static final String GROUP_MEMBERSHIP_VALIDATION_REGEX =3D "^\\p{= L}[\\p{L}\\d._\\-\\s*,\\s*]+$"; - public static final String GROUP_MEMBERSHIP_LOCALIZATION_KEY =3D "UIGro= upMembershipForm.msg.Invalid-char"; - - private static Map configurations =3D n= ew HashMap(3); - - public static final String KEY_PREFIX =3D "gatein.validators."; - - static - { - String gateinConfDir =3D System.getProperty("gatein.conf.dir"); - File conf =3D new File(gateinConfDir, "configuration.properties"); - if (conf.exists()) - { - try - { - Properties properties =3D new Properties(); - properties.load(new FileInputStream(conf)); - int length =3D KEY_PREFIX.length(); - for (Object objectKey : properties.keySet()) - { - String key =3D (String)objectKey; - if (key.startsWith(KEY_PREFIX)) - { - // extract property key - String propertyKey =3D key.substring(length, key.indexOf= ('.', length)); - if(!configurations.containsKey(propertyKey)) - { - configurations.put(propertyKey, new ValidatorConfigur= ation(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 m= essageLocalizationKey) - { - this.exceptionOnMissingMandatory =3D true; - this.trimValue =3D true; - localizationKey =3D messageLocalizationKey !=3D null ? messageLocali= zationKey : DEFAULT_LOCALIZATION_KEY; - this.validatorName =3D validatorName !=3D null ? validatorName : USE= RNAME; - } - - public UserConfigurableUsernameValidator(String validatorName) - { - this(validatorName, DEFAULT_LOCALIZATION_KEY); - } - - @Override - protected void validate(String value, String label, CompoundApplication= Message messages, UIFormInput uiInput) - { - ValidatorConfiguration configuration =3D configurations.get(validato= rName); - - if (configuration =3D=3D null) - { - // we don't have a user-configured validator for this validator n= ame - - if (USERNAME.equals(validatorName)) - { - // if the validator name is USERNAME constant, we have a usern= ame to validate with the original, non-configured behavior - UsernameValidator.validate(value, label, messages, UsernameVal= idator.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.max= Length.toString()}); - } - - if (!Pattern.matches(configuration.pattern, value)) - { - messages.addMessage(localizationKey, new Object[]{label, confi= guration.formatMessage}); - } - } - } - - private static class ValidatorConfiguration - { - private Integer minLength; - private Integer maxLength; - private String pattern; - private String formatMessage; - - private ValidatorConfiguration(String propertyKey, Properties proper= ties) - { - // used to assign backward compatible default values - boolean isUser =3D USERNAME.equals(propertyKey); - String prefixedKey =3D KEY_PREFIX + propertyKey; - - String property =3D properties.getProperty(prefixedKey + ".min.le= ngth"); - minLength =3D property !=3D null ? Integer.valueOf(property) : (i= sUser ? UsernameValidator.DEFAULT_MIN_LENGTH : 0); - - property =3D properties.getProperty(prefixedKey + ".max.length"); - maxLength =3D property !=3D null ? Integer.valueOf(property) : (i= sUser ? UsernameValidator.DEFAULT_MAX_LENGTH : Integer.MAX_VALUE); - - pattern =3D properties.getProperty(prefixedKey + ".regexp", Utils= .USER_NAME_VALIDATOR_REGEX); - formatMessage =3D properties.getProperty(prefixedKey + ".format.m= essage", pattern); - } - - } -} Copied: portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/va= lidator/UserConfigurableValidator.java (from rev 8347, portal/trunk/webui/c= ore/src/main/java/org/exoplatform/webui/form/validator/UserConfigurableUser= nameValidator.java) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/valida= tor/UserConfigurableValidator.java (rev 0) +++ portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/valida= tor/UserConfigurableValidator.java 2012-02-01 18:56:16 UTC (rev 8348) @@ -0,0 +1,175 @@ +/* + * JBoss, a division of Red Hat + * Copyright 2012, Red Hat Middleware, LLC, and individual + * contributors as indicated by the @authors tag. See the + * copyright.txt in the distribution for a full listing of + * individual contributors. + * + * This 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 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.exoplatform.webui.form.validator; + +import org.exoplatform.commons.serialization.api.annotations.Serialized; +import org.exoplatform.portal.pom.config.Utils; +import org.exoplatform.services.log.ExoLogger; +import org.exoplatform.services.log.Log; +import org.exoplatform.web.application.CompoundApplicationMessage; +import org.exoplatform.webui.form.UIFormInput; + +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 Chris Laprun */ +(a)Serialized +public class UserConfigurableValidator extends MultipleConditionsValidator +{ + protected static Log log =3D ExoLogger.getLogger(UserConfigurableValida= tor.class); + + public static final String USERNAME =3D "username"; + public static final String GROUPMEMBERSHIP =3D "groupmembership"; + public static final String DEFAULT_LOCALIZATION_KEY =3D "ExpressionVali= dator.msg.value-invalid"; + public static final String GROUP_MEMBERSHIP_VALIDATION_REGEX =3D "^\\p{= L}[\\p{L}\\d._\\-\\s*,\\s*]+$"; + public static final String GROUP_MEMBERSHIP_LOCALIZATION_KEY =3D "UIGro= upMembershipForm.msg.Invalid-char"; + + private static Map configurations =3D n= ew HashMap(3); + + public static final String KEY_PREFIX =3D "gatein.validators."; + + static + { + String gateinConfDir =3D System.getProperty("gatein.conf.dir"); + File conf =3D new File(gateinConfDir, "configuration.properties"); + if (conf.exists()) + { + try + { + Properties properties =3D new Properties(); + properties.load(new FileInputStream(conf)); + int length =3D KEY_PREFIX.length(); + for (Object objectKey : properties.keySet()) + { + String key =3D (String)objectKey; + if (key.startsWith(KEY_PREFIX)) + { + // extract property key + String propertyKey =3D key.substring(length, key.indexOf= ('.', length)); + if(!configurations.containsKey(propertyKey)) + { + configurations.put(propertyKey, new ValidatorConfigur= ation(propertyKey, properties)); + } + } + } + } + catch (IOException e) + { + log.info(e.getLocalizedMessage()); + log.debug(e); + } + } + } + + private final String validatorName; + private final String localizationKey; + + // needed by @Serialized + public UserConfigurableValidator() + { + this(USERNAME, DEFAULT_LOCALIZATION_KEY); + } + + public UserConfigurableValidator(String validatorName, String messageLo= calizationKey) + { + this.exceptionOnMissingMandatory =3D true; + this.trimValue =3D true; + localizationKey =3D messageLocalizationKey !=3D null ? messageLocali= zationKey : DEFAULT_LOCALIZATION_KEY; + this.validatorName =3D validatorName !=3D null ? validatorName : USE= RNAME; + } + + public UserConfigurableValidator(String validatorName) + { + this(validatorName, DEFAULT_LOCALIZATION_KEY); + } + + @Override + protected void validate(String value, String label, CompoundApplication= Message messages, UIFormInput uiInput) + { + ValidatorConfiguration configuration =3D configurations.get(validato= rName); + + if (configuration =3D=3D null) + { + // we don't have a user-configured validator for this validator n= ame + + if (USERNAME.equals(validatorName)) + { + // if the validator name is USERNAME constant, we have a usern= ame to validate with the original, non-configured behavior + UsernameValidator.validate(value, label, messages, UsernameVal= idator.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.max= Length.toString()}); + } + + if (!Pattern.matches(configuration.pattern, value)) + { + messages.addMessage(localizationKey, new Object[]{label, confi= guration.formatMessage}); + } + } + } + + private static class ValidatorConfiguration + { + private Integer minLength; + private Integer maxLength; + private String pattern; + private String formatMessage; + + private ValidatorConfiguration(String propertyKey, Properties proper= ties) + { + // used to assign backward compatible default values + boolean isUser =3D USERNAME.equals(propertyKey); + String prefixedKey =3D KEY_PREFIX + propertyKey; + + String property =3D properties.getProperty(prefixedKey + ".min.le= ngth"); + minLength =3D property !=3D null ? Integer.valueOf(property) : (i= sUser ? UsernameValidator.DEFAULT_MIN_LENGTH : 0); + + property =3D properties.getProperty(prefixedKey + ".max.length"); + maxLength =3D property !=3D null ? Integer.valueOf(property) : (i= sUser ? UsernameValidator.DEFAULT_MAX_LENGTH : Integer.MAX_VALUE); + + pattern =3D properties.getProperty(prefixedKey + ".regexp", Utils= .USER_NAME_VALIDATOR_REGEX); + formatMessage =3D properties.getProperty(prefixedKey + ".format.m= essage", pattern); + } + + } +} Modified: portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/= validator/TestWebuiValidator.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/valida= tor/TestWebuiValidator.java 2012-02-01 18:54:13 UTC (rev 8347) +++ portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/valida= tor/TestWebuiValidator.java 2012-02-01 18:56:16 UTC (rev 8348) @@ -31,7 +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.UserConfigurableUsernameValida= tor; +import org.exoplatform.webui.form.validator.UserConfigurableValidator; import org.exoplatform.webui.form.validator.UsernameValidator; import org.exoplatform.webui.form.validator.Validator; = @@ -96,7 +96,7 @@ Validator validator =3D new UsernameValidator(3, 30); validateUsernames(validator); = - validator =3D new UserConfigurableUsernameValidator(UserConfigurable= UsernameValidator.USERNAME); + validator =3D new UserConfigurableValidator(UserConfigurableValidato= r.USERNAME); validateUsernames(validator); } = --===============5287649442531382368==--