Skip to content

Commit 1bfb9a2

Browse files
committed
new test for programmatic registration of Servlets
1 parent 6f5ac01 commit 1bfb9a2

File tree

14 files changed

+402
-276
lines changed

14 files changed

+402
-276
lines changed

servlet/cookies/src/main/webapp/index-cookies.jsp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,8 @@
4949
<title>Show Cookies</title>
5050
</head>
5151
<body>
52-
<h1>Show Cookies</h1>
53-
5452
<script type="text/javascript">
5553
document.write(document.cookie);
5654
</script>
57-
<br/><br/>
58-
"myHttpOnlyCookieKey" cookie is not visible, right ?
59-
</form>
6055
</body>
6156
</html>

servlet/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
<!-- <module>security-annotated</module>
3434
<module>security-digest</module>-->
3535
<module>security-basicauth-omission</module>
36+
<module>programmatic-registration</module>
3637
</modules>
3738
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.servlet</groupId>
6+
<artifactId>servlet-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.servlet</groupId>
12+
<artifactId>programmatic-registration</artifactId>
13+
<packaging>war</packaging>
14+
</project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.javaee7.servlet.programmatic.registration;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import javax.servlet.ServletException;
6+
import javax.servlet.http.HttpServlet;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
/**
11+
* @author Arun Gupta
12+
*/
13+
public class ChildServlet extends HttpServlet {
14+
15+
/**
16+
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
17+
* methods.
18+
*
19+
* @param request servlet request
20+
* @param response servlet response
21+
* @throws ServletException if a servlet-specific error occurs
22+
* @throws IOException if an I/O error occurs
23+
*/
24+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
25+
throws ServletException, IOException {
26+
response.setContentType("text/html;charset=UTF-8");
27+
try (final PrintWriter out = response.getWriter()) {
28+
out.println("<!DOCTYPE html>");
29+
out.println("<html>");
30+
out.println("<head>");
31+
out.println("<title>Child Servlet</title>");
32+
out.println("</head>");
33+
out.println("<body>");
34+
out.println("<h1>Child Servlet</h1>");
35+
out.println("</body>");
36+
out.println("</html>");
37+
}
38+
}
39+
40+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
41+
/**
42+
* Handles the HTTP <code>GET</code> method.
43+
*
44+
* @param request servlet request
45+
* @param response servlet response
46+
* @throws ServletException if a servlet-specific error occurs
47+
* @throws IOException if an I/O error occurs
48+
*/
49+
@Override
50+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
51+
throws ServletException, IOException {
52+
processRequest(request, response);
53+
}
54+
55+
/**
56+
* Handles the HTTP <code>POST</code> method.
57+
*
58+
* @param request servlet request
59+
* @param response servlet response
60+
* @throws ServletException if a servlet-specific error occurs
61+
* @throws IOException if an I/O error occurs
62+
*/
63+
@Override
64+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
65+
throws ServletException, IOException {
66+
processRequest(request, response);
67+
}
68+
69+
/**
70+
* Returns a short description of the servlet.
71+
*
72+
* @return a String containing servlet description
73+
*/
74+
@Override
75+
public String getServletInfo() {
76+
return "Short description";
77+
}// </editor-fold>
78+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.javaee7.servlet.programmatic.registration;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import javax.annotation.Resource;
6+
import javax.enterprise.concurrent.ManagedExecutorService;
7+
import javax.servlet.AsyncContext;
8+
import javax.servlet.AsyncEvent;
9+
import javax.servlet.AsyncListener;
10+
import javax.servlet.ServletException;
11+
import javax.servlet.annotation.WebServlet;
12+
import javax.servlet.http.HttpServlet;
13+
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletResponse;
15+
16+
/**
17+
* @author Arun Gupta
18+
*/
19+
@WebServlet("/parent")
20+
public class ParentServlet extends HttpServlet {
21+
22+
/**
23+
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
24+
* methods.
25+
*
26+
* @param request servlet request
27+
* @param response servlet response
28+
* @throws ServletException if a servlet-specific error occurs
29+
* @throws IOException if an I/O error occurs
30+
*/
31+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
32+
throws ServletException, IOException {
33+
response.setContentType("text/html;charset=UTF-8");
34+
try (final PrintWriter out = response.getWriter()) {
35+
out.println("<!DOCTYPE html>");
36+
out.println("<html>");
37+
out.println("<head>");
38+
out.println("<title>Parent Servlet</title>");
39+
out.println("</head>");
40+
out.println("<body>");
41+
out.println("<h1>Parent Servlet</h1>");
42+
getServletContext().addServlet("childServlet", ChildServlet.class);
43+
out.println("</body>");
44+
out.println("</html>");
45+
}
46+
}
47+
48+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
49+
/**
50+
* Handles the HTTP <code>GET</code> method.
51+
*
52+
* @param request servlet request
53+
* @param response servlet response
54+
* @throws ServletException if a servlet-specific error occurs
55+
* @throws IOException if an I/O error occurs
56+
*/
57+
@Override
58+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
59+
throws ServletException, IOException {
60+
processRequest(request, response);
61+
}
62+
63+
/**
64+
* Handles the HTTP <code>POST</code> method.
65+
*
66+
* @param request servlet request
67+
* @param response servlet response
68+
* @throws ServletException if a servlet-specific error occurs
69+
* @throws IOException if an I/O error occurs
70+
*/
71+
@Override
72+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
73+
throws ServletException, IOException {
74+
processRequest(request, response);
75+
}
76+
77+
/**
78+
* Returns a short description of the servlet.
79+
*
80+
* @return a String containing servlet description
81+
*/
82+
@Override
83+
public String getServletInfo() {
84+
return "Short description";
85+
}// </editor-fold>
86+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.javaee7.servlet.programmatic.registration;
2+
3+
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
4+
import com.gargoylesoftware.htmlunit.HttpMethod;
5+
import com.gargoylesoftware.htmlunit.TextPage;
6+
import com.gargoylesoftware.htmlunit.WebClient;
7+
import com.gargoylesoftware.htmlunit.WebRequest;
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.net.URL;
11+
import org.jboss.arquillian.container.test.api.Deployment;
12+
import org.jboss.arquillian.junit.Arquillian;
13+
import org.jboss.arquillian.test.api.ArquillianResource;
14+
import org.jboss.shrinkwrap.api.ShrinkWrap;
15+
import org.jboss.shrinkwrap.api.spec.WebArchive;
16+
import org.junit.Before;
17+
import org.junit.Test;
18+
import static org.junit.Assert.*;
19+
import org.junit.runner.RunWith;
20+
import org.xml.sax.SAXException;
21+
22+
/**
23+
* @author arungupta
24+
*/
25+
@RunWith(Arquillian.class)
26+
public class ServletTest {
27+
28+
@ArquillianResource
29+
private URL base;
30+
31+
WebClient webClient;
32+
33+
@Deployment(testable = false)
34+
public static WebArchive createDeployment() {
35+
WebArchive war = ShrinkWrap.create(WebArchive.class).
36+
addClass(ParentServlet.class).
37+
addClass(ChildServlet.class);
38+
return war;
39+
}
40+
41+
@Before
42+
public void setup() {
43+
webClient = new WebClient();
44+
}
45+
46+
@Test
47+
public void testChildServlet() throws IOException, SAXException {
48+
try {
49+
webClient.getPage(base + "/ChildServlet");
50+
} catch (FailingHttpStatusCodeException e) {
51+
assertNotNull(e);
52+
assertEquals(404, e.getStatusCode());
53+
return;
54+
}
55+
fail("/ChildSevlet could be accessed with programmatic registration");
56+
webClient.getPage(base + "/ParentServlet");
57+
webClient.getPage(base + "/ChildServlet");
58+
}
59+
}

servlet/security-annotated/src/main/java/org/javaee7/servlet/security/annotated/SecureServlet.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.io.PrintWriter;
5+
import javax.annotation.security.RolesAllowed;
56
import javax.servlet.ServletException;
67
import javax.servlet.annotation.HttpConstraint;
78
import javax.servlet.annotation.HttpMethodConstraint;
@@ -15,11 +16,13 @@
1516
* @author Arun Gupta
1617
*/
1718
@WebServlet("/SecureServlet")
18-
@ServletSecurity(value = @HttpConstraint(rolesAllowed = {"g1"}),
19-
httpMethodConstraints = {
20-
@HttpMethodConstraint(value = "GET", rolesAllowed = {"g1"}),
21-
@HttpMethodConstraint(value = "POST", rolesAllowed = {"g1"})
22-
})
19+
//@ServletSecurity(value = @HttpConstraint(rolesAllowed = {"g1"}),
20+
// httpMethodConstraints = {
21+
// @HttpMethodConstraint(value = "GET", rolesAllowed = {"g1"}),
22+
// @HttpMethodConstraint(value = "POST", rolesAllowed = {"g1"})
23+
// })
24+
@ServletSecurity(@HttpConstraint(rolesAllowed={"g1"}))
25+
@RolesAllowed("g1")
2326
public class SecureServlet extends HttpServlet {
2427

2528
protected void processRequest(HttpServletRequest request, HttpServletResponse response, String method)

0 commit comments

Comments
 (0)