Project

General

Profile

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

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

    
14
import eu.etaxonomy.cdm.database.PermissionDeniedException;
15

    
16
/**
17
 * This utility class helps finding security related exceptions in Throwables
18
 *
19
 * @author a.kohlbecker
20
 * @date Feb 11, 2014
21
 *
22
 */
23
public class SecurityExceptionUtils {
24

    
25
    /**
26
     * Finds a nested RuntimeExceptions of the types {@link PermissionDeniedException}, {@link AccessDeniedException}
27
     * or returns null.
28
     * @param exception
29
     * @return
30
     */
31
    public static RuntimeException findSecurityRuntimeException(Throwable exception) {
32

    
33
        if( PermissionDeniedException.class.isInstance(exception) || AccessDeniedException.class.isInstance(exception) ){
34
            return (RuntimeException) exception;
35
        } else if(exception != null ){
36
            return findSecurityRuntimeException(exception.getCause());
37
        }
38
        return null;
39

    
40
    }
41

    
42

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

    
60
}
(9-9/10)