topics = java8Integration.filterJsonArrayToList();
23 |
24 | assertThat(topics).contains("Cloud");
25 | assertThat(topics).contains("Cognitive");
26 | }
27 |
28 | @Test
29 | public void givenJsonArray_shouldFilterAllTopicsStartingCToJsonArray() throws Exception {
30 | Java8Integration java8Integration = new Java8Integration();
31 | JsonArray topics = java8Integration.filterJsonArrayToJsonArray();
32 |
33 | assertThat(topics.contains(Json.createValue("Cloud"))).isTrue();
34 | assertThat(topics.contains(Json.createValue("Cognitive"))).isTrue();
35 | }
36 |
37 | }
--------------------------------------------------------------------------------
/json-p-1-1/src/test/java/com/readlearncode/JsonMergeDiffExampleTest.java:
--------------------------------------------------------------------------------
1 | package com.readlearncode;
2 |
3 | import org.junit.Test;
4 |
5 | import javax.json.JsonMergePatch;
6 | import javax.json.JsonString;
7 |
8 | import static org.assertj.core.api.Java6Assertions.assertThat;
9 |
10 | /**
11 | * Source code github.com/readlearncode
12 | *
13 | * @author Alex Theedom www.readlearncode.com
14 | * @version 1.0
15 | */
16 | public class JsonMergeDiffExampleTest {
17 |
18 | @Test
19 | public void givenSourceAndTarget_shouldCreateMergePatch() {
20 | JsonMergeDiffExample jsonMergeDiffExample = new JsonMergeDiffExample();
21 | JsonMergePatch mergePatch = jsonMergeDiffExample.createMergePatch();
22 | JsonString jsonString = (JsonString) mergePatch.toJsonValue();
23 |
24 | assertThat(jsonString.getString()).isEqualToIgnoringCase("{\"colour\":\"red\"}");
25 | }
26 |
27 | }
--------------------------------------------------------------------------------
/json-p-1-1/src/test/java/com/readlearncode/JsonMergePatchExampleTest.java:
--------------------------------------------------------------------------------
1 | package com.readlearncode;
2 |
3 | import org.junit.Ignore;
4 | import org.junit.Test;
5 |
6 | import javax.json.JsonString;
7 | import javax.json.JsonValue;
8 |
9 | import static org.assertj.core.api.Java6Assertions.assertThat;
10 |
11 | /**
12 | * Source code github.com/readlearncode
13 | *
14 | * @author Alex Theedom www.readlearncode.com
15 | * @version 1.0
16 | */
17 | public class JsonMergePatchExampleTest {
18 |
19 | @Test
20 | public void givenPatch_sourceValueChanges() throws Exception {
21 | JsonMergePatchExample jsonMergePatchExample = new JsonMergePatchExample();
22 | JsonValue result = jsonMergePatchExample.changeValue();
23 |
24 | assertThat(((JsonString) result).getString()).isEqualToIgnoringCase("{\"colour\":\"red\"}");
25 | }
26 |
27 | @Test
28 | @Ignore // Behaviour is not as specified in https://tools.ietf.org/html/rfc7386
29 | public void givenPatch_addNewJsonToSource() throws Exception {
30 | JsonMergePatchExample jsonMergePatchExample = new JsonMergePatchExample();
31 | JsonValue result = jsonMergePatchExample.addValue();
32 |
33 | assertThat(((JsonString) result).getString()).isEqualToIgnoringCase("{\"colour\":\"blue\",\"blue\":\"light\"}");
34 | }
35 |
36 | @Test
37 | @Ignore // Behaviour is not as specified in https://tools.ietf.org/html/rfc7386
38 | public void givenPatch_deleteValue() throws Exception {
39 | JsonMergePatchExample jsonMergePatchExample = new JsonMergePatchExample();
40 | JsonValue result = jsonMergePatchExample.deleteValue();
41 |
42 | assertThat(((JsonString) result).getString()).isEqualToIgnoringCase("{}");
43 | }
44 |
45 | }
--------------------------------------------------------------------------------
/json-p-1-1/src/test/java/com/readlearncode/JsonPointerExampleTest.java:
--------------------------------------------------------------------------------
1 | package com.readlearncode;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.assertj.core.api.Java6Assertions.assertThat;
6 |
7 | /**
8 | * Source code github.com/readlearncode
9 | *
10 | * @author Alex Theedom www.readlearncode.com
11 | * @version 1.0
12 | */
13 | public class JsonPointerExampleTest {
14 |
15 | @Test
16 | public void givenPointerToTopic_shouldReturnTopic() {
17 | JsonPointerExample jsonPointerExample = new JsonPointerExample();
18 | String topic = jsonPointerExample.find();
19 | assertThat(topic).isEqualToIgnoringCase("Cloud");
20 | }
21 |
22 | @Test
23 | public void givenPointerToTopic_shouldReplaceTopic() {
24 | JsonPointerExample jsonPointerExample = new JsonPointerExample();
25 | String topic = jsonPointerExample.replace();
26 | assertThat(topic).isEqualToIgnoringCase("Big Data");
27 | }
28 |
29 | @Test
30 | public void givenPointerToArrayElement_shouldInsertTopicInToList() {
31 | JsonPointerExample jsonPointerExample = new JsonPointerExample();
32 | String topic = jsonPointerExample.add();
33 | assertThat(topic).isEqualToIgnoringCase("Java EE");
34 | }
35 | }
--------------------------------------------------------------------------------
/json-p-1-1/src/test/java/com/readlearncode/hashtag100daysofjavaee8/challenges.java:
--------------------------------------------------------------------------------
1 | package com.readlearncode.hashtag100daysofjavaee8;
2 |
3 | import org.junit.Test;
4 |
5 | import javax.json.*;
6 | import java.io.StringReader;
7 |
8 | import static org.assertj.core.api.Java6Assertions.assertThat;
9 |
10 | /**
11 | * Source code github.com/readlearncode
12 | *
13 | * @author Alex Theedom www.readlearncode.com
14 | * @version 1.0
15 | */
16 | public class challenges {
17 |
18 | @Test
19 | public void givenObservers_shouldCallInPriorityOrder() {
20 |
21 | JsonValue target = Json.createValue("{\"colour\":\"blue\",\"size\":10}");
22 | JsonValue patch = Json.createValue("{\"colour\":\"red\"}");
23 | JsonValue result = Json.createMergePatch(patch).apply(target);
24 |
25 | System.out.println(result);
26 |
27 | assertThat(((JsonString) result).getString()).isEqualToIgnoringCase("{\"colour\":\"red\"}");
28 | }
29 |
30 |
31 | @Test
32 | public void givenATarget_JSONPointerShouldAddAndRemoveElementValue() {
33 |
34 | /* Target JSON document
35 | {
36 | "name": "Duke",
37 | "likes": [
38 | "Java",
39 | "Coffee"
40 | ]
41 | }
42 | */
43 |
44 | String target = "{\"name\":\"Duke\",\"likes\":[\"Java\",\"Coffee\"]}";
45 | JsonObject jsonObject = Json.createReader(new StringReader(target)).readObject();
46 | JsonPointer pointer = Json.createPointer("/likes/0");
47 | jsonObject = pointer.add(jsonObject, Json.createValue("Java EE 8"));
48 | pointer = Json.createPointer("/likes/2");
49 | JsonObject newJsonObject = pointer.replace(jsonObject, Json.createValue("Ice Cream"));
50 |
51 | // What is the shape of the new JSON Object?
52 |
53 | assertThat(newJsonObject.toString()).isEqualToIgnoringCase("{\"name\":\"Duke\",\"likes\":[\"Java EE 8\",\"Java\",\"Ice Cream\"]}");
54 | }
55 |
56 |
57 | }
--------------------------------------------------------------------------------
/json-p-1-1/target/classes/jsondata-object.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "IBM developerWorks",
3 | "URL": "https://www.ibm.com/developerworks/",
4 | "topics": [
5 | "Cognitive",
6 | "Cloud",
7 | "Data",
8 | "IoT",
9 | "Java"
10 | ],
11 | "series": [
12 | "Sample Code...",
13 | "5 things you didn't know...",
14 | "Introduction to..."
15 | ],
16 | "notes": null,
17 | "live": true,
18 | "visitors": 100000000
19 | }
--------------------------------------------------------------------------------
/security-1-0/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | Java-EE-8-Sampler
7 | com.readlearncode
8 | 1.0
9 |
10 |
11 | 4.0.0
12 |
13 | security-1-0
14 |
15 |
16 | security-1-0
17 |
18 |
19 | org.apache.maven.plugins
20 | maven-compiler-plugin
21 | ${maven-compiler-plugin.version}
22 |
23 | ${java.version}
24 | ${java.version}
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/security-1-0/src/main/java/com/readlearncode/httpauthenticationmechanism/AdminServlet.java:
--------------------------------------------------------------------------------
1 | package com.readlearncode.httpauthenticationmechanism;
2 |
3 | import javax.annotation.security.DeclareRoles;
4 | import javax.security.enterprise.authentication.mechanism.http.CustomFormAuthenticationMechanismDefinition;
5 | import javax.security.enterprise.authentication.mechanism.http.LoginToContinue;
6 | import javax.servlet.annotation.HttpConstraint;
7 | import javax.servlet.annotation.ServletSecurity;
8 | import javax.servlet.annotation.WebServlet;
9 | import javax.servlet.http.HttpServlet;
10 |
11 | /**
12 | * Source code github.com/readlearncode
13 | *
14 | * @author Alex Theedom www.readlearncode.com
15 | * @version 1.0
16 | */
17 | @CustomFormAuthenticationMechanismDefinition(
18 | loginToContinue = @LoginToContinue(
19 | loginPage = "/login.do"
20 | )
21 | )
22 | @WebServlet("/admin")
23 | @DeclareRoles({"admin", "user", "demo"})
24 | @ServletSecurity(@HttpConstraint(rolesAllowed = "admin"))
25 | public class AdminServlet extends HttpServlet {
26 |
27 | // Servlet Code
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/security-1-0/src/main/java/com/readlearncode/httpauthenticationmechanism/ApplicationConfig.java:
--------------------------------------------------------------------------------
1 | package com.readlearncode.httpauthenticationmechanism;
2 |
3 | import javax.enterprise.context.ApplicationScoped;
4 | import javax.security.enterprise.authentication.mechanism.http.FormAuthenticationMechanismDefinition;
5 | import javax.security.enterprise.authentication.mechanism.http.LoginToContinue;
6 |
7 | /**
8 | * Source code github.com/readlearncode
9 | *
10 | * @author Alex Theedom www.readlearncode.com
11 | * @version 1.0
12 | */
13 | @FormAuthenticationMechanismDefinition(
14 | loginToContinue = @LoginToContinue(
15 | loginPage = "/login-servlet",
16 | errorPage = "/login-servlet-fail",
17 | useForwardToLoginExpression = "${appConfigs.forward}"
18 | )
19 | )
20 | @ApplicationScoped
21 | public class ApplicationConfig {
22 |
23 | // Config Code
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/security-1-0/src/main/java/com/readlearncode/httpauthenticationmechanism/CustomAuthenticationMechanism.java:
--------------------------------------------------------------------------------
1 | package com.readlearncode.httpauthenticationmechanism;
2 |
3 | import javax.enterprise.context.ApplicationScoped;
4 | import javax.inject.Inject;
5 | import javax.security.enterprise.AuthenticationStatus;
6 | import javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism;
7 | import javax.security.enterprise.authentication.mechanism.http.HttpMessageContext;
8 | import javax.security.enterprise.authentication.mechanism.http.RememberMe;
9 | import javax.security.enterprise.identitystore.IdentityStoreHandler;
10 | import javax.servlet.http.HttpServletRequest;
11 | import javax.servlet.http.HttpServletResponse;
12 |
13 | @RememberMe(
14 | cookieMaxAgeSeconds = 3600
15 | )
16 | @ApplicationScoped
17 | public class CustomAuthenticationMechanism implements HttpAuthenticationMechanism {
18 |
19 | @Inject
20 | private IdentityStoreHandler idStoreHandler;
21 |
22 | @Override
23 | public AuthenticationStatus validateRequest(HttpServletRequest req, HttpServletResponse res, HttpMessageContext msg) {
24 | return msg.doNothing();
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/security-1-0/src/main/java/com/readlearncode/httpauthenticationmechanism/UserServlet.java:
--------------------------------------------------------------------------------
1 | package com.readlearncode.httpauthenticationmechanism;
2 |
3 | import javax.annotation.security.DeclareRoles;
4 | import javax.security.enterprise.authentication.mechanism.http.BasicAuthenticationMechanismDefinition;
5 | import javax.servlet.annotation.HttpConstraint;
6 | import javax.servlet.annotation.ServletSecurity;
7 | import javax.servlet.annotation.WebServlet;
8 | import javax.servlet.http.HttpServlet;
9 |
10 | /**
11 | * Source code github.com/readlearncode
12 | *
13 | * @author Alex Theedom www.readlearncode.com
14 | * @version 1.0
15 | */
16 | @BasicAuthenticationMechanismDefinition(realmName = "user-realm")
17 | @WebServlet("/user")
18 | @DeclareRoles({"admin", "user", "demo"})
19 | @ServletSecurity(@HttpConstraint(rolesAllowed = "user"))
20 | public class UserServlet extends HttpServlet {
21 |
22 | // Servlet Code
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/security-1-0/src/main/java/com/readlearncode/identitystore/AdminServlet.java:
--------------------------------------------------------------------------------
1 | package com.readlearncode.identitystore;
2 |
3 | import javax.annotation.security.DeclareRoles;
4 | import javax.security.enterprise.identitystore.LdapIdentityStoreDefinition;
5 | import javax.servlet.annotation.WebServlet;
6 | import javax.servlet.http.HttpServlet;
7 |
8 | /**
9 | * Source code github.com/readlearncode
10 | *
11 | * @author Alex Theedom www.readlearncode.com
12 | * @version 1.0
13 | */
14 | @LdapIdentityStoreDefinition(
15 | url = "ldap://localhost:33389/",
16 | callerBaseDn = "ou=caller,dc=jsr375,dc=net",
17 | groupSearchBase = "ou=group,dc=jsr375,dc=net"
18 | )
19 | @DeclareRoles({"admin", "user", "demo"})
20 | @WebServlet("/admin")
21 | public class AdminServlet extends HttpServlet {
22 |
23 | // Servlet Code
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/security-1-0/src/main/java/com/readlearncode/identitystore/ApplicationConfig.java:
--------------------------------------------------------------------------------
1 | package com.readlearncode.identitystore;
2 |
3 | import javax.enterprise.context.ApplicationScoped;
4 | import javax.inject.Named;
5 | import javax.security.enterprise.identitystore.DatabaseIdentityStoreDefinition;
6 | import javax.security.enterprise.identitystore.PasswordHash;
7 |
8 | /**
9 | * Source code github.com/readlearncode
10 | *
11 | * @author Alex Theedom www.readlearncode.com
12 | * @version 1.0
13 | */
14 | @DatabaseIdentityStoreDefinition(
15 | dataSourceLookup = "${'java:global/permissions_db'}",
16 | callerQuery = "#{'select password from caller where name = ?'}",
17 | groupsQuery = "select group_name from caller_groups where caller_name = ?",
18 | hashAlgorithm = PasswordHash.class,
19 | priority = 10
20 | )
21 | @ApplicationScoped
22 | @Named
23 | public class ApplicationConfig {
24 |
25 | // Config code
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/security-1-0/src/main/java/com/readlearncode/identitystore/LiteAuthenticationMechanism.java:
--------------------------------------------------------------------------------
1 |
2 |
3 | package com.readlearncode.identitystore;
4 |
5 |
6 | import javax.enterprise.context.ApplicationScoped;
7 | import javax.inject.Inject;
8 | import javax.security.enterprise.AuthenticationStatus;
9 | import javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism;
10 | import javax.security.enterprise.authentication.mechanism.http.HttpMessageContext;
11 | import javax.security.enterprise.credential.UsernamePasswordCredential;
12 | import javax.security.enterprise.identitystore.CredentialValidationResult;
13 | import javax.security.enterprise.identitystore.IdentityStoreHandler;
14 | import javax.servlet.http.HttpServletRequest;
15 | import javax.servlet.http.HttpServletResponse;
16 |
17 | import static javax.security.enterprise.identitystore.CredentialValidationResult.Status.VALID;
18 |
19 | /**
20 | * Source code github.com/readlearncode
21 | *
22 | * @author Alex Theedom www.readlearncode.com
23 | * @version 1.0
24 | */
25 | @ApplicationScoped
26 | public class LiteAuthenticationMechanism implements HttpAuthenticationMechanism {
27 |
28 | @Inject
29 | private IdentityStoreHandler idStoreHandler;
30 |
31 | @Override
32 | public AuthenticationStatus validateRequest(HttpServletRequest req, HttpServletResponse res, HttpMessageContext context) {
33 |
34 | CredentialValidationResult result = idStoreHandler.validate(
35 | new UsernamePasswordCredential(
36 | req.getParameter("name"), req.getParameter("password")));
37 |
38 | if (result.getStatus() == VALID) {
39 | return context.notifyContainerAboutLogin(result);
40 | } else {
41 | return context.responseUnauthorized();
42 | }
43 |
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/security-1-0/src/main/java/com/readlearncode/identitystore/LiteWeightIdentityStore.java:
--------------------------------------------------------------------------------
1 | package com.readlearncode.identitystore;
2 |
3 | import javax.enterprise.context.ApplicationScoped;
4 | import javax.security.enterprise.credential.UsernamePasswordCredential;
5 | import javax.security.enterprise.identitystore.CredentialValidationResult;
6 | import javax.security.enterprise.identitystore.IdentityStore;
7 | import java.util.HashSet;
8 |
9 | import static java.util.Arrays.asList;
10 | import static javax.security.enterprise.identitystore.CredentialValidationResult.INVALID_RESULT;
11 |
12 | /**
13 | * Source code github.com/readlearncode
14 | *
15 | * @author Alex Theedom www.readlearncode.com
16 | * @version 1.0
17 | */
18 | @ApplicationScoped
19 | public class LiteWeightIdentityStore implements IdentityStore {
20 |
21 | public CredentialValidationResult validate(UsernamePasswordCredential userCredential) {
22 |
23 | if (userCredential.compareTo("admin", "pwd1")) {
24 | return new CredentialValidationResult("admin", new HashSet<>(asList("admin", "user", "demo")));
25 | }
26 |
27 | return INVALID_RESULT;
28 | }
29 |
30 | }
31 |
32 |
--------------------------------------------------------------------------------
/security-1-0/src/main/java/com/readlearncode/securitycontext/SecretServlet.java:
--------------------------------------------------------------------------------
1 | package com.readlearncode.securitycontext;
2 |
3 | import javax.annotation.security.DeclareRoles;
4 | import javax.servlet.annotation.HttpConstraint;
5 | import javax.servlet.annotation.ServletSecurity;
6 | import javax.servlet.annotation.WebServlet;
7 | import javax.servlet.http.HttpServlet;
8 |
9 | /**
10 | * Source code github.com/readlearncode
11 | *
12 | * @author Alex Theedom www.readlearncode.com
13 | * @version 1.0
14 | */
15 |
16 | @WebServlet("/secretServlet")
17 | @DeclareRoles({"admin", "user", "demo"})
18 | @ServletSecurity(@HttpConstraint(rolesAllowed = "admin"))
19 | public class SecretServlet extends HttpServlet { }
--------------------------------------------------------------------------------
/security-1-0/src/main/webapp/WEB-INF/beans.xml:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/readlearncode/Java-EE-8-Sampler/403bafbb48fff8feb01a6e2156fb221bd526b2d2/security-1-0/src/main/webapp/WEB-INF/beans.xml
--------------------------------------------------------------------------------
/security-1-0/src/main/webapp/WEB-INF/faces-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
--------------------------------------------------------------------------------
/security-1-0/src/main/webapp/WEB-INF/glassfish-web.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/security-1-0/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
10 | javax.faces.PROJECT_STAGE
11 | Development
12 |
13 |
14 |
15 |
16 | primefaces.THEME
17 | ui-lightness
18 |
19 |
20 |
21 | Faces Servlet
22 | javax.faces.webapp.FacesServlet
23 | 1
24 |
25 |
26 |
27 | Faces Servlet
28 | *.xhtml
29 |
30 |
31 |
32 | 60
33 |
34 |
35 |
36 | index.xhtml
37 | index.html
38 |
39 |
40 |
--------------------------------------------------------------------------------
/security-1-0/src/main/webapp/login.xhtml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | Login to continue
14 |
15 |
16 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/servlets-4-0/src/main/java/servlet4/ShowLogoServlet.java:
--------------------------------------------------------------------------------
1 | package servlet4;
2 |
3 | import javax.servlet.ServletException;
4 | import javax.servlet.annotation.WebServlet;
5 | import javax.servlet.http.HttpServlet;
6 | import javax.servlet.http.HttpServletRequest;
7 | import javax.servlet.http.HttpServletResponse;
8 | import java.io.IOException;
9 |
10 | /**
11 | * A simple POC use of the Server Push feature.
12 | *
13 | * Source code github.com/readlearncode
14 | *
15 | * @author Alex Theedom www.readlearncode.com
16 | * @version 1.0
17 | */
18 | @WebServlet("/showlogoservlet")
19 | public class ShowLogoServlet extends HttpServlet {
20 |
21 | @Override
22 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
23 |
24 | request.newPushBuilder().path("resources/images/logo.jpg").push();
25 |
26 | getServletContext()
27 | .getRequestDispatcher("/showlogo.jsp")
28 | .forward(request, response);
29 |
30 | }
31 | }
--------------------------------------------------------------------------------
/servlets-4-0/src/main/java/servlet4/hashtag100DaysOfJavaEE8/ServerPushExample.java:
--------------------------------------------------------------------------------
1 | package servlet4.hashtag100DaysOfJavaEE8;
2 |
3 | /**
4 | * A simple POC use of the Server Push feature.
5 | *
6 | * Source code github.com/readlearncode
7 | *
8 | * @author Alex Theedom www.readlearncode.com
9 | * @version 1.0
10 | */
11 |
12 |
13 | /*
14 | Servlets 4.0 introduces support for HTTP/2 feature
15 | ServerPush. What happens when a request is made to the
16 | URI /duke over an insecure connection. i.e. over HTTP
17 | connection?
18 | */
19 |
20 | // @WebServlet("/duke")
21 | // public class ServerPushExample extends HttpServlet {
22 | // @Override
23 | // protected void doGet(HttpServletRequest request,
24 | // HttpServletResponse response)
25 | // throws ServletException, IOException {
26 | //
27 | // request.newPushBuilder()
28 | // .path("resources/images/java-ee-logo.png")
29 | // .push();
30 | //
31 | // getServletContext()
32 | // .getRequestDispatcher("/duke.jsp")
33 | // .forward(request, response);
34 | // }
35 | // }
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/servlets-4-0/src/main/java/servlet4/mapping/ServletMapping.java:
--------------------------------------------------------------------------------
1 | package servlet4.mapping;
2 |
3 | import javax.servlet.annotation.WebServlet;
4 | import javax.servlet.http.HttpServlet;
5 | import javax.servlet.http.HttpServletMapping;
6 | import javax.servlet.http.HttpServletRequest;
7 | import javax.servlet.http.HttpServletResponse;
8 | import java.io.IOException;
9 |
10 | /**
11 | * Source code github.com/readlearncode
12 | *
13 | * @author Alex Theedom www.readlearncode.com
14 | * @version 1.0
15 | */
16 | @WebServlet({"/path/", "/path/to/*", "*.ext", "/path/file.ext"})
17 | public class ServletMapping extends HttpServlet {
18 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
19 | HttpServletMapping servletMapping = request.getHttpServletMapping();
20 | response.getWriter()
21 | .append("
")
22 | .append("Value Matched: ").append(servletMapping.getMatchValue())
23 | .append("
")
24 | .append("Pattern Used: ").append(servletMapping.getPattern())
25 | .append("
")
26 | .append("Mapping Matched: ").append(servletMapping.getMappingMatch().name())
27 | .append("
")
28 | .append("Servlet Name: ").append(servletMapping.getServletName())
29 | .append("
")
30 | .append("");
31 | }
32 | }
--------------------------------------------------------------------------------
/servlets-4-0/src/main/java/servlet4/pushbuilder/ObtainPushBuilder.java:
--------------------------------------------------------------------------------
1 | package servlet4.pushbuilder;
2 |
3 | import javax.servlet.ServletException;
4 | import javax.servlet.annotation.WebServlet;
5 | import javax.servlet.http.HttpServlet;
6 | import javax.servlet.http.HttpServletRequest;
7 | import javax.servlet.http.HttpServletResponse;
8 | import javax.servlet.http.PushBuilder;
9 | import java.io.IOException;
10 |
11 | /**
12 | * Source code github.com/readlearncode
13 | *
14 | * @author Alex Theedom www.readlearncode.com
15 | * @version 1.0
16 | */
17 | @WebServlet("/obtainpushbuilder")
18 | public class ObtainPushBuilder extends HttpServlet {
19 |
20 | @Override
21 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22 |
23 | PushBuilder pushBuilder = request.newPushBuilder();
24 |
25 | if (pushBuilder != null) {
26 | pushBuilder.path("images/hero-banner.jpg").push();
27 | pushBuilder.path("css/menu.css").push();
28 | pushBuilder.path("js/marquee.js").push();
29 | }
30 |
31 | getServletContext().getRequestDispatcher("/duke.jsp").forward(request, response);
32 |
33 | }
34 | }
--------------------------------------------------------------------------------
/servlets-4-0/src/main/java/servlet4/pushbuilder/SimplestExample.java:
--------------------------------------------------------------------------------
1 | package servlet4.pushbuilder;
2 |
3 | import javax.servlet.ServletException;
4 | import javax.servlet.annotation.WebServlet;
5 | import javax.servlet.http.HttpServlet;
6 | import javax.servlet.http.HttpServletRequest;
7 | import javax.servlet.http.HttpServletResponse;
8 | import java.io.IOException;
9 |
10 | /**
11 | * A simple POC use of the Server Push feature.
12 | *
13 | * Source code github.com/readlearncode
14 | *
15 | * @author Alex Theedom www.readlearncode.com
16 | * @version 1.0
17 | */
18 | @WebServlet("/duke")
19 | public class SimplestExample extends HttpServlet {
20 |
21 | @Override
22 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
23 |
24 | request.newPushBuilder()
25 | .path("resources/images/java-ee-logo.png")
26 | .push();
27 |
28 | getServletContext().getRequestDispatcher("/duke.jsp").forward(request, response);
29 |
30 | }
31 | }
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/WEB-INF/beans.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/WEB-INF/faces-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/WEB-INF/glassfish-web.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 | /Servlet4Push
6 |
7 |
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 | Servlet4Push
10 | /*
11 | GET
12 |
13 |
14 |
15 | CONFIDENTIAL
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 | Faces Servlet
32 | javax.faces.webapp.FacesServlet
33 | 1
34 |
35 |
36 |
37 | Faces Servlet
38 | *.xhtml
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/duke.jsp:
--------------------------------------------------------------------------------
1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2 |
3 |
4 | Servlet 4.0 ServerPush Example
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/duke.xhtml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
9 |
10 | JSF 2.3 ServerPush Example
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/index.jsp:
--------------------------------------------------------------------------------
1 | <%@ page contentType="text/html;charset=UTF-8" %>
2 |
3 |
4 | Servlet 4.0
5 |
6 |
7 | <%--Welcome to the Server Push example.
--%>
8 | <%--Open the network tab and click here.
--%>
9 | <%--Open the network tab and click here.
--%>
10 | Welcome to the Runtime Discovery of URL Mappings example.
11 |
12 | Activate the ServletMapping servlet with:
13 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/resources/css/coffee-cup.css:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/readlearncode/Java-EE-8-Sampler/403bafbb48fff8feb01a6e2156fb221bd526b2d2/servlets-4-0/src/main/webapp/resources/css/coffee-cup.css
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/resources/css/logo.css:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/readlearncode/Java-EE-8-Sampler/403bafbb48fff8feb01a6e2156fb221bd526b2d2/servlets-4-0/src/main/webapp/resources/css/logo.css
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/resources/images/java-ee-logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/readlearncode/Java-EE-8-Sampler/403bafbb48fff8feb01a6e2156fb221bd526b2d2/servlets-4-0/src/main/webapp/resources/images/java-ee-logo.png
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/resources/images/logo.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/readlearncode/Java-EE-8-Sampler/403bafbb48fff8feb01a6e2156fb221bd526b2d2/servlets-4-0/src/main/webapp/resources/images/logo.jpg
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/resources/js/logo.js:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/readlearncode/Java-EE-8-Sampler/403bafbb48fff8feb01a6e2156fb221bd526b2d2/servlets-4-0/src/main/webapp/resources/js/logo.js
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/showlogo.jsp:
--------------------------------------------------------------------------------
1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2 |
3 |
4 | Servlet 4.0 ServerPush Example
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/servlets-4-0/src/main/webapp/simplejsfpage.xhtml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 | JSF 2.3 ServerPush Example
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------