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


##########
grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsCharacterEncodingInitializer.java:
##########
@@ -0,0 +1,82 @@
+/*
+ *  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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.springframework.boot.env.DefaultPropertiesPropertySource;
+import org.springframework.context.ApplicationContextInitializer;
+import org.springframework.context.ConfigurableApplicationContext;
+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>It is a context initializer, not an environment post-processor, because 
files named by
+ * {@code grails.config.locations} are loaded by {@code 
ExternalConfigRunListener} after the post-processors run.
+ * A translated value is contributed as a Spring Boot default property (lowest 
precedence), and only when the
+ * application does not set the Spring Boot property itself.
+ *
+ * @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 GrailsCharacterEncodingInitializer implements 
ApplicationContextInitializer<ConfigurableApplicationContext> {
+
+    private static final Logger logger = 
LoggerFactory.getLogger(GrailsCharacterEncodingInitializer.class);
+
+    @Override
+    @SuppressWarnings("removal")
+    public void initialize(ConfigurableApplicationContext applicationContext) {
+        var environment = applicationContext.getEnvironment();
+        var translated = new LinkedHashMap<String, Object>();
+        translate(environment, Settings.FILTER_ENCODING, 
"spring.servlet.encoding.charset", translated);

Review Comment:
   Fixed in 2e7b0a07ce. The initializer now adds a bean factory post-processor 
to the context and translates there. Spring runs it after the configuration 
classes are processed, which is when `@PropertySource` adds its source, and 
before any bean is created, so before Boot binds `spring.servlet.encoding`. 
Files named by `grails.config.locations` are loaded before that too. 
`GrailsCharacterEncodingInitializerSpec` has a new case: a configuration class 
whose `@PropertySource` sets `grails.filter.encoding=ISO-8859-1` and 
`grails.filter.forceEncoding=false`, with `HttpEncodingAutoConfiguration`. The 
encoding filter gets ISO-8859-1 and does not force the request encoding; with 
the translation in the initializer itself, it got UTF-8. 
`LegacyEncodingConfigSpec` and the rest of the external-configuration example 
still pass, as do the `grails-controllers` tests.
   



-- 
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