throws TemplateException, IOException
{
// Determine the path
final TemplateModel path = (TemplateModel)params.get("path");
if(path == null) {
throw new _MiscTemplateException(env, "Missing required parameter \"path\"");
}
if(!(path instanceof TemplateScalarModel)) {
throw new _MiscTemplateException(env, new Object[] {
"Expected a scalar model. \"path\" is instead ",
new _DelayedFTLTypeDescription(path) });
}
final String strPath = ((TemplateScalarModel)path).getAsString();
if(strPath == null) {
throw new _MiscTemplateException(env, "String value of \"path\" parameter is null");
}
// 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 _MiscTemplateException(env, new Object[] {
"\"inherit_params\" should be a boolean but it's a(n) ",
inheritParamsModel.getClass().getName(), " instead" });
}
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
// specified, so use the original request
wrappedRequest = request;
}
else {
// In any other case, use a custom request wrapper
final Map paramsMap;
if(paramsModel != null) {
// Convert params to a Map
final Object unwrapped = DeepUnwrap.unwrap(paramsModel);
if(!(unwrapped instanceof Map)) {
throw new _MiscTemplateException(env, new Object[] {
"Expected \"params\" to unwrap into a java.util.Map. It unwrapped into ",
unwrapped.getClass().getName(), " instead." });
}
paramsMap = (Map)unwrapped;
}
else {
paramsMap = Collections.EMPTY_MAP;
}
wrappedRequest = new CustomParamsRequest(request, paramsMap,
inheritParams);
}
// Finally, do the include
try {
request.getRequestDispatcher(strPath).include(wrappedRequest,
wrappedResponse);
}
catch (ServletException e) {
throw new _MiscTemplateException(e, env);
}
}