com.mysema.commons.freemarker.LinkDirective Maven / Gradle / Ivy
/*
* Copyright (c) 2009 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.commons.freemarker;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.mysema.commons.web.ContextURL;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
/**
* LinkDirective provides
*
* @author tiwe
* @version $Id$
*/
public class LinkDirective extends FreeMarkerHelper{
@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
HttpServletRequest req = getRequest(env);
if (params.containsKey("self")){
String link = join(req.getRequestURI(), req.getQueryString());
env.getOut().write(link);
}else if (params.containsKey("selfInContext")){
String link = join(req.getRequestURI(), req.getQueryString());
if (req.getContextPath().length() > 1){
link = link.substring(req.getContextPath().length());
}
env.getOut().write(link);
}else{
StringWriter writer = new StringWriter();
body.render(writer);
env.getOut().write(ContextURL.resolve(req, writer.toString()));
}
}
private String join(String url, String queryString){
if (queryString != null){
return url + "?" + queryString;
}else{
return url;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy