001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: Validation.java,v 1.4 2009/01/28 05:34:52 ww203982 Exp $
026 *
027 */
028
029package com.iplanet.ums.validation;
030
031import java.util.Enumeration;
032
033import com.sun.identity.shared.ldap.LDAPModification;
034
035import com.iplanet.services.ldap.Attr;
036import com.iplanet.services.ldap.AttrSet;
037import com.iplanet.services.ldap.ModSet;
038import com.iplanet.services.util.I18n;
039import com.iplanet.ums.CreationTemplate;
040import com.iplanet.ums.Guid;
041import com.iplanet.ums.IUMSConstants;
042import com.iplanet.ums.TemplateManager;
043import com.iplanet.ums.UMSException;
044
045/**
046 * Validation handles all validation routines. This class is constructed using
047 * default constructor (no argument constructor) and validateAttribute function
048 * is used to validate set of attributes against optional rules passed and
049 * validator specific rules. UMSException is throw if there is instanciation
050 * problems DataConstraintException is thrown when the attributes fail to
051 * validations.
052 *
053 * @supported.all.api
054 */
055public class Validation {
056
057    /**
058     * Determines whether a specific attribute is valid. Called by
059     * validateAttribute(Attr, Class). This method calls the validation method
060     * for this attribute.
061     * 
062     * @param attr
063     *            attribute to test
064     * @param validatorClass
065     *            the validator class name
066     * @param rule
067     *            optional rule applies to the validator
068     * @exception UMSException
069     *                failure
070     * @exception DataConstraintException
071     *                data validation failure
072     */
073    public static void validateAttribute(Attr attr, String validatorClass,
074            String rule) throws UMSException, DataConstraintException {
075
076        if (attr != null) {
077            String[] values = attr.getStringValues();
078            for (int i = 0; i < values.length; i++) {
079                String aValue = values[i];
080
081                if ((aValue != null) && (!aValue.equalsIgnoreCase(""))
082                        && (validatorClass != null)) {
083
084                    IValidator validator = null;
085                    try {
086                        Class theClass = Class.forName(validatorClass);
087                        validator = (IValidator) theClass.newInstance();
088                    } catch (Exception e) {
089                        throw new UMSException(i18n
090                                .getString(IUMSConstants.INSTANCE_FAILED), e);
091                    }
092
093                    if (!validator.validate(aValue, rule)) {
094                        String msg = i18n
095                                .getString(IUMSConstants.DATA_CONSTRAINT);
096                        throw new DataConstraintException(msg + ": "
097                                + "{ type=" + attr.getName() + ", value="
098                                + aValue + " }");
099                    }
100                }
101            }
102        }
103    }
104
105    /**
106     * Determines whether attribute is valid. Check the attribute if there is a
107     * validation method that needs to execute.
108     * 
109     * @param attr
110     *            attribute to test
111     * @param cls
112     *            Class associatd with this attribute
113     * @param guid
114     *            the guid of the Organization where the config data is stored
115     * @exception UMSException
116     *                failure
117     * @exception DataConstraintException
118     *                data validation failure
119     */
120    public static void validateAttribute(Attr attr, Class cls, Guid guid)
121            throws UMSException, DataConstraintException {
122
123        if (attr == null) {
124            return;
125        }
126
127        String validatorClass = null;
128        String rule = null;
129        String attrName = attr.getName();
130
131        // Gets the Template associates with the Class
132        CreationTemplate ct = TemplateManager.getTemplateManager()
133                .getCreationTemplate(cls, guid);
134        if (ct != null) {
135            // Gets an enumeration of ValidationElements of this attriubte
136            Enumeration en = ct.getValidation(attrName);
137
138            while (en.hasMoreElements()) {
139                ValidationElement vElement = (ValidationElement) en
140                        .nextElement();
141                validatorClass = vElement.getValidator();
142                rule = vElement.getRule();
143                if (validatorClass != null) {
144                    validateAttribute(attr, validatorClass, rule);
145
146                }
147            }
148        }
149    }
150
151    /**
152     * Determines whether each attribute in the attribute set is valid. Iterates
153     * though the set checking each element to see if there is a validation
154     * method that needs to execute.
155     * 
156     * @param attrSet
157     *            attribute set to test
158     * @param cls
159     *            Class associated with these attribute set
160     * @param guid
161     *            the guid of the Organization where the config data is stored
162     * @exception UMSException
163     *                failure
164     * @exception DataConstraintException
165     *                data validation failure
166     */
167    public static void validateAttributes(AttrSet attrSet, Class cls, Guid guid)
168            throws UMSException, DataConstraintException {
169
170        if (attrSet == null) {
171            return;
172        }
173
174        String[] attrNames = attrSet.getAttributeNames();
175        for (int i = 0; i < attrNames.length; i++) {
176            Attr attr = attrSet.getAttribute(attrNames[i]);
177            validateAttribute(attr, cls, guid);
178        }
179    }
180
181    /**
182     * Determines whether each attribute in the ModSet is valid.
183     * 
184     * @param modSet
185     *            modSet to test
186     * @param cls
187     *            Class associated with these attributes
188     * @param guid
189     *            the guid of the Organization where the config data is stored
190     * @exception UMSException
191     *                failure
192     * @exception DataConstraintException
193     *                data validation failure
194     */
195    public static void validateAttributes(ModSet modSet, Class cls, Guid guid)
196            throws UMSException, DataConstraintException {
197
198        if (modSet == null) {
199            return;
200        }
201
202        for (int i = 0; i < modSet.size(); i++) {
203            LDAPModification ldapMod = modSet.elementAt(i);
204            if (ldapMod.getOp() != LDAPModification.DELETE) {
205                Attr attr = new Attr(ldapMod.getAttribute());
206                validateAttribute(attr, cls, guid);
207            }
208        }
209    }
210
211    private static I18n i18n = I18n.getInstance(IUMSConstants.UMS_PKG);
212}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.