use of freemarker.template.TemplateModel in project freemarker by apache.
the class FreemarkerServlet method process.
private void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Give chance to subclasses to perform preprocessing
if (preprocessRequest(request, response)) {
return;
}
if (bufferSize != null && !response.isCommitted()) {
try {
response.setBufferSize(bufferSize.intValue());
} catch (IllegalStateException e) {
LOG.debug("Can't set buffer size any more,", e);
}
}
String templatePath = requestUrlToTemplatePath(request);
if (LOG.isDebugEnabled()) {
LOG.debug("Requested template " + StringUtil.jQuoteNoXSS(templatePath) + ".");
}
Locale locale = request.getLocale();
if (locale == null || overrideResponseLocale != OverrideResponseLocale.NEVER) {
locale = deduceLocale(templatePath, request, response);
}
final Template template;
try {
template = config.getTemplate(templatePath, locale);
} catch (TemplateNotFoundException e) {
if (exceptionOnMissingTemplate) {
throw newServletExceptionWithFreeMarkerLogging("Template not found for name " + StringUtil.jQuoteNoXSS(templatePath) + ".", e);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Responding HTTP 404 \"Not found\" for missing template " + StringUtil.jQuoteNoXSS(templatePath) + ".", e);
}
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Page template not found");
return;
}
} catch (freemarker.core.ParseException e) {
throw newServletExceptionWithFreeMarkerLogging("Parsing error with template " + StringUtil.jQuoteNoXSS(templatePath) + ".", e);
} catch (Exception e) {
throw newServletExceptionWithFreeMarkerLogging("Unexpected error when loading template " + StringUtil.jQuoteNoXSS(templatePath) + ".", e);
}
boolean tempSpecContentTypeContainsCharset = false;
if (response.getContentType() == null || overrideResponseContentType != OverrideResponseContentType.NEVER) {
ContentType templateSpecificContentType = getTemplateSpecificContentType(template);
if (templateSpecificContentType != null) {
// With ResponseCharacterEncoding.LEGACY we should append the charset, but we don't do that for b. c.
response.setContentType(responseCharacterEncoding != ResponseCharacterEncoding.DO_NOT_SET ? templateSpecificContentType.httpHeaderValue : templateSpecificContentType.getMimeType());
tempSpecContentTypeContainsCharset = templateSpecificContentType.containsCharset;
} else if (response.getContentType() == null || overrideResponseContentType == OverrideResponseContentType.ALWAYS) {
if (responseCharacterEncoding == ResponseCharacterEncoding.LEGACY && !contentType.containsCharset) {
// In legacy mode we don't call response.setCharacterEncoding, so the charset must be set here:
response.setContentType(contentType.httpHeaderValue + "; charset=" + getTemplateSpecificOutputEncoding(template));
} else {
response.setContentType(contentType.httpHeaderValue);
}
}
}
if (responseCharacterEncoding != ResponseCharacterEncoding.LEGACY && responseCharacterEncoding != ResponseCharacterEncoding.DO_NOT_SET) {
// Using the Servlet 2.4 way of setting character encoding.
if (responseCharacterEncoding != ResponseCharacterEncoding.FORCE_CHARSET) {
if (!tempSpecContentTypeContainsCharset) {
response.setCharacterEncoding(getTemplateSpecificOutputEncoding(template));
}
} else {
response.setCharacterEncoding(forcedResponseCharacterEncoding.name());
}
}
setBrowserCachingPolicy(response);
ServletContext servletContext = getServletContext();
try {
logWarnOnObjectWrapperMismatch();
TemplateModel model = createModel(wrapper, servletContext, request, response);
// Give subclasses a chance to hook into preprocessing
if (preTemplateProcess(request, response, template, model)) {
try {
// Process the template
Environment env = template.createProcessingEnvironment(model, response.getWriter());
if (responseCharacterEncoding != ResponseCharacterEncoding.LEGACY) {
String actualOutputCharset = response.getCharacterEncoding();
if (actualOutputCharset != null) {
env.setOutputEncoding(actualOutputCharset);
}
}
processEnvironment(env, request, response);
} finally {
// Give subclasses a chance to hook into postprocessing
postTemplateProcess(request, response, template, model);
}
}
} catch (TemplateException e) {
final TemplateExceptionHandler teh = config.getTemplateExceptionHandler();
// Ensure that debug handler responses aren't rolled back:
if (teh == TemplateExceptionHandler.HTML_DEBUG_HANDLER || teh == TemplateExceptionHandler.DEBUG_HANDLER || teh.getClass().getName().indexOf("Debug") != -1) {
response.flushBuffer();
}
throw newServletExceptionWithFreeMarkerLogging("Error executing FreeMarker template", e);
}
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class TransformBlock method accept.
@Override
TemplateElement[] accept(Environment env) throws TemplateException, IOException {
TemplateTransformModel ttm = env.getTransform(transformExpression);
if (ttm != null) {
Map args;
if (namedArgs != null && !namedArgs.isEmpty()) {
args = new HashMap();
for (Iterator it = namedArgs.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
Expression valueExp = (Expression) entry.getValue();
TemplateModel value = valueExp.eval(env);
args.put(key, value);
}
} else {
args = EmptyMap.instance;
}
env.visitAndTransform(getChildBuffer(), ttm, args);
} else {
TemplateModel tm = transformExpression.eval(env);
throw new UnexpectedTypeException(transformExpression, tm, "transform", new Class[] { TemplateTransformModel.class }, env);
}
return null;
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class UnaryPlusMinusExpression method _eval.
@Override
TemplateModel _eval(Environment env) throws TemplateException {
TemplateNumberModel targetModel = null;
TemplateModel tm = target.eval(env);
try {
targetModel = (TemplateNumberModel) tm;
} catch (ClassCastException cce) {
throw new NonNumericalException(target, tm, env);
}
if (!isMinus) {
return targetModel;
}
target.assertNonNull(targetModel, env);
Number n = targetModel.getAsNumber();
n = ArithmeticEngine.CONSERVATIVE_ENGINE.multiply(MINUS_ONE, n);
return new SimpleNumber(n);
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class PropertySetting method accept.
@Override
TemplateElement[] accept(Environment env) throws TemplateException {
TemplateModel mval = value.eval(env);
String strval;
if (mval instanceof TemplateScalarModel) {
strval = ((TemplateScalarModel) mval).getAsString();
} else if (mval instanceof TemplateBooleanModel) {
strval = ((TemplateBooleanModel) mval).getAsBoolean() ? "true" : "false";
} else if (mval instanceof TemplateNumberModel) {
strval = ((TemplateNumberModel) mval).getAsNumber().toString();
} else {
strval = value.evalAndCoerceToStringOrUnsupportedMarkup(env);
}
env.setSetting(key, strval);
return null;
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class MethodCall method _eval.
@Override
TemplateModel _eval(Environment env) throws TemplateException {
TemplateModel targetModel = target.eval(env);
if (targetModel instanceof TemplateMethodModel) {
TemplateMethodModel targetMethod = (TemplateMethodModel) targetModel;
List argumentStrings = targetMethod instanceof TemplateMethodModelEx ? arguments.getModelList(env) : arguments.getValueList(env);
Object result = targetMethod.exec(argumentStrings);
return env.getObjectWrapper().wrap(result);
} else if (targetModel instanceof Macro) {
Macro func = (Macro) targetModel;
env.setLastReturnValue(null);
if (!func.isFunction()) {
throw new _MiscTemplateException(env, "A macro cannot be called in an expression. (Functions can be.)");
}
Writer prevOut = env.getOut();
try {
env.setOut(NullWriter.INSTANCE);
env.invoke(func, null, arguments.items, null, this);
} catch (IOException e) {
// Should not occur
throw new TemplateException("Unexpected exception during function execution", e, env);
} finally {
env.setOut(prevOut);
}
return env.getLastReturnValue();
} else {
throw new NonMethodException(target, targetModel, env);
}
}
Aggregations