3535import okhttp3 .Response ;
3636import org .thymeleaf .TemplateEngine ;
3737import 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 ());
0 commit comments