Skip to content

Commit

Permalink
Improved #5543; drop pesky Optional and improved code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
BalusC committed Jan 18, 2025
1 parent a30bdf8 commit 81b1912
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
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;
Expand Down Expand Up @@ -905,19 +904,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 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 @@ -929,7 +929,7 @@ 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 as per spec section "2.5.2.2. Determining the Character 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);
}
Expand Down
8 changes: 3 additions & 5 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 Down Expand Up @@ -1677,9 +1677,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 81b1912

Please sign in to comment.