public FreemarkerDefaultConfigurationFactory(ServletContext servletContext) { super(); // Create different loaders. final List<TemplateLoader> loaders = new ArrayList<>(); if (servletContext != null) { loaders.add(new WebappTemplateLoader(servletContext)); } loaders.add(new ClassTemplateLoader(FreemarkerDefaultConfigurationFactory.class, "/")); try { loaders.add(new FileTemplateLoader(new File("/"))); } catch (IOException e) { // NOOP } // Create Base configuration. configuration = new Configuration(); configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()]))); }
/** * Show class name and some details that are useful in template-not-found errors. * * @since 2.3.21 */ @Override public String toString() { return TemplateLoaderUtils.getClassNameForToString(this) + "(subdirPath=" + StringUtil.jQuote(subdirPath) + ", servletContext={contextPath=" + StringUtil.jQuote(getContextPath()) + ", displayName=" + StringUtil.jQuote(servletContext.getServletContextName()) + "})"; }
public Object findTemplateSource(String name) throws IOException { String fullPath = subdirPath + name; if (attemptFileAccess) { // First try to open as plain file (to bypass servlet container resource caches). try { String realPath = servletContext.getRealPath(fullPath); if (realPath != null) { File file = new File(realPath); if (file.canRead() && file.isFile()) { return file; } } } catch (SecurityException e) { ;// ignore } } // If it fails, try to open it with servletContext.getResource. URL url = null; try { url = servletContext.getResource(fullPath); } catch (MalformedURLException e) { LOG.warn("Could not retrieve resource " + StringUtil.jQuoteNoXSS(fullPath), e); return null; } return url == null ? null : new URLTemplateSource(url, getURLConnectionUsesCaches()); }
throw new TemplatePathParsingException("Template paths starting with \"{\" are reseved for future purposes"); } else { templateLoader = new WebappTemplateLoader(srvCtx, pureTemplatePath);
/** * Show class name and some details that are useful in template-not-found errors. * * @since 2.3.21 */ @Override public String toString() { return TemplateLoaderUtils.getClassNameForToString(this) + "(subdirPath=" + StringUtil.jQuote(subdirPath) + ", servletContext={contextPath=" + StringUtil.jQuote(getContextPath()) + ", displayName=" + StringUtil.jQuote(servletContext.getServletContextName()) + "})"; }
public Object findTemplateSource(String name) throws IOException { String fullPath = subdirPath + name; if (attemptFileAccess) { // First try to open as plain file (to bypass servlet container resource caches). try { String realPath = servletContext.getRealPath(fullPath); if (realPath != null) { File file = new File(realPath); if (file.canRead() && file.isFile()) { return file; } } } catch (SecurityException e) { ;// ignore } } // If it fails, try to open it with servletContext.getResource. URL url = null; try { url = servletContext.getResource(fullPath); } catch (MalformedURLException e) { LOG.warn("Could not retrieve resource " + StringUtil.jQuoteNoXSS(fullPath), e); return null; } return url == null ? null : new URLTemplateSource(url, getURLConnectionUsesCaches()); }
private Template loadFreemarkerTemplate() throws IOException { Configuration cfg = new Configuration(); cfg.setTemplateLoader(new WebappTemplateLoader(ctx)); return cfg.getTemplate(TEMPLATE_PATH); }
/** * Show class name and some details that are useful in template-not-found errors. * * @since 2.3.21 */ @Override public String toString() { return TemplateLoaderUtils.getClassNameForToString(this) + "(subdirPath=" + StringUtil.jQuote(subdirPath) + ", servletContext={contextPath=" + StringUtil.jQuote(getContextPath()) + ", displayName=" + StringUtil.jQuote(servletContext.getServletContextName()) + "})"; }
public Object findTemplateSource(String name) throws IOException { String fullPath = subdirPath + name; if (attemptFileAccess) { // First try to open as plain file (to bypass servlet container resource caches). try { String realPath = servletContext.getRealPath(fullPath); if (realPath != null) { File file = new File(realPath); if (file.canRead() && file.isFile()) { return file; } } } catch (SecurityException e) { ;// ignore } } // If it fails, try to open it with servletContext.getResource. URL url = null; try { url = servletContext.getResource(fullPath); } catch (MalformedURLException e) { LOG.warn("Could not retrieve resource " + StringUtil.jQuoteNoXSS(fullPath), e); return null; } return url == null ? null : new URLTemplateSource(url, getURLConnectionUsesCaches()); }
public FreemarkerDefaultConfigurationFactory(ServletContext servletContext) { super(); // Create different loaders. final List<TemplateLoader> loaders = new ArrayList<>(); if (servletContext != null) { loaders.add(new WebappTemplateLoader(servletContext)); } loaders.add(new ClassTemplateLoader(FreemarkerDefaultConfigurationFactory.class, "/")); try { loaders.add(new FileTemplateLoader(new File("/"))); } catch (IOException e) { // NOOP } // Create Base configuration. configuration = new Configuration(); configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()]))); }
/** * Creates freemarker configuration settings, * default output format to trigger auto-escaping policy * and template loaders. * * @param servletContext servlet context * @return freemarker configuration settings */ private Configuration getFreemarkerConfiguration(ServletContext servletContext) { Configuration configuration = new Configuration(Configuration.VERSION_2_3_26); configuration.setOutputFormat(HTMLOutputFormat.INSTANCE); List<TemplateLoader> loaders = new ArrayList<>(); loaders.add(new WebappTemplateLoader(servletContext)); loaders.add(new ClassTemplateLoader(DrillRestServer.class, "/")); try { loaders.add(new FileTemplateLoader(new File("/"))); } catch (IOException e) { logger.error("Could not set up file template loader.", e); } configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()]))); return configuration; }
@Override protected TemplateLoader newDelegate() { final WebContext webCtx = MgnlContext.getWebContextOrNull(); if (webCtx != null) { final ServletContext sc = webCtx.getServletContext(); if (sc != null) { return new WebappTemplateLoader(sc, ""); } } return null; } }
/** * Create the template loader. The default implementation will create a * {@link ClassTemplateLoader} if the template path starts with "class://", * a {@link FileTemplateLoader} if the template path starts with "file://", * and a {@link WebappTemplateLoader} otherwise. * @param templatePath the template path to create a loader for * @return a newly created template loader * @throws IOException */ protected TemplateLoader createTemplateLoader(String templatePath) throws IOException { if (templatePath.startsWith("class://")) { // substring(7) is intentional as we "reuse" the last slash return new ClassTemplateLoader(getClass(), templatePath.substring(7)); } else { if (templatePath.startsWith("file://")) { templatePath = templatePath.substring(7); return new FileTemplateLoader(new File(templatePath)); } else { return new WebappTemplateLoader(this.getServletContext(), templatePath); } } }
new MultiTemplateLoader(new TemplateLoader[]{ templatePathLoader, new WebappTemplateLoader(servletContext), new WebWorkClassTemplateLoader() }) : new MultiTemplateLoader(new TemplateLoader[]{ new WebappTemplateLoader(servletContext), new WebWorkClassTemplateLoader() });
new MultiTemplateLoader( new TemplateLoader[]{ new WebappTemplateLoader( context, ROOT_PATH ), new ClassTemplateLoader( getClass(), "/"), new URLTemplateLoader() {
throw new TemplatePathParsingException("Template paths starting with \"{\" are reseved for future purposes"); } else { templateLoader = new WebappTemplateLoader(srvCtx, pureTemplatePath);
throw new TemplatePathParsingException("Template paths starting with \"{\" are reseved for future purposes"); } else { templateLoader = new WebappTemplateLoader(srvCtx, pureTemplatePath);