package core.wicket.misc;

import javax.servlet.http.HttpServletRequest;

import wicket.AttributeModifier;
import wicket.markup.ComponentTag;
import wicket.markup.html.WebComponent;
import wicket.model.Model;
import wicket.protocol.http.WebRequest;

/**
 * Component for creating BASE html tag with path pointing to the context root.
 * Example:
 * 
 * <head> ... <base wicket:id="componentId"/> ... </head>
 * 
 * @author Igor Vaynberg ivaynberg@privesec.com
 * 
 */
public class BaseTag extends WebComponent {

	public BaseTag(String id) {
		super(id);

		HttpServletRequest request = ((WebRequest) getRequest())
				.getHttpServletRequest();

		final String path = new StringBuilder().append(request.getScheme())
				.append("://").append(request.getServerName()).append(":")
				.append(request.getServerPort()).append(
						request.getContextPath()).toString();

		add(new AttributeModifier("href", true, new Model(path)));
	}

	@Override
	protected void onComponentTag(ComponentTag tag) {
		checkComponentTag(tag, "base");
		super.onComponentTag(tag);
	}

}
