com.neocotic.bloggaer.account.MissingCapabilityException.java Source code

Java tutorial

Introduction

Here is the source code for com.neocotic.bloggaer.account.MissingCapabilityException.java

Source

/* Copyright (C) 2012 Alasdair Mercer, http://neocotic.com/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.neocotic.bloggaer.account;

import java.util.Arrays;
import java.util.Collection;

import org.apache.commons.lang3.StringUtils;

import com.neocotic.bloggaer.common.exception.BloggaerRuntimeException;
import com.neocotic.bloggaer.common.soa.Service;

/**
 * {@code MissingCapabilityException} is an implementation of {@link BloggaerRuntimeException} that is thrown when the
 * user attempts to perform a {@link Service} operation without having the necessary {@link Capability Capabilities} in
 * their {@link Role}.
 * 
 * @author Alasdair Mercer
 */
// TODO: Use localised error messages
public class MissingCapabilityException extends BloggaerRuntimeException {

    /**
     * Creates a suitable error message for the {@code capabilities} provided.
     * 
     * @param capabilities
     *            the {@link Collection} missing {@link Capability Capabilities}, may be {@code null}
     * @return The error message.
     */
    private static String createMessage(Collection<Capability> capabilities) {
        StringBuilder buffer = new StringBuilder("Account is missing required capabilities");
        if (capabilities != null) {
            buffer.append(": ");
            buffer.append(StringUtils.join(capabilities, ", "));
        }
        return buffer.toString();
    }

    /**
     * Creates a new {@link MissingCapabilityException} with a suitable error message.
     */
    public MissingCapabilityException() {
        super(createMessage(null));
    }

    /**
     * Creates a new {@link MissingCapabilityException} with a suitable error message based on the {@code capabilities}
     * provided.
     * 
     * @param capabilities
     *            the missing {@link Capability Capabilities}
     */
    public MissingCapabilityException(Capability... capabilities) {
        super(createMessage(Arrays.asList(capabilities)));
    }

    /**
     * Creates a new {@link MissingCapabilityException} with a suitable error message based on the {@code capabilities}
     * provided.
     * 
     * @param capabilities
     *            the {@link Collection} of missing {@link Capability Capabilities}
     */
    public MissingCapabilityException(Collection<Capability> capabilities) {
        super(createMessage(capabilities));
    }
}