Skip to content

Commit

Permalink
Merge pull request #5425 from eclipse-ee4j/mojarra_issue_5414_only_ht…
Browse files Browse the repository at this point in the history
…tp2push_real_resource_urls

Only http/2-push real faces resource urls
  • Loading branch information
BalusC committed Mar 30, 2024
2 parents c9192d6 + 93aea33 commit 0be8cc5
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions impl/src/main/java/com/sun/faces/context/ExternalContextImpl.java
Expand Up @@ -42,16 +42,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.faces.RIConstants;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.context.flash.ELFlash;
import com.sun.faces.renderkit.html_basic.ScriptRenderer;
import com.sun.faces.renderkit.html_basic.StylesheetRenderer;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.MessageUtils;
import com.sun.faces.util.TypedCollections;
import com.sun.faces.util.Util;

import jakarta.faces.FacesException;
import jakarta.faces.FactoryFinder;
import jakarta.faces.application.ProjectStage;
Expand All @@ -74,6 +64,16 @@
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.PushBuilder;

import com.sun.faces.RIConstants;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.context.flash.ELFlash;
import com.sun.faces.renderkit.html_basic.ScriptRenderer;
import com.sun.faces.renderkit.html_basic.StylesheetRenderer;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.MessageUtils;
import com.sun.faces.util.TypedCollections;
import com.sun.faces.util.Util;

/**
* <p>
* This implementation of {@link ExternalContext} is specific to the servlet implementation.
Expand Down Expand Up @@ -576,10 +576,10 @@ public String encodeResourceURL(String url) {
throw new NullPointerException(getExceptionMessageString(NULL_PARAMETERS_ERROR_MESSAGE_ID, "url"));
}

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

return result;
return encodedURL;
}

/**
Expand Down Expand Up @@ -1032,26 +1032,25 @@ public void release() {
flash = null;
}

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

// 1. Don't bother trying to push if we've already pushed this URL for this request
@SuppressWarnings("unchecked")
Set<String> resourceUrls = (Set<String>) context.getAttributes().computeIfAbsent(PUSH_RESOURCE_URLS_KEY_NAME,
k -> new HashSet<>());
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.contains(result)) {
if (!resourceUrls.add(encodedURL)) {
return;
}
resourceUrls.add(result);

// 2. At this point we know we haven't pushed this URL for this request before
PushBuilder pushBuilder = getPushBuilder(context);

if (pushBuilder != null) {
// and now we also know there was no If-Modified-Since header
pushBuilder.path(result).push();
pushBuilder.path(encodedURL).push();
}

}

private PushBuilder getPushBuilder(FacesContext context) {
Expand Down

0 comments on commit 0be8cc5

Please sign in to comment.