Skip to content

Commit b616a52

Browse files
committed
Start with adding invoker test. WIP
1 parent c366a6e commit b616a52

File tree

14 files changed

+345
-3
lines changed

14 files changed

+345
-3
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: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
private void callCDIBean(HttpServletResponse response, String phase) {
76+
try {
77+
CDIBean cdiBean = CDI.current().select(CDIBean.class).get();
78+
response.getWriter().write(phase + ": " + cdiBean.getText());
79+
} catch (Exception e) {
80+
logger.log(SEVERE, "", e);
81+
}
82+
}
83+
84+
private void callEJBBean(HttpServletResponse response, String phase) {
85+
try {
86+
EJBBean ejbBean = (EJBBean) new InitialContext().lookup("java:module/EJBBean");
87+
response.getWriter().write(phase + ": " + ejbBean.getText());
88+
} catch (Exception e) {
89+
logger.log(SEVERE, "", e);
90+
}
91+
}
92+
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+
109+
110+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
24+
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
24+
25+
}

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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.javaee7.jaspictest.invoke;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.IOException;
6+
7+
import org.javaee7.jaspic.common.ArquillianBase;
8+
import org.jboss.arquillian.container.test.api.Deployment;
9+
import org.jboss.arquillian.junit.Arquillian;
10+
import org.jboss.shrinkwrap.api.Archive;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.xml.sax.SAXException;
14+
15+
/**
16+
*
17+
* @author Arjan Tijms
18+
*
19+
*/
20+
@RunWith(Arquillian.class)
21+
public class InvokeCDIBeanTest extends ArquillianBase {
22+
23+
@Deployment(testable = false)
24+
public static Archive<?> createDeployment() {
25+
return tryWrapEAR(
26+
defaultWebArchive()
27+
.addAsWebInfResource(resource("beans.xml"))
28+
);
29+
}
30+
31+
@Test
32+
public void invokeCDIBeanFromSAM() throws IOException, SAXException {
33+
34+
String response = getFromServerPath("public/servlet?tech=cdi");
35+
36+
assertTrue(
37+
"Response did not contain output from CDI bean for validateRequest.",
38+
response.contains("validateRequest: Called from CDI")
39+
);
40+
}
41+
42+
@Test
43+
public void invokeEJBBeanInValidateRequest() throws IOException, SAXException {
44+
45+
String response = getFromServerPath("public/servlet?tech=ejb");
46+
47+
assertTrue(
48+
"Response did not contain output from EJB bean for validateRequest.",
49+
response.contains("validateRequest: Called from EJB")
50+
);
51+
}
52+
53+
54+
55+
}

jaspic/pom.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@
88
<version>1.0-SNAPSHOT</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
11-
<groupId>org.javaee7</groupId>
11+
1212
<artifactId>jaspic</artifactId>
13-
<version>1.0-SNAPSHOT</version>
1413
<packaging>pom</packaging>
14+
1515
<name>Java EE 7 Sample: jaspic</name>
1616

1717
<modules>
1818
<!-- Not a module with tests, but contains common code for the other modules -->
1919
<module>common</module>
20+
21+
<!-- Tests that a SAM is able to invoke EJB beans and CDI bean. NOTE; JASPIC 1.1 does not require this,
22+
although a JASPIC 1.1 compatible container "should" at least be able to call EJB beans (even though
23+
the spec is silent on this)
24+
-->
25+
<module>invoke-ejb-cdi</module>
2026

21-
<!-- Tests behavior of authentication involving asynchronous requests -->
27+
<!-- Tests behavior of authentication involving asynchronous requests -->
2228
<module>async-authentication</module>
2329

2430
<!-- Tests a simple authentication from both public and protected resources -->

0 commit comments

Comments
 (0)