001    package com.khubla.pragmatach.plugin.freemarker;
002    
003    import java.io.PrintWriter;
004    import java.io.Writer;
005    import java.util.Map;
006    
007    import javax.servlet.http.HttpServletResponse;
008    
009    import com.khubla.pragmatach.framework.api.PragmatachException;
010    import com.khubla.pragmatach.framework.controller.impl.AbstractResponse;
011    
012    import freemarker.template.Template;
013    
014    /**
015     * @author tome
016     */
017    public class FreemarkerResponse extends AbstractResponse {
018       /**
019        * template
020        */
021       private final Template template;
022       /**
023        * context
024        */
025       private final Map<String, Object> context;
026    
027       public FreemarkerResponse(Map<String, String> cacheHeaders, Template template, Map<String, Object> context) {
028          super(cacheHeaders);
029          this.template = template;
030          this.context = context;
031       }
032    
033       @Override
034       public String getContentType() throws PragmatachException {
035          return CONTENT_TYPE_HTML;
036       }
037    
038       @Override
039       public Map<String, String> getHeaders() throws PragmatachException {
040          return super.getCacheHeaders();
041       }
042    
043       @Override
044       public void render(HttpServletResponse httpServletResponse) throws PragmatachException {
045          try {
046             final Writer writer = new PrintWriter(httpServletResponse.getOutputStream());
047             template.process(context, writer);
048          } catch (final Exception e) {
049             throw new PragmatachException("Exception in render", e);
050          }
051       }
052    }