Skip to content
This repository was archived by the owner on Dec 15, 2018. It is now read-only.

Commit d33cfb1

Browse files
committed
Adjusted view engines to provide access to HttpServletRequest
1 parent 897bbc4 commit d33cfb1

File tree

8 files changed

+100
-16
lines changed

8 files changed

+100
-16
lines changed

ext/freemarker/src/main/java/org/mvcspec/ozark/ext/freemarker/FreemarkerViewEngine.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@
1515
*/
1616
package org.mvcspec.ozark.ext.freemarker;
1717

18-
import org.mvcspec.ozark.engine.ViewEngineBase;
19-
import org.mvcspec.ozark.engine.ViewEngineConfig;
2018
import freemarker.template.Configuration;
2119
import freemarker.template.Template;
2220
import freemarker.template.TemplateException;
21+
import org.mvcspec.ozark.engine.ViewEngineBase;
22+
import org.mvcspec.ozark.engine.ViewEngineConfig;
2323

2424
import javax.enterprise.context.ApplicationScoped;
2525
import javax.inject.Inject;
2626
import javax.mvc.engine.ViewEngineContext;
2727
import javax.mvc.engine.ViewEngineException;
28+
import javax.servlet.http.HttpServletRequest;
2829
import java.io.IOException;
2930
import java.io.OutputStreamWriter;
3031
import java.io.Writer;
3132
import java.nio.charset.Charset;
33+
import java.util.HashMap;
34+
import java.util.Map;
3235

