1 package cn.home1.oss.lib.errorhandle.internal;
2
3 import cn.home1.oss.lib.errorhandle.api.ConcreteExceptionResolver;
4 import cn.home1.oss.lib.errorhandle.api.ExceptionResolver;
5 import cn.home1.oss.lib.errorhandle.api.ResolvedError;
6 import cn.home1.oss.lib.errorhandle.internal.resolver.DefaultExceptionResolver;
7 import cn.home1.oss.lib.errorhandle.starter.CompositeExceptionResolverFactoryBean;
8
9 import org.springframework.web.context.request.RequestAttributes;
10
11 import java.util.Map;
12 import java.util.Optional;
13
14 import javax.servlet.http.HttpServletRequest;
15
16
17
18
19
20
21
22 public class CompositeExceptionResolver implements ExceptionResolver<Throwable> {
23
24 private DefaultExceptionResolver defaultResolver;
25 @SuppressWarnings("rawtypes")
26 private Map<Class<? extends Throwable>, ConcreteExceptionResolver> resolverMap;
27
28 @Override
29 public ResolvedError resolve(final HttpServletRequest request, final Throwable throwable) {
30 final ResolvedError resolvedError;
31
32 final Optional<ExceptionResolver<Throwable>> resolverOptional = resolver(throwable);
33 if (resolverOptional.isPresent()) {
34 final ExceptionResolver<Throwable> resolver = resolverOptional.get();
35 final ResolvedError resolved = resolver.resolve(request, throwable);
36 if (resolved != null) {
37 resolvedError = resolved;
38 } else {
39 resolvedError = this.defaultResolver.resolve(request, throwable);
40 }
41 } else {
42 resolvedError = this.defaultResolver.resolve(request, throwable);
43 }
44
45 return resolvedError;
46 }
47
48 @Override
49 public ResolvedError resolve(final RequestAttributes requestAttributes, final Throwable throwable) {
50 final ResolvedError resolvedError;
51
52 final Optional<ExceptionResolver<Throwable>> resolverOptional = resolver(throwable);
53 if (resolverOptional.isPresent()) {
54 final ExceptionResolver<Throwable> resolver = resolverOptional.get();
55 final ResolvedError resolved = resolver.resolve(requestAttributes, throwable);
56 if (resolved != null) {
57 resolvedError = resolved;
58 } else {
59 resolvedError = this.defaultResolver.resolve(requestAttributes, throwable);
60 }
61 } else {
62 resolvedError = this.defaultResolver.resolve(requestAttributes, throwable);
63 }
64
65 return resolvedError;
66 }
67
68 @SuppressWarnings({"unchecked", "rawtypes"})
69 Optional<ExceptionResolver<Throwable>> resolver(final Throwable throwable) {
70 ExceptionResolver<Throwable> result = null;
71 if (throwable != null) {
72 for (Class type = throwable.getClass(); type != Throwable.class; type = type.getSuperclass()) {
73 if (this.resolverMap.containsKey(type)) {
74 result = this.resolverMap.get(type);
75 break;
76 }
77 }
78 }
79 return Optional.ofNullable(result);
80 }
81
82 public void setDefaultResolver(final DefaultExceptionResolver defaultResolver) {
83 this.defaultResolver = defaultResolver;
84 }
85
86 @SuppressWarnings("rawtypes")
87 public void setResolverMap(final Map<Class<? extends Throwable>, ConcreteExceptionResolver> resolverMap) {
88 this.resolverMap = resolverMap;
89 }
90 }