All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mycore.access.MCRAccessException Maven / Gradle / Ivy

There is a newer version: 2024.05
Show newest version
/*
 * This file is part of ***  M y C o R e  ***
 * See http://www.mycore.de/ for details.
 *
 * MyCoRe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MyCoRe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MyCoRe.  If not, see .
 */

package org.mycore.access;

import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.mycore.common.MCRCatchException;

public class MCRAccessException extends MCRCatchException {

    private static final long serialVersionUID = 6494399676882465653L;

    private Optional action;

    private Optional id;

    private Optional permission;

    public static MCRAccessException missingPrivilege(String action, String... privilege) {
        return new MCRAccessException(Optional.ofNullable(action), null, null, privilege);
    }

    public static MCRAccessException missingPermission(String action, String id, String permission) {
        return new MCRAccessException(Optional.ofNullable(action), id, permission);
    }

    private MCRAccessException(Optional action, String id, String permission, String... privilege) {
        super(getMessage(action, id, permission, privilege));
        this.action = action;
        this.id = Optional.ofNullable(id);
        this.permission = Optional.ofNullable(permission);
    }

    private static String getMessage(Optional action, String oid, String permission, String... privilege) {
        StringBuilder sb = new StringBuilder();
        switch (privilege.length) {
            case 0:
                //no privilige but permission was missing
                sb.append("You do not have the permission '").append(permission).append("' on '").append(oid)
                    .append('\'');
                break;
            case 1:
                sb.append("You do not have the privilege '").append(privilege[0]).append('\'');
                break;
            default:
                sb.append(
                    Stream.of(privilege).collect(
                        Collectors.joining("', '", "You do not have any of the required privileges ( '", ")")));
                break;
        }
        sb.append(
            action.map(s -> " to perfom: " + s).orElse("."));
        return sb.toString();
    }

    public Optional getAction() {
        return action;
    }

    public Optional getId() {
        return id;
    }

    public Optional getPermission() {
        return permission;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy