Skip to content

Commit

Permalink
Merge #5543 from 4.1 into 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BalusC committed Feb 1, 2025
2 parents bb69c51 + 5360f3b commit 1a84573
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static jakarta.faces.application.ProjectStage.Development;
import static jakarta.faces.application.Resource.COMPONENT_RESOURCE_KEY;
import static jakarta.faces.application.StateManager.IS_BUILDING_INITIAL_STATE;
import static jakarta.faces.application.ViewHandler.CHARACTER_ENCODING_KEY;
import static jakarta.faces.application.ViewHandler.DEFAULT_FACELETS_SUFFIX;
import static jakarta.faces.application.ViewVisitOption.RETURN_AS_MINIMAL_IMPLICIT_OUTCOME;
import static jakarta.faces.component.UIComponent.BEANINFO_KEY;
Expand All @@ -52,7 +53,6 @@
import static java.util.Collections.emptyList;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.FINEST;
import static java.util.logging.Level.SEVERE;
import static java.util.logging.Level.WARNING;

import java.beans.BeanDescriptor;
Expand All @@ -66,32 +66,12 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

import com.sun.faces.application.ApplicationAssociate;
import com.sun.faces.context.StateContext;
import com.sun.faces.facelets.compiler.FaceletDoctype;
import com.sun.faces.facelets.el.ContextualCompositeMethodExpression;
import com.sun.faces.facelets.el.VariableMapperWrapper;
import com.sun.faces.facelets.impl.DefaultFaceletFactory;
import com.sun.faces.facelets.impl.XMLFrontMatterSaver;
import com.sun.faces.facelets.tag.composite.CompositeComponentBeanInfo;
import com.sun.faces.facelets.tag.faces.CompositeComponentTagHandler;
import com.sun.faces.facelets.tag.ui.UIDebug;
import com.sun.faces.renderkit.RenderKitUtils;
import com.sun.faces.renderkit.html_basic.DoctypeRenderer;
import com.sun.faces.util.Cache;
import com.sun.faces.util.ComponentStruct;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.HtmlUtils;
import com.sun.faces.util.RequestStateManager;
import com.sun.faces.util.Util;

import jakarta.el.ELContext;
import jakarta.el.ExpressionFactory;
import jakarta.el.MethodExpression;
Expand Down Expand Up @@ -141,6 +121,25 @@
import jakarta.faces.view.facelets.FaceletContext;
import jakarta.servlet.http.HttpSession;

import com.sun.faces.application.ApplicationAssociate;
import com.sun.faces.context.StateContext;
import com.sun.faces.facelets.compiler.FaceletDoctype;
import com.sun.faces.facelets.el.ContextualCompositeMethodExpression;
import com.sun.faces.facelets.el.VariableMapperWrapper;
import com.sun.faces.facelets.impl.DefaultFaceletFactory;
import com.sun.faces.facelets.impl.XMLFrontMatterSaver;
import com.sun.faces.facelets.tag.composite.CompositeComponentBeanInfo;
import com.sun.faces.facelets.tag.faces.CompositeComponentTagHandler;
import com.sun.faces.facelets.tag.ui.UIDebug;
import com.sun.faces.renderkit.RenderKitUtils;
import com.sun.faces.renderkit.html_basic.DoctypeRenderer;
import com.sun.faces.util.Cache;
import com.sun.faces.util.ComponentStruct;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.HtmlUtils;
import com.sun.faces.util.RequestStateManager;
import com.sun.faces.util.Util;

/**
* This {@link ViewHandlingStrategy} handles Facelets/PDL-based views.
*/
Expand Down Expand Up @@ -894,19 +893,20 @@ protected ResponseWriter createResponseWriter(FacesContext context) throws IOExc
}
}

// get our content type
String contentType = (String) context.getAttributes().get("facelets.ContentType");
// Get the <f:view contentType> as default content type.
// See also ViewHandler#apply().
String defaultContentType = (String) context.getAttributes().get("facelets.ContentType");

