In Groovy we can use the head
and tail
methods for a long time on Collection
objects. With head
we get the first element and with tail
the remaining elements of a collection. Since Groovy 2.4 we have a new method init
which returns all elements but the last in a collection.
In the following example we have a simple list and apply the different methods:
Continue reading →
We know Groovy has a lot of nice methods for working with collections. For example in previous blog posts we have seen how to take or drop elements from a list and even with a condition. Since Groovy 2.4 we can now also use the dropRight
and takeRight
methods to take or drop elements from the end of the list.
In the following example we have a simple list and we use the dropRight
and takeRight
methods to get elements from the list:
Continue reading →
This third and final part in my Stateless Spring Security series is about mixing previous post about JWT token based authentication with spring-social-security. This post directly builds upon it and focusses mostly on the changed parts. The idea is to substitude the username/password based login with "Login with Facebook" functionality based on OAuth 2, but still use the same token based authentication after that.
The user clicks on the "Login with Facebook" button which is a simple link to "/auth/facebook", the SocialAuthenticationFilter notices the lack of additional query parameters and triggers a redirect leading the user of your site to Facebook. They login with their username/password and are redirected back, again to "/auth/facebook" but this time with "?code=...&state=..." parameters specified. (If the user previously logged in at facebook and had a cookie set, facebook will even instantly redirect back and no facebook screen is shown at all to the user.) The fun part is that you can follow this in a browsers network log as it's all done using plain HTTP 302 redirects. (The "Location" header in the HTTP response is used to tell the browser where to go next)
Continue reading →
Since version 2.2 Grails, has better support for managing namespace configuration. This helps to prevent common namespace problems. For example most applications which have security functionality, have for example a UserDetailService
which can conflict when you have the Grails SpringSecurity
plugin installed. Grails version 2.2. and later comes with four useful techniques to make sure the right class is used
If Grails does not find an existing service with a similar name, Grails will automatically generate an alias for you service with the name of the plugin prefix. For example when you have a plugin called UserUtilities
and a service called UserDetailService
, you can use UserUtilitiesUserDetailService
for dependency injection which will not conflict with the SpringSecurity UserDetailService
Continue reading →
Migrating from Ant to Gradle is very easy with the importBuild
method from AntBuilder
. We only have to add this single line and reference our existing Ant build XML file and all Ant tasks can now be executed as Gradle tasks. We can automatically rename the Ant tasks if we want to avoid task name collisions with Gradle task names. We use a closure argument with the importBuild
method and return the new task names. The existing Ant task name is the first argument of the closure.
Let's first create a simple Ant build.xml
file:
Continue reading →
At the release of Java 8 the most attention went to the Lamda’s, the new Date API and the Nashorn Javascript engine. In the shade of these, there are smaller but also interesting changes. Amongst them is the introduction of a StringJoiner. The StringJoiner is a utility to delimit a list of characters or strings. You may recognize the code below:
String getString(List<String> items)
StringBuilder sb = new StringBuilder();
for(String item : items) {
if(sb.length != 0) {
sb.append(",");
}
sb.append(item);
}
return sb.toString();
}
Continue reading →
In our Asciidoc markup we can include delimited blocks, like sidebars, examples, listings and admonitions. A delimited block is indicated by a balanced pair of delimiter characters. For example a sidebar starts and ends with four asterisk characters (****
). If we want to nest another delimited block of the same type we must add an extra delimiter character at the start and end of the nested block. So when we want to nest another sidebar block inside an existing sidebar block we must use five asterisk characters (*****
).
In the following example Asciidoc source we have several blocks with nested blocks:
Continue reading →
Grails is built on Groovy which is known as a dynamic language. The dynamic nature of Groovy offers a lot of powerful features but also defers the detection of errors from compile time to runtime. To shorten the feedback cycle for your code Groovy has a handy annotation which will make sure that your classes is are statically compiled. This will give you fast feedback for a lot of mistakes and you also will benefit from the increased performance offered by the static complication. Unfortunately in Grails this annotation prevents you from using the very useful dynamic GORM methods like list()
, get()
and the dynamic finder methods. Groovy does not recognize these Grails methods during compile time; see the example below.
@CompileStatic
class BookController(){
def save(){
//This will successfully compile
}
def get(){
Book.findByName(params.name)
//this will throw a compile error since the findByName method is not known
//at compile time
}
@CompileStatic(TypeCheckingMode.SKIP)
def delete(){
//by setting the TypeCheckinMode, this method will be skipped
}
}
Continue reading →
If we run a Gradle build and one of the tasks fails, the whole build stops immediately. So we have fast feedback of our build status. If we don't want to this and want Gradle to execute all tasks, even though some might have failed, we use the command line option --continue
. When we use the --continue
command line option Gradle will execute every task where the dependent tasks are not failing. This is also useful in a multi-module project where we might want to build all projects even though some may have failing tests, so we get a complete overview of failed tests for all modules.
In the following Gradle build file we have two tasks. The task failTask
throws a TaskExecutionException
to purposely fail the task. The successTask
will not fail:
Continue reading →
If we use Gradle in a multi-module project we can define project dependencies between modules. Gradle uses the information from the project dependencies to determine which tasks need to be run. For example if module B depends on module A and we want to build module B, Gradle will also build module A for us, because module B depends on it. But if we know for sure that module A is up to date and has not changed, we can also instruct Gradle to skip building module A, when we build module B.
Let's start with the following module structure, where each module depends on the module above it. So module services depends on common and module web depends on services:
Continue reading →