Posts by Hubert Klein Ikkink

Ratpacked: Special Routing Of Promise Values Using Predicates

Posted on by  
Hubert Klein Ikkink

One of the strengths of Ratpack is the asynchronous execution model. An important class is the Promise class. An instance of the class will represent a value that is available later. We can invoke several operations that need be applied to a value when a Promise is activated. Usually the activation happens when we subscribe to a Promise using the then method. We can use the route method for a Promise to have a different action when a certain predicate is true. The action will stop the flow of operations, so methods that are executed after the route method are not executed anymore if the predicate is true. If the predicate is false then those methods are invoked.

The Promise class has a method onNull as a shorthand for the route method where the predicate checks if the value is null. For example we could have a service in our application that returns a Promise. If the value is null we want some special behaviour like sending a 404 status code to the client. With the following code we could achieve this:

Continue reading →

Change Font Size With Mouse In IntelliJ IDEA

Posted on by  
Hubert Klein Ikkink

We can change the font size in our editor using shortcut keys in IntelliJ IDEA. But we can also use our mouse wheel to do this. We must enable this option in the settings of IntelliJ IDEA. We select the Preferences and then General | Editor. Here we select the option Change font size (Zoom) with Command+Mouse Wheel:

Continue reading →

Grails Goodness: Go To Related Classes In IntelliJ IDEA

Posted on by  
Hubert Klein Ikkink

Normally in a Grails application we have classes that are related to each other, but are located in different directories. For example a controller with several views. Or a Grails service with corresponding specifications. In IntelliJ IDEA we can use Choose Target and IDEA will show classes, files and methods that are relevant for the current file we are editing. The keybinding on my Mac OSX is Ctrl+Cmd+Up, but can be different on your computer and operating system. We can also choose the menu option Navigate | Related symbol....

In the following example we are editing the file MessagesController. We select the action Choose Target, IntelliJ IDEA shows a popup menu with the views for this controller and the specification class:

Continue reading →

Grails Goodness: Change Locale With Request Parameter

Posted on by  
Hubert Klein Ikkink

Grails has internationalisation (i18n) support built-in. It is very easy to add messages for different locales that can be displayed to the user. The messages are stored in properties files in the directory grails-app/i18n. Grails checks the request Accept-Language header to set the default locale for the application. If we want to force a specific locale, for example for testing new messages we added to the i18n property files, we can specify the request parameter lang. We specify a locale value and the application runs with that value for new requests.

The following screenshot shows a scaffold controller for a Book domain class with a default locale en:

Continue reading →

Grails Goodness: Enable Hot Reloading For Non-Development Environments

Posted on by  
Hubert Klein Ikkink

If we run our Grails 3 application in development mode our classes and GSP's are automatically recompiled if we change the source file. We change our source code, refresh the web browser and see the results of our new code. If we run our application with another environment, like production or a custom environment, then the reloading of classes is disabled. But sometimes we have a different environment, but still want to have hot reloading of our classes and GSP's. To enable this we must use the Java system property grails.reload.enabled and reconfigure the Gradle bootRun task to pass this system property.

Let's change our Gradle build file and pass the Java system property grails.reload.enabled to the bootRun task if it is set. We use the constant Environment.RELOAD_ENABLED to reference the Java system property.

Continue reading →

Ratpacked: Validating Forms

Posted on by  
Hubert Klein Ikkink

Ratpack is a lean library to build HTTP applications. Ratpack for example doesn't include functionality to validate forms that are submitted from a web page. To add form validation to our Ratpack application we must write our own implementation.

Let's write a simple application with a HTML form. We will use Hibernate Validator as a JSR 303 Bean Validation API implementation to validate the form fields. IN our application we also use the MarkupTemplateModule so we can use Groovy templates to generate HTML. We have a simple form with two fields: username and email. The username field is required and the email field needs to have a valid e-mail address. The following class uses annotations from Hibernate Validator to specify the constraints for these two fields:

Continue reading →

Ratpacked: Use Command Line Arguments For Configuration

Posted on by  
Hubert Klein Ikkink

Ratpack 1.1 introduced a feature to use command line arguments for our application configuration. We must use the args method of the ConfigDataBuilder class. We can define a common prefix for the arguments and the separator between the configuration property and value. If we don't specify any arguments then Ratpack assumes there is no prefix and the separator is the equal sign (=).

In the following example Ratpack application we use the args method and rely on all the default settings:

Continue reading →

Ratpacked: Using Regular Expressions For Path Tokens

Posted on by  
Hubert Klein Ikkink

When we define a path in Ratpack we can use regular expressions for matching a request. We must start a token with a double colon (::) and then we can define a regular expression. Ratpack will try to match the request path and uses our regular expression.

In the following example Ratpack application we use a regular expression to match requests that start with Gr after a fixed conferences prefix:

Continue reading →

Ratpacked: Type Conversion For Path Tokens

Posted on by  
Hubert Klein Ikkink

In Ratpack we can use path token as variable parts of a request. We can access a path token via the PathBinding instance that is added to the context registry by Ratpack. There is a shorthand method getPathTokens to get the PathTokens object. To get the value for a token we use the get method or with the Groovy DSL we can use square brackets with the name of the token. If we expect a token value to be of a certain type, like a Long or Boolean, we can use the asLong or asBoolean method. The value is then converted to the correct type and if the conversion fails an exception is thrown. The exception will be converted to a 500 status code in the response by the default error handler.

The PathTokens class has methods for converting to Long, Byte, Integer, Short and Boolean types with the methods asLong, asByte, asInteger, asShort and asBoolean

Continue reading →

Ratpacked: Using Optional Path Tokens

Posted on by  
Hubert Klein Ikkink

To define endpoints in our Ratpack application we can use optional path tokens. This means that a part of the request can have a value or not, but we can a have single path definition that will match. Normally we define a variable path token with a colon (:) followed by a variable name. To make this token optional we follow it with a question mark (?). This tells Ratpack that the token can have a value or not. In our handler we now need to take into account that the path token is optional as well.

Let's write a sample Ratpack application with a path definition containing an optional token. We define a path binding for profiles/:username?. The path token username is available in our handler if the value is set, but also a request for profiles will match for this binding. Then the value for username is not set.

Continue reading →

Ratpacked: Deploy Application As Docker Container

Posted on by  
Hubert Klein Ikkink

If we use the Ratpack Gradle plugin for our project then we automatically get the Gradle application plugin. We can use this together with the Gradle Docker application plugin to deploy our Ratpack application as a Docker container very easily.

To make it work we must apply the com.bmuschko.docker-java-application in our Gradle build file. With this plugin we can define some configuration properties in the docker.javaApplication configuration block. We can set a base image instead of the default java base image. The default image tag is a made up of the project group name, application name and version. To set the exposed port number we use the port property.

Continue reading →

shadow-left