001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2012-2015 ForgeRock AS.
025 */
026package org.opends.dsml.protocol;
027
028import java.util.HashMap;
029
030/**
031 * A utility class to help creating ResultCode objects containing a
032 * code value (integer) and a descr value (String).
033 */
034public class ResultCodeFactory
035{
036  static HashMap<Integer,LDAPResultCode> codeToDescr = new HashMap<>();
037  static
038  {
039    codeToDescr.put(0, LDAPResultCode.SUCCESS);
040    codeToDescr.put(1, LDAPResultCode.OPERATIONS_ERROR);
041    codeToDescr.put(2, LDAPResultCode.PROTOCOL_ERROR);
042    codeToDescr.put(3, LDAPResultCode.TIME_LIMIT_EXCEEDED);
043    codeToDescr.put(4, LDAPResultCode.SIZE_LIMIT_EXCEEDED);
044    codeToDescr.put(5, LDAPResultCode.COMPARE_FALSE);
045    codeToDescr.put(6, LDAPResultCode.COMPARE_TRUE);
046    codeToDescr.put(7, LDAPResultCode.AUTH_METHOD_NOT_SUPPORTED);
047    // Note not STRONGER_AUTH_REQUIRED, that's the RFC 4511 name
048    codeToDescr.put(8, LDAPResultCode.STRONG_AUTH_REQUIRED);
049    codeToDescr.put(10, LDAPResultCode.REFERRAL);
050    codeToDescr.put(11, LDAPResultCode.ADMIN_LIMIT_EXCEEDED);
051    codeToDescr.put(12, LDAPResultCode.UNAVAILABLE_CRITICAL_EXTENSION);
052    codeToDescr.put(13, LDAPResultCode.CONFIDENTIALITY_REQUIRED);
053    codeToDescr.put(14, LDAPResultCode.SASL_BIND_IN_PROGRESS);
054    codeToDescr.put(16, LDAPResultCode.NO_SUCH_ATTRIBUTE);
055    codeToDescr.put(17, LDAPResultCode.UNDEFINED_ATTRIBUTE_TYPE);
056    codeToDescr.put(18, LDAPResultCode.INAPPROPRIATE_MATCHING);
057    codeToDescr.put(19, LDAPResultCode.CONSTRAINT_VIOLATION);
058    codeToDescr.put(20, LDAPResultCode.ATTRIBUTE_OR_VALUE_EXISTS);
059    codeToDescr.put(21, LDAPResultCode.INVALID_ATTRIBUTE_SYNTAX);
060    codeToDescr.put(32, LDAPResultCode.NO_SUCH_OBJECT);
061    codeToDescr.put(33, LDAPResultCode.ALIAS_PROBLEM);
062    codeToDescr.put(34, LDAPResultCode.INVALID_DN_SYNTAX);
063    codeToDescr.put(36, LDAPResultCode.ALIAS_DEREFERENCING_PROBLEM);
064    codeToDescr.put(48, LDAPResultCode.INAPPROPRIATE_AUTHENTICATION);
065    codeToDescr.put(49, LDAPResultCode.INVALID_CREDENTIALS);
066    codeToDescr.put(50, LDAPResultCode.INSUFFICIENT_ACCESS_RIGHTS);
067    codeToDescr.put(51, LDAPResultCode.BUSY);
068    codeToDescr.put(52, LDAPResultCode.UNAVAILABLE);
069    codeToDescr.put(53, LDAPResultCode.UNWILLING_TO_PERFORM);
070    codeToDescr.put(54, LDAPResultCode.LOOP_DETECT);
071    codeToDescr.put(64, LDAPResultCode.NAMING_VIOLATION);
072    codeToDescr.put(65, LDAPResultCode.OBJECT_CLASS_VIOLATION);
073    codeToDescr.put(66, LDAPResultCode.NOT_ALLOWED_ON_NON_LEAF);
074    codeToDescr.put(67, LDAPResultCode.NOT_ALLOWED_ON_RDN);
075    codeToDescr.put(68, LDAPResultCode.ENTRY_ALREADY_EXISTS);
076    codeToDescr.put(69, LDAPResultCode.OBJECT_CLASS_MODS_PROHIBITED);
077    // Note not AFFECTS_MULTIPLE_DSAS, xjc mangles the string.
078    codeToDescr.put(71, LDAPResultCode.AFFECT_MULTIPLE_DS_AS);
079    codeToDescr.put(80, LDAPResultCode.OTHER);
080  }
081
082  /**
083   * Create a ResultCode object that contains the resultCode, and, if valid,
084   * a text description (from RFC 2251) of the resultCode.
085   *
086   * @param objFactory
087   *                  The JAXB factory used to create the underlying object.
088   * @param resultCode
089   *                  The LDAP result code.
090   * @return A ResultCode object with a code and possibly a description.
091   */
092  public static ResultCode create(ObjectFactory objFactory, int resultCode)
093  {
094    ResultCode result = objFactory.createResultCode();
095    result.setCode(resultCode);
096    Integer r = Integer.valueOf(resultCode);
097    if (ResultCodeFactory.codeToDescr.containsKey(r))
098    {
099      result.setDescr(ResultCodeFactory.codeToDescr.get(r));
100    }
101    return result;
102  }
103}