1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.catalina.util;
18
19 import java.util.HashSet;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import org.apache.tomcat.util.descriptor.web.ErrorPage;
25
26 /**
27 * Provides support for tracking per exception type and per HTTP status code
28 * error pages.
29 */
30 public class ErrorPageSupport {
31
32 // Fully qualified class name to error page
33 private Map<String, ErrorPage> exceptionPages = new ConcurrentHashMap<>();
34
35 // HTTP status code to error page
36 private Map<Integer, ErrorPage> statusPages = new ConcurrentHashMap<>();
37
38
39 public void add(ErrorPage errorPage) {
40 String exceptionType = errorPage.getExceptionType();
41 if (exceptionType == null) {
42 statusPages.put(Integer.valueOf(errorPage.getErrorCode()), errorPage);
43 } else {
44 exceptionPages.put(exceptionType, errorPage);
45 }
46 }
47
48
49 public void remove(ErrorPage errorPage) {
50 String exceptionType = errorPage.getExceptionType();
51 if (exceptionType == null) {
52 statusPages.remove(Integer.valueOf(errorPage.getErrorCode()), errorPage);
53 } else {
54 exceptionPages.remove(exceptionType, errorPage);
55 }
56 }
57
58
59 public ErrorPage find(int statusCode) {
60 return statusPages.get(Integer.valueOf(statusCode));
61 }
62
63
64 /**
65 * Find the ErrorPage, if any, for the named exception type.
66 *
67 * @param exceptionType The fully qualified class name of the exception type
68 *
69 * @return The ErrorPage for the named exception type, or {@code null} if
70 * none is configured
71 */
72 public ErrorPage find(String exceptionType) {
73 return exceptionPages.get(exceptionType);
74 }
75
76
77 public ErrorPage find(Throwable exceptionType) {
78 if (exceptionType == null) {
79 return null;
80 }
81 Class<?> clazz = exceptionType.getClass();
82 String name = clazz.getName();
83 while (!Object.class.equals(clazz)) {
84 ErrorPage errorPage = exceptionPages.get(name);
85 if (errorPage != null) {
86 return errorPage;
87 }
88 clazz = clazz.getSuperclass();
89 if (clazz == null) {
90 break;
91 }
92 name = clazz.getName();
93 }
94 return null;
95 }
96
97
98 public ErrorPage[] findAll() {
99 Set<ErrorPage> errorPages = new HashSet<>();
100 errorPages.addAll(exceptionPages.values());
101 errorPages.addAll(statusPages.values());
102 return errorPages.toArray(new ErrorPage[errorPages.size()]);
103 }
104 }
105