Project

General

Profile

Download (2.93 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.test.integration;
10

    
11
import org.springframework.security.access.AccessDeniedException;
12

    
13
/**
14
 * This utility class helps finding security related exceptions in Throwables
15
 *
16
 * Note: This class is more or less a clone of the same named class in cdmlib-persistence.
17
 * Any changes in either of these classes should be also done in the other class.
18
 * In future theses classes should be merged. But this requires a change in dependencies.
19
 *
20
 * @author a.kohlbecker
21
 * @since Feb 11, 2014
22
 */
23
public class SecurityExceptionUtils {
24

    
25
   //this is to decouple SecurityExceptionUtils from persistence and spring-security
26
    public static Class<?> permissionDeniedExceptionClass;
27
    public static Class<?> accessDeniedException;
28

    
29
    static {
30
        try {
31
            //PermissionDeniedException is not available here yet, therefore we use Class.forName
32
            permissionDeniedExceptionClass = Class.forName("eu.etaxonomy.cdm.database.PermissionDeniedException");
33
        } catch (ClassNotFoundException e) {
34
            throw new RuntimeException ("PermissionDeniedException class could not be found. Propably it moved to another folder", e);
35
        }
36
        try {
37
            accessDeniedException = Class.forName("org.springframework.security.access.AccessDeniedException");
38
        } catch (ClassNotFoundException e) {
39
            throw new RuntimeException ("PermissionDeniedException class could not be found. Propably it moved to another folder", e);
40
        }
41
    }
42

    
43
    /**
44
     * Finds a nested RuntimeExceptions of the types {@link PermissionDeniedException}, {@link AccessDeniedException}
45
     * or returns null.
46
     * @param exception
47
     * @return
48
     */
49
    public static RuntimeException findSecurityRuntimeException(Throwable exception) {
50
        if( permissionDeniedExceptionClass.isInstance(exception) || accessDeniedException.isInstance(exception) ){
51
            return (RuntimeException) exception;
52
        } else if(exception != null ){
53
            return findSecurityRuntimeException(exception.getCause());
54
        }
55
        return null;
56

    
57
    }
58

    
59

    
60
    /**
61
     * Find in the nested <code>exception</code> the exception of type <code>clazz</code>
62
     * or returns <code>null</code> if no such exception is found.
63
     *
64
     * @param clazz
65
     * @param exception the nested <code>Throwable</code> to search in.
66
     * @return
67
     */
68
    public static <T extends Throwable> T  findThrowableOfTypeIn(Class<T> clazz, Throwable exception) {
69
        if( permissionDeniedExceptionClass.isInstance(exception) ){
70
            return (T)exception;
71
        } else if(exception != null ){
72
            return findThrowableOfTypeIn(clazz, exception.getCause());
73
        }
74
        return null;
75
    }
76

    
77
}
(6-6/6)