// get the encoding
String encoding = (String) context.getAttributes().get(FACELETS_ENCODING_KEY);
// Get the <f:view encoding> or otherwise Facelets default encoding of UTF-8 as default encoding.
// See also SAXCompiler#doCompile() and EncodingHandler#apply().
String defaultEncoding = (String) context.getAttributes().get(FACELETS_ENCODING_KEY);

// Create a dummy ResponseWriter with a bogus writer,
// so we can figure out what content type and encoding the ReponseWriter
// is really going to ask for
ResponseWriter initWriter = renderKit.createResponseWriter(NullWriter.INSTANCE, contentType, encoding);
// Create a dummy ResponseWriter with a bogus writer, so we can figure out what
// content type and default encoding the ResponseWriter is ultimately going to need.
ResponseWriter initWriter = renderKit.createResponseWriter(NullWriter.INSTANCE, defaultContentType, defaultEncoding);

contentType = getResponseContentType(context, initWriter.getContentType());
encoding = Util.getResponseEncoding(context, Optional.ofNullable(initWriter.getCharacterEncoding()));
String contentType = getResponseContentType(context, initWriter.getContentType());
String encoding = Util.getResponseEncoding(context, initWriter.getCharacterEncoding());

// apply them to the response
char[] buffer = new char[1028];
Expand All @@ -918,6 +918,11 @@ protected ResponseWriter createResponseWriter(FacesContext context) throws IOExc
// Save encoding in UIViewRoot for faster consult when Util#getResponseEncoding() is invoked again elsewhere.
context.getViewRoot().getAttributes().put(FACELETS_ENCODING_KEY, encoding);

// Save encoding in Session for consult in subsequent postback request as per spec section "2.5.2.2. Determining the Character Encoding".
if (context.getExternalContext().getSession(false) != null) {
context.getExternalContext().getSessionMap().put(CHARACTER_ENCODING_KEY, encoding);
}

// Now, clone with the real writer
ResponseWriter writer = initWriter.cloneWithWriter(extContext.getResponseOutputWriter());

Expand Down
12 changes: 5 additions & 7 deletions impl/src/main/java/com/sun/faces/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -1654,15 +1654,15 @@ public static int extractFirstNumericSegment(String clientId, char separatorChar
* @return the encoding to be used for the response
*/
public static String getResponseEncoding(FacesContext context) {
return getResponseEncoding(context, Optional.empty());
return getResponseEncoding(context, null);
}

/**
* @param context the {@link FacesContext} for the current request
* @param defaultEncoding the default encoding, if any
* @return the encoding to be used for the response
*/
public static String getResponseEncoding(FacesContext context, Optional<String> defaultEncoding) {
public static String getResponseEncoding(FacesContext context, String defaultEncoding) {

// 1. First get it from viewroot, if any.
if (context.getViewRoot() != null) {
Expand Down Expand Up @@ -1694,8 +1694,8 @@ public static String getResponseEncoding(FacesContext context, Optional<String>
}

if (encoding == null && context.getExternalContext().getSession(false) != null) {
// 4. If still none found then get previously known request encoding from session.
// See also ViewHandler#initView().
// 4. If still none found then get previously known request or response encoding from session.
// See also ViewHandler#initView() and FaceletViewHandlingStrategy#createResponseWriter().
encoding = (String) context.getExternalContext().getSessionMap().get(CHARACTER_ENCODING_KEY);

if (encoding != null && LOGGER.isLoggable(FINEST)) {
Expand All @@ -1705,9 +1705,7 @@ public static String getResponseEncoding(FacesContext context, Optional<String>

if (encoding == null) {
// 5. If still none found then fall back to specified default.
if (defaultEncoding.isPresent()) {
encoding = defaultEncoding.get();
}
encoding = defaultEncoding;

if (encoding != null && !encoding.isBlank()) {
if (LOGGER.isLoggable(FINEST)) {
Expand Down

0 comments on commit 1a84573

Please sign in to comment.