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 2015 ForgeRock AS.
015 */
016
017package org.forgerock.audit.util;
018
019import org.forgerock.json.JsonValueException;
020import org.forgerock.json.resource.NotSupportedException;
021import org.forgerock.json.resource.Request;
022import org.forgerock.json.resource.ResourceException;
023
024/**
025 * Utility class to use on ResourceExceptions.
026 */
027public final class ResourceExceptionsUtil {
028
029    private ResourceExceptionsUtil() { }
030    /**
031     * Adapts a {@code Throwable} to a {@code ResourceException}. If the
032     * {@code Throwable} is an JSON {@code JsonValueException} then an
033     * appropriate {@code ResourceException} is returned, otherwise an
034     * {@code InternalServerErrorException} is returned.
035     *
036     * @param t
037     *            The {@code Throwable} to be converted.
038     * @return The equivalent resource exception.
039     */
040    public static ResourceException adapt(final Throwable t) {
041        int resourceResultCode;
042        try {
043            throw t;
044        } catch (final ResourceException e) {
045            return e;
046        } catch (final JsonValueException e) {
047            resourceResultCode = ResourceException.BAD_REQUEST;
048        } catch (final Throwable tmp) {
049            resourceResultCode = ResourceException.INTERNAL_ERROR;
050        }
051        return ResourceException.getException(resourceResultCode, t.getMessage(), t);
052    }
053
054    /**
055     * Creates a NotSupportedException.
056     * @param request the crest request
057     * @return a NotSupportedException
058     */
059    public static ResourceException notSupported(final Request request) {
060        return new NotSupportedException(request.getRequestType().name() + " operations are not supported");
061    }
062
063    /**
064     * Creates a NotSupportedException.
065     * @param request the crest request
066     * @return a NotSupportedException
067     */
068    public static ResourceException notSupportedOnCollection(final Request request) {
069        return new NotSupportedException(request.getRequestType().name() + " operations are not supported");
070    }
071
072    /**
073     * Creates a NotSupportedException.
074     * @param request the crest request
075     * @return a NotSupportedException
076     */
077    public static ResourceException notSupportedOnInstance(final Request request) {
078        return new NotSupportedException(request.getRequestType().name() + " operations are not supported");
079    }
080}