Project

General

Profile

Download (2.04 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.persistence.permission;
2

    
3
import org.springframework.security.access.AccessDeniedException;
4

    
5
import eu.etaxonomy.cdm.database.PermissionDeniedException;
6

    
7
/**
8
 * This utility class helps finding security related exceptions in Throwables.
9
 *
10
 * Note: This class is more or less a clone of the same named class in cdmlib-test.
11
 * Any changes in either of these classes should be also done in the other class.
12
 * In future theses classes should be merged.
13
 *
14
 * @author a.kohlbecker
15
 * @author a.mueller
16
 * @since Jun 05, 2020
17
 */
18
public class SecurityExceptionUtils {
19

    
20
   //this is to decouple SecurityExceptionUtils from persistence and spring-security
21
    public static Class<?> permissionDeniedExceptionClass = PermissionDeniedException.class;
22
    public static Class<?> accessDeniedException = AccessDeniedException.class;
23

    
24
    /**
25
     * Finds a nested RuntimeExceptions of the types {@link PermissionDeniedException}, {@link AccessDeniedException}
26
     * or returns null.
27
     * @param exception
28
     * @return
29
     */
30
    public static RuntimeException findSecurityRuntimeException(Throwable exception) {
31
        if( permissionDeniedExceptionClass.isInstance(exception) || accessDeniedException.isInstance(exception) ){
32
            return (RuntimeException) exception;
33
        } else if(exception != null ){
34
            return findSecurityRuntimeException(exception.getCause());
35
        }
36
        return null;
37
    }
38

    
39

    
40
    /**
41
     * Find in the nested <code>exception</code> the exception of type <code>clazz</code>
42
     * or returns <code>null</code> if no such exception is found.
43
     *
44
     * @param clazz
45
     * @param exception the nested <code>Throwable</code> to search in.
46
     * @return
47
     */
48
    public static <T extends Throwable> T  findThrowableOfTypeIn(Class<T> clazz, Throwable exception) {
49
        if( permissionDeniedExceptionClass.isInstance(exception) ){
50
            return (T)exception;
51
        } else if(exception != null ){
52
            return findThrowableOfTypeIn(clazz, exception.getCause());
53
        }
54
        return null;
55
    }
56

    
57
}
(8-8/10)