com.mitchellbosecke.pebble.error.PebbleException Java Examples
The following examples show how to use
com.mitchellbosecke.pebble.error.PebbleException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CMSURLHandler.java From fenixedu-cms with GNU Lesser General Public License v3.0 | 7 votes |
private void handleLeadingSlash(final HttpServletRequest req, HttpServletResponse res, Site sites) throws PebbleException, IOException, ServletException { if (req.getMethod().equals("GET")) { res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); res.setHeader("Location", rewritePageUrl(req)); return; } else if (req.getMethod().equals("POST")) { if (CoreConfiguration.getConfiguration().developmentMode()) { PebbleEngine engine = new PebbleEngine.Builder().loader(new StringLoader()).extension(new CMSExtensions()).build(); PebbleTemplate compiledTemplate = engine.getTemplate("<html><head></head><body><h1>POST action with backslash</h1><b>You posting data with a URL with a backslash. Alter the form to post with the same URL without the backslash</body></html>"); res.setStatus(500); res.setContentType("text/html"); compiledTemplate.evaluate(res.getWriter(), I18N.getLocale()); } else { renderer.errorPage(req, res, sites, 500); } } }
Example #2
Source File: NotificationSender.java From herd-mdl with Apache License 2.0 | 6 votes |
protected String getFormatChangeMsg( FormatChange change, int version, JobDefinition job, HiveTableSchema existing, HiveTableSchema newColumns ) throws PebbleException, IOException { PebbleTemplate template = engine.getTemplate( "templates/formatChangeNotificationTemplate.txt" ); Map<String, Object> context = new HashMap<>(); context.put( "changes", change ); context.put( "nameSpace", job.getObjectDefinition().getNameSpace() ); context.put( "tableName", job.getTableName() ); context.put( "version", version ); context.put( "existing", existing ); context.put( "new", newColumns ); StringWriter writer = new StringWriter(); template.evaluate( writer, context ); return writer.toString(); }
Example #3
Source File: TemplateUtils.java From tablesaw with Apache License 2.0 | 6 votes |
public static PebbleEngine getNewEngine() { PebbleEngine engine; try { Loader<?> loader = new ClasspathLoader(); if (templateLocations != null && !templateLocations.isEmpty()) { List<Loader<?>> loaders = new ArrayList<>(); for (String templateLocation : templateLocations) { FileLoader fileLoader = new FileLoader(); fileLoader.setPrefix(templateLocation); loaders.add(fileLoader); } // add this one last, so it is shadowed loaders.add(loader); loader = new DelegatingLoader(loaders); } engine = new PebbleEngine.Builder().loader(loader).strictVariables(false).build(); } catch (PebbleException e) { throw new IllegalStateException(e); } return engine; }
Example #4
Source File: TemplateUtils.java From tablesaw with Apache License 2.0 | 6 votes |
public static PebbleEngine getNewEngine() { PebbleEngine engine; try { Loader<?> loader = new ClasspathLoader(); if (templateLocations != null && !templateLocations.isEmpty()) { List<Loader<?>> loaders = new ArrayList<>(); for (String templateLocation : templateLocations) { FileLoader fileLoader = new FileLoader(); fileLoader.setPrefix(templateLocation); loaders.add(fileLoader); } // add this one last, so it is shadowed loaders.add(loader); loader = new DelegatingLoader(loaders); } engine = new PebbleEngine.Builder().loader(loader).strictVariables(false).build(); } catch (PebbleException e) { throw new IllegalStateException(e); } return engine; }
Example #5
Source File: PebbleTemplateEngine.java From spark-template-engines with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public String render(ModelAndView modelAndView) { Object model = modelAndView.getModel(); if (model == null || model instanceof Map) { try { StringWriter writer = new StringWriter(); PebbleTemplate template = engine.getTemplate(modelAndView.getViewName()); if (model == null) { template.evaluate(writer); } else { template.evaluate(writer, (Map<String, Object>) modelAndView.getModel()); } return writer.toString(); } catch (PebbleException | IOException e) { throw new IllegalArgumentException(e); } } else { throw new IllegalArgumentException("Invalid model, model must be instance of Map."); } }
Example #6
Source File: CMSRenderer.java From fenixedu-cms with GNU Lesser General Public License v3.0 | 6 votes |
void errorPage(final HttpServletRequest req, HttpServletResponse res, Site site, int errorCode) throws ServletException, IOException { CMSTheme cmsTheme = site.getTheme(); if (cmsTheme != null && cmsTheme.definesPath(errorCode + ".html")) { try { PebbleTemplate compiledTemplate = engine.getTemplate(cmsTheme.getType() + "/" + errorCode + ".html"); TemplateContext global = new TemplateContext(); populateSiteInfo(req, null, site, global); res.setStatus(errorCode); res.setContentType("text/html"); compiledTemplate.evaluate(res.getWriter(), global, I18N.getLocale()); } catch (PebbleException e) { throw new ServletException("Could not render error page for " + errorCode); } } else { res.sendError(errorCode, req.getRequestURI()); } }
Example #7
Source File: PebbleTemplateEngine.java From pippo with Apache License 2.0 | 6 votes |
private PebbleTemplate getTemplate(String templateName, String localePart) throws PebbleException { PebbleTemplate template = null; try { if (StringUtils.isNullOrEmpty(localePart)) { template = engine.getTemplate(templateName); } else { String localizedName = StringUtils.removeEnd(templateName, "." + getFileExtension()) + "_" + localePart; template = engine.getTemplate(localizedName); } } catch (LoaderException e) { log.debug(e.getMessage()); } return template; }
Example #8
Source File: PebbleTemplateServiceTest.java From spring-boot-email-tools with Apache License 2.0 | 5 votes |
@Test public void shouldThrowExceptionOnWrongTemplate() throws Exception { //Arrange final Map<String, Object> modelObject = new ImmutableMap.Builder<String, Object>().build(); expectedException.expect(TemplateException.class); expectedException.expectCause(instanceOf(PebbleException.class)); //Act templateService.mergeTemplateIntoString(TemplatingTestUtils.WRONG_TEMPLATE, modelObject); //Assert fail("TemplateException expected"); }
Example #9
Source File: CMSRenderer.java From fenixedu-cms with GNU Lesser General Public License v3.0 | 5 votes |
void renderCMSPage(final HttpServletRequest req, HttpServletResponse res, Site sites, String pageSlug) throws ServletException, IOException, PebbleException { if (pageSlug.startsWith("/")) { pageSlug = pageSlug.substring(1); } String[] parts = pageSlug.split("/"); String pageName = parts[0]; Page page; if (Strings.isNullOrEmpty(pageName) && sites.getInitialPage() != null) { page = sites.getInitialPage(); } else { page = sites.getPagesSet().stream().filter(p -> pageName.equals(p.getSlug())).findAny().orElse(null); } if (page == null || page.getTemplate() == null) { errorPage(req, res, sites, 404); } else if (!page.getPublished() || !page.getCanViewGroup().isMember(Authenticate.getUser())) { if (Authenticate.isLogged()) { errorPage(req, res, sites, 403); } else { errorPage(req, res, sites, 401); } } else { try { renderPage(req, pageSlug, res, sites, page, parts); } catch (ResourceNotFoundException e) { errorPage(req, res, sites, 404); } } }
Example #10
Source File: CMSRenderer.java From fenixedu-cms with GNU Lesser General Public License v3.0 | 5 votes |
private void renderPage(final HttpServletRequest req, String reqPagePath, HttpServletResponse res, Site site, Page page, String[] requestContext) throws PebbleException, IOException { TemplateContext global = new TemplateContext(); global.setRequestContext(requestContext); for (String key : req.getParameterMap().keySet()) { global.put(key, req.getParameter(key)); } global.put("page", makePageWrapper(page)); populateSiteInfo(req, page, site, global); List<TemplateContext> components = new ArrayList<TemplateContext>(); for (Component component : page.getComponentsSet()) { TemplateContext local = new TemplateContext(); component.handle(page, local, global); components.add(local); } global.put("components", components); for (RenderingPageHandler handler : HANDLERS){ handler.accept(page,global); } CMSTheme theme = site.getTheme(); PebbleTemplate compiledTemplate = engine.getTemplate(theme.getType() + "/" + page.getTemplate().getFilePath()); res.setStatus(200); res.setContentType("text/html"); compiledTemplate.evaluate(res.getWriter(), global, I18N.getLocale()); }
Example #11
Source File: PrettyTimeExtension.java From pippo with Apache License 2.0 | 5 votes |
@Override public Object apply(Object input, Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) throws PebbleException { if (input == null) { return null; } Locale locale = context.getLocale(); String result = getPrettyTime(locale).format(getFormattableObject(input)); return new SafeString(result); }
Example #12
Source File: PebbleViewEngine.java From krazo with Apache License 2.0 | 4 votes |
@Override public void processView(ViewEngineContext context) throws ViewEngineException { Charset charset = resolveCharsetAndSetContentType(context); try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) { PebbleTemplate template = pebbleEngine.getTemplate(resolveView(context)); Map<String, Object> model = new HashMap<>(context.getModels().asMap()); model.put("request", context.getRequest(HttpServletRequest.class)); template.evaluate(writer, model); } catch (PebbleException | IOException ex) { throw new ViewEngineException(String.format("Could not process view %s.", context.getView()), ex); } }
Example #13
Source File: PebbleViewEngine.java From ozark with Apache License 2.0 | 4 votes |
@Override public void processView(ViewEngineContext context) throws ViewEngineException { Charset charset = resolveCharsetAndSetContentType(context); try(Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) { PebbleTemplate template = pebbleEngine.getTemplate(resolveView(context)); Map<String, Object> model = new HashMap<>(context.getModels().asMap()); model.put("request", context.getRequest(HttpServletRequest.class)); template.evaluate(writer, model); } catch (PebbleException | IOException ex) { throw new ViewEngineException(String.format("Could not process view %s.", context.getView()), ex); } }
Example #14
Source File: Pebble.java From template-benchmark with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Setup public void setup() throws PebbleException { PebbleEngine engine = new PebbleEngine.Builder().autoEscaping(false).build(); template = engine.getTemplate("templates/stocks.pebble.html"); this.context = getContext(); }
Example #15
Source File: Pebble.java From template-benchmark with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Benchmark public String benchmark() throws PebbleException, IOException { StringWriter writer = new StringWriter(); template.evaluate(writer, context); return writer.toString(); }
Example #16
Source File: ExpectedOutputTest.java From template-benchmark with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Test public void testPebbleOutput() throws IOException, PebbleException { Pebble pebble = new Pebble(); pebble.setup(); assertOutput(pebble.benchmark()); }