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 * Portions Copyright 2015 ForgeRock AS.
028 */
029
030package com.iplanet.ums.validation;
031
032import java.util.Enumeration;
033
034import com.iplanet.services.ldap.Attr;
035import com.iplanet.services.ldap.AttrSet;
036import com.iplanet.services.ldap.ModSet;
037import com.iplanet.services.util.I18n;
038import com.iplanet.ums.CreationTemplate;
039import com.iplanet.ums.Guid;
040import com.iplanet.ums.IUMSConstants;
041import com.iplanet.ums.TemplateManager;
042import com.iplanet.ums.UMSException;
043import org.forgerock.opendj.ldap.Modification;
044import org.forgerock.opendj.ldap.ModificationType;
045
046/**
047 * Validation handles all validation routines. This class is constructed using
048 * default constructor (no argument constructor) and validateAttribute function
049 * is used to validate set of attributes against optional rules passed and
050 * validator specific rules. UMSException is throw if there is instanciation
051 * problems DataConstraintException is thrown when the attributes fail to
052 * validations.
053 *
054 * @supported.all.api
055 */
056public class Validation {
057
058    /**
059     * Determines whether a specific attribute is valid. Called by
060     * validateAttribute(Attr, Class). This method calls the validation method
061     * for this attribute.
062     * 
063     * @param attr
064     *            attribute to test
065     * @param validatorClass
066     *            the validator class name
067     * @param rule
068     *            optional rule applies to the validator
069     * @exception UMSException
070     *                failure
071     * @exception DataConstraintException
072     *                data validation failure
073     */
074    public static void validateAttribute(Attr attr, String validatorClass,
075            String rule) throws UMSException, DataConstraintException {
076
077        if (attr != null) {
078            String[] values = attr.getStringValues();
079            for (int i = 0; i < values.length; i++) {
080                String aValue = values[i];
081
082                if ((aValue != null) && (!aValue.equalsIgnoreCase(""))
083                        && (validatorClass != null)) {
084
085                    IValidator validator = null;
086                    try {
087                        Class theClass = Class.forName(validatorClass);
088                        validator = (IValidator) theClass.newInstance();
089                    } catch (Exception e) {
090                        throw new UMSException(i18n
091                                .getString(IUMSConstants.INSTANCE_FAILED), e);
092                    }
093
094                    if (!validator.validate(aValue, rule)) {
095                        String msg = i18n
096                                .getString(IUMSConstants.DATA_CONSTRAINT);
097                        throw new DataConstraintException(msg + ": "
098                                + "{ type=" + attr.getName() + ", value="
099                                + aValue + " }");
100                    }
101                }
102            }
103        }
104    }
105
106    /**
107     * Determines whether attribute is valid. Check the attribute if there is a
108     * validation method that needs to execute.
109     * 
110     * @param attr
111     *            attribute to test
112     * @param cls
113     *            Class associatd with this attribute
114     * @param guid
115     *            the guid of the Organization where the config data is stored
116     * @exception UMSException
117     *                failure
118     * @exception DataConstraintException
119     *                data validation failure
120     */
121    public static void validateAttribute(Attr attr, Class cls, Guid guid)
122            throws UMSException, DataConstraintException {
123
124        if (attr == null) {
125            return;
126        }
127
128        String validatorClass = null;
129        String rule = null;
130        String attrName = attr.getName();
131
132        // Gets the Template associates with the Class
133        CreationTemplate ct = TemplateManager.getTemplateManager()
134                .getCreationTemplate(cls, guid);
135        if (ct != null) {
136            // Gets an enumeration of ValidationElements of this attriubte
137            Enumeration en = ct.getValidation(attrName);
138
139            while (en.hasMoreElements()) {
140                ValidationElement vElement = (ValidationElement) en
141                        .nextElement();
142                validatorClass = vElement.getValidator();
143                rule = vElement.getRule();
144                if (validatorClass != null) {
145                    validateAttribute(attr, validatorClass, rule);
146
147                }
148            }
149        }
150    }
151
152    /**
153     * Determines whether each attribute in the attribute set is valid. Iterates
154     * though the set checking each element to see if there is a validation
155     * method that needs to execute.
156     * 
157     * @param attrSet
158     *            attribute set to test
159     * @param cls
160     *            Class associated with these attribute set
161     * @param guid
162     *            the guid of the Organization where the config data is stored
163     * @exception UMSException
164     *                failure
165     * @exception DataConstraintException
166     *                data validation failure
167     */
168    public static void validateAttributes(AttrSet attrSet, Class cls, Guid guid)
169            throws UMSException, DataConstraintException {
170
171        if (attrSet == null) {
172            return;
173        }
174
175        String[] attrNames = attrSet.getAttributeNames();
176        for (int i = 0; i < attrNames.length; i++) {
177            Attr attr = attrSet.getAttribute(attrNames[i]);
178            validateAttribute(attr, cls, guid);
179        }
180    }
181
182    /**
183     * Determines whether each attribute in the ModSet is valid.
184     * 
185     * @param modSet
186     *            modSet to test
187     * @param cls
188     *            Class associated with these attributes
189     * @param guid
190     *            the guid of the Organization where the config data is stored
191     * @exception UMSException
192     *                failure
193     * @exception DataConstraintException
194     *                data validation failure
195     */
196    public static void validateAttributes(ModSet modSet, Class cls, Guid guid)
197            throws UMSException, DataConstraintException {
198
199        if (modSet == null) {
200            return;
201        }
202
203        for (int i = 0; i < modSet.size(); i++) {
204            Modification ldapMod = modSet.elementAt(i);
205            if (!ModificationType.DELETE.equals(ldapMod.getModificationType())) {
206                Attr attr = new Attr(ldapMod.getAttribute());
207                validateAttribute(attr, cls, guid);
208            }
209        }
210    }
211
212    private static I18n i18n = I18n.getInstance(IUMSConstants.UMS_PKG);
213}