01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.http.service;
16:
17: import java.io.Writer;
18: import javax.servlet.http.HttpServletResponse;
19: import org.apache.commons.lang.exception.ExceptionUtils;
20: import org.araneaframework.Environment;
21: import org.araneaframework.InputData;
22: import org.araneaframework.OutputData;
23: import org.araneaframework.Path;
24: import org.araneaframework.Service;
25: import org.araneaframework.core.BaseService;
26: import org.araneaframework.framework.ExceptionHandlerFactory;
27: import org.araneaframework.http.HttpOutputData;
28: import org.araneaframework.http.util.ServletUtil;
29:
30: public class SimpleCriticalErrorHandlerService extends BaseService {
31: protected Throwable exception;
32:
33: public SimpleCriticalErrorHandlerService() {
34: //Factory constructor
35: }
36:
37: public SimpleCriticalErrorHandlerService(Throwable exception) {
38: this .exception = exception;
39: }
40:
41: protected void action(Path path, InputData input, OutputData output)
42: throws Exception {
43: Writer out = ((HttpOutputData) output).getWriter();
44:
45: ServletUtil.getResponse(output).setStatus(
46: HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
47:
48: ((HttpOutputData) output)
49: .setContentType("text/html; charset=UTF-8");
50: out
51: .write("<html><head><title>Critical error occured!</title></head><body>");
52: if (ExceptionUtils.getRootCause(exception) != null) {
53: out.write("<b>Root cause:</b><br/>");
54: out.write("<pre>"
55: + ExceptionUtils.getFullStackTrace(ExceptionUtils
56: .getRootCause(exception)) + "</pre>");
57: }
58: out.write("<b>Stack trace:</b><br/>");
59: out.write("<pre>" + ExceptionUtils.getFullStackTrace(exception)
60: + "</pre>");
61: out.write("</body></html>");
62: }
63:
64: public static class Factory implements ExceptionHandlerFactory {
65: public Service buildExceptionHandler(Throwable e,
66: Environment environment) {
67: return new SimpleCriticalErrorHandlerService(e);
68: }
69: }
70: }
|