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 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027package org.forgerock.opendj.config.client;
028
029import static com.forgerock.opendj.ldap.config.AdminMessages.*;
030
031import org.forgerock.i18n.LocalizableMessage;
032import org.forgerock.opendj.config.PropertyException;
033import org.forgerock.opendj.config.OperationsException;
034import org.forgerock.opendj.config.PropertyDefinition;
035import org.forgerock.opendj.config.PropertyDefinitionUsageBuilder;
036
037/**
038 * Thrown when an attempt is made to create a new managed object with an illegal
039 * name.
040 * <p>
041 * This exception can occur when a new managed object is given a name which is
042 * either an empty string, a string containing just white-spaces, or a string
043 * which is invalid according to the managed object's naming property (if it has
044 * one).
045 */
046public class IllegalManagedObjectNameException extends OperationsException {
047
048    /**
049     * Serialization ID.
050     */
051    private static final long serialVersionUID = 7491748228684293291L;
052
053    /** Create the message. */
054    private static LocalizableMessage createMessage(String illegalName,
055            PropertyDefinition<?> namingPropertyDefinition) {
056        if (illegalName.length() == 0) {
057            return ERR_ILLEGAL_MANAGED_OBJECT_NAME_EXCEPTION_EMPTY.get();
058        } else if (illegalName.trim().length() == 0) {
059            return ERR_ILLEGAL_MANAGED_OBJECT_NAME_EXCEPTION_BLANK.get();
060        } else if (namingPropertyDefinition != null) {
061            try {
062                namingPropertyDefinition.decodeValue(illegalName);
063            } catch (PropertyException e) {
064                PropertyDefinitionUsageBuilder builder = new PropertyDefinitionUsageBuilder(true);
065                return ERR_ILLEGAL_MANAGED_OBJECT_NAME_EXCEPTION_SYNTAX.get(illegalName,
066                        namingPropertyDefinition.getName(), builder
067                                .getUsage(namingPropertyDefinition));
068            }
069        }
070
071        return ERR_ILLEGAL_MANAGED_OBJECT_NAME_EXCEPTION_OTHER.get(illegalName);
072    }
073
074    /** The illegal name. */
075    private final String illegalName;
076
077    /** The naming property definition if applicable. */
078    private final PropertyDefinition<?> namingPropertyDefinition;
079
080    /**
081     * Create a new illegal name exception and no naming property definition.
082     *
083     * @param illegalName
084     *            The illegal managed object name.
085     */
086    public IllegalManagedObjectNameException(String illegalName) {
087        this(illegalName, null);
088    }
089
090    /**
091     * Create a new illegal name exception and a naming property definition.
092     *
093     * @param illegalName
094     *            The illegal managed object name.
095     * @param namingPropertyDefinition
096     *            The naming property definition.
097     */
098    public IllegalManagedObjectNameException(String illegalName, PropertyDefinition<?> namingPropertyDefinition) {
099        super(createMessage(illegalName, namingPropertyDefinition));
100
101        this.illegalName = illegalName;
102        this.namingPropertyDefinition = namingPropertyDefinition;
103    }
104
105    /**
106     * Get the illegal managed object name.
107     *
108     * @return Returns the illegal managed object name.
109     */
110    public String getIllegalName() {
111        return illegalName;
112    }
113
114    /**
115     * Get the naming property definition if applicable.
116     *
117     * @return Returns naming property definition, or <code>null</code> if none
118     *         was specified.
119     */
120    public PropertyDefinition<?> getNamingPropertyDefinition() {
121        return namingPropertyDefinition;
122    }
123
124}