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 2009 Sun Microsystems, Inc.
015 * Portions Copyright 2016 ForgeRock AS.
016 */
017
018package org.forgerock.opendj.ldap;
019
020import java.io.IOException;
021
022import org.forgerock.i18n.LocalizableException;
023import org.forgerock.i18n.LocalizableMessage;
024
025/**
026 * Thrown when data from an input source cannot be decoded, perhaps due to the
027 * data being malformed in some way. By default decoding exceptions are fatal,
028 * indicating that the associated input source is no longer usable.
029 */
030@SuppressWarnings("serial")
031public final class DecodeException extends IOException implements LocalizableException {
032    /**
033     * Creates a new non-fatal decode exception with the provided message.
034     *
035     * @param message
036     *            The message that explains the problem that occurred.
037     * @return The new non-fatal decode exception.
038     */
039    public static DecodeException error(final LocalizableMessage message) {
040        return new DecodeException(message, false, null);
041    }
042
043    /**
044     * Creates a new non-fatal decode exception with the provided message and
045     * root cause.
046     *
047     * @param message
048     *            The message that explains the problem that occurred.
049     * @param cause
050     *            The underlying cause of this exception.
051     * @return The new non-fatal decode exception.
052     */
053    public static DecodeException error(final LocalizableMessage message, final Throwable cause) {
054        return new DecodeException(message, false, cause);
055    }
056
057    /**
058     * Creates a new fatal decode exception with the provided message. The
059     * associated input source can no longer be used.
060     *
061     * @param message
062     *            The message that explains the problem that occurred.
063     * @return The new fatal decode exception.
064     */
065    public static DecodeException fatalError(final LocalizableMessage message) {
066        return new DecodeException(message, true, null);
067    }
068
069    /**
070     * Creates a new fatal decode exception with the provided message and root
071     * cause. The associated input source can no longer be used.
072     *
073     * @param message
074     *            The message that explains the problem that occurred.
075     * @param cause
076     *            The underlying cause of this exception.
077     * @return The new fatal decode exception.
078     */
079    public static DecodeException fatalError(final LocalizableMessage message, final Throwable cause) {
080        return new DecodeException(message, true, cause);
081    }
082
083    private final LocalizableMessage message;
084
085    private final boolean isFatal;
086
087    /** Construction is provided via factory methods. */
088    private DecodeException(final LocalizableMessage message, final boolean isFatal,
089            final Throwable cause) {
090        super(message.toString(), cause);
091        this.message = message;
092        this.isFatal = isFatal;
093    }
094
095    @Override
096    public LocalizableMessage getMessageObject() {
097        return message;
098    }
099
100    /**
101     * Indicates whether the error was fatal and the associated input
102     * source can no longer be used.
103     *
104     * @return {@code true} if the error was fatal and the associated input
105     *         source can no longer be used, otherwise {@code false} .
106     */
107    public boolean isFatal() {
108        return isFatal;
109    }
110}