package foodplanet.aspects.listeners;

import foodplanet.management.HttpAccessMbean;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HttpAspectImpl implements HttpAspect{
    Log log = LogFactory.getLog(HttpAspectImpl.class);
    private HttpAccessMbean mbean;
    
    public HttpAspectImpl() {
    }

    public void doBeforeRequest(HttpServletRequest request, HttpServletResponse response) {
        log.info("***** AOP: Http Access Listener - Running Before Advice *****");
        try {
            if(!mbean.isStarted() && mbean.getStoppedView() != null) {
                request.getRequestDispatcher(mbean.getStoppedView()).forward(request,response);
            }
        } catch (ServletException ex) {
            log.error("AOP: Http Access Listener - Unable to foward request to destination");
        } catch (IOException ex) {
            log.error("AOP: Http Access Listener - Unable to foward request to destination");
        }
    }

    public void doAfterRequest(HttpServletRequest request, HttpServletResponse response) {
        log.info("***** AOP: Completing Http Request *****");
        mbean.incRequestCount();
    }

    public void setHttpAccessMbean(HttpAccessMbean mbean) {
        this.mbean = mbean;
    }
}
