Skip to content

Commit 9f59054

Browse files
kweinmeisterSita04
authored andcommitted
chore: migrate GAE samples to thyme 3.1
1 parent 155fe52 commit 9f59054

8 files changed

Lines changed: 48 additions & 36 deletions

File tree

appengine-java11/gaeinfo/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Copyright 2019 Google LLC
7373
<dependency>
7474
<groupId>org.thymeleaf</groupId>
7575
<artifactId>thymeleaf</artifactId>
76-
<version>3.0.14.RELEASE</version>
76+
<version>3.1.1.RELEASE</version>
7777
<scope>provided</scope>
7878
</dependency>
7979
</dependencies>

appengine-java11/gaeinfo/src/main/java/com/example/appengine/standard/GaeInfoServlet.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
import okhttp3.Response;
3636
import org.thymeleaf.TemplateEngine;
3737
import org.thymeleaf.context.WebContext;
38-
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
38+
import org.thymeleaf.templateresolver.WebApplicationTemplateResolver;
39+
import org.thymeleaf.web.servlet.JavaxServletWebApplication;
3940

4041
@SuppressWarnings({"serial"})
4142
@WebServlet(
@@ -66,6 +67,7 @@ public class GaeInfoServlet extends HttpServlet {
6667
private final String metadata = "http://metadata.google.internal";
6768

6869
private TemplateEngine templateEngine;
70+
private JavaxServletWebApplication application;
6971

7072
// Use OkHttp from Square as it's quite easy to use for simple fetches.
7173
private final OkHttpClient ok =
@@ -76,7 +78,6 @@ public class GaeInfoServlet extends HttpServlet {
7678

7779
// Setup to pretty print returned json
7880
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
79-
private final JsonParser jp = new JsonParser();
8081

8182
// Fetch Metadata
8283
String fetchMetadata(String key) throws IOException {
@@ -101,15 +102,16 @@ String fetchJsonMetadata(String prefix) throws IOException {
101102

102103
Response response = ok.newCall(request).execute();
103104

104-
// Convert json to prety json
105-
return gson.toJson(jp.parse(response.body().string()));
105+
// Convert json to pretty json
106+
return gson.toJson(JsonParser.parseString(response.body().string()));
106107
}
107108

108109
@Override
109110
public void init() {
110111
// Setup ThymeLeaf
111-
ServletContextTemplateResolver templateResolver =
112-
new ServletContextTemplateResolver(this.getServletContext());
112+
application = JavaxServletWebApplication.buildApplication(this.getServletContext());
113+
WebApplicationTemplateResolver templateResolver =
114+
new WebApplicationTemplateResolver(application);
113115

114116
templateResolver.setPrefix("/WEB-INF/templates/");
115117
templateResolver.setSuffix(".html");
@@ -126,7 +128,8 @@ public void init() {
126128
@Override
127129
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
128130
String key;
129-
WebContext ctx = new WebContext(req, resp, getServletContext(), req.getLocale());
131+
WebContext ctx = new WebContext(application.buildExchange(req, resp));
132+
ctx.setLocale(req.getLocale());
130133

131134
resp.setContentType("text/html");
132135

@@ -149,7 +152,7 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
149152

150153
Properties properties = System.getProperties();
151154
m = new TreeMap<>();
152-
for (Enumeration e = properties.propertyNames(); e.hasMoreElements(); ) {
155+
for (Enumeration<?> e = properties.propertyNames(); e.hasMoreElements(); ) {
153156
key = (String) e.nextElement();
154157
m.put(key, (String) properties.get(key));
155158
}
@@ -168,10 +171,10 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
168171

169172
ctx.setVariable("sam", m.descendingMap());
170173

171-
// Recursivly get all info about service accounts -- Note tokens are leftout by default.
174+
// Recursively get all info about service accounts -- Note tokens are leftout by default.
172175
ctx.setVariable(
173176
"rsa", fetchJsonMetadata("/computeMetadata/v1/instance/service-accounts/?recursive=true"));
174-
// Recursivly get all data on Metadata server.
177+
// Recursively get all data on Metadata server.
175178
ctx.setVariable("ram", fetchJsonMetadata("/?recursive=true"));
176179

177180
templateEngine.process("index", ctx, resp.getWriter());

appengine-java8/gaeinfo/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Copyright 2017 Google Inc.
6868
<dependency>
6969
<groupId>org.thymeleaf</groupId>
7070
<artifactId>thymeleaf</artifactId>
71-
<version>3.0.14.RELEASE</version>
71+
<version>3.1.1.RELEASE</version>
7272
</dependency>
7373

7474
</dependencies>

appengine-java8/gaeinfo/src/main/java/com/example/appengine/standard/GaeInfoServlet.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
import okhttp3.Response;
4040
import org.thymeleaf.TemplateEngine;
4141
import org.thymeleaf.context.WebContext;
42-
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
42+
import org.thymeleaf.templateresolver.WebApplicationTemplateResolver;
43+
import org.thymeleaf.web.servlet.JavaxServletWebApplication;
4344

4445
// [START example]
4546
@SuppressWarnings({"serial"})
@@ -73,6 +74,7 @@ public class GaeInfoServlet extends HttpServlet {
7374
private final String metadata = "http://metadata.google.internal";
7475

7576
private TemplateEngine templateEngine;
77+
private JavaxServletWebApplication application;
7678

7779
// Use OkHttp from Square as it's quite easy to use for simple fetches.
7880
private final OkHttpClient ok =
@@ -83,7 +85,6 @@ public class GaeInfoServlet extends HttpServlet {
8385

8486
// Setup to pretty print returned json
8587
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
86-
private final JsonParser jp = new JsonParser();
8788

8889
// Fetch Metadata
8990
String fetchMetadata(String key) throws IOException {
@@ -109,14 +110,15 @@ String fetchJsonMetadata(String prefix) throws IOException {
109110
Response response = ok.newCall(request).execute();
110111

111112
// Convert json to prety json
112-
return gson.toJson(jp.parse(response.body().string()));
113+
return gson.toJson(JsonParser.parseString(response.body().string()));
113114
}
114115

115116
@Override
116117
public void init() {
117118
// Setup ThymeLeaf
118-
ServletContextTemplateResolver templateResolver =
119-
new ServletContextTemplateResolver(this.getServletContext());
119+
application = JavaxServletWebApplication.buildApplication(this.getServletContext());
120+
WebApplicationTemplateResolver templateResolver =
121+
new WebApplicationTemplateResolver(application);
120122

121123
templateResolver.setPrefix("/WEB-INF/templates/");
122124
templateResolver.setSuffix(".html");
@@ -134,7 +136,8 @@ public void init() {
134136
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
135137
String key = "";
136138
final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
137-
WebContext ctx = new WebContext(req, resp, getServletContext(), req.getLocale());
139+
WebContext ctx = new WebContext(application.buildExchange(req, resp));
140+
ctx.setLocale(req.getLocale());
138141

139142
resp.setContentType("text/html");
140143

@@ -183,7 +186,7 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
183186

184187
Properties properties = System.getProperties();
185188
m = new TreeMap<>();
186-
for (Enumeration e = properties.propertyNames(); e.hasMoreElements(); ) {
189+
for (Enumeration<?> e = properties.propertyNames(); e.hasMoreElements(); ) {
187190
key = (String) e.nextElement();
188191
m.put(key, (String) properties.get(key));
189192
}
@@ -210,11 +213,11 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
210213
}
211214
ctx.setVariable("sam", m.descendingMap());
212215

213-
// Recursivly get all info about service accounts -- Note tokens are leftout by default.
216+
// Recursively get all info about service accounts -- Note tokens are leftout by default.
214217
ctx.setVariable(
215218
"rsa",
216219
fetchJsonMetadata("/computeMetadata/v1/instance/service-accounts/?recursive=true"));
217-
// Recursivly get all data on Metadata server.
220+
// Recursively get all data on Metadata server.
218221
ctx.setVariable("ram", fetchJsonMetadata("/?recursive=true"));
219222
}
220223

appengine-java8/metadata/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Copyright 2017 Google Inc.
6363
<dependency>
6464
<groupId>org.thymeleaf</groupId>
6565
<artifactId>thymeleaf</artifactId>
66-
<version>3.0.14.RELEASE</version>
66+
<version>3.1.1.RELEASE</version>
6767
</dependency>
6868

6969
</dependencies>

appengine-java8/metadata/src/main/java/com/example/appengine/standard/MetadataServlet.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
import okhttp3.Response;
3232
import org.thymeleaf.TemplateEngine;
3333
import org.thymeleaf.context.WebContext;
34-
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
34+
import org.thymeleaf.templateresolver.WebApplicationTemplateResolver;
35+
import org.thymeleaf.web.servlet.JavaxServletWebApplication;
3536

3637

3738
// [START example]
@@ -63,6 +64,7 @@ public class MetadataServlet extends HttpServlet {
6364

6465
private final String metadata = "http://metadata.google.internal";
6566
private TemplateEngine templateEngine;
67+
private JavaxServletWebApplication application;
6668

6769
// Use OkHttp from Square as it's quite easy to use for simple fetches.
6870
private final OkHttpClient ok = new OkHttpClient.Builder()
@@ -74,7 +76,6 @@ public class MetadataServlet extends HttpServlet {
7476
private final Gson gson = new GsonBuilder()
7577
.setPrettyPrinting()
7678
.create();
77-
private final JsonParser jp = new JsonParser();
7879

7980
// Fetch Metadata
8081
String fetchMetadata(String key) throws IOException {
@@ -98,14 +99,15 @@ String fetchJsonMetadata(String prefix) throws IOException {
9899
Response response = ok.newCall(request).execute();
99100

100101
// Convert json to prety json
101-
return gson.toJson(jp.parse(response.body().string()));
102+
return gson.toJson(JsonParser.parseString(response.body().string()));
102103
}
103104

104105
@Override
105106
public void init() {
106107
// Setup ThymeLeaf
107-
ServletContextTemplateResolver templateResolver =
108-
new ServletContextTemplateResolver(this.getServletContext());
108+
application = JavaxServletWebApplication.buildApplication(this.getServletContext());
109+
WebApplicationTemplateResolver templateResolver =
110+
new WebApplicationTemplateResolver(application);
109111

110112
templateResolver.setPrefix("/WEB-INF/templates/");
111113
templateResolver.setSuffix(".html");
@@ -122,7 +124,8 @@ public void init() {
122124
@Override
123125
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
124126
String defaultServiceAccount = "";
125-
WebContext ctx = new WebContext(req, resp, getServletContext(), req.getLocale());
127+
WebContext ctx = new WebContext(application.buildExchange(req, resp));
128+
ctx.setLocale(req.getLocale());
126129

127130
resp.setContentType("text/html");
128131

flexible/gaeinfo/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Copyright 2017 Google Inc.
6565
<dependency>
6666
<groupId>org.thymeleaf</groupId>
6767
<artifactId>thymeleaf</artifactId>
68-
<version>3.0.14.RELEASE</version>
68+
<version>3.1.1.RELEASE</version>
6969
</dependency>
7070

7171
</dependencies>

flexible/gaeinfo/src/main/java/com/example/appengine/flex_compat/GaeInfoServlet.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
import okhttp3.Response;
4444
import org.thymeleaf.TemplateEngine;
4545
import org.thymeleaf.context.WebContext;
46-
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
46+
import org.thymeleaf.templateresolver.WebApplicationTemplateResolver;
47+
import org.thymeleaf.web.servlet.JavaxServletWebApplication;
4748

4849
@SuppressWarnings({"serial"})
4950
// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
@@ -74,6 +75,7 @@ public class GaeInfoServlet extends HttpServlet {
7475

7576
private final String metadata = "http://metadata.google.internal";
7677
private TemplateEngine templateEngine;
78+
private JavaxServletWebApplication application;
7779

7880
// Use OkHttp from Square as it's quite easy to use for simple fetches.
7981
private final OkHttpClient ok =
@@ -84,7 +86,6 @@ public class GaeInfoServlet extends HttpServlet {
8486

8587
// Setup to pretty print returned json
8688
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
87-
private final JsonParser jp = new JsonParser();
8889

8990
// Fetch Metadata
9091
String fetchMetadata(String key) throws IOException {
@@ -117,7 +118,7 @@ String fetchJsonMetadata(String prefix) throws IOException {
117118
Response response = ok.newCall(request).execute();
118119
// Convert json to prety json
119120
json = response.body().string();
120-
return gson.toJson(jp.parse(json));
121+
return gson.toJson(JsonParser.parseString(json));
121122
} catch (Exception e) {
122123
log("fetchJsonMetadata - " + metadata + prefix + " : ", e);
123124
}
@@ -127,8 +128,9 @@ String fetchJsonMetadata(String prefix) throws IOException {
127128
@Override
128129
public void init() {
129130
// Setup ThymeLeaf
130-
ServletContextTemplateResolver templateResolver =
131-
new ServletContextTemplateResolver(this.getServletContext());
131+
application = JavaxServletWebApplication.buildApplication(this.getServletContext());
132+
WebApplicationTemplateResolver templateResolver =
133+
new WebApplicationTemplateResolver(application);
132134

133135
templateResolver.setPrefix("/WEB-INF/templates/");
134136
templateResolver.setSuffix(".html");
@@ -146,7 +148,8 @@ public void init() {
146148
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
147149
String key = "";
148150
final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
149-
WebContext ctx = new WebContext(req, resp, getServletContext(), req.getLocale());
151+
WebContext ctx = new WebContext(application.buildExchange(req, resp));
152+
ctx.setLocale(req.getLocale());
150153

151154
resp.setContentType("text/html");
152155

@@ -195,7 +198,7 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
195198

196199
Properties properties = System.getProperties();
197200
m = new TreeMap<>();
198-
for (Enumeration e = properties.propertyNames(); e.hasMoreElements(); ) {
201+
for (Enumeration<?> e = properties.propertyNames(); e.hasMoreElements(); ) {
199202
key = (String) e.nextElement();
200203
m.put(key, (String) properties.get(key));
201204
}

0 commit comments

Comments
 (0)