Skip to content

Commit 7270296

Browse files
committed
Added test for obtaining and invoking EJB and CDI beans from a SAM
1 parent b616a52 commit 7270296

File tree

9 files changed

+290
-91
lines changed

9 files changed

+290
-91
lines changed

jaspic/invoke-ejb-cdi/src/main/java/org/javaee7/jaspic/invoke/sam/TestServerAuthModule.java

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,39 @@ public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject
7171
throw (AuthException) new AuthException().initCause(e);
7272
}
7373
}
74+
75+
@Override
76+
public Class<?>[] getSupportedMessageTypes() {
77+
return supportedMessageTypes;
78+
}
79+
80+
@Override
81+
public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException {
82+
83+
HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
84+
HttpServletResponse response = (HttpServletResponse) messageInfo.getResponseMessage();
85+
86+
if ("cdi".equals(request.getParameter("tech"))) {
87+
callCDIBean(response, "secureResponse");
88+
} else if ("ejb".equals(request.getParameter("tech"))) {
89+
callEJBBean(response, "secureResponse");
90+
}
91+
92+
return SEND_SUCCESS;
93+
}
94+
95+
@Override
96+
public void cleanSubject(MessageInfo messageInfo, Subject subject) throws AuthException {
97+
98+
HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
99+
HttpServletResponse response = (HttpServletResponse) messageInfo.getResponseMessage();
100+
101+
if ("cdi".equals(request.getParameter("tech"))) {
102+
callCDIBean(response, "cleanSubject");
103+
} else if ("ejb".equals(request.getParameter("tech"))) {
104+
callEJBBean(response, "cleanSubject");
105+
}
106+
}
74107

75108
private void callCDIBean(HttpServletResponse response, String phase) {
76109
try {
@@ -90,21 +123,5 @@ private void callEJBBean(HttpServletResponse response, String phase) {
90123
}
91124
}
92125

93-
94-
@Override
95-
public Class<?>[] getSupportedMessageTypes() {
96-
return supportedMessageTypes;
97-
}
98-
99-
@Override
100-
public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException {
101-
return SEND_SUCCESS;
102-
}
103-
104-
@Override
105-
public void cleanSubject(MessageInfo messageInfo, Subject subject) throws AuthException {
106-
107-
}
108-
109126

110127
}

jaspic/invoke-ejb-cdi/src/main/java/org/javaee7/jaspic/invoke/servlet/ProtectedServlet.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class ProtectedServlet extends HttpServlet {
2020
@Override
2121
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
2222
response.getWriter().write("Resource invoked\n");
23+
request.logout();
2324
}
2425

2526
}

jaspic/invoke-ejb-cdi/src/main/java/org/javaee7/jaspic/invoke/servlet/PublicServlet.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class PublicServlet extends HttpServlet {
2020
@Override
2121
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
2222
response.getWriter().write("Resource invoked\n");
23+
request.logout();
2324
}
2425

