001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2016 ForgeRock AS.
015 */
016package org.opends.server.util;
017
018import static org.opends.server.schema.SchemaConstants.SYNTAX_AUTH_PASSWORD_OID;
019import static org.opends.server.schema.SchemaConstants.SYNTAX_USER_PASSWORD_OID;
020
021import java.util.Collection;
022import java.util.HashSet;
023import java.util.Set;
024
025import org.forgerock.opendj.ldap.schema.AttributeType;
026import org.forgerock.opendj.ldap.schema.ObjectClass;
027
028/** Utility methods related to schema. */
029public class SchemaUtils
030{
031
032  /** Private constructor to prevent instantiation. */
033  private SchemaUtils() {
034    // No implementation required.
035  }
036
037  /** Represents a password type, including a "not a password" value. */
038  public enum PasswordType
039  {
040    /** Auth Password. */
041    AUTH_PASSWORD,
042    /** User Password. */
043    USER_PASSWORD,
044    /** Not a password. */
045    NOT_A_PASSWORD
046  }
047
048  /**
049   * Checks if the provided attribute type contains a password.
050   *
051   * @param attrType
052   *            The attribute type to check.
053   * @return a PasswordTypeCheck result
054   */
055  public static PasswordType checkPasswordType(AttributeType attrType)
056  {
057    final String syntaxOID = attrType.getSyntax().getOID();
058    if (syntaxOID.equals(SYNTAX_AUTH_PASSWORD_OID))
059    {
060      return PasswordType.AUTH_PASSWORD;
061    }
062    else if (attrType.hasName("userPassword") || syntaxOID.equals(SYNTAX_USER_PASSWORD_OID))
063    {
064      return PasswordType.USER_PASSWORD;
065    }
066    return PasswordType.NOT_A_PASSWORD;
067  }
068
069  /**
070   * Returns a new collection with the result of calling {@link ObjectClass#getNameOrOID()} on each
071   * element of the provided collection.
072   *
073   * @param objectClasses
074   *          the schema elements on which to act
075   * @return a new collection comprised of the names or OIDs of each element
076   */
077  public static Collection<String> getNameOrOIDsForOCs(Collection<ObjectClass> objectClasses)
078  {
079    Set<String> results = new HashSet<>(objectClasses.size());
080    for (ObjectClass objectClass : objectClasses)
081    {
082      results.add(objectClass.getNameOrOID());
083    }
084    return results;
085  }
086
087  /**
088   * Returns a new collection with the result of calling {@link AttributeType#getNameOrOID()} on
089   * each element of the provided collection.
090   *
091   * @param attributeTypes
092   *          the schema elements on which to act
093   * @return a new collection comprised of the names or OIDs of each element
094   */
095  public static Collection<String> getNameOrOIDsForATs(Collection<AttributeType> attributeTypes)
096  {
097    Set<String> results = new HashSet<>(attributeTypes.size());
098    for (AttributeType attrType : attributeTypes)
099    {
100      results.add(attrType.getNameOrOID());
101    }
102    return results;
103  }
104}