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..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, Optional.empty()); + return getResponseEncoding(context, null); } /** @@ -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,24 +1677,23 @@ 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; + } - 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 { - // 6. If specified default is null or blank then finally fall back to hardcoded default. - encoding = RIConstants.CHAR_ENCODING; + } + else { - if (LOGGER.isLoggable(FINEST)) { - LOGGER.log(FINEST, "No encoding found, defaulting to {0}", encoding); - } + encoding = RIConstants.CHAR_ENCODING; + + if (LOGGER.isLoggable(FINEST)) { + LOGGER.log(FINEST, "No encoding found, defaulting to {0}", encoding); } }