package de.snailshell.consensus.demo;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts.Globals;
import org.apache.struts.config.ModuleConfig;

/**
 * @author Thomas Much
 * @version 2006-06-14
 */
public class CheckLoginTag extends TagSupport {

	private String bean    = Constants.SESSION_ATTR_USER;
	private String forward = "/login.jsp";

	public void setBean(String bean) {
		this.bean = bean;
	}

	public void setForward(String forward) {
		this.forward = forward;
	}

	public int doStartTag() throws JspException {
		return SKIP_BODY;
	}

	public int doEndTag() throws JspException {
		HttpSession session = pageContext.getSession();
		if ((session != null) && (session.getAttribute(bean) != null))
		{
            return EVAL_PAGE;
        }
		else
		{
			ModuleConfig config = (ModuleConfig)pageContext
									.getServletContext()
									.getAttribute(Globals.MODULE_KEY);
			try {
				pageContext.forward(config.getPrefix() + forward);
			}
			catch (Exception e) {
				throw new JspException( e.toString() );
			}
			return SKIP_PAGE;
		}
	}

	public void release() {
		super.release();
	}
}
