/*
 * $Id: RewriteTag.java,v 1.1 2006/10/04 03:10:58 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: RewriteTag.java,v $
 * Revision 1.1  2006/10/04 03:10:58  marbobo
 * + Initiale Version
 *
 *
 */
package vkc.view.tag;

import java.net.MalformedURLException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import vkc.db.entities.VKCUser;
import vkc.util.ServletUtils;

public class RewriteTag extends LinkTag {
	/**
	 * Render the appropriately encoded URI.
	 * 
	 * @exception JspException
	 *                if a JSP exception has occurred
	 */
	public int doStartTag() throws JspException {
		// Generate the hyperlink URL
		Map params = RequestUtils.computeParameters(pageContext, paramId,
				paramName, paramProperty, paramScope, name, property, scope,
				transaction);

		VKCUser user = ServletUtils.getUser((HttpServletRequest)pageContext.getRequest());
		String url = null;
		forward = ServletUtils.replacePathVars(forward, user);
		href = ServletUtils.replacePathVars(href, user);
		page = ServletUtils.replacePathVars(page, user);
		
		
		try {
			// Note that we're encoding the & character to &amp; in XHTML mode
			// only,
			// otherwise the & is written as is to work in javascripts.
			url = RequestUtils.computeURL(pageContext, forward, href, page,
					null, params, anchor, false, this.isXhtml());

		} catch (MalformedURLException e) {
			RequestUtils.saveException(pageContext, e);
			throw new JspException(messages.getMessage("rewrite.url", e
					.toString()));
		}

		ResponseUtils.write(pageContext, url);

		return SKIP_BODY;

	}

	/**
	 * Ignore the end of this tag.
	 * 
	 * @exception JspException
	 *                if a JSP exception has occurred
	 */
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}
}