jamesfredley commented on code in PR #16760: URL: https://github.com/apache/grails-core/pull/16760#discussion_r4369964502
########## grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsCharacterEncodingEnvironmentPostProcessor.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: This runs during Boot's environment post-processing. Grails loads `grails.config.locations` later, in `ExternalConfigRunListener`. An external file with `grails.filter.encoding=ISO-8859-1` and `forceEncoding=false` is never translated, so the app gets UTF-8 and a forced request instead. Latin-1 form input is then corrupted. Translate after external configuration is loaded and before encoding is bound. Please cover it through `grails.config.locations` on a full startup. ########## grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsCharacterEncodingEnvironmentPostProcessor.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: `DefaultPropertiesPropertySource.addOrMerge` replaces existing keys. An app that sets `spring.servlet.encoding.charset` through `setDefaultProperties` and still has `grails.filter.encoding` gets the legacy value instead. Only add the translated value when that Spring key is not already set. -- 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]
