package org.ambraproject.wombat.freemarker;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import org.ambraproject.wombat.service.remote.LeopardService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.io.Reader;
import java.util.Map;

public class FetchHtmlDirective implements TemplateDirectiveModel {

  @Autowired
  private LeopardService leopardService;

  @Override
  public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    if (leopardService == null) {
      throw new RuntimeException("Leopard service is not configured");
    }

    Object pathObj = params.get("path");
    if (pathObj == null) {
      throw new TemplateModelException("path parameter required");
    }

    try (Reader reader = leopardService.readHtml(pathObj.toString())) {
      IOUtils.copy(reader, env.getOut());
    }
  }

}

@@ -621,16 +621,780 @@
 odel %7B%0A%0A
+  /*%0A   * This directive is capable of fetching HTML from any arbitrary HTTP endpoint. In FreeMarker code, an invocation%0A   * looks like %3C@fetchHtml path=%22...%22 /%3E and does not specify which service the path applies to.%0A   *%0A   * To keep things simple, the directive class is currently hard-coded to call only the leopardService bean, which%0A   * creates an implicit link between %22fetchHtml%22 in FreeMarker code and %22leopardService%22 in the config code. The link%0A   * is not intuitive, and will need to be generalized if this directive must ever connect to more than one service.%0A   *%0A   * However, we are choosing not to complicate it until we have a motivating case. So, leopardService remains%0A   * hard-coded as the one and only way to %22fetch HTML%22 for now.%0A   */%0A%0A
   @Autow

Comment on FetchHtmlDirective, explaining relationship to LeopardService