Skip to content

Commit 5f53c52

Browse files
committed
Merge pull request javaee-samples#329 from arjantijms/master
Added test for obtaining and invoking EJB and CDI beans from a SAM
2 parents a68b415 + 7270296 commit 5f53c52

File tree

17 files changed

+557
-16
lines changed

17 files changed

+557
-16
lines changed

jaspic/invoke-ejb-cdi/pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
5+
<parent>
6+
<groupId>org.javaee7</groupId>
7+
<artifactId>jaspic</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>jaspic-invoke-ejb-cdi</artifactId>
13+
14+
<packaging>war</packaging>
15+
16+
<name>Java EE 7 Sample: jaspic - invoke EJB and CDI</name>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.javaee7</groupId>
21+
<artifactId>jaspic-common</artifactId>
22+
<version>1.0-SNAPSHOT</version>
23+
</dependency>
24+
</dependencies>
25+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.javaee7.jaspic.invoke.bean;
2+
3+
import javax.enterprise.context.RequestScoped;
4+
import javax.inject.Named;
5+
6+
@Named
7+
@RequestScoped
8+
public class CDIBean {
9+
10+
public String getText() {
11+
return "Called from CDI";
12+
}
13+
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.javaee7.jaspic.invoke.bean;
2+
3+
import javax.ejb.Stateless;
4+
5+
@Stateless
6+
public class EJBBean {
7+
8+
public String getText() {
9+
return "Called from EJB";
10+
}
11+
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.javaee7.jaspic.invoke.sam;
2+
3+
import javax.servlet.ServletContextEvent;
4+
import javax.servlet.annotation.WebListener;
5+
6+
import org.javaee7.jaspic.common.BaseServletContextListener;
7+
import org.javaee7.jaspic.common.JaspicUtils;
8+
9+
/**
10+
*
11+
* @author Arjan Tijms
12+
*
13+
*/
14+
@WebListener
15+
public class SamAutoRegistrationListener extends BaseServletContextListener {
16+
17+
@Override
18+
public void contextInitialized(ServletContextEvent sce) {
19+
JaspicUtils.registerSAM(sce.getServletContext(), new TestServerAuthModule());
20+
}
21+
22+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package org.javaee7.jaspic.invoke.sam;
2+
3+
import static java.util.logging.Level.SEVERE;
4+
import static javax.security.auth.message.AuthStatus.SEND_SUCCESS;
5+
import static javax.security.auth.message.AuthStatus.SUCCESS;
6+
7+
import java.io.IOException;
8+
import java.util.Map;
9+
import java.util.logging.Logger;
10+
11+
import javax.enterprise.inject.spi.CDI;
12+
import javax.naming.InitialContext;
13+
import javax.security.auth.Subject;
14+
import javax.security.auth.callback.Callback;
15+
import javax.security.auth.callback.CallbackHandler;
16+
import javax.security.auth.callback.UnsupportedCallbackException;
17+
import javax.security.auth.message.AuthException;
18+
import javax.security.auth.message.AuthStatus;
19+
import javax.security.auth.message.MessageInfo;
20+
import javax.security.auth.message.MessagePolicy;
21+
import javax.security.auth.message.callback.CallerPrincipalCallback;
22+
import javax.security.auth.message.callback.GroupPrincipalCallback;
23+
import javax.security.auth.message.module.ServerAuthModule;
24+
import javax.servlet.http.HttpServletRequest;
25+
import javax.servlet.http.HttpServletResponse;
26+
27+
import org.javaee7.jaspic.invoke.bean.CDIBean;
28+
import org.javaee7.jaspic.invoke.bean.EJBBean;
29+
30+
/**
31+
*
32+
* @author Arjan Tijms
33+
*
34+
*/
35+
public class TestServerAuthModule implements ServerAuthModule {
36+
37+
private final static Logger logger = Logger.getLogger(TestServerAuthModule.class.getName());
38+
39+
private CallbackHandler handler;
40+
private Class<?>[] supportedMessageTypes = new Class[] { HttpServletRequest.class, HttpServletResponse.class };
41+
42+
43+
44+
@Override
45+
public void initialize(MessagePolicy requestPolicy, MessagePolicy responsePolicy, CallbackHandler handler,
46+
@SuppressWarnings("rawtypes") Map options) throws AuthException {
47+
this.handler = handler;
48+
}
49+
50+
@Override
51+
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
52+
53+
HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
54+
HttpServletResponse response = (HttpServletResponse) messageInfo.getResponseMessage();
55+
56+
if ("cdi".equals(request.getParameter("tech"))) {
57+
callCDIBean(response, "validateRequest");
58+
} else if ("ejb".equals(request.getParameter("tech"))) {
59+
callEJBBean(response, "validateRequest");
60+
}
61+
62+
try {
63+
handler.handle(new Callback[] {
64+
new CallerPrincipalCallback(clientSubject, "test"),
65+
new GroupPrincipalCallback(clientSubject, new String[] { "architect" })
66+
});
67+
68+
return SUCCESS;
69+
70+
} catch (IOException | UnsupportedCallbackException e) {
71+
throw (AuthException) new AuthException().initCause(e);
72+
}
73+
}
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+
}
107+
108+
private void callCDIBean(HttpServletResponse response, String phase) {
109+
try {
110+
CDIBean cdiBean = CDI.current().select(CDIBean.class).get();
111+
response.getWriter().write(phase + ": " + cdiBean.getText());
112+
} catch (Exception e) {
113+
logger.log(SEVERE, "", e);
114+
}
115+
}
116+
117+
private void callEJBBean(HttpServletResponse response, String phase) {
118+
try {
119+
EJBBean ejbBean = (EJBBean) new InitialContext().lookup("java:module/EJBBean");
120+
response.getWriter().write(phase + ": " + ejbBean.getText());
121+
} catch (Exception e) {
122+
logger.log(SEVERE, "", e);
123+
}
124+
}
125+
126+
127+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.javaee7.jaspic.invoke.servlet;
2+
import java.io.IOException;
3+
4+
import javax.servlet.ServletException;
5+
import javax.servlet.annotation.WebServlet;
6+
import javax.servlet.http.HttpServlet;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
/**
11+
*
12+
* @author Arjan Tijms
13+
*
14+
*/
15+
@WebServlet(urlPatterns = "/protected/servlet")
16+
public class ProtectedServlet extends HttpServlet {
17+
18+
private static final long serialVersionUID = 1L;
19+
20+
@Override
21+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22+
response.getWriter().write("Resource invoked\n");
23+
request.logout();
24+
}
25+
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.javaee7.jaspic.invoke.servlet;
2+
import java.io.IOException;
3+
4+
import javax.servlet.ServletException;
5+
import javax.servlet.annotation.WebServlet;
6+
import javax.servlet.http.HttpServlet;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
/**
11+
*
12+
* @author Arjan Tijms
13+
*
14+
*/
15+
@WebServlet(urlPatterns = "/public/servlet")
16+
public class PublicServlet extends HttpServlet {
17+
18+
private static final long serialVersionUID = 1L;
19+
20+
@Override
21+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22+
response.getWriter().write("Resource invoked\n");
23+
request.logout();
24+
}
25+
26+
}

jaspic/invoke-ejb-cdi/src/main/webapp/WEB-INF/beans.xml

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
3+
<glassfish-web-app>
4+
5+
<security-role-mapping>
6+
<role-name>architect</role-name>
7+
<group-name>architect</group-name>
8+
</security-role-mapping>
9+
10+
<parameter-encoding default-charset="UTF-8" />
11+
12+
</glassfish-web-app>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<application-bnd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-application-bnd_1_2.xsd"
4+
xmlns="http://websphere.ibm.com/xml/ns/javaee"
5+
version="1.2">
6+
7+
<security-role name="architect">
8+
<group name="architect" />
9+
</security-role>
10+
11+
</application-bnd>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
3+
<jboss-web>
4+
<security-domain>jaspitest</security-domain>
5+
</jboss-web>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
4+
version="3.0">
5+
6+
<security-constraint>
7+
<web-resource-collection>
8+
<web-resource-name>Test</web-resource-name>
9+
<url-pattern>/protected/*</url-pattern>
10+
</web-resource-collection>
11+
<auth-constraint>
12+
<role-name>architect</role-name>
13+
</auth-constraint>
14+
</security-constraint>
15+
16+
<security-role>
17+
<role-name>architect</role-name>
18+
</security-role>
19+
20+
</web-app>
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+
}

0 commit comments

Comments
 (0)