package misclases;

import com.opensymphony.xwork2.*;
import com.opensymphony.xwork2.interceptor.*;
import org.apache.struts2.*;
import org.apache.struts2.interceptor.*;
import org.apache.struts2.util.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class UserInterceptor extends AbstractInterceptor implements StrutsStatics{
    public String intercept(ActionInvocation invocation) throws Exception {
	ComprobarAction action = (ComprobarAction)invocation.getAction();

	//la interfaz StrutsStatics contiene una serie de 
	//constantes que pueden ser utilizadas por el método
	//get() de ActionContext para obtener los distintos
	//objetos del contexto de la aplicación
	
	ActionContext context=invocation.getInvocationContext();
	HttpServletRequest request=(HttpServletRequest)context.get(HTTP_REQUEST);
	Cookie [] cookies=request.getCookies();
	if (cookies != null) {
            for (int i=0; i< cookies.length; i++) {
                if(cookies[i].getName().equals("user")){
			action.setExiste(true);
			action.setUser(cookies[i].getValue());
		}
	    }
	}

	return invocation.invoke();
    }
} 
