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: MapValueValidator.java,v 1.3 2008/07/03 09:39:13 veiming Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna/**
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna * Portions Copyrighted 2011-2016 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
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 map value properties in Agent Properties. e.g.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>com.sun.identity.agents.config.response.attribute.mapping[]=</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Map values should be Strings of the form:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * [somekey] = somevalue
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * etc
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * l.h.s is key value surrounded by brackets
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * separator is mandatory "=" equals sign
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * r.h.s is some string value, anything since this is not so fined-
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * grained to test values of any one specific property's value set.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * r.h.s can be empty or just whitespace
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * l.h.s key value can NOT be empty
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * l.h.s key values can not be duplicates
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * white space is allowed everywhere
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * blank or empty values are allowed as some props dont have any value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * to be specified
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Some strange examples that would be acceptable values:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * blank or empty set
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * [a_key_but_no_value]= (note no value on r.h.s)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * []= (a common default value)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * [ ] = (variation on default value)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * [key_and_or_value_contains_=_sign] == (equal sign is valid data)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Note:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * brackets are not allowed as part of key or value data
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * exact duplicate keys not allowed
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class MapValueValidator implements ServiceAttributeValidator {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * 1) [[\\S]&&[^\\[]&&[^\\]]]+ means at least one non-whitespace
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * character except not a [ or ]
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * 2) [[^\\[]&&[^\\]]]* means zero or more of anything
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * except not a [ or ]
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * 1 & 2 are combined to match values that have a key
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * 3) \\s*\\[\\s*\\]\\s*=\\s* means the case of []= which is used for
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * many default map values
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //also used by GlobalMapValueValidator so package scoped
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna static final String KEY_WITH_NO_BRACKETS = "(\\s*\\[\\s*[[\\S]&&[^\\[]&&[^\\]]]+[[^\\[]&&[^\\]]]*\\s*\\]\\s*=.*)";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //also used by GlobalMapValueValidator so package scoped
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna static final String DEFAULT_NO_KEY_JUST_BRACKETS = "(\\s*\\[\\s*\\]\\s*=\\s*)";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna private static final String regularExpression = KEY_WITH_NO_BRACKETS + "|" + DEFAULT_NO_KEY_JUST_BRACKETS;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final Pattern pattern = Pattern.compile(regularExpression);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns <code>true</code> if values are of map type format.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param values contains the set of values to be validated
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return <code>true</code> if values are of map type format.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna public boolean validate(Set<String> values) {
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna boolean valid = true; //blank or empty values set are valid
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna if (!CollectionUtils.isEmpty(values)) {
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna for (String value : values) {
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna if (!valid) {
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna break;
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna }
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna if (value.length() > 0) {
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna Matcher m = pattern.matcher(value);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster valid = m.matches();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna if (valid) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster valid = MapDuplicateKeyChecker.checkForNoDuplicateKeyInValue(values);
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna }
f2b7e2373c5d1aacf05b91ab8e316d6aea0b948bDavid Luna
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return valid;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}