javax.mvc.engine.ViewEngineContext Java Examples

The following examples show how to use javax.mvc.engine.ViewEngineContext. 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: HandlebarsViewEngine.java    From ozark with Apache License 2.0 6 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {

    Map<String, Object> model = new HashMap<>(context.getModels().asMap());
    model.put("request", context.getRequest(HttpServletRequest.class));
    
    Charset charset = resolveCharsetAndSetContentType(context);

    try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset);
         
        InputStream resourceAsStream = servletContext.getResourceAsStream(resolveView(context));
        InputStreamReader in = new InputStreamReader(resourceAsStream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(in);) {

        String viewContent = bufferedReader.lines().collect(Collectors.joining());

        Template template = handlebars.compileInline(viewContent);
        template.apply(model, writer);
        
    } catch (IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #2
Source File: ThymeleafViewEngine.java    From krazo with Apache License 2.0 6 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {
    try {
        HttpServletRequest request = context.getRequest(HttpServletRequest.class);
        HttpServletResponse response = context.getResponse(HttpServletResponse.class);

        CDIWebContext ctx = new CDIWebContext(beanManager, request, response, servletContext, context.getLocale());

        Map<String, Object> model = new HashMap<>(context.getModels().asMap());
        model.put("request", request);
        ctx.setVariables(model);

        try {
            engine.process(resolveView(context), ctx, response.getWriter());
            response.flushBuffer();
        } finally {
            ctx.close();
        }
    } catch (IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #3
Source File: AsciiDocViewEngine.java    From krazo with Apache License 2.0 6 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {
    Charset charset = resolveCharsetAndSetContentType(context);
    try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset);
         InputStream is = servletContext.getResourceAsStream(resolveView(context));
         InputStreamReader isr = new InputStreamReader(is, "UTF-8");
         BufferedReader reader = new BufferedReader(isr)) {

        Options options = new Options();
        options.setAttributes(new HashMap<>(context.getModels().asMap()));

        asciidoctor.convert(reader, writer, options);
    } catch (IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #4
Source File: HandlebarsViewEngine.java    From krazo with Apache License 2.0 6 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {
    handlebars.with(new ServletContextTemplateLoader(servletContext, getViewFolder(context)));

    Map<String, Object> model = new HashMap<>(context.getModels().asMap());
    model.put("request", context.getRequest(HttpServletRequest.class));

    Charset charset = resolveCharsetAndSetContentType(context);

    try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset);

        InputStream resourceAsStream = servletContext.getResourceAsStream(resolveView(context));
        InputStreamReader in = new InputStreamReader(resourceAsStream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(in);) {

        String viewContent = bufferedReader.lines().collect(Collectors.joining());

        Template template = handlebars.compileInline(viewContent);
        template.apply(model, writer);

    } catch (IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #5
Source File: AsciiDocViewEngine.java    From ozark with Apache License 2.0 6 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {
    Charset charset = resolveCharsetAndSetContentType(context);
    try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset);
         InputStream is = servletContext.getResourceAsStream(resolveView(context));
         InputStreamReader isr = new InputStreamReader(is, "UTF-8");
         BufferedReader reader = new BufferedReader(isr)) {

        Options options = new Options();
        options.setAttributes(new HashMap<>(context.getModels().asMap()));

        asciidoctor.convert(reader, writer, options);
    } catch (IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #6
Source File: ThymeleafViewEngine.java    From ozark with Apache License 2.0 6 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {
    try {
        HttpServletRequest request = context.getRequest(HttpServletRequest.class);
        HttpServletResponse response = context.getResponse(HttpServletResponse.class);

        CDIWebContext ctx = new CDIWebContext(beanManager, request, response, servletContext, context.getLocale());

        Map<String, Object> model = new HashMap<>(context.getModels().asMap());
        model.put("request", request);
        ctx.setVariables(model);

        try {
            engine.process(resolveView(context), ctx, response.getWriter());
            response.flushBuffer();
        } finally {
            ctx.close();
        }
    } catch (IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #7
Source File: ViewEngineJspImpl.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
@Override
public void processView(ViewEngineContext viewEngineContext) throws ViewEngineException {

	String view = viewEngineContext.getView();

	String viewFolder = (String) configuration.getProperty(ViewEngine.VIEW_FOLDER);

	if (viewFolder == null) {
		viewFolder = ViewEngine.DEFAULT_VIEW_FOLDER;
	}

	String viewPath = viewFolder.concat(view);

	PortletRequestDispatcher requestDispatcher = portletContext.getRequestDispatcher(viewPath);

	try {
		requestDispatcher.include(viewEngineContext.getRequest(PortletRequest.class),
			viewEngineContext.getResponse(PortletResponse.class));
	}
	catch (Exception e) {
		throw new ViewEngineException(e);
	}
}
 
Example #8
Source File: ReactViewEngine.java    From ozark-react with MIT License 5 votes vote down vote up
private String resolveView(final ViewEngineContext context, final String view) {
    if (!view.startsWith("/")) {
        String viewFolder = (String) context.getConfiguration().getProperty(VIEW_FOLDER);
        viewFolder = viewFolder == null ? DEFAULT_VIEW_FOLDER : viewFolder;
        viewFolder += !viewFolder.endsWith("/") ? "/" : "";
        return viewFolder + view;
    }
    return view;
}
 
Example #9
Source File: GroovyViewEngine.java    From ozark with Apache License 2.0 5 votes vote down vote up
public void processView(ViewEngineContext context) throws ViewEngineException {

        Map<String, Object> model = new HashMap<>(context.getModels().asMap());
        model.put("request", context.getRequest(HttpServletRequest.class));
        Charset charset = resolveCharsetAndSetContentType(context);
        try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset);
            InputStream resourceAsStream = servletContext.getResourceAsStream(resolveView(context));
            InputStreamReader in = new InputStreamReader(resourceAsStream, "UTF-8");) {
            Template template = markupTemplateEngine.createTemplate(in);
            Writable output = template.make(model);
            output.writeTo(writer);
        } catch (IOException | CompilationFailedException | ClassNotFoundException e) {
            throw new ViewEngineException(e);
        }
    }
 
Example #10
Source File: JspViewEngine.java    From ozark with Apache License 2.0 5 votes vote down vote up
/**
 * Forwards request to servlet container.
 *
 * @param context view engine context.
 * @throws ViewEngineException if any error occurs.
 */
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {
    try {
        forwardRequest(context, "*.jsp", "*.jspx");
    } catch (ServletException | IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #11
Source File: ViewEngineBaseTest.java    From ozark with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveViewCustomFolder() {
	ViewEngineContext ctx = EasyMock.createMock(ViewEngineContext.class);
	Configuration config = EasyMock.createMock(Configuration.class);
	expect(config.getProperty(eq(ViewEngine.VIEW_FOLDER))).andReturn("/somewhere/else");
	expect(ctx.getConfiguration()).andReturn(config);
	expect(ctx.getView()).andReturn("index.jsp");
	replay(ctx, config);
	assertThat(viewEngineBase.resolveView(ctx), is("/somewhere/else/index.jsp"));
	verify(ctx, config);
}
 
Example #12
Source File: ViewEngineBaseTest.java    From ozark with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveView() {
	ViewEngineContext ctx = EasyMock.createMock(ViewEngineContext.class);
	Configuration config = EasyMock.createMock(Configuration.class);
	expect(config.getProperty(eq(ViewEngine.VIEW_FOLDER))).andReturn(null);
	expect(ctx.getConfiguration()).andReturn(config);
	expect(ctx.getView()).andReturn("index.jsp");
	expect(ctx.getView()).andReturn("/somewhere/else/index.jsp");
	replay(ctx, config);
	assertThat(viewEngineBase.resolveView(ctx), is("/WEB-INF/views/index.jsp"));
	assertThat(viewEngineBase.resolveView(ctx), is("/somewhere/else/index.jsp"));
	verify(ctx, config);
}
 
Example #13
Source File: FaceletsViewEngine.java    From ozark with Apache License 2.0 5 votes vote down vote up
/**
 * Forwards request to servlet container.
 *
 * @param context view engine context.
 * @throws ViewEngineException if any error occurs.
 */
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {
    try {
        forwardRequest(context, "*.xhtml");
    } catch (ServletException | IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #14
Source File: ThymeleafViewEngine.java    From generator-jvm with MIT License 5 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {
  Try.run(() -> {

    final HttpServletRequest request = context.getRequest();
    final HttpServletResponse response = context.getResponse();
    final WebContext webContext = new WebContext(request, response, servletContext, request.getLocale());

    webContext.setVariables(context.getModels());
    request.setAttribute("view", context.getView());
    engine.process(/*layout*/ "default", webContext, response.getWriter());

  }).getOrElseThrow(ViewEngineException::new);
}
 
Example #15
Source File: ViewEngineContextProducer.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Dependent
@Produces
ViewEngineContext getViewEngineContext(Configuration configuration, PortletRequest portletRequest,
	MimeResponse mimeResponse, ModelsImpl modelsImpl) {
	return new ViewEngineContextImpl(configuration, portletRequest, mimeResponse, modelsImpl,
			portletRequest.getLocale());
}
 
Example #16
Source File: ViewEngineBase.java    From ozark with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves a view path based on {@link javax.mvc.engine.ViewEngine#VIEW_FOLDER}
 * in the active configuration. If the view is absolute, starts with '/', then
 * it is returned unchanged.
 *
 * @param context view engine context.
 * @return resolved view.
 */
protected String resolveView(ViewEngineContext context) {
    final String view = context.getView();
    if (!PathUtils.hasStartingSlash(view)) {        // Relative?
        final String viewFolder = PropertyUtils.getProperty(context.getConfiguration(), VIEW_FOLDER, DEFAULT_VIEW_FOLDER);
        return PathUtils.ensureEndingSlash(viewFolder) + view;
    }
    return view;
}
 
Example #17
Source File: TrimouViewEngine.java    From trimou with Apache License 2.0 5 votes vote down vote up
@Override
public void processView(ViewEngineContext context)
        throws ViewEngineException {
    try {
        Models models = context.getModels();
        models.put("request", context.getRequest());
        models.put("locale", context.getRequest().getLocale());
        Writer writer = context.getResponse().getWriter();
        engine.getMustache(context.getView()).render(writer, models);
        writer.flush();
    } catch (IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #18
Source File: JspViewEngine.java    From krazo with Apache License 2.0 5 votes vote down vote up
/**
 * Forwards request to servlet container.
 *
 * @param context view engine context.
 * @throws ViewEngineException if any error occurs.
 */
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {
    try {
        forwardRequest(context, "*.jsp", "*.jspx");
    } catch (ServletException | IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #19
Source File: FaceletsViewEngine.java    From krazo with Apache License 2.0 5 votes vote down vote up
/**
 * Forwards request to servlet container.
 *
 * @param context view engine context.
 * @throws ViewEngineException if any error occurs.
 */
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {
    try {
        forwardRequest(context, "*.xhtml");
    } catch (ServletException | IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #20
Source File: ViewEngineBaseTest.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveView() {
	ViewEngineContext ctx = EasyMock.createMock(ViewEngineContext.class);
	Configuration config = EasyMock.createMock(Configuration.class);
	expect(config.getProperty(eq(ViewEngine.VIEW_FOLDER))).andReturn(null);
	expect(ctx.getConfiguration()).andReturn(config);
	expect(ctx.getView()).andReturn("index.jsp");
	expect(ctx.getView()).andReturn("/somewhere/else/index.jsp");
	replay(ctx, config);
	assertThat(viewEngineBase.resolveView(ctx), is("/WEB-INF/views/index.jsp"));
	assertThat(viewEngineBase.resolveView(ctx), is("/somewhere/else/index.jsp"));
	verify(ctx, config);
}
 
Example #21
Source File: ViewEngineBaseTest.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveViewCustomFolder() {
	ViewEngineContext ctx = EasyMock.createMock(ViewEngineContext.class);
	Configuration config = EasyMock.createMock(Configuration.class);
	expect(config.getProperty(eq(ViewEngine.VIEW_FOLDER))).andReturn("/somewhere/else");
	expect(ctx.getConfiguration()).andReturn(config);
	expect(ctx.getView()).andReturn("index.jsp");
	replay(ctx, config);
	assertThat(viewEngineBase.resolveView(ctx), is("/somewhere/else/index.jsp"));
	verify(ctx, config);
}
 
Example #22
Source File: MustacheViewEngine.java    From krazo with Apache License 2.0 4 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {

    Charset charset = resolveCharsetAndSetContentType(context);

    try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) {

        Mustache mustache = factory.compile(resolveView(context));

        Map<String, Object> model = new HashMap<>(context.getModels().asMap());
        model.put("request", context.getRequest(HttpServletRequest.class));

        mustache.execute(writer, model).flush();

    } catch (IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #23
Source File: ThymeleafViewEngine.java    From portals-pluto with Apache License 2.0 4 votes vote down vote up
@Override
public void processView(ViewEngineContext viewEngineContext) throws ViewEngineException {
	templateEngine.process(viewEngineContext.getView(), webContext, writerSupplier.get(viewEngineContext));
}
 
Example #24
Source File: JadeViewEngine.java    From ozark with Apache License 2.0 4 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {

    Charset charset = resolveCharsetAndSetContentType(context);

    try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) {

        JadeTemplate template = jade.getTemplate(resolveView(context));

        Map<String, Object> model = new HashMap<>(context.getModels().asMap());
        model.put("request", context.getRequest(HttpServletRequest.class));

        jade.renderTemplate(template, model, writer);

    } catch (JadeException | IOException ex) {
        throw new ViewEngineException(String.format("Could not process view %s.", context.getView()), ex);
    }
}
 
Example #25
Source File: MustacheViewEngine.java    From ozark with Apache License 2.0 4 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {

    Charset charset = resolveCharsetAndSetContentType(context);

    try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) {

        Mustache mustache = factory.compile(resolveView(context));

        Map<String, Object> model = new HashMap<>(context.getModels().asMap());
        model.put("request", context.getRequest(HttpServletRequest.class));

        mustache.execute(writer, model).flush();

    } catch (IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #26
Source File: JetbrickViewEngine.java    From ozark with Apache License 2.0 4 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {

    Charset charset = resolveCharsetAndSetContentType(context);

    try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) {

        JetTemplate template = jetEngine.getTemplate(resolveView(context));

        Map<String, Object> model = new HashMap<>(context.getModels().asMap());
        model.put("request", context.getRequest(HttpServletRequest.class));

        template.render(model, writer);

    } catch (TemplateException | IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #27
Source File: VelocityViewEngine.java    From ozark with Apache License 2.0 4 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {
    
    Charset charset = resolveCharsetAndSetContentType(context);
    
    try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) {
        
        Template template = velocityEngine.getTemplate(resolveView(context));

        Map<String, Object> model = new HashMap<>(context.getModels().asMap());
        model.put("request", context.getRequest(HttpServletRequest.class));
        VelocityContext velocityContext = new VelocityContext(model);
        
        template.merge(velocityContext, writer);
        
    } catch (IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #28
Source File: FreemarkerViewEngine.java    From ozark with Apache License 2.0 4 votes vote down vote up
@Override
public void processView(ViewEngineContext context) throws ViewEngineException {

    Charset charset = resolveCharsetAndSetContentType(context);

    try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) {

        Template template = configuration.getTemplate(resolveView(context));

        Map<String, Object> model = new HashMap<>(context.getModels().asMap());
        model.put("request", context.getRequest(HttpServletRequest.class));

        template.process(model, writer);

    } catch (TemplateException | IOException e) {
        throw new ViewEngineException(e);
    }
}
 
Example #29
Source File: JtwigViewEngine.java    From ozark with Apache License 2.0 4 votes vote down vote up
public void processView(ViewEngineContext context) throws ViewEngineException {

        try {

            Map<String, Object> model = new HashMap<>(context.getModels().asMap());
            model.put("request", context.getRequest(HttpServletRequest.class));

            HttpServletRequest request = context.getRequest(HttpServletRequest.class);
            HttpServletResponse response = context.getResponse(HttpServletResponse.class);

            jtwigRenderer.dispatcherFor(resolveView(context)).with(model).render(request,response);

        } catch (ServletException | IOException e) {
            throw new ViewEngineException(e);
		}
    }
 
Example #30
Source File: PebbleViewEngine.java    From ozark with Apache License 2.0 4 votes vote down vote up
@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);
  }
}