001 package com.khubla.pragmatach.plugin.freemarker; 002 003 import java.io.InputStream; 004 import java.io.InputStreamReader; 005 006 import com.khubla.pragmatach.framework.api.PragmatachException; 007 import com.khubla.pragmatach.framework.api.Request; 008 import com.khubla.pragmatach.framework.api.Response; 009 import com.khubla.pragmatach.framework.controller.impl.AbstractController; 010 import com.khubla.pragmatach.framework.controller.impl.FormPostBeanBoundController; 011 012 import freemarker.template.Configuration; 013 import freemarker.template.Template; 014 015 /** 016 * @author tome 017 */ 018 public class FreemarkerController extends FormPostBeanBoundController { 019 /** 020 * ctor 021 */ 022 public FreemarkerController() { 023 } 024 025 /** 026 * freemarker config 027 */ 028 private Configuration getConfiguration() { 029 final Configuration configuration = new Configuration(); 030 configuration.setLocalizedLookup(false); 031 configuration 032 .setTemplateExceptionHandler(new TemplateExceptionHandlerImpl()); 033 final Request request = getRequest(); 034 if (null != request) { 035 final PragmatachTemplateLoader pragmatachTemplateLoader = new PragmatachTemplateLoader( 036 getRequest().getServletContext()); 037 configuration.setTemplateLoader(pragmatachTemplateLoader); 038 } else { 039 throw new RuntimeException(); 040 } 041 return configuration; 042 } 043 044 /** 045 * get the Freemarker Template 046 */ 047 private Template getFreemarkerTemplate() throws PragmatachException { 048 try { 049 final String templateName = getTemplateName(); 050 if (null != templateName) { 051 final InputStream templateInputStream = getResource(templateName); 052 if (null != templateInputStream) { 053 return new Template(templateName, new InputStreamReader( 054 templateInputStream, "UTF-8"), getConfiguration()); 055 } else { 056 throw new Exception("Unable to load template '" 057 + templateName + "'"); 058 } 059 } else { 060 throw new PragmatachException( 061 "Unable to get template name for controller '" 062 + AbstractController.getControllerName(this) 063 + "'. Does it have an @View annotation?"); 064 } 065 } catch (final Exception e) { 066 throw new PragmatachException("Exception in getTemplate", e); 067 } 068 } 069 070 /** 071 * render 072 */ 073 public Response render() throws PragmatachException { 074 try { 075 final Template template = getFreemarkerTemplate(); 076 return new FreemarkerResponse(getCacheHeaders(), template, 077 getTemplateContext()); 078 } catch (final Exception e) { 079 throw new PragmatachException("Exception in render", e); 080 } 081 } 082 083 /** 084 * render 085 */ 086 public Response renderTemplate(String templateString) 087 throws PragmatachException { 088 try { 089 final Template template = new Template("/", templateString, 090 getConfiguration()); 091 return new FreemarkerResponse(getCacheHeaders(), template, 092 getTemplateContext()); 093 } catch (final Exception e) { 094 throw new PragmatachException("Exception in render", e); 095 } 096 } 097 }