From 76f4334740c69b368a1bcc9c5ff1692de7de450f Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sun, 17 Nov 2024 23:55:29 -0800 Subject: [PATCH 1/2] Remove deprecated mimetypes which are overriding MimeTypesConfiguration --- .../mime/AbstractMimeTypesGrailsPlugin.groovy | 42 --------- .../web/mime/MimeTypesFactoryBean.groovy | 93 ------------------- .../web/mime/MimeTypesGrailsPlugin.groovy | 9 +- 3 files changed, 1 insertion(+), 143 deletions(-) delete mode 100644 grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/AbstractMimeTypesGrailsPlugin.groovy delete mode 100644 grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesFactoryBean.groovy diff --git a/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/AbstractMimeTypesGrailsPlugin.groovy b/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/AbstractMimeTypesGrailsPlugin.groovy deleted file mode 100644 index 016cd6d2e2a..00000000000 --- a/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/AbstractMimeTypesGrailsPlugin.groovy +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2004-2019 the original author or authors. - * - * Licensed 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 - * - * http://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.mime - -import grails.plugins.Plugin -import org.grails.web.mime.DefaultMimeTypeResolver -import grails.web.mime.MimeTypeResolver -import org.grails.web.mime.DefaultMimeUtility -import grails.web.mime.MimeType - -/** - * Provides content negotiation capabilities to Grails via a new withFormat method on controllers - * as well as a format property on the HttpServletRequest instance. - * - * @author Graeme Rocher - * @since 1.0 - * @deprecated Use {@link MimeTypesConfiguration} instead - */ -@Deprecated -abstract class AbstractMimeTypesGrailsPlugin extends Plugin { - - Closure doWithSpring() {{-> - "${MimeType.BEAN_NAME}"(MimeTypesFactoryBean) - final mimeTypesBeanRef = ref(MimeType.BEAN_NAME) - - grailsMimeUtility(DefaultMimeUtility, mimeTypesBeanRef) - "${MimeTypeResolver.BEAN_NAME}"(DefaultMimeTypeResolver) - }} -} diff --git a/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesFactoryBean.groovy b/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesFactoryBean.groovy deleted file mode 100644 index 2f256b626ce..00000000000 --- a/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesFactoryBean.groovy +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2004-2005 the original author or authors. - * - * Licensed 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 - * - * http://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.mime - -import grails.config.Config -import grails.config.Settings -import grails.core.GrailsApplication -import grails.web.mime.MimeType -import grails.web.mime.MimeTypeProvider -import groovy.transform.CompileStatic -import groovy.transform.TypeCheckingMode -import org.springframework.beans.factory.FactoryBean -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.context.ApplicationContext -import org.springframework.context.ApplicationContextAware - -/** - * Creates the MimeType[] object that defines the configured mime types. - * - * @author Graeme Rocher - * @since 2.0 - * @deprecated Use {@link MimeTypesConfiguration} instead - */ -@CompileStatic -@Deprecated -class MimeTypesFactoryBean implements FactoryBean, ApplicationContextAware{ - - ApplicationContext applicationContext - GrailsApplication grailsApplication - - private MimeType[] mimeTypes - - @Autowired(required = false) - Collection mimeTypeProviders = [] - - @Override - MimeType[] getObject() { - final grailsApplication = this.grailsApplication ?: applicationContext.getBean(GrailsApplication) - def config = grailsApplication?.config - def mimeConfig = getMimeConfig(config) - if (!mimeConfig) { - mimeTypes = MimeType.createDefaults() - return mimeTypes - } - - def mimes = [] - for (entry in mimeConfig.entrySet()) { - if (entry.value instanceof List) { - for (i in entry.value) { - mimes << new MimeType(i.toString(),entry.key.toString()) - } - } - else { - mimes << new MimeType(entry.value.toString(), entry.key.toString()) - } - } - for(MimeTypeProvider mtp in mimeTypeProviders) { - for(MimeType mt in mtp.mimeTypes) { - if (!mimes.contains(mt)) { - mimes << mt - } - } - } - mimeTypes = mimes as MimeType[] - mimeTypes - - } - - @Override - Class getObjectType() { MimeType[].class } - - @Override - boolean isSingleton() { true } - - - @CompileStatic(TypeCheckingMode.SKIP) - protected Map getMimeConfig(Config config) { - return config.getProperty(Settings.MIME_TYPES, Map.class) - } -} diff --git a/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesGrailsPlugin.groovy b/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesGrailsPlugin.groovy index dd80eff0f2b..4e0b58d789b 100644 --- a/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesGrailsPlugin.groovy +++ b/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesGrailsPlugin.groovy @@ -23,17 +23,10 @@ import grails.util.GrailsUtil * * @author Graeme Rocher * @since 1.0 - * @deprecated Use {@link MimeTypesConfiguration} instead */ -@Deprecated -class MimeTypesGrailsPlugin extends AbstractMimeTypesGrailsPlugin { +class MimeTypesGrailsPlugin { def version = GrailsUtil.getGrailsVersion() def dependsOn = [core:version, controllers:version] def observe = ['controllers'] - - @Override - Closure doWithSpring() { - return super.doWithSpring() - } } From 9dbb74f72be073b17449dec5d1075d8286b6f460 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Mon, 18 Nov 2024 01:09:43 -0800 Subject: [PATCH 2/2] Fix tests --- .../web/mime/MimeTypesConfiguration.groovy | 5 +--- .../grails/web/mime/MimeUtilitySpec.groovy | 15 ++++++++---- .../RequestAndResponseMimeTypesApiSpec.groovy | 1 - .../render/BaseDomainClassRendererSpec.groovy | 23 ++++++++++++++----- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy b/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy index aa2a224402f..ff0aca43536 100644 --- a/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy +++ b/grails-plugin-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy @@ -27,7 +27,6 @@ import groovy.transform.CompileStatic import groovy.transform.TypeCheckingMode import org.grails.web.mime.DefaultMimeTypeResolver import org.grails.web.mime.DefaultMimeUtility -import org.springframework.context.ApplicationContext import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Primary @@ -43,12 +42,10 @@ import org.springframework.context.annotation.Primary class MimeTypesConfiguration { private final GrailsApplication grailsApplication - private final ApplicationContext applicationContext private final List mimeTypeProviders MimeTypesConfiguration(GrailsApplication grailsApplication, List mimeTypeProviders) { this.grailsApplication = grailsApplication - this.applicationContext = grailsApplication.getMainContext() this.mimeTypeProviders = mimeTypeProviders } @@ -86,7 +83,7 @@ class MimeTypesConfiguration { final List mimeTypeProviders = this.mimeTypeProviders processProviders(mimes, mimeTypeProviders) - final Map childTypes = applicationContext.getBeansOfType(MimeTypeProvider.class) + final Map childTypes = grailsApplication.mainContext.getBeansOfType(MimeTypeProvider.class) processProviders(mimes, childTypes.values()) mimeTypes = mimes.toArray(new MimeType[0]) } diff --git a/grails-plugin-mimetypes/src/test/groovy/grails/web/mime/MimeUtilitySpec.groovy b/grails-plugin-mimetypes/src/test/groovy/grails/web/mime/MimeUtilitySpec.groovy index db043070deb..71497b6db79 100644 --- a/grails-plugin-mimetypes/src/test/groovy/grails/web/mime/MimeUtilitySpec.groovy +++ b/grails-plugin-mimetypes/src/test/groovy/grails/web/mime/MimeUtilitySpec.groovy @@ -1,8 +1,10 @@ package grails.web.mime import grails.core.DefaultGrailsApplication -import org.grails.plugins.web.mime.MimeTypesFactoryBean +import grails.spring.BeanBuilder +import org.grails.plugins.web.mime.MimeTypesConfiguration import org.grails.web.mime.DefaultMimeUtility +import org.springframework.context.ApplicationContext import spock.lang.Specification /** @@ -26,10 +28,15 @@ class MimeUtilitySpec extends Specification { multipartForm: 'multipart/form-data' ] - final factory = new MimeTypesFactoryBean(grailsApplication: ga) + def bb = new BeanBuilder() + bb.beans { + grailsApplication = ga + mimeConfiguration(MimeTypesConfiguration, ga, []) + } + ga.setApplicationContext(bb.createApplicationContext()) - def mimeTypes = factory.getObject() - return new DefaultMimeUtility(mimeTypes) + MimeTypesConfiguration mimeTypesConfiguration = ga.mainContext.getBean(MimeTypesConfiguration) + return new DefaultMimeUtility(mimeTypesConfiguration.mimeTypes()) } void "Test get mime by extension method"() { diff --git a/grails-plugin-mimetypes/src/test/groovy/org/grails/web/servlet/mvc/RequestAndResponseMimeTypesApiSpec.groovy b/grails-plugin-mimetypes/src/test/groovy/org/grails/web/servlet/mvc/RequestAndResponseMimeTypesApiSpec.groovy index e42fb4ca2bb..360cb08f74f 100644 --- a/grails-plugin-mimetypes/src/test/groovy/org/grails/web/servlet/mvc/RequestAndResponseMimeTypesApiSpec.groovy +++ b/grails-plugin-mimetypes/src/test/groovy/org/grails/web/servlet/mvc/RequestAndResponseMimeTypesApiSpec.groovy @@ -9,7 +9,6 @@ import grails.web.mime.MimeType import org.grails.config.PropertySourcesConfig import org.grails.core.lifecycle.ShutdownOperations import org.grails.plugins.web.mime.MimeTypesConfiguration -import org.grails.plugins.web.mime.MimeTypesFactoryBean import org.grails.web.mime.DefaultMimeUtility import org.springframework.context.ApplicationContext import org.springframework.context.support.GenericApplicationContext diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/plugins/web/rest/render/BaseDomainClassRendererSpec.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/plugins/web/rest/render/BaseDomainClassRendererSpec.groovy index a2214ffc4c5..77db50fca89 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/plugins/web/rest/render/BaseDomainClassRendererSpec.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/plugins/web/rest/render/BaseDomainClassRendererSpec.groovy @@ -7,6 +7,7 @@ import grails.persistence.Entity import grails.rest.Linkable import grails.rest.Resource import grails.rest.render.Renderer +import grails.spring.BeanBuilder import grails.util.GrailsWebMockUtil import grails.web.CamelCaseUrlConverter import grails.web.mapping.LinkGenerator @@ -15,13 +16,15 @@ import grails.web.mime.MimeType import org.grails.config.PropertySourcesConfig import org.grails.datastore.mapping.keyvalue.mapping.config.KeyValueMappingContext import org.grails.datastore.mapping.model.MappingContext -import org.grails.plugins.web.mime.MimeTypesFactoryBean +import org.grails.plugins.web.mime.MimeTypesConfiguration import org.grails.support.MockApplicationContext import org.grails.web.mapping.DefaultLinkGenerator import org.grails.web.mapping.DefaultUrlMappingEvaluator import org.grails.web.mapping.DefaultUrlMappingsHolder import org.grails.web.mime.DefaultMimeUtility import org.grails.web.servlet.mvc.GrailsWebRequest +import org.springframework.context.ApplicationContext +import org.springframework.context.support.GenericApplicationContext import org.springframework.core.env.MapPropertySource import org.springframework.core.env.MutablePropertySources import org.springframework.mock.web.MockHttpServletRequest @@ -89,11 +92,19 @@ abstract class BaseDomainClassRendererSpec extends Specification { return new PropertySourcesConfig(propertySources) } - protected static MimeType[] buildMimeTypes(application) { - def mimeTypesFactory = new MimeTypesFactoryBean() - mimeTypesFactory.grailsApplication = application - def mimeTypes = mimeTypesFactory.getObject() - return mimeTypes + private MimeType[] buildMimeTypes(application) { + final def mainContext = new GenericApplicationContext() + mainContext.refresh() + application.setApplicationContext(mainContext) + + def bb = new BeanBuilder() + bb.beans { + grailsApplication = application + mimeConfiguration(MimeTypesConfiguration, application, []) + } + final ApplicationContext context = bb.createApplicationContext() + final MimeTypesConfiguration mimeTypesConfiguration = context.getBean(MimeTypesConfiguration) + mimeTypesConfiguration.mimeTypes() } protected static String toCompactXml(String xml) {