001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.isis.core.webapp.diagnostics; 020 021import java.io.IOException; 022 023import javax.servlet.Filter; 024import javax.servlet.FilterChain; 025import javax.servlet.FilterConfig; 026import javax.servlet.ServletException; 027import javax.servlet.ServletRequest; 028import javax.servlet.ServletResponse; 029import javax.servlet.http.HttpServletRequest; 030 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034/** 035 * Simply logs the URL of any request that causes an exception to be thrown. 036 */ 037public class IsisLogOnExceptionFilter implements Filter { 038 039 private static final Logger LOG = LoggerFactory.getLogger(IsisLogOnExceptionFilter.class); 040 041 @Override 042 public void init(FilterConfig filterConfig) throws ServletException { 043 } 044 045 @Override 046 public void destroy() { 047 } 048 049 @Override 050 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 051 try { 052 chain.doFilter(request, response); 053 } catch (IOException e) { 054 logRequestUrl(request, e); 055 throw e; 056 } catch (ServletException e) { 057 logRequestUrl(request, e); 058 throw e; 059 } catch (RuntimeException e) { 060 logRequestUrl(request, e); 061 throw e; 062 } 063 } 064 065 private static void logRequestUrl(ServletRequest request, Exception e) { 066 if(!(request instanceof HttpServletRequest)) { 067 return; 068 } 069 final HttpServletRequest httpServletRequest = (HttpServletRequest) request; 070 final StringBuffer buf = httpServletRequest.getRequestURL(); 071 final String queryString = httpServletRequest.getQueryString(); 072 if(queryString != null) { 073 buf.append("?" + queryString); 074 } 075 076 LOG.error("Request caused " + e.getClass().getName() + ": " + buf.toString()); 077 } 078}