com.nec.harvest.servlet.interceptor.LoggerInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.nec.harvest.servlet.interceptor.LoggerInterceptor.java

Source

/**
 * Copyright(C) 2014
 * NEC Corporation All rights reserved.
 * 
 * No permission to use, copy, modify and distribute this software
 * and its documentation for any purpose is granted.
 * This software is provided under applicable license agreement only.
 */
package com.nec.harvest.servlet.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;

/**
 * Abstract adapter class for the HandlerInterceptor interface, for simplified
 * implementation of pre-only/post-only interceptors
 * 
 * @author sondn
 * @version LoggerInterceptor.java May 4, 2014
 * 
 */
public class LoggerInterceptor extends HandlerInterceptorAdapterWithAddons {

    private static final Logger logger = LoggerFactory.getLogger(LoggerInterceptor.class);

    private static final String STRAT_EXECUTION_TIME_KEY = "executionStartTime";

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        final long startEcecutionTime = (Long) request.getAttribute(STRAT_EXECUTION_TIME_KEY);
        final long endExecutionTime = System.currentTimeMillis();
        final long timeUse = endExecutionTime - startEcecutionTime;

        logger.info("Start execution time :" + startEcecutionTime + " & End execution time :" + endExecutionTime
                + "  & Time use: " + timeUse + "[milliseconds] on the request Url: "
                + request.getRequestURL().toString());
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        request.setAttribute(STRAT_EXECUTION_TIME_KEY, System.currentTimeMillis());
        return super.preHandle(request, response, handler);
    }

}