package it.mice.voila.runtime.springmvc.interceptors;

import it.mice.voila.runtime.springmvc.controller.DefaultController;
import it.mice.voila.runtime.springmvc.util.SpringMvcUtil;

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

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class ParametersExposingInterceptor extends HandlerInterceptorAdapter {
	Log log = LogFactory.getLog(ParametersExposingInterceptor.class);
	public static final String CONTROLLER_BASE_URL = "controllerBaseUrl";
	public static final String REQUEST_URL = "requestURL";
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		exposeJumpToParameter(request, response, handler);
		return true;
	}

	protected void exposeJumpToParameter(HttpServletRequest request,
			HttpServletResponse response, Object handler) {

		String jumpTo = (String)SpringMvcUtil.getRequestParameterOrAttribute(DefaultController.JUMP_TO);
		if (!StringUtils.isEmpty(jumpTo)) {
			request.setAttribute(DefaultController.JUMP_TO, jumpTo);
		}
	}
}
