All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.quincy.auth.freemarker.AbstractHtmlTemplateDirectiveModel Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package com.quincy.auth.freemarker;

import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.quincy.auth.o.DSession;
import com.quincy.sdk.Constants;
import com.quincy.sdk.helper.CommonHelper;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

public abstract class AbstractHtmlTemplateDirectiveModel implements TemplateDirectiveModel {
	protected abstract String realExecute(Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws IOException;
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
			throws TemplateException, IOException {
		boolean output = false;
		Object permission = params.get("permission");
		if(permission!=null) {
			String permissionName = permission.toString();
			HttpServletRequest request = CommonHelper.getRequest();
			DSession session = (DSession)request.getAttribute(Constants.ATTR_SESSION);
			List permissions = session.getPermissions();
			for(String p:permissions) {
				if(p.equalsIgnoreCase(permissionName)) {
					output = true;
					break;
				}
			}
		} else {
			output = true;
		}
		if(output) {
			String html = this.realExecute(params, loopVars, body);
			if(html!=null) {
				if(body!=null) {
					body.render(new PlaceHolderWriter(env.getOut(), html));
				} else {
					env.getOut().write(html);
				}
			}
		}
	}

	private static class PlaceHolderWriter extends Writer {
		private final Writer out;
		private String html;
		
		public PlaceHolderWriter(Writer out, String html) {
            this.out = out;
            this.html = html;
        }

		@Override
		public void write(char[] cbuf, int off, int len) throws IOException {
			int index = html.indexOf("




© 2015 - 2024 Weber Informatics LLC | Privacy Policy