2526
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.javaee7.jaspictest.invoke;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.javaee7.jaspic.common.ArquillianBase;
6+
import org.jboss.arquillian.container.test.api.Deployment;
7+
import org.jboss.arquillian.junit.Arquillian;
8+
import org.jboss.shrinkwrap.api.Archive;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
12+
/**
13+
* This tests that a SAM is able to obtain and call a CDI bean when the request is to a protected resource
14+
* (a resource for which security constraints have been set).
15+
*
16+
* @author Arjan Tijms
17+
*
18+
*/
19+
@RunWith(Arquillian.class)
20+
public class InvokeCDIBeanProtectedTest extends ArquillianBase {
21+
22+
@Deployment(testable = false)
23+
public static Archive<?> createDeployment() {
24+
return tryWrapEAR(
25+
defaultWebArchive()
26+
.addAsWebInfResource(resource("beans.xml"))
27+
);
28+
}
29+
30+
@Test
31+
public void protectedInvokeCDIFromValidateRequest() {
32+
String response = getFromServerPath("protected/servlet?tech=cdi");
33+
34+
assertTrue(
35+
"Response did not contain output from CDI bean for validateRequest for protected resource. (note: this is not required by the spec)",
36+
response.contains("validateRequest: Called from CDI")
37+
);
38+
}
39+
40+
@Test
41+
public void protectedInvokeCDIFromCleanSubject() {
42+
String response = getFromServerPath("protected/servlet?tech=cdi");
43+
44+
assertTrue(
45+
"Response did not contain output from CDI bean for cleanSubject for protected resource. (note: this is not required by the spec)",
46+
response.contains("cleanSubject: Called from CDI")
47+
);
48+
}
49+
50+
@Test
51+
public void protectedInvokeCDIFromSecureResponse() {
52+
String response = getFromServerPath("protected/servlet?tech=cdi");
53+
54+
assertTrue(
55+
"Response did not contain output from CDI bean for secureResponse for protected resource. (note: this is not required by the spec)",
56+
response.contains("secureResponse: Called from CDI")
57+
);
58+
}
59+
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.javaee7.jaspictest.invoke;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.javaee7.jaspic.common.ArquillianBase;
6+
import org.jboss.arquillian.container.test.api.Deployment;
7+
import org.jboss.arquillian.junit.Arquillian;
8+
import org.jboss.shrinkwrap.api.Archive;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
12+
/**
13+
* This tests that a SAM is able to obtain and call a CDI bean when the request is to a public resource
14+
* (a resource for which no security constraints have been set).
15+
*
16+
* @author Arjan Tijms
17+
*
18+
*/
19+
@RunWith(Arquillian.class)
20+
public class InvokeCDIBeanPublicTest extends ArquillianBase {
21+
22+
@Deployment(testable = false)
23+
public static Archive<?> createDeployment() {
24+
return tryWrapEAR(
25+
defaultWebArchive()
26+
.addAsWebInfResource(resource("beans.xml"))
27+
);
28+
}
29+
30+
@Test
31+
public void publicInvokeCDIFromValidateRequest() {
32+
String response = getFromServerPath("public/servlet?tech=cdi");
33+
34+
assertTrue(
35+
"Response did not contain output from CDI bean for validateRequest for public resource. (note: this is not required by the spec)",
36+
response.contains("validateRequest: Called from CDI")
37+
);
38+
}
39+
40+
@Test
41+
public void publicInvokeCDIFromCleanSubject() {
42+
String response = getFromServerPath("public/servlet?tech=cdi");
43+
44+
assertTrue(
45+
"Response did not contain output from CDI bean for cleanSubject for public resource. (note: this is not required by the spec)",
46+
response.contains("cleanSubject: Called from CDI")
47+
);
48+
}
49+
50+
@Test
51+
public void publicInvokeCDIFromSecureResponse() {
52+
String response = getFromServerPath("public/servlet?tech=cdi");
53+
54+
assertTrue(
55+
"Response did not contain output from CDI bean for secureResponse for public resource. (note: this is not required by the spec)",
56+
response.contains("secureResponse: Called from CDI")
57+
);
58+
}
59+
60+
}

jaspic/invoke-ejb-cdi/src/test/java/org/javaee7/jaspictest/invoke/InvokeCDIBeanTest.java

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.javaee7.jaspictest.invoke;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.javaee7.jaspic.common.ArquillianBase;
6+
import org.jboss.arquillian.container.test.api.Deployment;
7+
import org.jboss.arquillian.junit.Arquillian;
8+
import org.jboss.shrinkwrap.api.Archive;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
12+
/**
13+
* This tests that a SAM is able to obtain and call an EJB bean when the request is to a protected resource
14+
* (a resource for which security constraints have been set).
15+
*
16+
* @author Arjan Tijms
17+
*
18+
*/
19+
@RunWith(Arquillian.class)
20+
public class InvokeEJBBeanProtectedTest extends ArquillianBase {
21+
22+
@Deployment(testable = false)
23+
public static Archive<?> createDeployment() {
24+
return defaultArchive();
25+
}
26+
27+
@Test
28+
public void protectedInvokeEJBFromValidateRequest() {
29+
String response = getFromServerPath("protected/servlet?tech=ejb");
30+
31+
assertTrue(
32+
"Response did not contain output from EJB bean for validateRequest for protected resource. (note: spec is silent on this, but it should work)",
33+
response.contains("validateRequest: Called from EJB")
34+
);
35+
}
36+
37+
@Test
38+
public void protectedInvokeEJBFromCleanSubject() {
39+
String response = getFromServerPath("protected/servlet?tech=ejb");
40+
41+
assertTrue(
42+
"Response did not contain output from EJB bean for cleanSubject for protected resource. (note: spec is silent on this, but it should work)",
43+
response.contains("cleanSubject: Called from EJB")
44+
);
45+
}
46+
47+
@Test
48+
public void protectedInvokeEJBFromSecureResponse() {
49+
String response = getFromServerPath("protected/servlet?tech=ejb");
50+
51+
assertTrue(
52+
"Response did not contain output from EJB bean for secureResponse for protected resource. (note: spec is silent on this, but it should work)",
53+
response.contains("secureResponse: Called from EJB")
54+
);
55+
}
56+
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.javaee7.jaspictest.invoke;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.javaee7.jaspic.common.ArquillianBase;
6+
import org.jboss.arquillian.container.test.api.Deployment;
7+
import org.jboss.arquillian.junit.Arquillian;
8+
import org.jboss.shrinkwrap.api.Archive;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
12+
/**
13+
* This tests that a SAM is able to obtain and call an EJB bean when the request is to a public resource
14+
* (a resource for which no security constraints have been set).
15+
*
16+
* @author Arjan Tijms
17+
*
18+
*/
19+
@RunWith(Arquillian.class)
20+
public class InvokeEJBBeanPublicTest extends ArquillianBase {
21+
22+
@Deployment(testable = false)
23+
public static Archive<?> createDeployment() {
24+
return defaultArchive();
25+
}
26+
27+
@Test
28+
public void publicInvokeEJBFromValidateRequest() {
29+
String response = getFromServerPath("public/servlet?tech=ejb");
30+
31+
assertTrue(
32+
"Response did not contain output from EJB bean for validateRequest for public resource.",
33+
response.contains("validateRequest: Called from EJB")
34+
);
35+
}
36+
37+
@Test
38+
public void publicInvokeEJBFromCleanSubject() {
39+
String response = getFromServerPath("public/servlet?tech=ejb");
40+
41+
assertTrue(
42+
"Response did not contain output from EJB bean for cleanSubject for public resource.",
43+
response.contains("cleanSubject: Called from EJB")
44+
);
45+
}
46+
47+
@Test
48+
public void publicInvokeEJBFromSecureResponse() {
49+
String response = getFromServerPath("public/servlet?tech=ejb");
50+
51+
assertTrue(
52+
"Response did not contain output from EJB bean for secureResponse for public resource.",
53+
response.contains("secureResponse: Called from EJB")
54+
);
55+
}
56+
57+
}

0 commit comments

Comments
 (0)