public void execute(final Environment env, Map params,
TemplateModel[] loopVars, TemplateDirectiveBody body)
throws TemplateException, IOException
{
// Determine the path
final TemplateModel path = (TemplateModel)params.get("path");
if(path == null) {
throw new TemplateException("Missing required parameter 'path'", env);
}
if(!(path instanceof TemplateScalarModel)) {
throw new TemplateException("Expected a scalar model. 'path' is instead " +
path.getClass().getName(), env);
}
final String strPath = ((TemplateScalarModel)path).getAsString();
if(strPath == null) {
throw new TemplateException("String value of 'path' parameter is null", env);
}
// See whether we need to use a custom response (if we're inside a TTM
// or TDM or macro nested body, we'll need to as then the current
// FM environment writer is not identical to HTTP servlet response
// writer.
final Writer envOut = env.getOut();
final HttpServletResponse wrappedResponse;
if(envOut == response.getWriter()) {
// Don't bother wrapping if environment's writer is same as
// response writer
wrappedResponse = response;
}
else {
final PrintWriter printWriter = (envOut instanceof PrintWriter) ?
(PrintWriter)envOut :
new PrintWriter(envOut);
// Otherwise, create a response wrapper that will pass the
// env writer, potentially first wrapping it in a print
// writer when it ain't one already.
wrappedResponse = new HttpServletResponseWrapper(response) {
public PrintWriter getWriter() {
return printWriter;
}
};
}
// Determine inherit_params value
final boolean inheritParams;
final TemplateModel inheritParamsModel = (TemplateModel)params.get("inherit_params");
if(inheritParamsModel == null) {
// defaults to true when not specified
inheritParams = true;
}
else {
if(!(inheritParamsModel instanceof TemplateBooleanModel)) {
throw new TemplateException("'inherit_params' should be a boolean but it is " +
inheritParamsModel.getClass().getName() + " instead", env);
}
inheritParams = ((TemplateBooleanModel)inheritParamsModel).getAsBoolean();
}
// Get explicit params, if any
final TemplateModel paramsModel = (TemplateModel)params.get("params");
// Determine whether we need to wrap the request
final HttpServletRequest wrappedRequest;
if(paramsModel == null && inheritParams) {
// Inherit original request params & no params explicitly