Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<artifactId>mojarra</artifactId>
<packaging>jar</packaging>

<name>Mojarra ${project.version}</name>
<name>Mojarra ${project.version} - Impl</name>
<description>EE4J Compatible Implementation for Jakarta Faces API</description>

<url>https://github.com/eclipse-ee4j/mojarra</url>
Expand Down
2 changes: 1 addition & 1 deletion impl/src/main/java/org/glassfish/mojarra/RIConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class RIConstants {

public static final String VIEWID_KEY_NAME = RI_PREFIX + "viewId";

public static final String PUSH_RESOURCE_URLS_KEY_NAME = RI_PREFIX + "resourceUrls";
public static final String EARLY_HINTS_RESOURCE_URLS_KEY_NAME = RI_PREFIX + "earlyHintsResourceUrls";

/**
* Marker used when saving the list of component adds and removes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.glassfish.mojarra.context;

import static org.glassfish.mojarra.RIConstants.PUSH_RESOURCE_URLS_KEY_NAME;
import static org.glassfish.mojarra.RIConstants.EARLY_HINTS_RESOURCE_URLS_KEY_NAME;
import static org.glassfish.mojarra.context.MojarraContextParam.SendPoweredByHeader;
import static org.glassfish.mojarra.context.UrlBuilder.PROTOCOL_SEPARATOR;
import static org.glassfish.mojarra.context.UrlBuilder.WEBSOCKET_PROTOCOL;
Expand Down Expand Up @@ -61,7 +61,6 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.PushBuilder;

import org.glassfish.mojarra.RIConstants;
import org.glassfish.mojarra.config.WebConfiguration;
Expand Down Expand Up @@ -574,7 +573,7 @@ public String encodeResourceURL(String url) {
Util.notNull("url", url);

String encodedURL = ((HttpServletResponse) response).encodeURL(url);
pushIfPossibleAndNecessary(encodedURL);
addEarlyHintIfPossibleAndNecessary(encodedURL);

return encodedURL;
}
Expand Down Expand Up @@ -1079,38 +1078,38 @@ public void release() {
}

@SuppressWarnings("unchecked")
private void pushIfPossibleAndNecessary(String encodedURL) {
private void addEarlyHintIfPossibleAndNecessary(String encodedURL) {
FacesContext context = FacesContext.getCurrentInstance();

if (!context.getApplication().getResourceHandler().isResourceURL(encodedURL)) {
return;
}

Set<String> resourceUrls = (Set<String>) context.getAttributes().computeIfAbsent(PUSH_RESOURCE_URLS_KEY_NAME, k -> new HashSet<>());

if (!resourceUrls.add(encodedURL)) {
// Skip when the client is revalidating, as it then most likely already has the resources cached.
if (!isEmpty(getRequestHeaderMap().get("If-Modified-Since"))) {
return;
}

PushBuilder pushBuilder = getPushBuilder(context);
if (response instanceof HttpServletResponse hres && !hres.isCommitted()) {
Set<String> resourceUrls = (Set<String>) context.getAttributes().computeIfAbsent(EARLY_HINTS_RESOURCE_URLS_KEY_NAME, k -> new HashSet<>());

if (pushBuilder != null) {
pushBuilder.path(encodedURL).push();
if (resourceUrls.add(encodedURL)) {
hres.addHeader("Link", "<" + encodedURL + ">;rel=preload");
}
}
}

private PushBuilder getPushBuilder(FacesContext context) {
ExternalContext extContext = context.getExternalContext();

if (extContext.getRequest() instanceof HttpServletRequest) {
HttpServletRequest hreq = (HttpServletRequest) extContext.getRequest();
/**
* Sends a single HTTP 103 (Early Hints) interim response with the {@code Link} preload headers collected so far,
* provided the response is not yet committed. Invoked once, right after the head resources have been rendered, so the
* client can start fetching the render-blocking resources while the body is still being rendered.
*/
public static void sendEarlyHints(FacesContext context) {
Object resourceUrls = context.getAttributes().get(EARLY_HINTS_RESOURCE_URLS_KEY_NAME);

if (isEmpty(extContext.getRequestHeaderMap().get("If-Modified-Since"))) {
return hreq.newPushBuilder();
}
if (resourceUrls instanceof Set<?> set && !set.isEmpty() && context.getExternalContext().getResponse() instanceof HttpServletResponse hres && !hres.isCommitted()) {
hres.sendEarlyHints();
}

return null;
}

private void doLastPhaseActions(FacesContext context, boolean outgoingResponseIsRedirect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import jakarta.faces.context.FacesContext;
import jakarta.faces.context.ResponseWriter;

import org.glassfish.mojarra.context.ExternalContextImpl;
import org.glassfish.mojarra.renderkit.Attribute;
import org.glassfish.mojarra.renderkit.AttributeManager;
import org.glassfish.mojarra.renderkit.RenderKitUtils;
Expand Down Expand Up @@ -62,6 +63,7 @@ public void encodeChildren(FacesContext context, UIComponent component) throws I
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
ResponseWriter writer = context.getResponseWriter();
encodeHeadResources(context);
ExternalContextImpl.sendEarlyHints(context);
RenderKitUtils.flushPendingBehaviorEventListeners(context, component, null);
writer.endElement("head");
}
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

<modules>
<module>impl</module>
<module>test</module>
</modules>

<properties>
Expand Down
32 changes: 32 additions & 0 deletions test/issue5746/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2026 Contributors to Eclipse Foundation.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.glassfish.mojarra.test</groupId>
<artifactId>pom</artifactId>
<version>5.0.0-SNAPSHOT</version>
</parent>

<artifactId>issue5746</artifactId>
<packaging>war</packaging>

<name>Mojarra ${project.version} - IT - ${project.artifactId}</name>
</project>
24 changes: 24 additions & 0 deletions test/issue5746/src/main/webapp/WEB-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2026 Contributors to Eclipse Foundation.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->
<beans
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_1.xsd"
version="4.1">
</beans>
24 changes: 24 additions & 0 deletions test/issue5746/src/main/webapp/WEB-INF/faces-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2026 Contributors to Eclipse Foundation.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->
<faces-config
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-facesconfig_4_1.xsd"
version="4.1">
</faces-config>
45 changes: 45 additions & 0 deletions test/issue5746/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2026 Contributors to Eclipse Foundation.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->
<web-app
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_1.xsd"
version="6.1">
<context-param>
<param-name>jakarta.faces.PROJECT_STAGE</param-name>
<param-value>${webapp.projectStage}</param-value>
</context-param>
<context-param>
<param-name>jakarta.faces.PARTIAL_STATE_SAVING</param-name>
<param-value>${webapp.partialStateSaving}</param-value>
</context-param>
<context-param>
<param-name>jakarta.faces.STATE_SAVING_METHOD</param-name>
<param-value>${webapp.stateSavingMethod}</param-value>
</context-param>
<context-param>
<param-name>jakarta.faces.SERIALIZE_SERVER_STATE</param-name>
<param-value>${webapp.serializeServerState}</param-value>
</context-param>

<context-param>
<param-name>jakarta.faces.ENABLE_CSP_NONCE</param-name>
<param-value>true</param-value>
</context-param>
</web-app>
33 changes: 33 additions & 0 deletions test/issue5746/src/main/webapp/issue5746.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<!--

Copyright (c) 2026 Contributors to Eclipse Foundation.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->
<html lang="en"
xmlns:h="jakarta.faces.html"
>
<h:head>
<title>Early Hints 103</title>
<h:outputStylesheet library="css" name="style1.css" />
<h:outputStylesheet library="css" name="style2.css" />
<h:outputScript library="js" name="script1.js" />
<h:outputScript library="js" name="script2.js" />
<h:outputScript library="js" name="script3.js" />
</h:head>
<h:body>
<h1 id="content">Early Hints 103 test page</h1>
</h:body>
</html>
4 changes: 4 additions & 0 deletions test/issue5746/src/main/webapp/resources/css/style1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Early Hints 103 test resource. */
body {
font-family: sans-serif;
}
4 changes: 4 additions & 0 deletions test/issue5746/src/main/webapp/resources/css/style2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Early Hints 103 test resource. */
h1 {
color: #336699;
}
2 changes: 2 additions & 0 deletions test/issue5746/src/main/webapp/resources/js/script1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Early Hints 103 test resource.
console.log("script1.js loaded");
2 changes: 2 additions & 0 deletions test/issue5746/src/main/webapp/resources/js/script2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Early Hints 103 test resource.
console.log("script2.js loaded");
2 changes: 2 additions & 0 deletions test/issue5746/src/main/webapp/resources/js/script3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Early Hints 103 test resource.
console.log("script3.js loaded");
Loading
Loading