Skip to content

Commit

Permalink
5.0.3 release with some upstream PR merged
Browse files Browse the repository at this point in the history
Signed-off-by: pizzi80 <[email protected]>
  • Loading branch information
pizzi80 committed Feb 26, 2024
2 parents 2de09da + 039c661 commit 39ed397
Show file tree
Hide file tree
Showing 52 changed files with 788 additions and 492 deletions.
4 changes: 3 additions & 1 deletion impl/src/main/java/com/sun/faces/RIConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.sun.faces;

import java.nio.charset.StandardCharsets;

import com.sun.faces.config.manager.FacesSchema;

import jakarta.faces.render.RenderKitFactory;
Expand Down Expand Up @@ -58,7 +60,7 @@ public class RIConstants {
public static final String APPLICATION_XML_CONTENT_TYPE = "application/xml";
public static final String TEXT_XML_CONTENT_TYPE = "text/xml";
public static final String ALL_MEDIA = "*/*";
public static final String CHAR_ENCODING = "UTF-8";
public static final String CHAR_ENCODING = StandardCharsets.UTF_8.name();
public static final String FACELETS_ENCODING_KEY = "facelets.Encoding";
public static final String DEFAULT_LIFECYCLE = FACES_PREFIX + "DefaultLifecycle";
public static final String DEFAULT_STATEMANAGER = FACES_PREFIX + "DefaultStateManager";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,12 @@ public static ApplicationAssociate getCurrentInstance() {
}

public static ApplicationAssociate getInstance() {
FacesContext facesContext = FacesContext.getCurrentInstance();
return getInstance(FacesContext.getCurrentInstance());
}

public static ApplicationAssociate getInstance(FacesContext facesContext) {
if (facesContext == null) {
return null;
return null;
}

return ApplicationAssociate.getInstance(facesContext.getExternalContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@

package com.sun.faces.application;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.FINEST;
import static java.util.logging.Level.WARNING;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
Expand Down Expand Up @@ -81,7 +80,6 @@ private static class Utf8InfoRef {
int length;

public Utf8InfoRef(int index, int length) {
super();
this.index = index;
this.length = length;
}
Expand All @@ -103,7 +101,6 @@ private static class Utf8InfoReplacement implements Comparable<Utf8InfoReplaceme
byte[] replacement;

public Utf8InfoReplacement(Utf8InfoRef ref, String replacement) {
super();
this.ref = ref;
this.replacement = getUtf8InfoBytes(replacement);
}
Expand Down Expand Up @@ -543,8 +540,7 @@ private static String getVMClassName(Class<?> c) {
* @return the bytes for the UTF8Info constant pool entry, including the tag, length, and utf8 content.
*/
private static byte[] getUtf8InfoBytes(String text) {
byte[] utf8;
utf8 = text.getBytes(StandardCharsets.UTF_8);
byte[] utf8 = text.getBytes(UTF_8);
byte[] info = new byte[utf8.length + 3];
info[0] = 1;
info[1] = (byte) (utf8.length >> 8 & 0xff);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static com.sun.faces.RIConstants.FLOW_IN_JAR_PREFIX;
import static com.sun.faces.config.WebConfiguration.META_INF_CONTRACTS_DIR;
import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.FaceletsSuffix;
import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.WebAppContractsDirectory;
import static jakarta.faces.application.ResourceVisitOption.TOP_LEVEL_VIEWS_ONLY;
import static java.util.Spliterator.DISTINCT;
import static java.util.Spliterators.spliteratorUnknownSize;
Expand All @@ -32,13 +31,16 @@
import java.util.Enumeration;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.stream.Stream;

import com.sun.faces.application.ApplicationAssociate;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.util.Util;

import jakarta.enterprise.inject.Any;
import jakarta.faces.FacesException;
import jakarta.faces.annotation.View;
import jakarta.faces.application.ResourceVisitOption;
import jakarta.faces.context.ExternalContext;
import jakarta.faces.context.FacesContext;
Expand All @@ -48,12 +50,12 @@ public class FaceletWebappResourceHelper extends ResourceHelper {

private static final String[] RESTRICTED_DIRECTORIES = { "/WEB-INF/", "/META-INF/" };

private final String webAppContractsDirectory;
private final ResourceHelper webappResourceHelper;
private final String[] configuredExtensions;

public FaceletWebappResourceHelper() {
public FaceletWebappResourceHelper(WebappResourceHelper webappResourceHelper) {
this.webappResourceHelper = webappResourceHelper;
WebConfiguration webConfig = WebConfiguration.getInstance();
webAppContractsDirectory = webConfig.getOptionValue(WebAppContractsDirectory);
configuredExtensions = webConfig.getOptionValue(FaceletsSuffix, " ");
}

Expand Down Expand Up @@ -114,9 +116,16 @@ public ResourceInfo findResource(LibraryInfo library, String resourceName, Strin
}

public Stream<String> getViewResources(FacesContext facesContext, String path, int maxDepth, ResourceVisitOption... options) {
return stream(spliteratorUnknownSize(
Stream<String> physicalViewResources = stream(spliteratorUnknownSize(
new ResourcePathsIterator(path, maxDepth, configuredExtensions, getRestrictedDirectories(options), facesContext.getExternalContext()),
DISTINCT), false);
Stream<String> programmaticViewResources = Util.getCdiBeanManager(facesContext)
.getBeans(Object.class, Any.Literal.INSTANCE).stream()
.map(bean -> bean.getBeanClass().getAnnotation(View.class))
.filter(Objects::nonNull)
.map(View::value);

return Stream.concat(physicalViewResources, programmaticViewResources);
}

private static String[] getRestrictedDirectories(final ResourceVisitOption... options) {
Expand Down Expand Up @@ -150,9 +159,9 @@ private URL findResourceInfoConsideringContracts(FacesContext ctx, String baseRe

for (String contract : contracts) {
if (baseResourceName.startsWith("/")) {
resourceName = webAppContractsDirectory + "/" + contract + baseResourceName;
resourceName = getBaseContractsPath() + "/" + contract + baseResourceName;
} else {
resourceName = webAppContractsDirectory + "/" + contract + "/" + baseResourceName;
resourceName = getBaseContractsPath() + "/" + contract + "/" + baseResourceName;
}

url = Resource.getResourceUrl(ctx, resourceName);
Expand Down Expand Up @@ -226,7 +235,7 @@ public String getBaseResourcePath() {

@Override
public String getBaseContractsPath() {
return webAppContractsDirectory;
return webappResourceHelper.getBaseContractsPath();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.sun.faces.application.resource;

import static com.sun.faces.util.Util.ensureLeadingSlash;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -57,22 +59,25 @@ public class ResourceManager {
*/
private static final Pattern CONFIG_MIMETYPE_PATTERN = Pattern.compile("[a-z-]*/[a-z0-9.\\*-]*");

private final FaceletWebappResourceHelper faceletWebappResourceHelper = new FaceletWebappResourceHelper();

/**
* {@link ResourceHelper} used for looking up webapp-based resources.
*/
private final ResourceHelper webappResourceHelper = new WebappResourceHelper();
private WebappResourceHelper webappResourceHelper = new WebappResourceHelper();

/**
* {@link ResourceHelper} used for looking up webapp-based facelets resources.
*/
private FaceletWebappResourceHelper faceletWebappResourceHelper = new FaceletWebappResourceHelper(webappResourceHelper);

/**
* {@link ResourceHelper} used for looking up classpath-based resources.
*/
private final ClasspathResourceHelper classpathResourceHelper = new ClasspathResourceHelper();
private ClasspathResourceHelper classpathResourceHelper = new ClasspathResourceHelper();

/**
* Cache for storing {@link ResourceInfo} instances to reduce the cost of the resource lookups.
*/
private final ResourceCache cache;
private ResourceCache cache;

/**
* Patterns used to find {@link ResourceInfo} instances that may have their content compressed.
Expand Down Expand Up @@ -176,6 +181,14 @@ public Stream<String> getViewResources(FacesContext facesContext, String path, i
return faceletWebappResourceHelper.getViewResources(facesContext, path, maxDepth, options);
}

public String getBaseContractsPath() {
return faceletWebappResourceHelper.getBaseContractsPath();
}

public boolean isContractsResource(String path) {
return ensureLeadingSlash(path).startsWith(getBaseContractsPath());
}

// ----------------------------------------------------- Private Methods

private ResourceInfo findResourceCompressed(String libraryName, String resourceName, boolean isViewResource, String localePrefix, List<String> contracts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class FaceletFullStateManagementStrategy extends StateManagementStrategy
/**
* Stores the skip hint.
*/
private static final String SKIP_ITERATION_HINT = "jakarta.faces.visit.SKIP_ITERATION";
private static final Set<VisitHint> SKIP_ITERATION_HINT = EnumSet.of(SKIP_ITERATION);

/**
* Stores the class map.
Expand Down Expand Up @@ -185,41 +185,34 @@ private UIComponent locateComponentByClientId(final FacesContext context, final
final List<UIComponent> found = new ArrayList<>();
UIComponent result = null;

try {
context.getAttributes().put(SKIP_ITERATION_HINT, true);
Set<VisitHint> hints = EnumSet.of(VisitHint.SKIP_ITERATION);

VisitContext visitContext = VisitContext.createVisitContext(context, null, hints);
subTree.visitTree(visitContext, (visitContext1, component) -> {
VisitResult result1 = ACCEPT;
if (component.getClientId(visitContext1.getFacesContext()).equals(clientId)) {
/*
* If the client id matches up we have found our match.
*/
found.add(component);
result1 = COMPLETE;
} else if (component instanceof UIForm) {
/*
* If the component is a UIForm and it is prepending its id then we can short circuit out of here if the the client id
* of the component we are trying to find does not begin with the id of the UIForm.
*/
UIForm form = (UIForm) component;
if (form.isPrependId() && !clientId.startsWith(form.getClientId(visitContext1.getFacesContext()))) {
result1 = REJECT;
}
} else if (component instanceof NamingContainer && !clientId.startsWith(component.getClientId(visitContext1.getFacesContext()))) {
/*
* If the component is a naming container then assume it is prepending its id so if our client id we are looking for
* does not start with the naming container id we can skip visiting this tree.
*/
VisitContext visitContext = VisitContext.createVisitContext(context, null, SKIP_ITERATION_HINT);
subTree.visitTree(visitContext, (visitContext1, component) -> {
VisitResult result1 = ACCEPT;
if (component.getClientId(visitContext1.getFacesContext()).equals(clientId)) {
/*
* If the client id matches up we have found our match.
*/
found.add(component);
result1 = COMPLETE;
} else if (component instanceof UIForm) {
/*
* If the component is a UIForm and it is prepending its id then we can short circuit out of here if the the client id
* of the component we are trying to find does not begin with the id of the UIForm.
*/
UIForm form = (UIForm) component;
if (form.isPrependId() && !clientId.startsWith(form.getClientId(visitContext1.getFacesContext()))) {
result1 = REJECT;
}
} else if (component instanceof NamingContainer && !clientId.startsWith(component.getClientId(visitContext1.getFacesContext()))) {
/*
* If the component is a naming container then assume it is prepending its id so if our client id we are looking for
* does not start with the naming container id we can skip visiting this tree.
*/
result1 = REJECT;
}

return result1;
});
} finally {
context.getAttributes().remove(SKIP_ITERATION_HINT);
}
return result1;
});

if (!found.isEmpty()) {
result = found.get(0);
Expand Down Expand Up @@ -311,36 +304,30 @@ private void restoreComponentState(final FacesContext context, final HashMap<Str
final StateContext stateContext = StateContext.getStateContext(context);
final UIViewRoot viewRoot = context.getViewRoot();

try {
context.getAttributes().put(SKIP_ITERATION_HINT, true);
Set<VisitHint> hints = EnumSet.of(SKIP_ITERATION);
VisitContext visitContext = VisitContext.createVisitContext(context, null, hints);
VisitContext visitContext = VisitContext.createVisitContext(context, null, SKIP_ITERATION_HINT);

viewRoot.visitTree(visitContext, (visitContext1, component) -> {
VisitResult result = ACCEPT;
viewRoot.visitTree(visitContext, (visitContext1, component) -> {
VisitResult result = ACCEPT;

String clientId = component.getClientId(context);
Object stateObj = state.get(clientId);
String clientId = component.getClientId(context);
Object stateObj = state.get(clientId);

if (stateObj != null && !stateContext.componentAddedDynamically(component)) {
boolean restoreStateNow = true;
if (stateObj instanceof StateHolderSaver) {
restoreStateNow = !((StateHolderSaver) stateObj).componentAddedDynamically();
}
if (restoreStateNow) {
try {
component.restoreState(context, stateObj);
} catch (Exception e) {
throw new FacesException(e);
}
if (stateObj != null && !stateContext.componentAddedDynamically(component)) {
boolean restoreStateNow = true;
if (stateObj instanceof StateHolderSaver) {
restoreStateNow = !((StateHolderSaver) stateObj).componentAddedDynamically();
}
if (restoreStateNow) {
try {
component.restoreState(context, stateObj);
} catch (Exception e) {
throw new FacesException(e);
}
}
}

return result;
});
} finally {
context.getAttributes().remove(SKIP_ITERATION_HINT);
}
return result;
});
}

/**
Expand Down Expand Up @@ -592,33 +579,27 @@ private Object saveComponentState(FacesContext context) {
final UIViewRoot viewRoot = context.getViewRoot();
final FacesContext finalContext = context;

context.getAttributes().put(SKIP_ITERATION_HINT, true);
Set<VisitHint> hints = EnumSet.of(SKIP_ITERATION);
VisitContext visitContext = VisitContext.createVisitContext(context, null, hints);
VisitContext visitContext = VisitContext.createVisitContext(context, null, SKIP_ITERATION_HINT);

try {
viewRoot.visitTree(visitContext, (context1, component) -> {
VisitResult result = ACCEPT;
Object stateObj;
if (!component.isTransient()) {
if (stateContext.componentAddedDynamically(component)) {
component.getAttributes().put(DYNAMIC_COMPONENT, new Integer(getProperChildIndex(component)));
stateObj = new StateHolderSaver(finalContext, component);
} else {
stateObj = component.saveState(finalContext);
}
if (stateObj != null) {
stateMap.put(component.getClientId(finalContext), stateObj);
}
viewRoot.visitTree(visitContext, (context1, component) -> {
VisitResult result = ACCEPT;
Object stateObj;
if (!component.isTransient()) {
if (stateContext.componentAddedDynamically(component)) {
component.getAttributes().put(DYNAMIC_COMPONENT, new Integer(getProperChildIndex(component)));
stateObj = new StateHolderSaver(finalContext, component);
} else {
result = REJECT;
stateObj = component.saveState(finalContext);
}
if (stateObj != null) {
stateMap.put(component.getClientId(finalContext), stateObj);
}
} else {
result = REJECT;
}

return result;
});
} finally {
context.getAttributes().remove(SKIP_ITERATION_HINT);
}
return result;
});

return stateMap;
}
Expand Down
Loading

0 comments on commit 39ed397

Please sign in to comment.