Skip to content

Commit 3b3e710

Browse files
committed
Add server push example
1 parent ef09403 commit 3b3e710

File tree

13 files changed

+312
-0
lines changed

13 files changed

+312
-0
lines changed

servlets-4-0/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<parent>
7+
<artifactId>Java-EE-8-Sampler</artifactId>
8+
<groupId>com.readlearncode</groupId>
9+
<version>1.0</version>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
14+
<artifactId>servlets-4-0</artifactId>
15+
16+
<repositories>
17+
<repository>
18+
<id>jvnet-nexus-promoted</id>
19+
<name>Java.net Promoted Repositories</name>
20+
<url>https://maven.java.net/content/repositories/promoted/</url>
21+
<releases>
22+
<enabled>true</enabled>
23+
</releases>
24+
<snapshots>
25+
<enabled>false</enabled>
26+
</snapshots>
27+
</repository>
28+
</repositories>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>org.glassfish</groupId>
33+
<artifactId>javax.faces</artifactId>
34+
<version>2.3.2</version>
35+
<scope>provided</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>javax.servlet</groupId>
39+
<artifactId>javax.servlet-api</artifactId>
40+
<version>4.0.0</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>javax.servlet.jsp</groupId>
45+
<artifactId>javax.servlet.jsp-api</artifactId>
46+
<version>2.3.2-b02</version>
47+
<scope>provided</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<finalName>servlets-4-0</finalName>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-compiler-plugin</artifactId>
57+
<version>${maven-compiler-plugin.version}</version>
58+
<configuration>
59+
<source>${java.version}</source>
60+
<target>${java.version}</target>
61+
</configuration>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
66+
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package servlet4.hashtag100DaysOfJavaEE8;
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+
19+
20+
/*
21+
Servlets 4.0 introduces support for HTTP/2 feature
22+
ServerPush. What happens when a request is made to the
23+
URI /duke over an insecure connection. i.e. over HTTP
24+
connection?
25+
*/
26+
27+
@WebServlet("/duke")
28+
public class ServerPushExample extends HttpServlet {
29+
@Override
30+
protected void doGet(HttpServletRequest request,
31+
HttpServletResponse response)
32+
throws ServletException, IOException {
33+
34+
request.newPushBuilder()
35+
.path("resources/images/java-ee-logo.png")
36+
.push();
37+
38+
getServletContext()
39+
.getRequestDispatcher("/duke.jsp")
40+
.forward(request, response);
41+
}
42+
}
43+
44+
45+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package servlet4.mapping;
2+
3+
import javax.servlet.ServletException;
4+
import javax.servlet.annotation.WebServlet;
5+
import javax.servlet.http.HttpServlet;
6+
import javax.servlet.http.HttpServletMapping;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
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({"/path/*", "*.ext"})
18+
public class ServletMapping extends HttpServlet {
19+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
20+
HttpServletMapping servletMapping = request.getHttpServletMapping();
21+
response.getWriter()
22+
.append("<html><body>")
23+
.append("Value Matched: ").append(servletMapping.getMatchValue())
24+
.append("<br/>")
25+
.append("Pattern Used: ").append(servletMapping.getPattern())
26+
.append("<br/>")
27+
.append("Mapping Matched: ").append(servletMapping.getMappingMatch().name())
28+
.append("<br/>")
29+
.append("Servlet Name: ").append(servletMapping.getServletName())
30+
.append("<br/>")
31+
.append("</body></html>");
32+
}
33+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package servlet4.pushbuilder;
2+
3+
import javax.servlet.FilterChain;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.ServletRequest;
6+
import javax.servlet.ServletResponse;
7+
import javax.servlet.annotation.WebFilter;
8+
import javax.servlet.http.HttpFilter;
9+
import javax.servlet.http.HttpServletMapping;
10+
import javax.servlet.http.HttpServletRequest;
11+
import java.io.IOException;
12+
import java.util.Collections;
13+
import java.util.Map;
14+
import java.util.Set;
15+
import java.util.concurrent.ConcurrentHashMap;
16+
17+
/**
18+
* A very simple and naive way to implement a push cache filter!!
19+
*
20+
* Source code github.com/readlearncode
21+
*
22+
* @author Alex Theedom www.readlearncode.com
23+
* @version 1.0
24+
*/
25+
@WebFilter("/PushCacheFilter")
26+
public class PushCacheFilter extends HttpFilter {
27+
28+
private Map<String, Set<String>> resourceCache = new ConcurrentHashMap<>();
29+
30+
@Override
31+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
32+
33+
HttpServletRequest httpServletRequest = ((HttpServletRequest) request);
34+
HttpServletMapping mapping = ((HttpServletRequest) request).getHttpServletMapping();
35+
String resourceURI = mapping.getMatchValue();
36+
37+
if (mapping.getServletName().equals("jsp")) {
38+
// Push resources
39+
resourceCache.keySet().stream()
40+
.filter(resourceURI::contains)
41+
.findFirst()
42+
.ifPresent(s -> resourceCache.get(s)
43+
.forEach(path -> httpServletRequest.newPushBuilder().path(path).push()));
44+
45+
// create empty resource list if absent
46+
resourceCache.putIfAbsent(resourceURI, Collections.newSetFromMap(new ConcurrentHashMap<>()));
47+
} else {
48+
// Add resource
49+
resourceCache.keySet().stream()
50+
.filter(httpServletRequest.getHeader("Referer")::contains)
51+
.forEach(page -> resourceCache.get(page).add(resourceURI));
52+
}
53+
54+
chain.doFilter(request, response);
55+
}
56+
57+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<faces-config version="2.2"
3+
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
6+
7+
</faces-config>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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"
3+
"http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
4+
<glassfish-web-app>
5+
<context-root>/Servlet4Push</context-root>
6+
</glassfish-web-app>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app version="3.1"
3+
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
6+
7+
<security-constraint>
8+
<web-resource-collection>
9+
<web-resource-name>Servlet4Push</web-resource-name>
10+
<url-pattern>/*</url-pattern>
11+
<http-method>GET</http-method>
12+
</web-resource-collection>
13+
14+
<user-data-constraint>
15+
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
16+
</user-data-constraint>
17+
18+
</security-constraint>
19+
20+
<!--<filter>-->
21+
<!--<filter-name>PushCacheFilter</filter-name>-->
22+
<!--<filter-class>com.readlearncode.servlet4.pushbuilder.PushCacheFilter</filter-class>-->
23+
<!--</filter>-->
24+
25+
<!--<filter-mapping>-->
26+
<!--<filter-name>PushCacheFilter</filter-name>-->
27+
<!--<url-pattern>/*</url-pattern>-->
28+
<!--</filter-mapping>-->
29+
30+
<servlet>
31+
<servlet-name>Faces Servlet</servlet-name>
32+
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
33+
<load-on-startup>1</load-on-startup>
34+
</servlet>
35+
36+
<servlet-mapping>
37+
<servlet-name>Faces Servlet</servlet-name>
38+
<url-pattern>*.xhtml</url-pattern>
39+
</servlet-mapping>
40+
41+
</web-app>

servlets-4-0/src/main/webapp/duke.jsp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<html>
3+
<head>
4+
<title>Servlet 4.0 ServerPush Example</title>
5+
</head>
6+
<body>
7+
<img src='resources/images/java-ee-logo.png'>
8+
</body>
9+
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4+
<html xmlns="http://www.w3.org/1999/xhtml"
5+
xmlns:h="http://java.sun.com/jsf/html">
6+
7+
<h:outputStylesheet library="css" name="coffee-cup.css"/>
8+
<h:outputScript library="js" name="logo.js" target="head"/>
9+
<h:head>
10+
<title>JSF 2.3 ServerPush Example</title>
11+
</h:head>
12+
<h:body>
13+
<h:form>
14+
<h:graphicImage library="images" name="java-ee-logo.png"/>
15+
</h:form>
16+
</h:body>
17+
</html>

servlets-4-0/src/main/webapp/resources/css/coffee-cup.css

Whitespace-only changes.

servlets-4-0/src/main/webapp/resources/js/logo.js

Whitespace-only changes.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy