NextBeer, a sample OpenApi application for T-Mobile

Last summer I responded to a request for proposal from T-Mobile in Poland. This rather large telco, wanted to share its services in an easy way on-line, so that every little private developer could use them, a bit like Facebook/Amazon does. We got the job, though the news came so late, I had already started another project (for yet another telco), and as an effect, could not participate in the one for T-Mobile.

The project has a fitting name: OpenApi, because that’s what it really is. An open API for everyone who wants to use it. I’ve heard there was an initiative, to build a common API for all telcos, quite a smart move which would ease creating applications even further, but for now it’s custom designed and build.

My friends finished coding a portal for developers, and I was asked to write a sample application. It made a lot of sense to me: the guys deeply in the guts of the system, should not create samples – their perspective is different to real users. I, on the other hand, while knowing what the system is supposed to do (I wrote the proposal together with Piotr Jagielski), haven’t seen in yet.

And so, a sample application was born, created in Grails, though a bit Java-style (I wanted to keep it familiar to all those Java/C# folks, who know no Groovy).

The user story is simple. It’s Friday, late evening, you are sitting in a pub, together with your friends, but the place is going to be closed at 10pm, which is very unfortunate, as the sweet brunette on your right has just noticed your presence.

You need to move the party forward, to another place, so you take your shiny, last-gen iSmartphone from your pants, only to find out its battery has died. Running all those apps of your design was really demanding.

So the sweet brunette on your right, pulls her old, dumb Nokia, and send an sms. Few minutes later she gets one back, with addresses and phone numbers of all the pubs in 3km range. That’s how far she can get on her high hills.

Your party is saved. Your sweet brunette may be truly yours someday. The application which responded to the sms and saved the day, is the sample application I wrote, using OpenAPI and Google Places. It’s called: NextBeer.

One picture is worth thousand words, so here is a sequence diagram for the whole thing.

You can find the code on github: https://github.com/jakubnabrdalik/nextbeer

I won’t go into details, the code is self documenting, and there is even a nice tutorial for all those who know nothing of Grails (though it’s in Polish, as that was the target audience of my example).

If you want to register to OpenAPI, to write your own, go here: https://developers.t-mobile.pl

Hope that saves you a nice brunette one day.

You May Also Like

Grails with Spock unit test + IntelliJ IDEA = No thread-bound request found

During my work with Grails project using Spock test in IntelliJ IDEA I've encountered this error:

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at org.codehaus.groovy.grails.plugins.web.api.CommonWebApi.currentRequestAttributes(CommonWebApi.java:205)
at org.codehaus.groovy.grails.plugins.web.api.CommonWebApi.getParams(CommonWebApi.java:65)
... // and few more lines of stacktrace ;)

It occurred when I tried to debug one of test from IDEA level. What is interesting, this error does not happen when I'm running all test using grails test-app for instance.

So what was the issue? With little of reading and tip from Tomek Kalkosiński (http://refaktor.blogspot.com/) it turned out that our test was missing @TestFor annotation and adding it solved all problems.

This annotation, according to Grails docs (link), indicates Spock what class is being tested and implicitly creates field with given type in test class. It is somehow strange as problematic test had explicitly and "manually" created field with proper controller type. Maybe there is a problem with mocking servlet requests?

Spring Security by example: securing methods

This is a part of a simple Spring Security tutorial:

1. Set up and form authentication
2. User in the backend (getting logged user, authentication, testing)
3. Securing web resources
4. Securing methods
5. OpenID (login via gmail)
6. OAuth2 (login via Facebook)
7. Writing on Facebook wall with Spring Social

Securing web resources is all nice and cool, but in a well designed application it's more natural to secure methods (for example on backend facade or even domain objects). While we may get away with role-based authorization in many intranet business applications, nobody will ever handle assigning roles to users in a public, free to use Internet service. We need authorization based on rules described in our domain.

For example: there is a service AlterStory, that allows cooperative writing of stories, where one user is a director (like a movie director), deciding which chapter proposed by other authors should make it to the final story.

The method for accepting chapters, looks like this:

Read more »