Permalink
Browse files

Remove MimeTypesTransformer and all references to ControllersMimeType…

…sApi
  • Loading branch information...
1 parent 765cb8f commit 0479e25d50911cbdb9b16270a8359fc48bc03403 @jeffbrown jeffbrown committed Jul 17, 2014
@@ -19,11 +19,13 @@ import grails.databinding.DataBindingSource
import grails.web.databinding.DataBindingUtils
import javax.servlet.http.HttpServletRequest
+import javax.servlet.http.HttpServletResponse
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.codehaus.groovy.runtime.InvokerHelper
import org.grails.core.artefact.DomainClassArtefactHandler
import org.grails.plugins.support.WebMetaUtils
+import org.grails.plugins.web.api.MimeTypesApiSupport
import org.grails.web.servlet.mvc.GrailsWebRequest
import org.springframework.beans.factory.config.AutowireCapableBeanFactory
import org.springframework.context.ApplicationContext
@@ -169,4 +171,26 @@ trait Controller {
}
commandObjectInstance
}
+
+ /**
+ * <p>The withFormat method is used to allow controllers to handle different types of
+ * request formats such as HTML, XML and so on. Example usage:</p>
+ *
+ * <pre>
+ * <code>
+ * withFormat {
+ * html { render "html" }
+ * xml { render "xml}
+ * }
+ * </code>
+ * </pre>
+ *
+ * @param callable
+ * @return The result of the closure execution selected
+ */
+ def withFormat(Closure callable) {
+ MimeTypesApiSupport mimeTypesSupport = new MimeTypesApiSupport()
+ HttpServletResponse response = GrailsWebRequest.lookup().currentResponse
+ mimeTypesSupport.withFormat((HttpServletResponse)response, callable)
+ }
}
@@ -1,119 +0,0 @@
-/*
- * Copyright 2011 SpringSource
- *
- * 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.compiler.web.mime;
-
-import grails.artefact.Artefact;
-import grails.web.controllers.ControllerMethod;
-import groovy.lang.Closure;
-
-import java.lang.reflect.Modifier;
-import java.net.URL;
-import java.util.regex.Pattern;
-
-import org.codehaus.groovy.ast.AnnotationNode;
-import org.codehaus.groovy.ast.ClassNode;
-import org.codehaus.groovy.ast.FieldNode;
-import org.codehaus.groovy.ast.InnerClassNode;
-import org.codehaus.groovy.ast.MethodNode;
-import org.codehaus.groovy.ast.Parameter;
-import org.codehaus.groovy.ast.expr.ArgumentListExpression;
-import org.codehaus.groovy.ast.expr.AttributeExpression;
-import org.codehaus.groovy.ast.expr.ConstantExpression;
-import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
-import org.codehaus.groovy.ast.expr.MethodCallExpression;
-import org.codehaus.groovy.ast.expr.VariableExpression;
-import org.codehaus.groovy.ast.stmt.BlockStatement;
-import org.codehaus.groovy.ast.stmt.ReturnStatement;
-import org.codehaus.groovy.classgen.GeneratorContext;
-import org.codehaus.groovy.control.SourceUnit;
-import org.grails.core.artefact.ControllerArtefactHandler;
-import grails.compiler.ast.AnnotatedClassInjector;
-import grails.compiler.ast.AstTransformer;
-import org.grails.compiler.injection.GrailsASTUtils;
-import grails.compiler.ast.GrailsArtefactClassInjector;
-import org.grails.io.support.GrailsResourceUtils;
-import org.grails.plugins.web.api.ControllersMimeTypesApi;
-
-/**
- * Adds the withFormat and other mime related methods to controllers at compile time.
- *
- * @author Graeme Rocher
- * @since 2.0
- */
-@AstTransformer
-public class MimeTypesTransformer implements GrailsArtefactClassInjector, AnnotatedClassInjector {
-
- public static Pattern CONTROLLER_PATTERN = Pattern.compile(".+/" +
- GrailsResourceUtils.GRAILS_APP_DIR + "/controllers/(.+)Controller\\.groovy");
-
- public static final String FIELD_MIME_TYPES_API = "mimeTypesApi";
- public static final Parameter[] CLOSURE_PARAMETER = new Parameter[]{ new Parameter(new ClassNode(Closure.class), "callable")};
- public static final String WITH_FORMAT_METHOD = "withFormat";
-
- public void performInjection(SourceUnit source, GeneratorContext context, ClassNode classNode) {
- // don't inject if already an @Artefact annotation is applied
- if(!classNode.getAnnotations(new ClassNode(Artefact.class)).isEmpty()) return;
-
- performInjectionOnAnnotatedClass(source, context,classNode);
- }
-
- public void performInjection(SourceUnit source, ClassNode classNode) {
- performInjection(source,null, classNode);
- }
-
- @Override
- public void performInjectionOnAnnotatedClass(SourceUnit source, ClassNode classNode) {
- performInjectionOnAnnotatedClass(source, null,classNode);
- }
-
- public boolean shouldInject(URL url) {
- return url != null && CONTROLLER_PATTERN.matcher(url.getFile()).find();
- }
-
- public String[] getArtefactTypes() {
- return new String[]{ControllerArtefactHandler.TYPE};
- }
-
- protected AnnotationNode getMarkerAnnotation() {
- return new AnnotationNode(new ClassNode(ControllerMethod.class).getPlainNodeReference());
- }
-
- public void performInjectionOnAnnotatedClass(SourceUnit source, GeneratorContext context, ClassNode classNode) {
- if(classNode instanceof InnerClassNode) return;
- if(classNode.isEnum()) return;
- FieldNode field = classNode.getField(FIELD_MIME_TYPES_API);
- if (field == null) {
- final ClassNode mimeTypesApiClass = new ClassNode(ControllersMimeTypesApi.class);
- field = new FieldNode(FIELD_MIME_TYPES_API, PRIVATE_STATIC_MODIFIER, mimeTypesApiClass, classNode, new ConstructorCallExpression(mimeTypesApiClass, GrailsArtefactClassInjector.ZERO_ARGS));
-
- classNode.addField(field);
-
- if(!classNode.hasMethod(WITH_FORMAT_METHOD, CLOSURE_PARAMETER)) {
- final BlockStatement methodBody = new BlockStatement();
- final ArgumentListExpression args = new ArgumentListExpression();
- args.addExpression(new VariableExpression("this", classNode))
- .addExpression(new VariableExpression("callable", new ClassNode(Closure.class)));
- MethodCallExpression methodCall = new MethodCallExpression(new AttributeExpression(new VariableExpression("this"), new ConstantExpression(FIELD_MIME_TYPES_API)), WITH_FORMAT_METHOD, args);
- methodCall.setMethodTarget(mimeTypesApiClass.getMethods(WITH_FORMAT_METHOD).get(0));
- methodBody.addStatement(new ReturnStatement(methodCall));
- MethodNode methodNode = new MethodNode(WITH_FORMAT_METHOD, Modifier.PUBLIC, new ClassNode(Object.class), CLOSURE_PARAMETER, null, methodBody);
- methodNode.addAnnotation(getMarkerAnnotation());
- classNode.addMethod(methodNode);
- GrailsASTUtils.addCompileStaticAnnotation(methodNode);
- }
- }
- }
-}
@@ -1,127 +0,0 @@
-package org.codehaus.groovy.grails.compiler.web
-
-import grails.util.GrailsWebUtil
-import org.grails.compiler.web.mime.MimeTypesTransformer
-
-import javax.servlet.http.HttpServletResponse
-
-import grails.core.DefaultGrailsApplication
-import org.grails.core.metaclass.MetaClassEnhancer
-import grails.compiler.ast.ClassInjector
-import org.grails.compiler.injection.GrailsAwareClassLoader
-import org.grails.plugins.web.api.ResponseMimeTypesApi
-import org.grails.plugins.web.mime.MimeTypesFactoryBean
-import org.springframework.web.context.request.RequestContextHolder
-
-import spock.lang.Specification
-
-class MimeTypesTransformerSpec extends Specification {
-
- void setup() {
- MetaClassEnhancer responseEnhancer = new MetaClassEnhancer()
- responseEnhancer.addApi responseMimeTypesAPi
- responseEnhancer.enhance HttpServletResponse.metaClass
- GrailsWebUtil.bindMockWebRequest()
- }
-
- void cleanup() {
- GroovySystem.metaClassRegistry.removeMetaClass(HttpServletResponse)
- RequestContextHolder.setRequestAttributes(null)
- }
-
- void "Test withFormat method injected at compile time"() {
- given:
- def gcl = new GrailsAwareClassLoader()
- def transformer = new MimeTypesTransformer() {
- @Override
- boolean shouldInject(URL url) { true }
- }
- gcl.classInjectors = [transformer] as ClassInjector[]
-
- when:
- def cls = getControllerClass(gcl)
- def controller = cls.newInstance()
- def format = controller.index()
-
- then:
- format == "html"
- }
-
- private Class getControllerClass(GrailsAwareClassLoader gcl) {
- return gcl.parseClass('''
-import org.codehaus.groovy.grails.web.mime.*
-import javax.servlet.AsyncContext
-import javax.servlet.AsyncEvent
-import javax.servlet.AsyncListener
-import javax.servlet.ServletRequest
-import javax.servlet.ServletResponse
-
-class MimeTypesCompiledController {
- def request = new MyMockRequest()
- def index() {
- withFormat {
- html { "html" }
- xml { "xml" }
- }
- }
-}
-class MyMockRequest extends org.springframework.mock.web.MockHttpServletRequest {
- javax.servlet.DispatcherType dispatcherType
- AsyncContext asyncContext
-
- String getFormat() { "html" }
-
- void putAt(String name, val) {}
- def getAt(String name) {}
-
- Collection<javax.servlet.http.Part> getParts() {
- }
-
- javax.servlet.http.Part getPart(String name) {
- }
-
- javax.servlet.AsyncContext startAsync() {
- }
-
- javax.servlet.AsyncContext startAsync(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse) {
- }
-
- boolean isAsyncStarted() { }
-
- boolean isAsyncSupported() { true }
-}
-
-''')
- }
-
- private ResponseMimeTypesApi getResponseMimeTypesAPi() {
- final application = new DefaultGrailsApplication()
- application.config = config
- def mimeTypesFactory = new MimeTypesFactoryBean()
- mimeTypesFactory.grailsApplication = application
-
- return new ResponseMimeTypesApi(application, mimeTypesFactory.getObject())
- }
-
- private getConfig() {
- def s = new ConfigSlurper()
-
- s.parse '''
-grails.mime.file.extensions = true // enables the parsing of file extensions from URLs into the request format
-grails.mime.use.accept.header = true
-grails.mime.types = [ html: ['text/html','application/xhtml+xml'],
- xml: ['text/xml', 'application/xml'],
- text: 'text/plain',
- js: 'text/javascript',
- rss: 'application/rss+xml',
- atom: 'application/atom+xml',
- css: 'text/css',
- csv: 'text/csv',
- all: '*/*',
- json: ['application/json','text/json'],
- form: 'application/x-www-form-urlencoded',
- multipartForm: 'multipart/form-data'
- ]
-'''
- }
-}
@@ -27,7 +27,6 @@ import javax.servlet.http.HttpServletResponse
import grails.core.GrailsDomainClassProperty
import org.grails.plugins.web.controllers.api.ControllersApi
-import org.grails.plugins.web.api.ControllersMimeTypesApi
import grails.core.support.proxy.ProxyHandler
import grails.web.mime.MimeType
import org.grails.web.pages.discovery.GroovyPageLocator
@@ -51,7 +50,6 @@ class ControllersRestApi {
public static final String PROPERTY_RESPONSE_FORMATS = "responseFormats"
protected @Delegate ControllersApi controllersApi
- protected @Delegate ControllersMimeTypesApi mimeTypesApi
protected RendererRegistry rendererRegistry
@Autowired(required = false)
ProxyHandler proxyHandler
@@ -62,9 +60,8 @@ class ControllersRestApi {
@Autowired
ResponseMimeTypesApi responseMimeTypesApi
- ControllersRestApi(RendererRegistry rendererRegistry, ControllersApi controllersApi, ControllersMimeTypesApi mimeTypesApi) {
+ ControllersRestApi(RendererRegistry rendererRegistry, ControllersApi controllersApi) {
this.controllersApi = controllersApi
- this.mimeTypesApi = mimeTypesApi
this.rendererRegistry = rendererRegistry
}
@@ -25,7 +25,6 @@ import org.grails.core.artefact.ControllerArtefactHandler
import org.grails.core.artefact.DomainClassArtefactHandler
import grails.core.GrailsApplication
import grails.core.GrailsClass
-import org.grails.plugins.web.api.ControllersMimeTypesApi
import org.grails.plugins.web.rest.api.ControllersRestApi
import org.grails.plugins.web.rest.render.DefaultRendererRegistry
@@ -47,7 +46,7 @@ class RestResponderGrailsPlugin implements GrailsApplicationAware{
rendererRegistry(DefaultRendererRegistry) { bean ->
modelSuffix = application.flatConfig.get('grails.scaffolding.templates.domainSuffix') ?: ''
}
- instanceControllersRestApi(ControllersRestApi, ref("rendererRegistry"), ref("instanceControllersApi"), new ControllersMimeTypesApi())
+ instanceControllersRestApi(ControllersRestApi, ref("rendererRegistry"), ref("instanceControllersApi"))
}
def onChange = { event ->
@@ -29,7 +29,6 @@ import org.grails.plugins.testing.GrailsMockHttpServletRequest
import org.grails.plugins.testing.GrailsMockHttpServletResponse
import org.grails.plugins.web.api.ControllerTagLibraryApi
import org.grails.plugins.web.controllers.api.ControllersApi
-import org.grails.plugins.web.api.ControllersMimeTypesApi
import grails.web.mime.MimeType
import org.grails.web.pages.GroovyPageUtils
import org.grails.plugins.support.WebMetaUtils
@@ -199,7 +198,6 @@ class ControllerUnitTestMixin extends GrailsUnitTestMixin {
enhancer.addApi(new ControllersApi())
enhancer.addApi(new ControllerTagLibraryApi())
- enhancer.addApi(new ControllersMimeTypesApi())
enhancer.enhance(GrailsMetaClassUtils.getExpandoMetaClass(controllerClass))
}
controllerArtefact
@@ -39,7 +39,6 @@ import org.grails.plugins.testing.GrailsMockHttpServletResponse
import org.grails.plugins.web.api.ControllerTagLibraryApi
import org.grails.plugins.web.controllers.api.ControllersApi
import org.grails.plugins.web.ServletsGrailsPluginSupport;
-import org.grails.plugins.web.api.ControllersMimeTypesApi
import org.grails.plugins.web.api.RequestMimeTypesApi
import org.grails.plugins.web.api.ResponseMimeTypesApi
import org.grails.plugins.web.mime.MimeTypesFactoryBean
@@ -89,7 +88,7 @@ class ControllerTestPlugin implements TestPlugin {
rendererRegistry(DefaultRendererRegistry) {
modelSuffix = config.flatten().get('grails.scaffolding.templates.domainSuffix') ?: ''
}
- instanceControllersRestApi(ControllersRestApi, ref("rendererRegistry"), ref("instanceControllersApi"), new ControllersMimeTypesApi())
+ instanceControllersRestApi(ControllersRestApi, ref("rendererRegistry"), ref("instanceControllersApi"))
instanceControllerTagLibraryApi(ControllerTagLibraryApi)
def urlConverterType = config?.grails?.web?.url?.converter
@@ -1,5 +1,6 @@
package org.codehaus.groovy.grails.web.binding
+import grails.artefact.Artefact
import grails.converters.JSON
import grails.converters.XML
import grails.persistence.Entity
@@ -64,6 +65,7 @@ class JSONBindingToNullTests {
}
}
+@Artefact('Controller')
class UserController {
def update() {
println params
Oops, something went wrong.

0 comments on commit 0479e25

Please sign in to comment.