From 98076fc60c31be00584c81d4ce7c2ef29d2c7eb2 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Wed, 30 Mar 2016 11:57:24 +0200 Subject: Catch and Log all exceptions into pdf-as log --- .../pdfas/web/filter/ExceptionCatchFilter.java | 126 +++++++++++---------- 1 file changed, 67 insertions(+), 59 deletions(-) (limited to 'pdf-as-web') diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/filter/ExceptionCatchFilter.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/filter/ExceptionCatchFilter.java index 7b126a40..675b1d6b 100644 --- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/filter/ExceptionCatchFilter.java +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/filter/ExceptionCatchFilter.java @@ -46,14 +46,13 @@ import at.gv.egiz.pdfas.web.helper.PdfAsHelper; */ public class ExceptionCatchFilter implements Filter { - private static final Logger logger = LoggerFactory - .getLogger(ExceptionCatchFilter.class); - - /** - * Default constructor. - */ - public ExceptionCatchFilter() { - } + private static final Logger logger = LoggerFactory.getLogger(ExceptionCatchFilter.class); + + /** + * Default constructor. + */ + public ExceptionCatchFilter() { + } /** * @see Filter#destroy() @@ -64,65 +63,74 @@ public class ExceptionCatchFilter implements Filter { /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { try { - - if(request instanceof HttpServletRequest) { - - logger.debug("Processing Parameters into Attributes"); - HttpServletRequest httpRequest = (HttpServletRequest)request; - MDC.put("SESSION_ID", httpRequest.getSession().getId()); - - logger.warn("Access from IP {}", getClientIpAddr(httpRequest)); - - PdfAsHelper.logAccess(httpRequest); - @SuppressWarnings("unchecked") - Enumeration parameterNames = httpRequest.getParameterNames(); - while(parameterNames.hasMoreElements()) { - String name = parameterNames.nextElement(); - String value = httpRequest.getParameter(name); - request.setAttribute(name, value); + + if (request instanceof HttpServletRequest) { + HttpServletRequest httpRequest = (HttpServletRequest) request; + MDC.put("SESSION_ID", httpRequest.getSession().getId()); + logger.debug("Processing Parameters into Attributes"); + logger.warn("Access from IP {}", getClientIpAddr(httpRequest)); + + PdfAsHelper.logAccess(httpRequest); + @SuppressWarnings("unchecked") + Enumeration parameterNames = httpRequest.getParameterNames(); + while (parameterNames.hasMoreElements()) { + String name = parameterNames.nextElement(); + String value = httpRequest.getParameter(name); + request.setAttribute(name, value); + } } - } - - try { - chain.doFilter(request, response); - } finally { - if(request instanceof HttpServletResponse) { - HttpServletResponse resp = (HttpServletResponse)response; - logger.debug("Got response status: {}", resp.getStatus()); + + try { + chain.doFilter(request, response); + } finally { + if (response != null) { + if (response instanceof HttpServletResponse) { + HttpServletResponse resp = (HttpServletResponse) response; + logger.debug("Got response status: {}", resp.getStatus()); + } else { + logger.warn("Response is not a HttpServletResponse!"); + } + } else { + logger.warn("Response is not a HttpServletResponse!"); + } } - } + } catch (Throwable e) { + logger.error("Unhandled exception found", e); + throw new ServletException(e.getMessage()); } finally { MDC.remove("SESSION_ID"); } - /*} catch(Throwable e) { - System.err.println("Unhandled Exception found!"); - e.printStackTrace(System.err); - logger.error("Unhandled Exception found!", e); - }*/ + /* + * } catch(Throwable e) { + * System.err.println("Unhandled Exception found!"); + * e.printStackTrace(System.err); + * logger.error("Unhandled Exception found!", e); } + */ + } + + public static String getClientIpAddr(HttpServletRequest request) { + String ip = request.getHeader("X-Forwarded-For"); + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("WL-Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("HTTP_CLIENT_IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("HTTP_X_FORWARDED_FOR"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getRemoteAddr(); + } + return ip; } - public static String getClientIpAddr(HttpServletRequest request) { - String ip = request.getHeader("X-Forwarded-For"); - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getHeader("Proxy-Client-IP"); - } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getHeader("WL-Proxy-Client-IP"); - } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getHeader("HTTP_CLIENT_IP"); - } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getHeader("HTTP_X_FORWARDED_FOR"); - } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getRemoteAddr(); - } - return ip; - } - /** * @see Filter#init(FilterConfig) */ -- cgit v1.2.3