public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { if (!params.isEmpty()) { throw new TemplateException("This directive supports no parameters.", env); } if (loopVars.length != 0) { throw new TemplateException("This directive supports no loop variables.", env); } if (body != null) { throw new TemplateException("This directive supports no nested content.", env); } env.include(template); } });
@Override public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { if (!params.isEmpty()) { throw new TemplateException("This directive doesn't allow parameters.", env); } if (loopVars.length != 0) { throw new TemplateException("This directive doesn't allow loop variables.", env); } Writer out = env.getOut(); out.append(this.context.getSession().getAuthenticityToken()); } }
/** * Get a Freemarker configuration that is safe against malicious templates * * @return */ public static Configuration getSafeConfiguration() { Configuration config = new Configuration(); config.setNewBuiltinClassResolver( (name, env, template) -> { if (ILLEGAL_FREEMARKER_CLASSES.stream().anyMatch(name::equals)) { throw new TemplateException( String.format( "Class %s is not allowed in Freemarker templates", name), env); } if (LEGAL_FREEMARKER_CLASSES.stream().anyMatch(name::equals)) { try { ClassUtil.forName(name); } catch (ClassNotFoundException e) { throw new TemplateException(e, env); } } return TemplateClassResolver.SAFER_RESOLVER.resolve(name, env, template); }); return config; } }
} catch (IOException e) { throw new TemplateException("Unexpected exception during function execution", e, env); } finally { env.setOut(prevOut);
@SuppressWarnings("unchecked") @Override public void execute(Environment environment, @SuppressWarnings("rawtypes") Map parameters, TemplateModel[] loopVars, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException { try { execute(new TemplateDirectiveHandler(parameters, loopVars, environment, templateDirectiveBody)); } catch (IOException e) { throw e; } catch (Exception e) { throw new TemplateException(e, environment); } }
@SuppressWarnings("unchecked") @Override public void execute(Environment environment, @SuppressWarnings("rawtypes") Map parameters, TemplateModel[] loopVars, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException { try { execute(new TemplateDirectiveHandler(parameters, loopVars, environment, templateDirectiveBody)); } catch (IOException e) { throw e; } catch (Exception e) { throw new TemplateException(e, environment); } }
Context ctx = env.getCurrentMacroContext(); if (ctx == null) { throw new TemplateException( "Can't get ." + name + " here, as there's no macro or function (that's " + "implemented in the template) call in context.", env);
protected void checkDateTypeNotUnknown(int dateType, Environment env) throws TemplateException { if (dateType == TemplateDateModel.UNKNOWN) { throw new TemplateException( "Unknown date type: ?" + biName + " needs a date value " + "where it's known if it's a date-only, time-only, or " + "date+time value. Use ?time, ?date or ?datetime " + "before ? " + biName + " to estabilish that.", env); } } }
public Class resolve(String className, Environment env, Template template) throws TemplateException { throw new TemplateException( "Instantiating " + className + " is not allowed in the " + "template for security reasons.", env); }
String getCurrentRecoveredErrorMesssage() throws TemplateException { if(recoveredErrorStack.isEmpty()) { throw new TemplateException( ".error is not available outside of a <#recover> block", this); } return (String) recoveredErrorStack.get(recoveredErrorStack.size() -1); }
public ConstructorFunction(String classname, Environment env, Template template) throws TemplateException { this.env = env; cl = env.getNewBuiltinClassResolver().resolve(classname, env, template); if (!TM_CLASS.isAssignableFrom(cl)) { throw new TemplateException("Class " + cl.getName() + " does not implement freemarker.template.TemplateModel", env); } if (BEAN_MODEL_CLASS.isAssignableFrom(cl)) { throw new TemplateException("Bean Models cannot be instantiated using the ?new built-in", env); } if (JYTHON_MODEL_CLASS != null && JYTHON_MODEL_CLASS.isAssignableFrom(cl)) { throw new TemplateException("Jython Models cannot be instantiated using the ?new built-in", env); } }
public static String getBlockName(Environment env, Map params, String paramName) throws TemplateException { SimpleScalar blockNameScalar = (SimpleScalar) params.get(paramName); if (blockNameScalar == null) { throw new TemplateException("This directive must have '" + paramName + "' attribute.", env); } return blockNameScalar.getAsString(); } }
private static int getClassCode(Number num) throws TemplateException { try { return ((Integer)classCodes.get(num.getClass())).intValue(); } catch(NullPointerException e) { if(num == null) { throw new TemplateException("Unknown number type null", Environment.getCurrentEnvironment()); } throw new TemplateException("Unknown number type " + num.getClass().getName(), Environment.getCurrentEnvironment()); } }
public Class resolve(String className, Environment env, Template template) throws TemplateException { try { return ClassUtil.forName(className); } catch (ClassNotFoundException e) { throw new TemplateException(e, env); } }
TemplateModel _getAsTemplateModel(Environment env) throws TemplateException { try { return env.getVariable(name); } catch (NullPointerException e) { if (env == null) { throw new TemplateException("Variables are not available " + "(certainly you are in a parse-time executed directive). The name of the variable " + "you tried to read: " + name, null); } else { throw e; } } }
private static String getRequiredStringParam(Map<String, ?> params, String name, Environment env) throws TemplateException { Object value = params.get(name); if (value instanceof SimpleScalar) { return ((SimpleScalar) value).getAsString(); } throw new TemplateException("Missing parameter: " + name, env); }
static Number getNumber(TemplateNumberModel model, Expression expr, Environment env) throws TemplateModelException, TemplateException { Number value = model.getAsNumber(); if(value == null) { throw new TemplateException(expr + " evaluated to null number.", env); } return value; }
static Date getDate(TemplateDateModel model, Expression expr, Environment env) throws TemplateModelException, TemplateException { Date value = model.getAsDate(); if(value == null) { throw new TemplateException(expr + " evaluated to null date.", env); } return value; } }
static TemplateException invalidTypeException(TemplateModel model, Expression exp, Environment env, String expected) throws TemplateException { assertNonNull(model, exp, env); return new TemplateException( "Expected " + expected + ". " + exp + " evaluated instead to " + model.getClass().getName() + " " + exp.getStartLocation() + ".", env); } /**
@Test(expected = AwsMockException.class) public void Test_getProcessTemplateExceptionWithNoData() throws Exception { Configuration config = Mockito.mock(Configuration.class); Template template = Mockito.mock(Template.class); Whitebox.setInternalState(TemplateUtils.class, "conf", config); Mockito.doNothing().when(config) .setClassForTemplateLoading(Mockito.eq(TemplateUtils.class), Mockito.anyString()); Mockito.when(config.getTemplate(Mockito.anyString())).thenReturn(template); Mockito.doThrow(new TemplateException("Forced TemplateException", null)).when(template) .process(Mockito.any(), Mockito.any(Writer.class)); TemplateUtils.get(errFTemplateFile, null); }