Skip to content

Commit 1300ab8

Browse files
ludochgae-java-bot
authored andcommitted
Add a new guestbook application using Javax and Jakarta EE APIs for testing JSP staging and execution phases, and the related e2e test.
PiperOrigin-RevId: 799314774 Change-Id: Ic900cd84f316aa40c6f388336254910f200551ba
1 parent 69dfcdb commit 1300ab8

30 files changed

Lines changed: 1834 additions & 31 deletions

File tree

applications/guestbook/pom.xml

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2021 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
22+
<modelVersion>4.0.0</modelVersion>
23+
<packaging>war</packaging>
24+
<parent>
25+
<groupId>com.google.appengine</groupId>
26+
<artifactId>applications</artifactId>
27+
<version>2.0.39-SNAPSHOT</version>
28+
</parent>
29+
<groupId>com.google.appengine.demos</groupId>
30+
<artifactId>guestbook</artifactId>
31+
<name>AppEngine :: guestbook</name>
32+
33+
<prerequisites>
34+
<maven>3.6.0</maven>
35+
</prerequisites>
36+
37+
<properties>
38+
<maven.deploy.skip>true</maven.deploy.skip>
39+
<appengine.target.version>2.0.39-SNAPSHOT</appengine.target.version>
40+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
41+
</properties>
42+
43+
<dependencies>
44+
<!-- Compile/runtime dependencies -->
45+
<dependency>
46+
<groupId>com.google.appengine</groupId>
47+
<artifactId>appengine-api-1.0-sdk</artifactId>
48+
<version>${appengine.target.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>javax.servlet</groupId>
52+
<artifactId>javax.servlet-api</artifactId>
53+
<version>3.1.0</version>
54+
<scope>provided</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>jstl</groupId>
58+
<artifactId>jstl</artifactId>
59+
<version>1.2</version>
60+
</dependency>
61+
62+
<!-- Test Dependencies -->
63+
<dependency>
64+
<groupId>junit</groupId>
65+
<artifactId>junit</artifactId>
66+
<version>4.13.2</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.mockito</groupId>
71+
<artifactId>mockito-all</artifactId>
72+
<version>2.0.2-beta</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>com.google.appengine</groupId>
77+
<artifactId>appengine-testing</artifactId>
78+
<version>${appengine.target.version}</version>
79+
<scope>test</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>com.google.appengine</groupId>
83+
<artifactId>appengine-api-stubs</artifactId>
84+
<version>${appengine.target.version}</version>
85+
<scope>test</scope>
86+
</dependency>
87+
</dependencies>
88+
89+
<build>
90+
<outputDirectory>target/${project.artifactId}-${project.version}/WEB-INF/classes</outputDirectory>
91+
92+
<plugins>
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-surefire-plugin</artifactId>
96+
<version>3.2.2</version>
97+
<configuration>
98+
<argLine>
99+
--add-opens java.base/java.lang=ALL-UNNAMED
100+
--add-opens java.base/java.nio.charset=ALL-UNNAMED
101+
--add-opens java.base/java.util=ALL-UNNAMED
102+
--add-opens java.base/java.util.concurrent=ALL-UNNAMED
103+
</argLine>
104+
</configuration>
105+
</plugin>
106+
<plugin>
107+
<groupId>org.apache.maven.plugins</groupId>
108+
<artifactId>maven-war-plugin</artifactId>
109+
<version>3.4.0</version>
110+
<configuration>
111+
<archiveClasses>true</archiveClasses>
112+
<webResources>
113+
<!-- in order to interpolate version from pom into appengine-web.xml -->
114+
<resource>
115+
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
116+
<filtering>true</filtering>
117+
<targetPath>WEB-INF</targetPath>
118+
</resource>
119+
</webResources>
120+
</configuration>
121+
</plugin>
122+
123+
<plugin>
124+
<groupId>com.google.cloud.tools</groupId>
125+
<artifactId>appengine-maven-plugin</artifactId>
126+
<version>2.5.0</version>
127+
<configuration>
128+
<projectId>ludo-in-in</projectId>
129+
<version>guestbook</version>
130+
<promote>false</promote>
131+
<automaticRestart>true</automaticRestart>
132+
<jvmFlags>
133+
<jvmFlag>-Xdebug</jvmFlag>
134+
<jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>
135+
</jvmFlags>
136+
</configuration>
137+
</plugin>
138+
<plugin>
139+
<groupId>org.apache.maven.plugins</groupId>
140+
<artifactId>maven-compiler-plugin</artifactId>
141+
<version>3.11.0</version>
142+
<configuration>
143+
<source>1.8</source>
144+
<target>1.8</target>
145+
</configuration>
146+
</plugin>
147+
</plugins>
148+
</build>
149+
150+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.appengine.demos.guestbook;
18+
19+
import com.google.appengine.api.users.User;
20+
import com.google.appengine.api.users.UserService;
21+
import com.google.appengine.api.users.UserServiceFactory;
22+
23+
import java.io.IOException;
24+
import java.util.Properties;
25+
26+
import javax.servlet.http.HttpServlet;
27+
import javax.servlet.http.HttpServletRequest;
28+
import javax.servlet.http.HttpServletResponse;
29+
30+
public class GuestbookServlet extends HttpServlet {
31+
@Override
32+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
33+
throws IOException {
34+
if (req.getParameter("testing") == null) {
35+
resp.setContentType("text/plain");
36+
resp.getWriter().println("Hello, this is a testing servlet. \n\n");
37+
Properties p = System.getProperties();
38+
p.list(resp.getWriter());
39+
40+
} else {
41+
UserService userService = UserServiceFactory.getUserService();
42+
User currentUser = userService.getCurrentUser();
43+
44+
if (currentUser != null) {
45+
resp.setContentType("text/plain");
46+
resp.getWriter().println("Hello, " + currentUser.getNickname());
47+
} else {
48+
resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
49+
}
50+
}
51+
}
52+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.appengine.demos.guestbook;
17+
18+
import java.io.IOException;
19+
import java.io.PrintWriter;
20+
import java.util.Enumeration;
21+
import java.util.Iterator;
22+
import java.util.Map;
23+
import java.util.logging.Level;
24+
import java.util.logging.Logger;
25+
import javax.servlet.ServletException;
26+
import javax.servlet.annotation.WebServlet;
27+
import javax.servlet.http.Cookie;
28+
import javax.servlet.http.HttpServlet;
29+
import javax.servlet.http.HttpServletRequest;
30+
import javax.servlet.http.HttpServletResponse;
31+
32+
@WebServlet(name = "viewer", urlPatterns = {"/view"})
33+
public class ServletViewer extends HttpServlet {
34+
35+
/**
36+
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
37+
* methods.
38+
*
39+
* @param request servlet request
40+
* @param response servlet response
41+
* @throws ServletException if a servlet-specific error occurs
42+
* @throws IOException if an I/O error occurs
43+
*/
44+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
45+
throws Exception {
46+
response.setContentType("text/html;charset=UTF-8");
47+
try ( PrintWriter out = response.getWriter()) {
48+
out.println("<!DOCTYPE html>");
49+
out.println("<html>");
50+
out.println("<head>");
51+
out.println("<title>Servlet System Viewer.</title>");
52+
out.println("</head>");
53+
out.println("<body>");
54+
out.println("<h1>System.getProperties()</h1>");
55+
out.println("<ul>");
56+
Iterator keys = System.getProperties().keySet().iterator();
57+
while (keys.hasNext()) {
58+
String key = (String) keys.next();
59+
String value = System.getProperty(key).replaceAll("\\+", " ");
60+
out.println("<li>" + key + "=" + value);
61+
}
62+
out.println("</ul>");
63+
64+
out.println("<h1>System.getenv()</h1>");
65+
out.println("<ul>");
66+
Map<String, String> variables = System.getenv();
67+
68+
variables.entrySet().stream().forEach((entry) -> {
69+
String name = entry.getKey();
70+
String value = entry.getValue();
71+
out.println("<li>" + name + "=" + value);
72+
});
73+
out.println("</ul>");
74+
75+
out.println("<h1>Headers</h1>");
76+
out.println("<ul>");
77+
for (Enumeration<String> e = request.getHeaderNames(); e.hasMoreElements();) {
78+
String key = e.nextElement();
79+
out.println("<li>" + key + "=" + request.getHeader(key));
80+
}
81+
out.println("</ul>");
82+
83+
out.println("<h1>Cookies</h1>");
84+
out.println("<ul>");
85+
Cookie[] cookies = request.getCookies();
86+
if (cookies != null && cookies.length != 0) {
87+
for (Cookie co : cookies) {
88+
out.println("<li>" + co.getName() + "=" + co.getValue());
89+
}
90+
}
91+
out.println("</ul>");
92+
93+
out.println("</body>");
94+
out.println("</html>");
95+
}
96+
}
97+
98+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
99+
/**
100+
* Handles the HTTP <code>GET</code> method.
101+
*
102+
* @param request servlet request
103+
* @param response servlet response
104+
* @throws ServletException if a servlet-specific error occurs
105+
* @throws IOException if an I/O error occurs
106+
*/
107+
@Override
108+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
109+
throws ServletException, IOException {
110+
try {
111+
processRequest(request, response);
112+
} catch (Exception ex) {
113+
Logger.getLogger(ServletViewer.class.getName()).log(Level.SEVERE, null, ex);
114+
}
115+
}
116+
117+
/**
118+
* Handles the HTTP <code>POST</code> method.
119+
*
120+
* @param request servlet request
121+
* @param response servlet response
122+
* @throws ServletException if a servlet-specific error occurs
123+
* @throws IOException if an I/O error occurs
124+
*/
125+
@Override
126+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
127+
throws ServletException, IOException {
128+
try {
129+
processRequest(request, response);
130+
} catch (Exception ex) {
131+
Logger.getLogger(ServletViewer.class.getName()).log(Level.SEVERE, null, ex);
132+
}
133+
}
134+
135+
/**
136+
* Returns a short description of the servlet.
137+
*
138+
* @return a String containing servlet description
139+
*/
140+
@Override
141+
public String getServletInfo() {
142+
return "System Viewer";
143+
}// </editor-fold>
144+
145+
}

0 commit comments

Comments
 (0)