Archive: January 2015

Groovy Goodness: Getting the Indices of a Collection

Posted on by  
Hubert Klein Ikkink

Since Groovy 2.4 we can use the indices property on a Collection to get the indices of the elements in the collection. We get an IntRange object as a result.

def list = [3, 20, 10, 2, 1]
assert list.indices == 0..4


// Combine letters in alphabet
// with position (zero-based).
def alphabet = 'a'..'z'
def alphabetIndices = [alphabet, alphabet.indices].transpose()
// alphabetIndices = [['a', 0], ['b', 1], ...]

// Find position of each letter
// from 'groovy' in alphabet.
def positionInAlphabet = 'groovy'.inject([]) { result, value ->
    result << alphabetIndices.find { it[0] == value }[1] + 1
    result
}

assert positionInAlphabet == [7, 18, 15, 15, 22, 25]

Continue reading →

Groovy Goodness: Pop And Push Items In a List

Posted on by  
Hubert Klein Ikkink

Groovy adds the pop and push methods to the List class. With the pop method we remove the last element of the list. And with the push method we add an element to the end of the list.

def list = ['Groovy', 'is', 'great!']

// Remove last item from list
// with pop().
assert list.pop() == 'great!'
assert list == ['Groovy', 'is']

// Remove last item
// which is now 'is'.
list.pop()

// Add new item to end of
// the list (equivalent for add()).
list.push('rocks!')

assert list == ['Groovy', 'rocks!']

Continue reading →

Groovy Goodness: Getting All But the Last Element in a Collection with Init Method

Posted on by  
Hubert Klein Ikkink

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 →

Groovy Goodness: Take Or Drop Last Items From a Collection

Posted on by  
Hubert Klein Ikkink

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 →

Stateless Spring Security Part 3: JWT + Social Authentication

Posted on by  
Robbert van Waveren

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 →

Grails: Preventing naming collisions

Posted on by  
Albert van Veen

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 →

shadow-left