Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Util.getResponseEncoding: defaultEncoding from Optional<String> to String #5438

Open
wants to merge 2 commits into
base: 4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
41 changes: 21 additions & 20 deletions impl/src/main/java/com/sun/faces/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -1626,15 +1626,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 All @@ -1655,44 +1655,45 @@ public static String getResponseEncoding(FacesContext context, Optional<String>
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)) {
LOGGER.log(FINEST, "Using Request encoding {0}", encoding);
}
}

// 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)) {
LOGGER.log(FINEST, "Using Session encoding {0}", encoding);
}
}

// 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);
}
}

Expand Down
Loading