@@ -17,7 +17,9 @@ package org.grails.web.mime
1717
1818import grails.web.http.HttpHeaders
1919import grails.web.mime.MimeType
20+ import groovy.transform.CompileStatic
2021import org.grails.web.util.GrailsApplicationAttributes
22+ import org.springframework.web.context.WebApplicationContext
2123import org.springframework.web.context.support.WebApplicationContextUtils
2224
2325import javax.servlet.http.HttpServletRequest
@@ -33,6 +35,7 @@ import org.grails.plugins.web.api.MimeTypesApiSupport
3335 * @since 3.0
3436 *
3537 */
38+ @CompileStatic
3639class HttpServletRequestExtension {
3740
3841 protected static MimeTypesApiSupport apiSupport = new MimeTypesApiSupport ()
@@ -52,7 +55,7 @@ class HttpServletRequestExtension {
5255 static String getFormat (HttpServletRequest request ) {
5356 def result = request. getAttribute(GrailsApplicationAttributes . CONTENT_FORMAT )
5457 if (! result) {
55- result = request . getMimeTypes()[0 ]. extension
58+ result = getMimeTypes(request )[0 ]. extension
5659 request. setAttribute(GrailsApplicationAttributes . CONTENT_FORMAT , result)
5760 }
5861 result
@@ -67,9 +70,10 @@ class HttpServletRequestExtension {
6770 static MimeType [] getMimeTypes (HttpServletRequest request ) {
6871 MimeType [] result = (MimeType [])request. getAttribute(GrailsApplicationAttributes . REQUEST_FORMATS )
6972 if (! result) {
70- def context = WebApplicationContextUtils . getRequiredWebApplicationContext(request. servletContext)
71- def parser = new DefaultAcceptHeaderParser ((MimeType [])context. getBean(MimeType . BEAN_NAME ))
72- def header = request. contentType
73+ WebApplicationContext context = WebApplicationContextUtils . getWebApplicationContext(request. servletContext)
74+ MimeType [] mimeTypes = context != null ? (MimeType [])context. getBean(MimeType . BEAN_NAME ) : MimeType . getConfiguredMimeTypes()
75+ def parser = new DefaultAcceptHeaderParser (mimeTypes)
76+ String header = request. contentType
7377 if (! header) header = request. getHeader(HttpHeaders . CONTENT_TYPE )
7478 result = parser. parse(header, header ? new MimeType (header) : MimeType . HTML )
7579
0 commit comments