com.fastchar.out.FastOutFreemarker Maven / Gradle / Ivy
package com.fastchar.out;
import com.fastchar.core.FastAction;
import com.fastchar.core.FastChar;
import freemarker.template.Template;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
public class FastOutFreemarker extends FastOut {
public FastOutFreemarker() {
this.contentType = "text/html";
}
@Override
public void response(FastAction action) throws Exception {
if (String.valueOf(data).toLowerCase().endsWith(".xml")) {
this.contentType = "text/xml";
}
HttpServletResponse response = action.getResponse();
HttpServletRequest request = action.getRequest();
response.setContentType(toContentType());
response.setCharacterEncoding(getCharset());
Map data = new HashMap<>(FastChar.getTemplates().getFinalContext());
for (Enumeration attrs = request.getAttributeNames(); attrs.hasMoreElements(); ) {
String attrName = attrs.nextElement();
data.put(attrName, request.getAttribute(attrName));
}
for (Enumeration attrs = request.getSession().getAttributeNames(); attrs.hasMoreElements();) {
String attrName = attrs.nextElement();
data.put(attrName, request.getSession().getAttribute(attrName));
}
Template template = FastChar.getTemplates().getFreemarker().getTemplate(String.valueOf(data));
PrintWriter writer = response.getWriter();
template.process(data, writer);
writer.flush();
}
}