01: /*
02: * Copyright 2007 Gerd Ziegler (www.gerdziegler.de)
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: * http://www.apache.org/licenses/LICENSE-2.0
07: * Unless required by applicable law or agreed to in writing,
08: * software distributed under the License is distributed on an
09: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
10: * either express or implied. See the License for the specific
11: * language governing permissions and limitations under the License.
12: * @author www.gerdziegler.de
13: */
14:
15: package org.ztemplates.web.impl;
16:
17: import java.io.PrintWriter;
18:
19: import javax.servlet.http.HttpServletRequest;
20: import javax.servlet.http.HttpServletResponse;
21:
22: import org.apache.log4j.Logger;
23: import org.ztemplates.actions.security.ZSecurityException;
24: import org.ztemplates.web.ZIExceptionService;
25: import org.ztemplates.web.ZIServiceRepository;
26:
27: public class ZExceptionServiceImpl implements ZIExceptionService {
28: protected static Logger log = Logger
29: .getLogger(ZIExceptionService.class);
30:
31: private ZIServiceRepository serviceRepository;
32:
33: public void init(ZIServiceRepository serviceRepository)
34: throws Exception {
35: this .serviceRepository = serviceRepository;
36: }
37:
38: public void handle(Throwable t) {
39: HttpServletRequest request = serviceRepository.getRequest();
40: if (t instanceof ZSecurityException) {
41: ZSecurityException se = (ZSecurityException) t;
42: String msg = request.getUserPrincipal() != null ? request
43: .getUserPrincipal().getName() : "no user";
44: log.error(msg + " " + request.isUserInRole("user"), se);
45: } else {
46: try {
47: HttpServletResponse response = serviceRepository
48: .getResponse();
49: log.error("[" + request.getRequestURI() + "]", t);
50: PrintWriter w = response.getWriter();
51: w.write("<html><body><pre>");
52: t.printStackTrace(w);
53: w.write("</pre></body></html>");
54: } catch (Exception e) {
55: log.error("error while handling exception: ", e);
56: }
57: }
58: }
59: }
|