Tag Archives: grails

Angularjs and Grails withFormat and json

I have been doing a mixed angularjs and grails project and ran into problems getting json to come back from the controller request with a withFormat block.

What I expected

I expected that it would call the grails app controller index method and with the withFormat to pick out that the request came is as a json request so return json. This is not the case, it would send me back the html page instead.

The problem was that grails would see */* as the accept headers and return just the html.

Grails code from the grails controller

def index() {
        def books = Book.findAll().collectEntries { book -> [book.title, book] }.values()
        withFormat {
            html books: books
            json { render books as JSON }
        }
    }

Angularjs json request that returns html

$http.get('/book/index').success(function(response) { };
}

Continue reading

Grails 1.3.x and plugin dependency name conflict resolution

I got this tip off of @rfletcherEW

Grails 1.3.x the plugin fails to install

Plugin something-0.2 installed

[delete] Deleting directory /Users/you/.grails/1.3.9/projects/myProject/resources

Plugin /Users/you/.ivy2/cache/org.somethingGroup/plugin/jars/something-0.2.jar is not a valid Grails plugin. No plugin.xml descriptor found!

The reason

You want to use plugin something-0.2 and it contains jar something-0.2.jar which is the same name as the plugin.  The dependency management system cannot cope with this in 1.3.x  This has been fixed in 2.0 but for people running 1.3.x it still causes issues.

The Work Around

It is a bit dirty it just involves creating a new plugin with a different name

  1. Clone the project.  Most of them are on github anyway.
  2. Get the right branch or version from the commit logs.
  3. Change the name of the  class SomethingGrailsPlugin to be different from its required dependencies.  E.G: class SomethingFishGrailsPlugin
  4. Compile the plugin.
  5. Package the plugin
  6. Deploy the plugin into your maven mirror.  If you don’t no one can build your project as they will not find your plugin, and if you delete your ivy cache you lose all your hard work.
  7. Push the source into your source control.
  8. Document what you have done to your project readme.md
  9. Push to migrate from 1.3.x as soon as possible to 2.x and remove your plugin and use the full version of the plugin again.
  10. If you need to upgrade your plugin you have to do all those steps again.

 

Groovy with Geb using Phantomjs and ssl certs

Here is my setup for using self signed certs and allowing the PhantomJsDriver to use Phantomjs and hit https pages while using self signed certificates.

Read Tomás Lin’s excellent post on setting up Geb with Phantomjs.

http://fbflex.wordpress.com/2013/03/18/how-to-configure-webdriver-in-grails-for-your-geb-tests/

The phantomjsdriver will producde a log file phantomjsdriver.log when creating the instance of phantomjs.

Negotiated Capabilities: {“browserName”:”phantomjs”,”version”:”1.9.1″,”driverName”:”ghostdriver”,”driverVersion”:”1.0.3″,”platform”:”mac-10.8 (Mountain Lion)-32bit”,”javascriptEnabled”:true,”takesScreenshot”:true,”handlesAlerts”:false,”databaseEnabled”:false,”locationContextEnabled”:false,”applicationCacheEnabled”:false,”browserConnectionEnabled”:false,”cssSelectorsEnabled”:true,”webStorageEnabled”:false,”rotatable”:false,“acceptSslCerts”:false,”nativeEvents”:true,”proxy”:{“proxyType”:”direct”}}

AcceptSslCerts is shown as false in the log, but the below code will use command line arguments to force phantomjs to accept any certificate.  The value we are most interested in is –ignore-ssl-errors=true

https://github.com/ariya/phantomjs/wiki/API-Reference

Continue reading

Unit testing Grails 2.0 Controllers withForm to handle duplicate submits

The documentation is a bit out of date for Grails 2.0.0 for testing controllers with the withForm { } closure to stop double submits.
New in Grails 2.0.0 is that it is more strict with its unit testing when using the withForm method
Add the following in to allow the form token to allow your method to get into the withForm{} block of your code

setup(){
    def serverUrl = "http://localhost:80"
    org.codehaus.groovy.grails.web.servlet.mvc.SynchronizerTokensHolder
    tokenHolder = SynchronizerTokensHolder.store(session)
    def token = tokenHolder.generateToken(serverUrl)
    params[SynchronizerTokensHolder.TOKEN_KEY] = token
    params[SynchronizerTokensHolder.TOKEN_URI] = serverUrl
}