<commit_before>package com.orctom.laputa.translator;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.orctom.laputa.exception.IllegalConfigException;
import com.orctom.laputa.service.exception.TemplateProcessingException;
import com.orctom.laputa.service.model.Context;
import com.orctom.laputa.service.model.RequestMapping;
import com.orctom.laputa.service.translator.TemplateResponseTranslator;
import com.orctom.laputa.utils.ClassUtils;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * Freemarker web page renderer
 * Created by hao on 2/19/17.
 */
public class FreemarkerResponseTranslator extends TemplateResponseTranslator {

  private static final Logger LOGGER = LoggerFactory.getLogger(FreemarkerResponseTranslator.class);

  private static final String TEMPLATE_SUFFIX = ".ftl";

  private static final Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);

  private static LoadingCache<RequestMapping, freemarker.template.Template> templates = CacheBuilder.newBuilder()
      .build(
          new CacheLoader<RequestMapping, freemarker.template.Template>() {
            @Override
            public freemarker.template.Template load(RequestMapping mapping) throws Exception {
              return getTemplate(mapping);
            }
          }
      );

  public FreemarkerResponseTranslator() {
    cfg.setClassForTemplateLoading(FreemarkerResponseTranslator.class, TEMPLATE_PREFIX);
    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    cfg.setLogTemplateExceptions(false);
  }

  @Override
  public byte[] translate(RequestMapping mapping, Object data, Context ctx) throws IOException {
    try {
      freemarker.template.Template template = templates.get(mapping);
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      Writer writer = new BufferedWriter(new OutputStreamWriter(out));
      if (ClassUtils.isSimpleValueType(data.getClass())) {
        Map<String, Object> model = new HashMap<>(ctx.getData());
        model.put("data", data);
        template.process(model, writer);
      } else {
        template.process(Lists.newArrayList(data, ctx), writer);
      }
      return out.toByteArray();
    } catch (TemplateException | ExecutionException e) {
      throw new TemplateProcessingException(e.getMessage(), e);
    }
  }<commit_msg>workaround for model and ctx as TemplateModel<commit_after>package com.orctom.laputa.translator;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.orctom.laputa.exception.IllegalConfigException;
import com.orctom.laputa.service.exception.TemplateProcessingException;
import com.orctom.laputa.service.model.Context;
import com.orctom.laputa.service.model.RequestMapping;
import com.orctom.laputa.service.translator.TemplateResponseTranslator;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * Freemarker web page renderer
 * Created by hao on 2/19/17.
 */
public class FreemarkerResponseTranslator extends TemplateResponseTranslator {

  private static final Logger LOGGER = LoggerFactory.getLogger(FreemarkerResponseTranslator.class);

  private static final String TEMPLATE_SUFFIX = ".ftl";

  private static final Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);

  private static LoadingCache<RequestMapping, freemarker.template.Template> templates = CacheBuilder.newBuilder()
      .build(
          new CacheLoader<RequestMapping, freemarker.template.Template>() {
            @Override
            public freemarker.template.Template load(RequestMapping mapping) throws Exception {
              return getTemplate(mapping);
            }
          }
      );

  public FreemarkerResponseTranslator() {
    cfg.setClassForTemplateLoading(FreemarkerResponseTranslator.class, TEMPLATE_PREFIX);
    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    cfg.setLogTemplateExceptions(false);
  }

  @Override
  public byte[] translate(RequestMapping mapping, Object data, Context ctx) throws IOException {
    try {
      freemarker.template.Template template = templates.get(mapping);
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      Writer writer = new BufferedWriter(new OutputStreamWriter(out));
      Map<String, Object> model = new HashMap<>(ctx.getData());
      model.put("model", data);
      template.process(model, writer);
      return out.toByteArray();
    } catch (TemplateException | ExecutionException e) {
      throw new TemplateProcessingException(e.getMessage(), e);
    }
  }
{'fraction_non_alphanumeric': 0.09343387897737723, 'fraction_numerical': 0.004414199006805224, 'mean_word_length': 4.652806652806653, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5401524', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1483, 'n_words': 296}