Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/4.0' into 4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
BalusC committed Jan 28, 2024
2 parents a473288 + facc1f9 commit 88b4ba4
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 225 deletions.
4 changes: 3 additions & 1 deletion impl/src/main/java/com/sun/faces/RIConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.sun.faces;

import java.nio.charset.StandardCharsets;

import com.sun.faces.config.manager.FacesSchema;

import jakarta.faces.render.RenderKitFactory;
Expand Down Expand Up @@ -58,7 +60,7 @@ public class RIConstants {
public static final String APPLICATION_XML_CONTENT_TYPE = "application/xml";
public static final String TEXT_XML_CONTENT_TYPE = "text/xml";
public static final String ALL_MEDIA = "*/*";
public static final String CHAR_ENCODING = "UTF-8";
public static final String CHAR_ENCODING = StandardCharsets.UTF_8.name();
public static final String FACELETS_ENCODING_KEY = "facelets.Encoding";
public static final String DEFAULT_LIFECYCLE = FACES_PREFIX + "DefaultLifecycle";
public static final String DEFAULT_STATEMANAGER = FACES_PREFIX + "DefaultStateManager";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@

package com.sun.faces.application;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.FINEST;
import static java.util.logging.Level.WARNING;

import com.sun.faces.util.FacesLogger;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
Expand All @@ -36,6 +35,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sun.faces.util.FacesLogger;

/**
* <p>
* Factory for dynamically generating PropertyEditor classes that extend {@link ConverterPropertyEditorBase} and replace
Expand Down Expand Up @@ -78,7 +79,6 @@ private static class Utf8InfoRef {
int length;

public Utf8InfoRef(int index, int length) {
super();
this.index = index;
this.length = length;
}
Expand All @@ -100,7 +100,6 @@ private static class Utf8InfoReplacement implements Comparable<Utf8InfoReplaceme
byte[] replacement;

public Utf8InfoReplacement(Utf8InfoRef ref, String replacement) {
super();
this.ref = ref;
this.replacement = getUtf8InfoBytes(replacement);
}
Expand Down Expand Up @@ -536,8 +535,7 @@ private static String getVMClassName(Class<?> c) {
* @return the bytes for the UTF8Info constant pool entry, including the tag, length, and utf8 content.
*/
private static byte[] getUtf8InfoBytes(String text) {
byte[] utf8;
utf8 = text.getBytes(StandardCharsets.UTF_8);
byte[] utf8 = text.getBytes(UTF_8);
byte[] info = new byte[utf8.length + 3];
info[0] = 1;
info[1] = (byte) (utf8.length >> 8 & 0xff);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import static jakarta.faces.application.Resource.COMPONENT_RESOURCE_KEY;
import static jakarta.faces.application.StateManager.IS_BUILDING_INITIAL_STATE;
import static jakarta.faces.application.StateManager.STATE_SAVING_METHOD_SERVER;
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 Down Expand Up @@ -72,6 +71,7 @@
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 @@ -908,22 +908,25 @@ 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 the ReponseWriter
// so we can figure out what content type and encoding the ReponseWriter
// is really going to ask for
ResponseWriter writer = renderKit.createResponseWriter(NullWriter.INSTANCE, contentType, encoding);
ResponseWriter initWriter = renderKit.createResponseWriter(NullWriter.INSTANCE, contentType, encoding);

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

// apply them to the response
char[] buffer = new char[1028];
HtmlUtils.writeTextForXML(writer, contentType, buffer);
HtmlUtils.writeTextForXML(initWriter, contentType, buffer);
String str = String.valueOf(buffer).trim();
extContext.setResponseContentType(str);
extContext.setResponseCharacterEncoding(encoding);

// Save encoding in UIViewRoot for faster consult when Util#getResponseEncoding() is invoked again elsewhere.
context.getViewRoot().getAttributes().put(FACELETS_ENCODING_KEY, encoding);

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

return writer;
}
Expand Down Expand Up @@ -974,58 +977,6 @@ protected void handleFaceletNotFound(FacesContext context, String viewId, String
context.responseComplete();
}

/**
* @param context the {@link FacesContext} for the current request
* @param orig the original encoding
* @return the encoding to be used for this response
*/
protected String getResponseEncoding(FacesContext context, String orig) {
String encoding = orig;

// 1. get it from request
encoding = context.getExternalContext().getRequestCharacterEncoding();

// 2. get it from the session
if (encoding == null) {
if (context.getExternalContext().getSession(false) != null) {
Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
encoding = (String) sessionMap.get(CHARACTER_ENCODING_KEY);
if (LOGGER.isLoggable(FINEST)) {
LOGGER.log(FINEST, "Session specified alternate encoding {0}", encoding);
}
}
}

// see if we need to override the encoding
Map<Object, Object> ctxAttributes = context.getAttributes();

// 3. check the request attribute
if (ctxAttributes.containsKey(FACELETS_ENCODING_KEY)) {
encoding = (String) ctxAttributes.get(FACELETS_ENCODING_KEY);
if (LOGGER.isLoggable(FINEST)) {
LOGGER.log(FINEST, "Facelet specified alternate encoding {0}", encoding);
}
if (null != context.getExternalContext().getSession(false)) {
Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
sessionMap.put(CHARACTER_ENCODING_KEY, encoding);
}
}

// 4. default it
if (encoding == null) {
if (null != orig && 0 < orig.length()) {
encoding = orig;
} else {
encoding = "UTF-8";
}
if (LOGGER.isLoggable(FINEST)) {
LOGGER.log(FINEST, "ResponseWriter created had a null CharacterEncoding, defaulting to {0}", orig);
}
}

return encoding;
}

/**
* @param context the {@link FacesContext} for the current request
* @param orig the original contentType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

package com.sun.faces.application.view;

import static com.sun.faces.RIConstants.FACELETS_ENCODING_KEY;
import static com.sun.faces.RIConstants.SAVESTATE_FIELD_MARKER;
import static com.sun.faces.renderkit.RenderKitUtils.PredefinedPostbackParameter.RENDER_KIT_ID_PARAM;
import static com.sun.faces.renderkit.RenderKitUtils.getResponseStateManager;
import static com.sun.faces.renderkit.RenderKitUtils.PredefinedPostbackParameter.RENDER_KIT_ID_PARAM;
import static com.sun.faces.util.MessageUtils.ILLEGAL_VIEW_ID_ID;
import static com.sun.faces.util.MessageUtils.getExceptionMessageString;
import static com.sun.faces.util.Util.getFacesMapping;
Expand All @@ -35,10 +34,8 @@
import static jakarta.servlet.http.MappingMatch.EXACT;
import static jakarta.servlet.http.MappingMatch.EXTENSION;
import static jakarta.servlet.http.MappingMatch.PATH;
import static java.text.MessageFormat.format;
import static java.util.Collections.unmodifiableSet;
import static java.util.Objects.requireNonNull;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.SEVERE;
import static java.util.logging.Level.WARNING;

Expand Down Expand Up @@ -350,24 +347,7 @@ public boolean removeProtectedView(String urlPattern) {
*/
@Override
public String getRedirectURL(FacesContext context, String viewId, Map<String, List<String>> parameters, boolean includeViewParams) {
String encodingFromContext = (String) context.getAttributes().get(FACELETS_ENCODING_KEY);
if (encodingFromContext == null) {
encodingFromContext = (String) context.getViewRoot().getAttributes().get(FACELETS_ENCODING_KEY);
}

String responseEncoding;

if (encodingFromContext == null) {
try {
responseEncoding = context.getExternalContext().getResponseCharacterEncoding();
} catch (Exception e) {
LOGGER.log(FINE, e, () ->
format("Unable to obtain response character encoding from ExternalContext {0}. Using UTF-8.", context.getExternalContext()));
responseEncoding = "UTF-8";
}
} else {
responseEncoding = encodingFromContext;
}
String responseEncoding = Util.getResponseEncoding(context);

if (parameters != null) {
Map<String, List<String>> decodedParameters = new HashMap<>();
Expand Down
32 changes: 5 additions & 27 deletions impl/src/main/java/com/sun/faces/context/ExternalContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,8 @@ public void redirect(String requestURI) throws IOException {
pwriter = ctx.getPartialViewContext().getPartialResponseWriter();
}
setResponseContentType(RIConstants.TEXT_XML_CONTENT_TYPE);
setResponseCharacterEncoding("UTF-8");
setResponseCharacterEncoding(RIConstants.CHAR_ENCODING);
addResponseHeader("Cache-Control", "no-cache");
// pwriter.writePreamble("<?xml version='1.0' encoding='UTF-8'?>\n");
pwriter.startDocument();
pwriter.redirect(requestURI);
pwriter.endDocument();
Expand Down Expand Up @@ -964,14 +963,7 @@ public boolean isSecure() {

@Override
public String encodeBookmarkableURL(String baseUrl, Map<String, List<String>> parameters) {
FacesContext context = FacesContext.getCurrentInstance();
String encodingFromContext = (String) context.getAttributes().get(RIConstants.FACELETS_ENCODING_KEY);
if (null == encodingFromContext) {
encodingFromContext = (String) context.getViewRoot().getAttributes().get(RIConstants.FACELETS_ENCODING_KEY);
}

String currentResponseEncoding = null != encodingFromContext ? encodingFromContext : getResponseCharacterEncoding();

String currentResponseEncoding = Util.getResponseEncoding(FacesContext.getCurrentInstance());
UrlBuilder builder = new UrlBuilder(baseUrl, currentResponseEncoding);
builder.addParameters(parameters);
return builder.createUrl();
Expand All @@ -980,14 +972,7 @@ public String encodeBookmarkableURL(String baseUrl, Map<String, List<String>> pa

@Override
public String encodeRedirectURL(String baseUrl, Map<String, List<String>> parameters) {
FacesContext context = FacesContext.getCurrentInstance();
String encodingFromContext = (String) context.getAttributes().get(RIConstants.FACELETS_ENCODING_KEY);
if (null == encodingFromContext) {
encodingFromContext = (String) context.getViewRoot().getAttributes().get(RIConstants.FACELETS_ENCODING_KEY);
}

String currentResponseEncoding = null != encodingFromContext ? encodingFromContext : getResponseCharacterEncoding();

String currentResponseEncoding = Util.getResponseEncoding(FacesContext.getCurrentInstance());
UrlBuilder builder = new UrlBuilder(baseUrl, currentResponseEncoding);
builder.addParameters(parameters);
return builder.createUrl();
Expand All @@ -1003,14 +988,7 @@ public String encodePartialActionURL(String url) {
String message = MessageUtils.getExceptionMessageString(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "url");
throw new NullPointerException(message);
}
FacesContext context = FacesContext.getCurrentInstance();
String encodingFromContext = (String) context.getAttributes().get(RIConstants.FACELETS_ENCODING_KEY);
if (null == encodingFromContext) {
encodingFromContext = (String) context.getViewRoot().getAttributes().get(RIConstants.FACELETS_ENCODING_KEY);
}

String currentResponseEncoding = null != encodingFromContext ? encodingFromContext : getResponseCharacterEncoding();

String currentResponseEncoding = Util.getResponseEncoding(FacesContext.getCurrentInstance());
UrlBuilder builder = new UrlBuilder(url, currentResponseEncoding);
return ((HttpServletResponse) response).encodeURL(builder.createUrl());
}
Expand Down Expand Up @@ -1068,7 +1046,7 @@ private void pushIfPossibleAndNecessary(String result) {

private PushBuilder getPushBuilder(FacesContext context) {
ExternalContext extContext = context.getExternalContext();

if (extContext.getRequest() instanceof HttpServletRequest) {
HttpServletRequest hreq = (HttpServletRequest) extContext.getRequest();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,6 @@ public void processPartial(PhaseId phaseId) {
exContext.setResponseContentType(RIConstants.TEXT_XML_CONTENT_TYPE);
exContext.addResponseHeader("Cache-Control", "no-cache");

// String encoding = writer.getCharacterEncoding( );
// if( encoding == null ) {
// encoding = "UTF-8";
// }
// writer.writePreamble("<?xml version='1.0' encoding='" + encoding + "'?>\n");
writer.startDocument();

if (isResetValues()) {
Expand Down Expand Up @@ -322,7 +317,7 @@ public void processPartial(PhaseId phaseId) {
}
}
}

private void doFlashPostPhaseActions(FacesContext ctx) {
try {
ctx.getExternalContext().getFlash().doPostPhaseActions(ctx);
Expand Down Expand Up @@ -630,7 +625,7 @@ public DelayedInitPartialResponseWriter(PartialViewContextImpl ctx) {
super(null);
this.ctx = ctx;
ExternalContext extCtx = ctx.ctx.getExternalContext();

if (extCtx.isResponseCommitted()) {
LOGGER.log(WARNING, "Response is already committed - cannot reconfigure it anymore");
}
Expand Down
5 changes: 1 addition & 4 deletions impl/src/main/java/com/sun/faces/context/UrlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.sun.faces.context;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
Expand Down Expand Up @@ -51,7 +49,6 @@ class UrlBuilder {
public static final String PARAMETER_NAME_VALUE_SEPARATOR = "=";
public static final String FRAGMENT_SEPARATOR = "#";
public static final char FRAGMENT_SEPARATOR_CHAR = FRAGMENT_SEPARATOR.charAt(0);
public static final String DEFAULT_ENCODING = UTF_8.name();
public static final String WEBSOCKET_PROTOCOL = "ws";
public static final String PROTOCOL_SEPARATOR = "://";

Expand All @@ -75,7 +72,7 @@ public UrlBuilder(String url, String encoding) {
}

public UrlBuilder(String url) {
this(url, DEFAULT_ENCODING);
this(url, CHAR_ENCODING);
}

// ---------------------------------------------------------- Public Methods
Expand Down
Loading

0 comments on commit 88b4ba4

Please sign in to comment.