codeconsole commented on code in PR #16760:
URL: https://github.com/apache/grails-core/pull/16760#discussion_r4371520063


##########
grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsCharacterEncodingInitializer.java:
##########
@@ -0,0 +1,83 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.plugins.web.controllers;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.springframework.boot.EnvironmentPostProcessor;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.env.DefaultPropertiesPropertySource;
+import org.springframework.boot.logging.DeferredLogFactory;
+import org.springframework.core.env.ConfigurableEnvironment;
+
+import grails.config.Settings;
+
+/**
+ * Translates the deprecated {@code grails.filter.encoding} and {@code 
grails.filter.forceEncoding} into
+ * {@code spring.servlet.encoding.charset} and {@code 
spring.servlet.encoding.force}, which configure the
+ * character encoding filter Spring Boot registers. Grails no longer registers 
an encoding filter of its own.
+ *
+ * <p>The translated values are contributed as Spring Boot default properties 
(lowest precedence), so an
+ * application that sets {@code spring.servlet.encoding} itself wins.
+ *
+ * @since 9.0
+ * @deprecated only needed while applications move to {@code 
spring.servlet.encoding}; it will be removed
+ * together with the two properties it translates
+ */
+@Deprecated(since = "9.0", forRemoval = true)
+public class GrailsCharacterEncodingEnvironmentPostProcessor implements 
EnvironmentPostProcessor {
+
+    // Spring Boot initializes logging after the environment post-processors 
run, and replays what was logged
+    // through the DeferredLogFactory it passes in once it has.
+    private final Log logger;
+
+    public GrailsCharacterEncodingEnvironmentPostProcessor() {
+        this.logger = 
LogFactory.getLog(GrailsCharacterEncodingEnvironmentPostProcessor.class);
+    }
+
+    public GrailsCharacterEncodingEnvironmentPostProcessor(DeferredLogFactory 
logFactory) {
+        this.logger = 
logFactory.getLog(GrailsCharacterEncodingEnvironmentPostProcessor.class);
+    }
+
+    @Override
+    @SuppressWarnings("removal")
+    public void postProcessEnvironment(ConfigurableEnvironment environment, 
SpringApplication application) {
+        var translated = new LinkedHashMap<String, Object>();
+        translate(environment, Settings.FILTER_ENCODING, 
"spring.servlet.encoding.charset", translated);

Review Comment:
   Moved the translation into an `ApplicationContextInitializer` 
(`GrailsCharacterEncodingInitializer`). It runs after every run listener, 
`ExternalConfigRunListener` included, and before Boot binds 
`spring.servlet.encoding`. `LegacyEncodingConfigSpec` in the 
external-configuration example starts the app with `grails.filter.encoding: 
ISO-8859-1` and `forceEncoding: false` in a `grails.config.locations` file and 
checks the encoding filter. Against the post-processor it failed with UTF-8 and 
a forced request. Fixed in 20b06ed0b8.
   



##########
grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsCharacterEncodingInitializer.java:
##########
@@ -0,0 +1,83 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.plugins.web.controllers;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.springframework.boot.EnvironmentPostProcessor;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.env.DefaultPropertiesPropertySource;
+import org.springframework.boot.logging.DeferredLogFactory;
+import org.springframework.core.env.ConfigurableEnvironment;
+
+import grails.config.Settings;
+
+/**
+ * Translates the deprecated {@code grails.filter.encoding} and {@code 
grails.filter.forceEncoding} into
+ * {@code spring.servlet.encoding.charset} and {@code 
spring.servlet.encoding.force}, which configure the
+ * character encoding filter Spring Boot registers. Grails no longer registers 
an encoding filter of its own.
+ *
+ * <p>The translated values are contributed as Spring Boot default properties 
(lowest precedence), so an
+ * application that sets {@code spring.servlet.encoding} itself wins.
+ *
+ * @since 9.0
+ * @deprecated only needed while applications move to {@code 
spring.servlet.encoding}; it will be removed
+ * together with the two properties it translates
+ */
+@Deprecated(since = "9.0", forRemoval = true)
+public class GrailsCharacterEncodingEnvironmentPostProcessor implements 
EnvironmentPostProcessor {
+
+    // Spring Boot initializes logging after the environment post-processors 
run, and replays what was logged
+    // through the DeferredLogFactory it passes in once it has.
+    private final Log logger;
+
+    public GrailsCharacterEncodingEnvironmentPostProcessor() {
+        this.logger = 
LogFactory.getLog(GrailsCharacterEncodingEnvironmentPostProcessor.class);
+    }
+
+    public GrailsCharacterEncodingEnvironmentPostProcessor(DeferredLogFactory 
logFactory) {
+        this.logger = 
logFactory.getLog(GrailsCharacterEncodingEnvironmentPostProcessor.class);
+    }
+
+    @Override
+    @SuppressWarnings("removal")
+    public void postProcessEnvironment(ConfigurableEnvironment environment, 
SpringApplication application) {
+        var translated = new LinkedHashMap<String, Object>();
+        translate(environment, Settings.FILTER_ENCODING, 
"spring.servlet.encoding.charset", translated);
+        translate(environment, Settings.FILTER_FORCE_ENCODING, 
"spring.servlet.encoding.force", translated);
+        if (!translated.isEmpty()) {

Review Comment:
   A key is now translated only when its Spring Boot property isn't set 
anywhere in the environment, so a default property the app sets stays, and the 
warning says the deprecated key is ignored. Covered in 
`GrailsCharacterEncodingInitializerSpec`. Fixed in 6f82a144af.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to