Monday, March 9, 2009

Grails configuration issue

As far as You know Grails offers many solutions for configuration of Grails application. But most of the configuration is handled by the Config.groovy file in grails-app/conf. I used standard configurations created by default and simply added mail settings in the end of the file as was described in one of my previous posts. But when I tried to read, for instance, grails.serverURL property using grailsApplication.config at controller's layer (and even using ConfigurationHolder at service layer) I've got empty string at runtime. Part of my Config.groovy file is listed below:
environments {
development {
grails.serverURL = "http://localhost:8080"
}
}

// Some other configuration.

grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "youracount@gmail.com"
password = "yourpassword"
props = ["mail.smtp.auth":"true"]
}
}

I was quiet confused cause I still could read configuration not connected with grails, for instance, log4j settings.
As I discovered later problem lied in fact that using grails { (and open bracket) rewrited all previous settings concerning grails. So one of the correct solution might be:
environments {
development {
grails.serverURL = "http://localhost:8080"
grails.mail.host ="smtp.gmail.com"
grails.mail.port = 465
grails.mail.username = "youracount@gmail.com"
grails.mail.password = "yourpassword"
grails.mail.props = ["mail.smtp.auth":"true"]
}
}

In this case settings are added not rewrited. Don't repeat somebody's mistakes. Enjoy!

Grails HTTPS problem

Today I secured some pages in Grails application using HTTPS. But when I tried to start application with using of a generated key store and SSL certificate for HTTPS:
grails run-app-https
I've got an exception:
Could not execute method PluginManagerHolder.
No such property: PluginManagerHolder for class: RunAppHttps_groovy
And application terminated immediately.
As I discovered later problem lied in RunAppHttps.groovy script which is located under GRAILS_HOME\scripts. You have to add missing import in it:
import org.codehaus.groovy.grails.plugins.PluginManagerHolder

That solved the problem. Enjoy!

Sunday, March 8, 2009

Grails GSP concatenation nuance

While working with GSP it is a common situation to perform actions with strings, domain objects or other classes. Recently I faced the simple situation where I needed to dynamically build full file name (with file extension) in order to allow user to download it from file system. In my simple GSP I created the code similar to listed below:



I use concatenation of strings in order to build full file name. But at runtime exception occurred which was very strange and gave me no tip for possible mistake made. As I discovered later in my example You can not use dot in double quotes (".") cause Grails wants to interprete expression in them and fails.
Therefore You can solve mentioned situation in two ways:
  • Use dot in single quotes ('.'). In that case correct solution might be:


  • Move mentioned logic to some separate class (for instance, let's call it DocumentFileNameBuilder) and call it as it listed below:


Simple stupid issue solved :)

Grails ReCapthcha Plugin custom theming

Recently I got acquainted with Grails ReCaptcha Plugin, which is very useful for integration with ReCaptcha service. All steps for its installation and basic usage are described here.
But one issue which was not covered in details and was not so easy personally for me is customization of ReCaptcha look'n'feel. In other words how to create own custom theme instead of using few standards (red, white, blackglass and clean). I've looked through ReCaptcha API Documentation and Wiki, investigated simple demo, and as a result created simple example for creation of custom theming.


Pay attention:
1. Create div with, for instance, recaptcha_widget identifier which is not displayed. After the ReCaptcha theming code will be fully loaded, it will make the div visible. You don't have to make it visible by hands.
2. Place mentioned div identified to appriate attribute of recaptcha tag (custom_theme_widget="recaptcha_widget").
3. Create div for recaptcha image, it has to be empty and has fixed size.
4. Create text field for response and some additional divs for errors, or for recaptcha options (reloading f recaptcha, changing it to audio, help, etc.). Tag identifiers and names must match!
5. As an addition You can download standard ReCaptcha images and integrate them into Your theme.
As a result all these divs, text field, links and images can be connected to CSS classes and are easy customizable. Also You can use Grails tags, for instance, to render links. Enjoy!

P.S. Sorry for using image - don't know how to ignore HTML tags, textarea is not a good solution.