1
|
package eu.etaxonomy.cdm.test.integration;
|
2
|
|
3
|
import org.springframework.security.access.AccessDeniedException;
|
4
|
|
5
|
/**
|
6
|
* This utility class helps finding security related exceptions in Throwables
|
7
|
*
|
8
|
* @author a.kohlbecker
|
9
|
\* @since Feb 11, 2014
|
10
|
*
|
11
|
*/
|
12
|
public class SecurityExceptionUtils {
|
13
|
|
14
|
//this is to decouple SecurityExceptionUtils from persistence and spring-security
|
15
|
public static Class<?> permissionDeniedExceptionClass;
|
16
|
public static Class<?> accessDeniedException;
|
17
|
|
18
|
static {
|
19
|
try {
|
20
|
permissionDeniedExceptionClass = Class.forName("eu.etaxonomy.cdm.database.PermissionDeniedException");
|
21
|
} catch (ClassNotFoundException e) {
|
22
|
throw new RuntimeException ("PermissionDeniedException class could not be found. Propably it moved to another folder", e);
|
23
|
}
|
24
|
try {
|
25
|
accessDeniedException = Class.forName("org.springframework.security.access.AccessDeniedException");
|
26
|
} catch (ClassNotFoundException e) {
|
27
|
throw new RuntimeException ("PermissionDeniedException class could not be found. Propably it moved to another folder", e);
|
28
|
}
|
29
|
}
|
30
|
|
31
|
/**
|
32
|
* Finds a nested RuntimeExceptions of the types {@link PermissionDeniedException}, {@link AccessDeniedException}
|
33
|
* or returns null.
|
34
|
* @param exception
|
35
|
* @return
|
36
|
*/
|
37
|
public static RuntimeException findSecurityRuntimeException(Throwable exception) {
|
38
|
if( permissionDeniedExceptionClass.isInstance(exception) || accessDeniedException.isInstance(exception) ){
|
39
|
return (RuntimeException) exception;
|
40
|
} else if(exception != null ){
|
41
|
return findSecurityRuntimeException(exception.getCause());
|
42
|
}
|
43
|
return null;
|
44
|
|
45
|
}
|
46
|
|
47
|
|
48
|
/**
|
49
|
* Find in the nested <code>exception</code> the exception of type <code>clazz</code>
|
50
|
* or returns <code>null</code> if no such exception is found.
|
51
|
*
|
52
|
* @param clazz
|
53
|
* @param exception the nested <code>Throwable</code> to search in.
|
54
|
* @return
|
55
|
*/
|
56
|
public static <T extends Throwable> T findThrowableOfTypeIn(Class<T> clazz, Throwable exception) {
|
57
|
if( permissionDeniedExceptionClass.isInstance(exception) ){
|
58
|
return (T)exception;
|
59
|
} else if(exception != null ){
|
60
|
return findThrowableOfTypeIn(clazz, exception.getCause());
|
61
|
}
|
62
|
return null;
|
63
|
}
|
64
|
|
65
|
}
|