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