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 2014-2015 ForgeRock AS
026 */
027
028package org.opends.server.admin.client;
029
030
031
032import static org.opends.messages.AdminMessages.*;
033
034import java.util.ArrayList;
035import java.util.Collection;
036import java.util.Collections;
037
038import org.forgerock.i18n.LocalizableMessage;
039import org.forgerock.i18n.LocalizableMessageBuilder;
040import org.forgerock.util.Reject;
041
042
043
044/**
045 * This exception is thrown when the client or server refuses to
046 * create, delete, or modify a managed object due to one or more
047 * constraints that cannot be satisfied.
048 * <p>
049 * Operations can be rejected either by a client-side constraint
050 * violation triggered by {@link ClientConstraintHandler}, or by a
051 * server-side error.
052 * <p>
053 * For example, the Directory Server might not be able perform an
054 * operation due to some OS related problem, such as lack of disk
055 * space, or missing files.
056 */
057public class OperationRejectedException extends AdminClientException {
058
059  /**
060   * The type of operation that caused this exception.
061   */
062  public enum OperationType {
063    /**
064     * A managed object could not be created.
065     */
066    CREATE,
067
068    /**
069     * A managed object could not be deleted.
070     */
071    DELETE,
072
073    /**
074     * A managed object could not be modified.
075     */
076    MODIFY;
077  }
078
079  /**
080   * Serialization ID.
081   */
082  private static final long serialVersionUID = 8547688890613079044L;
083
084
085
086  /** Gets the default message. */
087  private static LocalizableMessage getDefaultMessage(Collection<LocalizableMessage> messages) {
088    Reject.ifNull(messages);
089    Reject.ifFalse(!messages.isEmpty());
090
091    if (messages.size() == 1) {
092      return ERR_OPERATION_REJECTED_EXCEPTION_SINGLE.get(messages.iterator()
093          .next());
094    } else {
095      return ERR_OPERATION_REJECTED_EXCEPTION_PLURAL
096          .get(getSingleMessage(messages));
097    }
098  }
099
100
101
102  /** Merge the messages into a single message. */
103  private static LocalizableMessage getSingleMessage(Collection<LocalizableMessage> messages) {
104    if (messages.size() == 1) {
105      return messages.iterator().next();
106    } else {
107      LocalizableMessageBuilder builder = new LocalizableMessageBuilder();
108
109      boolean isFirst = true;
110      for (LocalizableMessage m : messages) {
111        if (!isFirst) {
112          builder.append(";  ");
113        }
114        builder.append(m);
115        isFirst = false;
116      }
117
118      return builder.toMessage();
119    }
120  }
121
122  /** The messages describing the constraint violations that occurred. */
123  private final Collection<LocalizableMessage> messages;
124
125  /** The type of operation that caused this exception. */
126  private final OperationType type;
127
128  /** The user friendly name of the component that caused this exception. */
129  private final LocalizableMessage ufn;
130
131
132
133  /**
134   * Creates a new operation rejected exception with a default
135   * message.
136   *
137   * @param type
138   *          The type of operation that caused this exception.
139   * @param ufn
140   *          The user friendly name of the component that caused this
141   *          exception.
142   */
143  public OperationRejectedException(OperationType type, LocalizableMessage ufn) {
144    this(type, ufn, ERR_OPERATION_REJECTED_DEFAULT.get());
145  }
146
147
148
149  /**
150   * Creates a new operation rejected exception with the provided
151   * messages.
152   *
153   * @param type
154   *          The type of operation that caused this exception.
155   * @param ufn
156   *          The user friendly name of the component that caused this
157   *          exception.
158   * @param messages
159   *          The messages describing the constraint violations that
160   *          occurred (must be non-<code>null</code> and
161   *          non-empty).
162   */
163  public OperationRejectedException(OperationType type, LocalizableMessage ufn,
164      Collection<LocalizableMessage> messages) {
165    super(getDefaultMessage(messages));
166
167    this.messages = new ArrayList<>(messages);
168    this.type = type;
169    this.ufn = ufn;
170  }
171
172
173
174  /**
175   * Creates a new operation rejected exception with the provided
176   * message.
177   *
178   * @param type
179   *          The type of operation that caused this exception.
180   * @param ufn
181   *          The user friendly name of the component that caused this
182   *          exception.
183   * @param message
184   *          The message describing the constraint violation that
185   *          occurred.
186   */
187  public OperationRejectedException(OperationType type, LocalizableMessage ufn,
188      LocalizableMessage message) {
189    this(type, ufn, Collections.singleton(message));
190  }
191
192
193
194  /**
195   * Gets an unmodifiable collection view of the messages describing
196   * the constraint violations that occurred.
197   *
198   * @return Returns an unmodifiable collection view of the messages
199   *         describing the constraint violations that occurred.
200   */
201  public Collection<LocalizableMessage> getMessages() {
202    return Collections.unmodifiableCollection(messages);
203  }
204
205
206
207  /**
208   * Creates a single message listing all the messages combined into a
209   * single list separated by semi-colons.
210   *
211   * @return Returns a single message listing all the messages
212   *         combined into a single list separated by semi-colons.
213   */
214  public LocalizableMessage getMessagesAsSingleMessage() {
215    return getSingleMessage(messages);
216  }
217
218
219
220  /**
221   * Gets the type of operation that caused this exception.
222   *
223   * @return Returns the type of operation that caused this exception.
224   */
225  public OperationType getOperationType() {
226    return type;
227  }
228
229
230
231  /**
232   * Gets the user friendly name of the component that caused this
233   * exception.
234   *
235   * @return Returns the user friendly name of the component that
236   *         caused this exception.
237   */
238  public LocalizableMessage getUserFriendlyName() {
239    return ufn;
240  }
241
242}