if (preprocessRequest(request, response)) { return; String templatePath = requestUrlToTemplatePath(request); locale = deduceLocale(templatePath, request, response); } catch (TemplateNotFoundException e) { if (exceptionOnMissingTemplate) { throw newServletExceptionWithFreeMarkerLogging( "Template not found for name " + StringUtil.jQuoteNoXSS(templatePath) + ".", e); } else { throw newServletExceptionWithFreeMarkerLogging( "Parsing error with template " + StringUtil.jQuoteNoXSS(templatePath) + ".", e); } catch (Exception e) { throw newServletExceptionWithFreeMarkerLogging( "Unexpected error when loading template " + StringUtil.jQuoteNoXSS(templatePath) + ".", e); ContentType templateSpecificContentType = getTemplateSpecificContentType(template); if (templateSpecificContentType != null) { contentType.httpHeaderValue + "; charset=" + getTemplateSpecificOutputEncoding(template)); } else { response.setContentType(contentType.httpHeaderValue); response.setCharacterEncoding(getTemplateSpecificOutputEncoding(template)); setBrowserCachingPolicy(response);
private void initialize() throws InitParamValueException, MalformedWebXmlException, ConflictingInitParamsException { config = createConfiguration(); final String iciInitParamValue = getInitParameter(Configuration.INCOMPATIBLE_IMPROVEMENTS_KEY); if (iciInitParamValue != null) { try { wrapper = createObjectWrapper(); if (LOG.isDebugEnabled()) { LOG.debug("Using object wrapper: " + wrapper); templatePath = getInitParameter(INIT_PARAM_TEMPLATE_PATH); if (templatePath == null && !config.isTemplateLoaderExplicitlySet()) { templatePath = InitParamParser.TEMPLATE_PATH_PREFIX_CLASS; config.setTemplateLoader(createTemplateLoader(templatePath)); } catch (Exception e) { throw new InitParamValueException(INIT_PARAM_TEMPLATE_PATH, templatePath, e); metaInfTldSources = createDefaultMetaInfTldSources(); classpathTlds = createDefaultClassPathTlds(); Enumeration initpnames = getServletConfig().getInitParameterNames(); while (initpnames.hasMoreElements()) { final String name = (String) initpnames.nextElement(); final String value = getInitParameter(name); if (name == null) { throw new MalformedWebXmlException( if (getInitParameter(Configuration.DEFAULT_ENCODING_KEY) != null) {
/** * Create the template loader. The default implementation will create a {@link ClassTemplateLoader} if the template * path starts with {@code "class://"}, a {@link FileTemplateLoader} if the template path starts with * {@code "file://"}, and a {@link WebappTemplateLoader} otherwise. Also, if * {@link Configuration#Configuration(freemarker.template.Version) incompatible_improvements} is 2.3.22 or higher, * it will create a {@link MultiTemplateLoader} if the template path starts with {@code "["}. * * @param templatePath * the template path to create a loader for * @return a newly created template loader */ protected TemplateLoader createTemplateLoader(String templatePath) throws IOException { return InitParamParser.createTemplateLoader(templatePath, getConfiguration(), getClass(), getServletContext()); }
String wrapper = getServletConfig().getInitParameter(DEPR_INITPARAM_OBJECT_WRAPPER); if (wrapper != null) { // BC if (getInitParameter(Configurable.OBJECT_WRAPPER_KEY) != null) { throw new RuntimeException("Conflicting init-params: " + Configurable.OBJECT_WRAPPER_KEY + " and " return createDefaultObjectWrapper(); } else { wrapper = getInitParameter(Configurable.OBJECT_WRAPPER_KEY); if (wrapper == null) { if (!config.isObjectWrapperExplicitlySet()) { return createDefaultObjectWrapper(); } else { return config.getObjectWrapper();
if (preprocessRequest(request, response)) { return; String path = requestUrlToTemplatePath(request); log("Requested template: " + StringUtil.jQuoteNoXSS(path)); template = config.getTemplate( path, deduceLocale(path, request, response)); } catch (FileNotFoundException e) { response.sendError(HttpServletResponse.SC_NOT_FOUND); setBrowserCachingPolicy(response); ServletContext servletContext = getServletContext(); try { TemplateModel model = createModel(wrapper, servletContext, request, response); if (preTemplateProcess(request, response, template, model)) { try { postTemplateProcess(request, response, template, model); if (config.getTemplateExceptionHandler() .getClass().getName().indexOf("Debug") != -1) { this.log("Error executing FreeMarker template", te); } else { ServletException e = new ServletException(
public void init() throws ServletException { try { config = createConfiguration(); wrapper = createObjectWrapper(); if (logger.isDebugEnabled()) { logger.debug("Using object wrapper of class " + wrapper.getClass().getName()); templatePath = getInitParameter(INITPARAM_TEMPLATE_PATH); if (templatePath == null) templatePath = "class://"; config.setTemplateLoader(createTemplateLoader(templatePath)); Enumeration initpnames = getServletConfig().getInitParameterNames(); while (initpnames.hasMoreElements()) { String name = (String) initpnames.nextElement(); String value = getInitParameter(name); if (getInitParameter(Configuration.DEFAULT_ENCODING_KEY) != null) { throw new ServletException( "Conflicting init-params: " if (getInitParameter(Configuration.TEMPLATE_UPDATE_DELAY_KEY) != null) { throw new ServletException( "Conflicting init-params: " if (getInitParameter(Configurable.TEMPLATE_EXCEPTION_HANDLER_KEY) != null) { throw new ServletException( "Conflicting init-params: "
if (this.servletContextModel == null) { servletContextModel = new ServletContextHashModel(this, objectWrapper); taglibFactory = createTaglibFactory(objectWrapper, servletContext); servletContext.setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibFactory); initializeServletContext(request, response); if (sessionModel == null || sessionModel.isOrphaned(session)) { sessionModel = new HttpSessionHashModel(session, objectWrapper); initializeSessionAndInstallModel(request, response, sessionModel, session); request.setAttribute( ATTR_REQUEST_PARAMETERS_MODEL, createRequestParametersHashModel(request));
ATTR_JSP_TAGLIBS_MODEL, taglibs); initializeServletContext(request, response); if (sessionModel == null || sessionModel.isOrphaned(session)) { sessionModel = new HttpSessionHashModel(session, wrapper); initializeSessionAndInstallModel(request, response, sessionModel, session); request.setAttribute( ATTR_REQUEST_PARAMETERS_MODEL, createRequestParametersHashModel(request));
String wrapper = getServletConfig().getInitParameter(DEPR_INITPARAM_OBJECT_WRAPPER); if (wrapper != null) { // BC if (getInitParameter(Configurable.OBJECT_WRAPPER_KEY) != null) { throw new RuntimeException("Conflicting init-params: " + Configurable.OBJECT_WRAPPER_KEY + " and " wrapper = getInitParameter(Configurable.OBJECT_WRAPPER_KEY); if (wrapper == null) {
@Override public void init() throws ServletException { super.init(); /* * we here need to inject our own template loader. We cannot do this in createConfiguration() as we would like, * because the FreemarkerServlet sets the template loader in the init() *after* createConfiguration() again, to the default * WebappTemplateLoader */ Configuration conf = super.getConfiguration(); TemplateLoader defaultLoader = conf.getTemplateLoader(); // repository template loader TemplateLoader repositoryLoader = new RepositoryTemplateLoader(); TemplateLoader[] loaders = new TemplateLoader[] { defaultLoader, repositoryLoader }; TemplateLoader multiLoader = new MultiTemplateLoader(loaders); conf.setTemplateLoader(multiLoader); conf.setLocalizedLookup(false); }
@Override protected TemplateModel createModel(ObjectWrapper wrapper, ServletContext servletContext, final HttpServletRequest request, final HttpServletResponse response) throws TemplateModelException { if (!lookupVirtualWebappLibResourcePathsEnabled) { return super.createModel(wrapper, servletContext, request, response); } TemplateModel params = super.createModel(wrapper, servletContext, request, response); if (params instanceof AllHttpScopesHashModel) { ((AllHttpScopesHashModel) params).putUnlistedModel(KEY_JSP_TAGLIBS, (TemplateModel) servletContext.getAttribute(ATTR_JSP_TAGLIBS_MODEL)); } return params; }
/** * Special dispatch info is included when the request contains the attribute {@link * ContainerConstants#DISPATCH_URI_PROTOCOL}. For example this value is 'classpath:' or 'jcr:' or 'webfile:' to * load a template from a classpath or repository */ @Override protected String requestUrlToTemplatePath(HttpServletRequest request) throws ServletException { String path = super.requestUrlToTemplatePath(request); if (request.getAttribute(ContainerConstants.DISPATCH_URI_PROTOCOL) != null) { path = request.getAttribute(ContainerConstants.DISPATCH_URI_PROTOCOL) + path; } return path; }
throws ServletException, IOException boolean result=super.preTemplateProcess(request, response, template, templateModel);
/** * Overrides {@link FreemarkerServlet#createTemplateLoader(String)} in order to use {@link MultiTemplateLoader} * instead which cascades {@link HstClassTemplateLoader} and {@link org.hippoecm.hst.freemarker.jcr.JcrTemplateLoader} * until it finds a template by the <code>templatePath</code>. */ @Override protected TemplateLoader createTemplateLoader(String templatePath) throws IOException { final String[] prefixExclusions = { FREEMARKER_CLASSPATH_TEMPLATE_PROTOCOL, FREEMARKER_JCR_TEMPLATE_PROTOCOL, FREEMARKER_WEB_FILE_TEMPLATE_PROTOCOL}; final TemplateLoader[] loaders = { new DelegatingTemplateLoader(super.createTemplateLoader(templatePath), null, prefixExclusions), new HstClassTemplateLoader(getClass()), new JcrTemplateLoader(), new WebFileTemplateLoader()}; return new MultiTemplateLoader(loaders); } }
if (this.servletContextModel == null) { servletContextModel = new ServletContextHashModel(this, objectWrapper); taglibFactory = createTaglibFactory(objectWrapper, servletContext); servletContext.setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibFactory); initializeServletContext(request, response); if (sessionModel == null || sessionModel.isOrphaned(session)) { sessionModel = new HttpSessionHashModel(session, objectWrapper); initializeSessionAndInstallModel(request, response, sessionModel, session); request.setAttribute( ATTR_REQUEST_PARAMETERS_MODEL, createRequestParametersHashModel(request));
String wrapper = getServletConfig().getInitParameter(DEPR_INITPARAM_OBJECT_WRAPPER); if (wrapper != null) { // BC if (getInitParameter(Configurable.OBJECT_WRAPPER_KEY) != null) { throw new RuntimeException("Conflicting init-params: " + Configurable.OBJECT_WRAPPER_KEY + " and " return createDefaultObjectWrapper(); } else { wrapper = getInitParameter(Configurable.OBJECT_WRAPPER_KEY); if (wrapper == null) { if (!config.isObjectWrapperExplicitlySet()) { return createDefaultObjectWrapper(); } else { return config.getObjectWrapper();
@Override public void init(final ServletConfig config) throws ServletException { super.init(config); Configuration conf = super.getConfiguration();
protected TemplateModel createModel(ObjectWrapper objectWrapper, ServletContext servletContext, final HttpServletRequest request, final HttpServletResponse response) throws TemplateModelException { AllHttpScopesHashModel params = (AllHttpScopesHashModel) super.createModel(objectWrapper, servletContext, request, response); for (String key : FREEMARKER_EXT_METHODS.keySet()) { params.putUnlistedModel(key, FREEMARKER_EXT_METHODS.get(key)); } params.putUnlistedModel("urlEncode", URLENCODEMETHOD); params.putUnlistedModel("sleep", SLEEPMETHOD); return params; }
protected String requestUrlToTemplatePath(HttpServletRequest request) throws ServletException { String templatePath = super.requestUrlToTemplatePath(request); if (templatePath != null && !"".equals(templatePath) && REWRITE_ARRAYS != null && REWRITE_ARRAYS.size() > 0) { // 计算rewrite for (Object object : REWRITE_ARRAYS) { JSONObject rewrite = (JSONObject) object; Pattern p = Pattern.compile(rewrite.getString("url")); Matcher m = p.matcher(templatePath); if (m.find()) { for (int i = 0; i <= m.groupCount(); i++) { request.setAttribute("rewriteParams" + i, m.group(i)); } request.setAttribute("rewriteName", rewrite.getString("name")); return rewrite.getString("page"); } } } if (!templatePath.endsWith(".html")) { templatePath += ".html"; } return templatePath; } }
boolean result = super.preTemplateProcess(request, response, template, templateModel);