3336
/**
3437
* Class FreemarkerViewEngine.
@@ -49,10 +52,18 @@ public boolean supports(String view) {
4952

5053
@Override
5154
public void processView(ViewEngineContext context) throws ViewEngineException {
55+
5256
Charset charset = resolveCharsetAndSetContentType(context);
57+
5358
try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) {
54-
final Template template = configuration.getTemplate(resolveView(context));
55-
template.process(context.getModels(), writer);
59+
60+
Template template = configuration.getTemplate(resolveView(context));
61+
62+
Map<String, Object> model = new HashMap<>(context.getModels());
63+
model.put("request", context.getRequest(HttpServletRequest.class));
64+
65+
template.process(model, writer);
66+
5667
} catch (TemplateException | IOException e) {
5768
throw new ViewEngineException(e);
5869
}

ext/handlebars/src/main/java/org/mvcspec/ozark/ext/handlebars/HandlebarsViewEngine.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@
2222

2323
import javax.enterprise.context.ApplicationScoped;
2424
import javax.inject.Inject;
25-
import javax.mvc.Models;
2625
import javax.mvc.engine.ViewEngineContext;
2726
import javax.mvc.engine.ViewEngineException;
2827
import javax.servlet.ServletContext;
28+
import javax.servlet.http.HttpServletRequest;
2929
import java.io.BufferedReader;
3030
import java.io.IOException;
3131
import java.io.InputStream;
3232
import java.io.InputStreamReader;
3333
import java.io.OutputStreamWriter;
3434
import java.io.Writer;
3535
import java.nio.charset.Charset;
36+
import java.util.HashMap;
37+
import java.util.Map;
3638
import java.util.stream.Collectors;
3739

3840
/**
@@ -57,18 +59,23 @@ public boolean supports(String view) {
5759

5860
@Override
5961
public void processView(ViewEngineContext context) throws ViewEngineException {
60-
Models models = context.getModels();
62+
63+
Map<String, Object> model = new HashMap<>(context.getModels());
64+
model.put("request", context.getRequest(HttpServletRequest.class));
65+
6166
Charset charset = resolveCharsetAndSetContentType(context);
6267

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

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

7076
Template template = handlebars.compileInline(viewContent);
71-
template.apply(models, writer);
77+
template.apply(model, writer);
78+
7279
} catch (IOException e) {
7380
throw new ViewEngineException(e);
7481
}

ext/jade/src/main/java/org/mvcspec/ozark/ext/jade/JadeViewEngine.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@
1717

1818
import de.neuland.jade4j.JadeConfiguration;
1919
import de.neuland.jade4j.exceptions.JadeException;
20+
import de.neuland.jade4j.template.JadeTemplate;
2021
import org.mvcspec.ozark.engine.ViewEngineBase;
2122
import org.mvcspec.ozark.engine.ViewEngineConfig;
2223

2324
import javax.enterprise.context.ApplicationScoped;
2425
import javax.inject.Inject;
2526
import javax.mvc.engine.ViewEngineContext;
2627
import javax.mvc.engine.ViewEngineException;
28+
import javax.servlet.http.HttpServletRequest;
2729
import java.io.IOException;
2830
import java.io.OutputStreamWriter;
2931
import java.io.Writer;
3032
import java.nio.charset.Charset;
33+
import java.util.HashMap;
34+
import java.util.Map;
3135

3236
/**
3337
* The Jade View Engine.
@@ -50,10 +54,18 @@ public boolean supports(String view) {
5054

5155
@Override
5256
public void processView(ViewEngineContext context) throws ViewEngineException {
53-
String viewPath = resolveView(context);
57+
5458
Charset charset = resolveCharsetAndSetContentType(context);
59+
5560
try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) {
56-
jade.renderTemplate(jade.getTemplate(viewPath), context.getModels(), writer);
61+
62+
JadeTemplate template = jade.getTemplate(resolveView(context));
63+
64+
Map<String, Object> model = new HashMap<>(context.getModels());
65+
model.put("request", context.getRequest(HttpServletRequest.class));
66+
67+
jade.renderTemplate(template, model, writer);
68+
5769
} catch (JadeException | IOException ex) {
5870
throw new ViewEngineException(String.format("Could not process view %s.", context.getView()), ex);
5971
}

ext/jetbrick/src/main/java/org/mvcspec/ozark/ext/jetbrick/JetbrickViewEngine.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
import javax.mvc.engine.ViewEngineContext;
2828
import javax.mvc.engine.ViewEngineException;
2929
import javax.servlet.ServletContext;
30+
import javax.servlet.http.HttpServletRequest;
3031
import java.io.IOException;
3132
import java.io.OutputStreamWriter;
3233
import java.io.Writer;
3334
import java.nio.charset.Charset;
35+
import java.util.HashMap;
36+
import java.util.Map;
3437

3538
/**
3639
* @author Daniel Dias
@@ -55,10 +58,18 @@ public boolean supports(String view) {
5558

5659
@Override
5760
public void processView(ViewEngineContext context) throws ViewEngineException {
61+
5862
Charset charset = resolveCharsetAndSetContentType(context);
63+
5964
try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) {
65+
6066
JetTemplate template = jetEngine.getTemplate(resolveView(context));
61-
template.render(context.getModels(), writer);
67+
68+
Map<String, Object> model = new HashMap<>(context.getModels());
69+
model.put("request", context.getRequest(HttpServletRequest.class));
70+
71+
template.render(model, writer);
72+
6273
} catch (TemplateException | IOException e) {
6374
throw new ViewEngineException(e);
6475
}

ext/mustache/src/main/java/org/mvcspec/ozark/ext/mustache/MustacheViewEngine.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
import javax.inject.Inject;
2525
import javax.mvc.engine.ViewEngineContext;
2626
import javax.mvc.engine.ViewEngineException;
27+
import javax.servlet.http.HttpServletRequest;
2728
import java.io.IOException;
2829
import java.io.OutputStreamWriter;
2930
import java.io.Writer;
3031
import java.nio.charset.Charset;
32+
import java.util.HashMap;
33+
import java.util.Map;
3134

3235
/**
3336
* Class MustacheViewEngine.
@@ -48,10 +51,18 @@ public boolean supports(String view) {
4851

4952
@Override
5053
public void processView(ViewEngineContext context) throws ViewEngineException {
51-
Mustache mustache = factory.compile(resolveView(context));
54+
5255
Charset charset = resolveCharsetAndSetContentType(context);
56+
5357
try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) {
54-
mustache.execute(writer, context.getModels()).flush();
58+
59+
Mustache mustache = factory.compile(resolveView(context));
60+
61+
Map<String, Object> model = new HashMap<>(context.getModels());
62+
model.put("request", context.getRequest(HttpServletRequest.class));
63+
64+
mustache.execute(writer, model).flush();
65+
5566
} catch (IOException e) {
5667
throw new ViewEngineException(e);
5768
}

ext/pebble/src/main/java/org/mvcspec/ozark/ext/pebble/PebbleViewEngine.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.mitchellbosecke.pebble.PebbleEngine;
1919
import com.mitchellbosecke.pebble.error.PebbleException;
20+
import com.mitchellbosecke.pebble.template.PebbleTemplate;
2021
import org.mvcspec.ozark.engine.ViewEngineBase;
2122

2223
import javax.enterprise.context.ApplicationScoped;
@@ -26,7 +27,11 @@
2627
import java.io.OutputStreamWriter;
2728
import java.io.Writer;
2829
import java.nio.charset.Charset;
30+
import java.util.HashMap;
31+
import java.util.Map;
2932
import javax.inject.Inject;
33+
import javax.servlet.http.HttpServletRequest;
34+
3035
import org.mvcspec.ozark.engine.ViewEngineConfig;
3136

3237
/**
@@ -49,11 +54,18 @@ public boolean supports(String view) {
4954

5055
@Override
5156
public void processView(ViewEngineContext context) throws ViewEngineException {
52-
String viewPath = resolveView(context);
5357

5458
Charset charset = resolveCharsetAndSetContentType(context);
59+
5560
try(Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) {
56-
pebbleEngine.getTemplate(viewPath).evaluate(writer, context.getModels());
61+
62+
PebbleTemplate template = pebbleEngine.getTemplate(resolveView(context));
63+
64+
Map<String, Object> model = new HashMap<>(context.getModels());
65+
model.put("request", context.getRequest(HttpServletRequest.class));
66+
67+
template.evaluate(writer, model);
68+
5769
} catch (PebbleException | IOException ex) {
5870
throw new ViewEngineException(String.format("Could not process view %s.", context.getView()), ex);
5971
}

ext/thymeleaf/src/main/java/org/mvcspec/ozark/ext/thymeleaf/ThymeleafViewEngine.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import javax.servlet.http.HttpServletRequest;
2929
import javax.servlet.http.HttpServletResponse;
3030
import java.io.IOException;
31+
import java.util.HashMap;
32+
import java.util.Map;
3133

3234
/**
3335
* Class Thymeleaf ViewEngine.
@@ -51,12 +53,19 @@ public boolean supports(String view) {
5153

5254
@Override
5355
public void processView(ViewEngineContext context) throws ViewEngineException {
56+
5457
try {
58+
5559
HttpServletRequest request = context.getRequest(HttpServletRequest.class);
5660
HttpServletResponse response = context.getResponse(HttpServletResponse.class);
5761
WebContext ctx = new WebContext(request, response, servletContext, request.getLocale());
58-
ctx.setVariables(context.getModels());
62+
63+
Map<String, Object> model = new HashMap<>(context.getModels());
64+
model.put("request", request);
65+
ctx.setVariables(model);
66+
5967
engine.process(resolveView(context), ctx, response.getWriter());
68+
6069
} catch (IOException e) {
6170
throw new ViewEngineException(e);
6271
}

ext/velocity/src/main/java/org/mvcspec/ozark/ext/velocity/VelocityViewEngine.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
import javax.inject.Inject;
2626
import javax.mvc.engine.ViewEngineContext;
2727
import javax.mvc.engine.ViewEngineException;
28+
import javax.servlet.http.HttpServletRequest;
2829
import java.io.IOException;
2930
import java.io.OutputStreamWriter;
3031
import java.io.Writer;
3132
import java.nio.charset.Charset;
33+
import java.util.HashMap;
34+
import java.util.Map;
3235

3336
/**
3437
* Class VelocityViewEngine.
@@ -49,11 +52,19 @@ public boolean supports(String view) {
4952

5053
@Override
5154
public void processView(ViewEngineContext context) throws ViewEngineException {
55+
5256
Charset charset = resolveCharsetAndSetContentType(context);
57+
5358
try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset)) {
59+
5460
Template template = velocityEngine.getTemplate(resolveView(context));
55-
VelocityContext velocityContext = new VelocityContext(context.getModels());
61+
62+
Map<String, Object> model = new HashMap<>(context.getModels());
63+
model.put("request", context.getRequest(HttpServletRequest.class));
64+
VelocityContext velocityContext = new VelocityContext(model);
65+
5666
template.merge(velocityContext, writer);
67+
5768
} catch (IOException e) {
5869
throw new ViewEngineException(e);
5970
}

0 commit comments

Comments
 (0)