From 594b1517308c45b3698f864b57eb67551c959240 Mon Sep 17 00:00:00 2001 From: pizzi80 Date: Wed, 10 Apr 2024 15:28:51 +0200 Subject: [PATCH 1/2] Issue #5436 + fix for PR 5430 Signed-off-by: pizzi80 --- .../view/FaceletViewHandlingStrategy.java | 4 ++-- .../main/java/com/sun/faces/util/Util.java | 23 ++++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/impl/src/main/java/com/sun/faces/application/view/FaceletViewHandlingStrategy.java b/impl/src/main/java/com/sun/faces/application/view/FaceletViewHandlingStrategy.java index 667e21b5e4..9f0f1b9cfb 100644 --- a/impl/src/main/java/com/sun/faces/application/view/FaceletViewHandlingStrategy.java +++ b/impl/src/main/java/com/sun/faces/application/view/FaceletViewHandlingStrategy.java @@ -908,12 +908,12 @@ protected ResponseWriter createResponseWriter(FacesContext context) throws IOExc String encoding = (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 + // so we can figure out what content type and encoding the ResponseWriter // is really going to ask for ResponseWriter initWriter = renderKit.createResponseWriter(NullWriter.INSTANCE, contentType, encoding); contentType = getResponseContentType(context, initWriter.getContentType()); - encoding = Util.getResponseEncoding(context, Optional.ofNullable(initWriter.getCharacterEncoding())); + encoding = Util.getResponseEncoding(context, initWriter.getCharacterEncoding()); // apply them to the response char[] buffer = new char[1028]; diff --git a/impl/src/main/java/com/sun/faces/util/Util.java b/impl/src/main/java/com/sun/faces/util/Util.java index ca481492aa..3f05218871 100644 --- a/impl/src/main/java/com/sun/faces/util/Util.java +++ b/impl/src/main/java/com/sun/faces/util/Util.java @@ -1626,7 +1626,7 @@ 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, RIConstants.CHAR_ENCODING); } /** @@ -1634,7 +1634,7 @@ public static String getResponseEncoding(FacesContext context) { * @param defaultEncoding the default encoding, if any * @return the encoding to be used for the response */ - public static String getResponseEncoding(FacesContext context, Optional defaultEncoding) { + public static String getResponseEncoding(FacesContext context, String defaultEncoding) { // 1. First get it from viewroot, if any. if (context.getViewRoot() != null) { @@ -1655,9 +1655,10 @@ public static String getResponseEncoding(FacesContext context, Optional LOGGER.log(FINEST, "Using Facelet encoding {0}", encoding); } + // 3. If none found then get it from request (could happen when the view isn't built yet). + // See also ViewHandler#initView() and ViewHandler#calculateCharacterEncoding(). if (encoding == null) { - // 3. If none found then get it from request (could happen when the view isn't built yet). - // See also ViewHandler#initView() and ViewHandler#calculateCharacterEncoding(). + encoding = context.getExternalContext().getRequestCharacterEncoding(); if (encoding != null && LOGGER.isLoggable(FINEST)) { @@ -1665,9 +1666,10 @@ public static String getResponseEncoding(FacesContext context, Optional } } + // 4. If still none found then get previously known request encoding from session. + // See also ViewHandler#initView(). 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(). + encoding = (String) context.getExternalContext().getSessionMap().get(CHARACTER_ENCODING_KEY); if (encoding != null && LOGGER.isLoggable(FINEST)) { @@ -1675,19 +1677,18 @@ public static String getResponseEncoding(FacesContext context, Optional } } + // 5. If still none found then fall back to specified default. if (encoding == null) { - // 5. If still none found then fall back to specified default. - if (defaultEncoding.isPresent()) { - encoding = defaultEncoding.get(); - } + encoding = defaultEncoding; + // 6. If specified default is null or blank then finally fall back to hardcoded default. if (encoding != null && !encoding.isBlank()) { if (LOGGER.isLoggable(FINEST)) { LOGGER.log(FINEST, "Using specified default encoding {0}", encoding); } } else { - // 6. If specified default is null or blank then finally fall back to hardcoded default. + encoding = RIConstants.CHAR_ENCODING; if (LOGGER.isLoggable(FINEST)) { From ec3ca586b6102103bb2f5893331cf489b28a6f4e Mon Sep 17 00:00:00 2001 From: pizzi80 Date: Wed, 10 Apr 2024 17:28:26 +0200 Subject: [PATCH 2/2] Further simplification Signed-off-by: pizzi80 --- .../main/java/com/sun/faces/util/Util.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/impl/src/main/java/com/sun/faces/util/Util.java b/impl/src/main/java/com/sun/faces/util/Util.java index 3f05218871..d75e717a30 100644 --- a/impl/src/main/java/com/sun/faces/util/Util.java +++ b/impl/src/main/java/com/sun/faces/util/Util.java @@ -1626,7 +1626,7 @@ 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, RIConstants.CHAR_ENCODING); + return getResponseEncoding(context, null); } /** @@ -1680,20 +1680,20 @@ public static String getResponseEncoding(FacesContext context, String defaultEnc // 5. If still none found then fall back to specified default. if (encoding == null) { encoding = defaultEncoding; + } - // 6. If specified default is null or blank then finally fall back to hardcoded default. - if (encoding != null && !encoding.isBlank()) { - if (LOGGER.isLoggable(FINEST)) { - LOGGER.log(FINEST, "Using specified default encoding {0}", encoding); - } + // 6. If specified default is null or blank then finally fall back to hardcoded default. + if (encoding != null && !encoding.isBlank()) { + if (LOGGER.isLoggable(FINEST)) { + LOGGER.log(FINEST, "Using specified default encoding {0}", encoding); } - else { + } + else { - encoding = RIConstants.CHAR_ENCODING; + encoding = RIConstants.CHAR_ENCODING; - if (LOGGER.isLoggable(FINEST)) { - LOGGER.log(FINEST, "No encoding found, defaulting to {0}", encoding); - } + if (LOGGER.isLoggable(FINEST)) { + LOGGER.log(FINEST, "No encoding found, defaulting to {0}", encoding); } }