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    /**
039     * Serialization ID.
040     */
041    private static final long serialVersionUID = 7491748228684293291L;
042
043    /** Create the message. */
044    private static LocalizableMessage createMessage(String illegalName,
045            PropertyDefinition<?> namingPropertyDefinition) {
046        if (illegalName.length() == 0) {
047            return ERR_ILLEGAL_MANAGED_OBJECT_NAME_EXCEPTION_EMPTY.get();
048        } else if (illegalName.trim().length() == 0) {
049            return ERR_ILLEGAL_MANAGED_OBJECT_NAME_EXCEPTION_BLANK.get();
050        } else if (namingPropertyDefinition != null) {
051            try {
052                namingPropertyDefinition.decodeValue(illegalName);
053            } catch (PropertyException e) {
054                PropertyDefinitionUsageBuilder builder = new PropertyDefinitionUsageBuilder(true);
055                return ERR_ILLEGAL_MANAGED_OBJECT_NAME_EXCEPTION_SYNTAX.get(illegalName,
056                        namingPropertyDefinition.getName(), builder
057                                .getUsage(namingPropertyDefinition));
058            }
059        }
060
061        return ERR_ILLEGAL_MANAGED_OBJECT_NAME_EXCEPTION_OTHER.get(illegalName);
062    }
063
064    /** The illegal name. */
065    private final String illegalName;
066
067    /** The naming property definition if applicable. */
068    private final PropertyDefinition<?> namingPropertyDefinition;
069
070    /**
071     * Create a new illegal name exception and no naming property definition.
072     *
073     * @param illegalName
074     *            The illegal managed object name.
075     */
076    public IllegalManagedObjectNameException(String illegalName) {
077        this(illegalName, null);
078    }
079
080    /**
081     * Create a new illegal name exception and a naming property definition.
082     *
083     * @param illegalName
084     *            The illegal managed object name.
085     * @param namingPropertyDefinition
086     *            The naming property definition.
087     */
088    public IllegalManagedObjectNameException(String illegalName, PropertyDefinition<?> namingPropertyDefinition) {
089        super(createMessage(illegalName, namingPropertyDefinition));
090
091        this.illegalName = illegalName;
092        this.namingPropertyDefinition = namingPropertyDefinition;
093    }
094
095    /**
096     * Get the illegal managed object name.
097     *
098     * @return Returns the illegal managed object name.
099     */
100    public String getIllegalName() {
101        return illegalName;
102    }
103
104    /**
105     * Get the naming property definition if applicable.
106     *
107     * @return Returns naming property definition, or <code>null</code> if none
108     *         was specified.
109     */
110    public PropertyDefinition<?> getNamingPropertyDefinition() {
111        return namingPropertyDefinition;
112    }
113
114}