Configuring Embedded Tomcat in Grails Development Environment
While you are developing a grails app, by default you are using an embedded Tomcat instance.
Sometimes you’ll need to do some configuration, in my case I had to proxy my tomcat instance with an Apache web server through mod_proxy_ajp .
When you have a standalone server running, you can easily change your server.xml file to add an AJP conector which listens for the Apache connections.
To do this within your grails app simply create a _Events.groovy file in your scripts folder of the grails project, and add the configurations needed by your app to the tomcat instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import grails.util.GrailsUtil import org.apache.catalina.connector.Connector import org.apache.coyote.http11.Http11AprProtocol eventConfigureTomcat = {tomcat -> if (GrailsUtil.environment == 'development') { def ajpConnector = new Connector("org.apache.coyote.ajp.AjpProtocol") ajpConnector.port = 8009 ajpConnector.protocol = "AJP/1.3" ajpConnector.redirectPort = 8443 ajpConnector.enableLookups = false ajpConnector.setProperty("redirectPort", "8443") ajpConnector.setProperty("protocol", "AJP/1.3") ajpConnector.setProperty("enableLookups", "false") tomcat.service.addConnector ajpConnector println "Configured Tomcat ajp connector on port 8009" } } |
You can also add other tomcat configuration such as Tomcat users and roles: link
Instruct Groovy HTTPBuilder to handle a JSON response with wrong content type
While developing a utility script to monitor my mobile data consumed, I found a problem with
the response I was getting from the server. It content-type was set to ‘text/json’ instead of ‘application/json’, ‘application/javascript’ or ‘text/javascript’ wich are the content types used by HTTPBuilder to parse a response as JSON.
To solve this problem, one must add a new content type to the ParserRegistry and parse the response.
There are two ways to do it:
1 2 3 4 5 6 | def httpBuilder = new HTTPBuilder(baseUrl) httpBuilder.parser.'text/json' = { resp -> def bufferedText = resp.entity.content.getText( ParserRegistry.getCharset( resp ) ).trim() return new JsonSlurper().parseText( bufferedText ) } |
The second ( reuse default )
1 2 | def httpBuilder = new HTTPBuilder(baseUrl) httpBuilder.parser.'text/json' = httpBuilder.parser.'application/json' |
Now everything works again.
See you
























