package net.etalia.crepuscolo.mvc;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class RequestLoggerFilter implements Filter {

	protected Log log = LogFactory.getLog(RequestLoggerFilter.class);

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		long start = System.currentTimeMillis();
		chain.doFilter(request, response);
		long end = System.currentTimeMillis();
		long time = end - start;
		String url = null;
		String queryString = null;
		String method = null;
		if (request instanceof HttpServletRequest) {
			url = ((HttpServletRequest)request).getContextPath();
			url += ((HttpServletRequest)request).getServletPath();
			queryString = ((HttpServletRequest)request).getQueryString();
			method = ((HttpServletRequest)request).getMethod();
		}
		log.info("TIME: " + time + " - METHOD: " + method + " - URL: " + url + " - PARAMS: " + queryString);
	}

	@Override
	public void destroy() {
	}

}

@@ -315,16 +315,63 @@
 Request;
+%0Aimport javax.servlet.http.HttpServletResponse;
 %0A%0Aimport
@@ -1307,16 +1307,142 @@
 ();%0A%09%09%7D%0A
+%09%09int code = -1;%0A%09%09if (response instanceof HttpServletResponse) %7B%0A%09%09%09code = ((HttpServletResponse) response).getStatus();%0A%09%09%7D%0A
 %09%09log.in
@@ -1465,16 +1465,37 @@
 me + %22 -
+ CODE: %22 + code + %22 -
  METHOD:

Add response status code to RequestLoggerFilter