8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2008 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: GlobalMapValueValidator.java,v 1.4 2008/07/10 22:03:17 veiming Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna/**
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna * Portions Copyrighted 2016 ForgeRock AS.
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.common.configuration;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ServiceAttributeValidator;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.regex.Matcher;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.regex.Pattern;
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Lunaimport org.forgerock.openam.utils.CollectionUtils;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Validates Global Map property value in Agent Properties.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * These "Global" Map properties are just like Map properties,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * except they ALSO allow you to specify one global value as the default for
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the property, so when a key is not specified so mapping exists, then
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * this global value is used.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * e.g.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>com.sun.identity.agents.config.someglobalmapprop=somevalue</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Note, can have only *one* global value:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * -where value has no brackets or context root key and is just a value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * -also allow a global value format: "=SomeValue" etc, which
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * includes an equal sign since in property file style an entry like
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * com.sun.someprop=ALL would have a value "=SomeValue"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * -can not have a blank r.h.s like "= "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * -can not have space inside characters of r.h.s, like "= some space"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This validator accepts all the values accepted by MapValueValidator and
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna * additionally accepts global format style values.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the MapValueValidator.java class for non global map
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * regular expressions and examples of acceptable values.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class GlobalMapValueValidator implements ServiceAttributeValidator {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //global format is no whitespace in between characters
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // and no brackets allowed
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna private static final String globalRegularExpression = "(\\s*[\\S&&[^\\[]&&[^\\]]]+\\s*)";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String appSpecificRegularExpression =
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna MapValueValidator.KEY_WITH_NO_BRACKETS + "|" + MapValueValidator.DEFAULT_NO_KEY_JUST_BRACKETS;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna private static final String regularExpression =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "(" + appSpecificRegularExpression + "|" + globalRegularExpression + ")";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final Pattern pattern = Pattern.compile(regularExpression);
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna private static final Pattern globalPattern = Pattern.compile(globalRegularExpression);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns <code>true</code> if values are of global map format.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param values the set of values to be validated
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return <code>true</code> if values are of global map format.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna public boolean validate(Set<String> values) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean valid = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean globalFound = false; //can only have zero or one global value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna if (!CollectionUtils.isEmpty(values)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna for (String val : values) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna if (!valid) {
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna break;
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna }
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna String trimmed = val.trim();
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna Matcher matcher = pattern.matcher(trimmed);
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna valid = matcher.matches();
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna //now test for duplicate global value
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna Matcher globalMatcher = globalPattern.matcher(trimmed);
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna boolean globalMatch = globalMatcher.matches();
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna //if value matches global and previously found one too
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna if (globalFound && globalMatch && valid) {
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna valid = false; //more than one global value so invalid
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna } else if (globalMatch && valid) {
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna globalFound = true; //found first global
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna valid = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna if (valid) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster valid = MapDuplicateKeyChecker.checkForNoDuplicateKeyInValue(values);
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna }
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return valid;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}