Make nginx Return a JSON Response

This is more of a “write here so I do not forget” kind of post.

I set out trying to make a simple “maintenance” mode for a REST API (that speaks only JSON) without having to handle it in the code base.

nginx has a really cool support for maintenance mode that is dead easy to set up:

if (-f $document_root/maintenance.html) {
    return 503;

Obviously, life is not that easy.
In my case, the same URL is used to show static content (the API docs) and only sub-dirs in the URL are used for the API resources:

I already handle the API resource definitions in nginx, and I only need to override the root maintenance definition. In addition, the JSON response should also have the correct Content-Type header set.

In this case, is probably “easier done than said”, and is all done in here:

if (-f $document_root/maintenance.html) {
  more_set_headers "Content-Type: application/json; charset=UTF-8";
  return 503 '{"code": 503, "reason": "Service maintenance."}';

The trick is in having the nginx_headers_more module enabled and installed in your system.

On a Debian based distribution, the module is in the nginx-extras package.

New Feature for Linaro Image Tools: Android Hwpack

Finally, after some delays, Linaro Image Tools now has support for Android hardware pack (or Android hwpack).

What is an Android hwpack

Let’s start with with clearing out the confusion the term “hwpack” has in this context.

Generally speaking, for Linaro Image Tools an hwpack is a tar-ball that contains Debian packages, some configuration and metadata files, and some directory structures, needed to create and install bootable operating system images.

Linaro Image Tools has had support for this kind hwpack since the beginning of time, but they are not necessary to create Android bootable images. The process to create an Android image is slightly different.

This new hwpack, the Android hwpack, is just a configuration file, based on the same syntax of previous Linaro Image Tools hwpack, that stores information necessary to correctly create Android images. An Android hwpack does not contain any Debian packages nor binary blob or anything else, it is just a text file (a YAML file for instance).

It’s probably better to call is “Android configuration file“.

Why an Android hwpack

The necessity for this came out during last Linaro Connect Europe, from the Android team. The needs were an easy way to add support for new boards (without having to hack into Linaro Image Tools code), the possibility to change board parameters (almost) “on-the-fly” for testing (without having to touch code), and the possibility to maintain backward compatibility by providing some kind of “versioned” hwpack, guaranteed to work for specific Linaro releases.

How to use it

In order to use it, you need to have at least Linaro Image Tools version 2013.01. At the time of this writing, Android hwpack support is also being built into the Android boot tarball, so it should be totally transparent for the user: no need to add command line arguments and to pass file paths.

Still, it is possible to pass an Android hwpack to Linaro Android Media Create, the command line tools that creates Android bootable images. The command line argument is the same as Linaro Media Create one: –hwpack

So, if you want to use the new feature, arm yourself with an Android hwpack file (examples can be found on GitHub), and run:

linaro-android-media-create --mmc /dev/sdc --dev panda --boot boot.tar.bz2 --system system.tar.bz2 --userdata userdata.tar.bz2 --hwpack panda-hwpack

Backward compatibility has been maintained: the old hard-coded values have been kept, so it is still possible to use everything in the old way.

If you find bugs, of other possible improvements, feel free to leave them in the comments below, or better on Launchpad.

Book Review: Think Like a Programmer

This is my first attempt at writing a book review, so bare with me if it is not the best around. And here the “usual” disclaimer: I received a free copy of “Think Like a Programmer” from NoStarch Press.

The book: Think Like a Programmer an Itroduction to Creative Problem Solving

Think Like a Programmer Book Cover
Think Like a Programmer book cover.

As the titles goes, this is not only a book about programming or development, it is an exercise on problem solving and thinking. The book has one of the best starts I could imagine: puzzles. Funny puzzles, those that nowadays scare candidates during the hiring processes at big companies.

The puzzle-pace is kept for the first two chapters, and the author guides you into well detailed examples of problem solving: how to break a problem into smaller parts, how to apply your knowledge to unknown problems, where and how to recognize patterns.

After these two introductory chapters, things start to get serious. Complex concepts are introduced: arrays, pointers, classes, dynamic memory… and the problems accompanying them resemble real-life ones. To fully grok these sections, a good knowledge of C++ is needed. The author introduces each new topic with the basic needed coding knowledges, but if you do not have any previous C or C++ skills at all, the examples and the exercises might be daunting.

The book is broken into 8 chapters and you can perceive the complexity increasing up to a peak in the middle ones. The last two chapters are less demanding and the concepts introduced are not tied to any particular programming language: they step into the realm of software engineering, code reuse and something I would refer to as task management.

In the End…

It is not meant to be a cookbook, nor something you can use as a reference manual. You have to embark yourself , and let the author sail you through a journey of discoveries, learning and problem solving, being patient and going through the proposed exercises. It is a training for your brain, for your code writing-fu, and mostly for how to approach problems and how to tackle them.

Working from Home, Six Months Later

Almost six months ago I started working for Linaro, from home. It was the first time for me with a home-based job, and I’m enjoying every minutes of it.

Before joining Linaro I did my fairly amount of researches on the Internet about how to organize your workday and also your life: try to do as if you had to get ready for office, eat & drink, do not always stand sit…

I didn’t change much of my usual habits, and I’m trying to keep home and work activities as much as possible separate from each other. Sometimes they will interfere, but I guess it is normal, as long as you remember to stick to your plan of separating them.

So, this is something I would call my usual day…

Getting Out of Bed

I’m an “early bird” as to speak.

I like getting up early in the morning. Luckily from where we live, opening the window we can see the sea and the sun rising. Getting out of bed is not that bad after all.

During the warmer months I also enjoy going out jogging for 40 minutes, during the colder months I usually start my days with a 30-40 minutes training. Nothing much heart-pumping, and everything weight-free, just free body weight: abs, squat, push-ups, and lots of stretching (sometimes even yoga, but my yoga-skills are really rudimentary). On the Internet you can find plenty of videos and web-sites on how to train in the correct way and for how much, so you still get to vary your training (I still haven’t found a really good Android app for something I would call zen-training: a mix of body and mind exercises, that doesn’t get much into the way, and that suggests you exercises).

Shower, that helps me like a caffeine shot (I do not drink coffee, even if I drink tea), and a big breakfast. Breakfast that for me is the most important meal of the day.

No computer or any electronic gadgets activities until after breakfast.

The Work Day

I sit in front of the PC around 8-8.30, checking emails, reading newspapers and the usual social websites. At 9, my (virtual) pomodoro clock usually starts ticking. I try to organize my working days in that way, sticking to the pomodoro technique as much as possible, and having pomodoro times also for checking IRC and other work related activities I have to do online.

Code, Eat & Drink

I always have something to drink next to my keyboard. Sometimes it even gets on my keyboard, but that’s another story…

Tea in the morning, that usually propels me almost close to lunch time, then water. I do not eat anything else in the morning, and in the afternoon sometimes I have a little break and usually I eat a fruit. At lunch time, I cook: I enjoy cooking, and it is one activity that me and my girlfriend enjoy doing in the evening together. It is a good moment for talking, and for preparing food.

Getting Into Bed Again

After dinner, we spend time on the couch watching movies, or doing our open-source-communities related activities. I also enjoy doing a 10-15 minutes streching/yoga session before getting into bed again. And before falling asleep, I always read a good book.

As in all rules that you set for yourself, form time to time I do not follow them, without feeling guilty. I need the liberty of doing what I like to do, with the confidence of getting back to my habits.

Convert a bazaar repository into a git one

Yesterday, for a work task, I needed to convert a bazaar repository into a git one, to store code for other teams (that mostly use git) to work on. The bazaar repo was very simple actually, small history and it didn’t contain any merges.

Read The Docs™

Or almost… The first thing I did was heading other bazaar extensive documentation, in particular to its wiki sections on plugins. And there you have it, just an install-command away, ready to work: bzr-fastimport. Yeah, it says import, but it works also on the export part.

The steps I took:

bzr branch repo git-repo
cd git-repo && git init
bzr fast-export `pwd` | git fast-import
rm -rf .bzr

It should work also for fairly complex projects, though I didn’t try it on one.

Eternal Editor Search

Finding the perfect code editor

In my eternal search for a no resource hungry (Python) code editor that can help you getting things done easily and quickly, I started using SublimeText at its 2.0 version (albeit not being an open source project).

I never used Mac OS X TextMate, but some people described SublimeText as a valid alternative to that editor, and I always heard good words about TextMate.

After using SublimeText for almost 4 months, I’m finding myself really happy with the cool working experience it provides and its capabilities.

What I found most interesting in these months are:

  • Loads of plugins
  • Code completion (through one of its many plugins)
  • JSON based configuration files
  • Great project support, with almost instant project switch through the command palette
  • A very responsive command palette
  • Integration with static code analysis tools
  • Powerful editing capabilities (multi-selection editing in primis)
  • Possibility to integrate the Python debugger
  • Integrated Python console
  • A “disctraction free” mode (now called it like that, but it is just full screen editing

It has extensive documentation, and also nifty video tutorial to set-up a dev environment with ease.

In the next months I will polish my vim-fu, I want to do some tests using the Chromebook with its Chrome OS for development purposes. Almost all of my dev activities happen online, and through a Google hosted account and Chrome OS has a decent shell with SSH support (and also different SSH plugins available in the Chrome Web Store). I just need a VM, or a VPS somewhere, where to store the code and all the development tools, and see how it plays out.

JavaMail Session to the Rescue

There might comes the time when you need to send emails to your users base within your Java application, deployed in Glassfish. So you start to code some simple Java mail classes, and you find yourself hardcoding host names, user names, passwords and all the other good sensible information in your code, that is open source.

This is the situation I found myself in while doing some maintenance on our code base: subscription emails, or any other emails for the matter, were sent out using one simple Mail Java class, that had everything hardcoded in it. Not good.

But we are using Glassfish, and this is good (well, it depends who you are asking, but in this case it is good as probably any other app-server out there). We can use Glassfish to handle our “mail session”, and inject the necessary values inside our class when needed, leaving us free from storing sensible data in our Java code.

Obviously this is all good in theory, in practice this works if your Java code is managed directly by your app-server, and our is not, since we do not need that. But do not despair, all is possible.

With non-managed code, you need to access the “context” of your Java application, where you have objects bound to an exclusive name.

So, lets make this work.

Creating a new JavaMail Session in Glassfish is very simple either through the admin interface, or via the command line. There are  two important aspects to keep in mind: whatever you need SSL enabled or not, and your JNDI name. Since we are using Gmail, and we want to use its SMTP server, we are going to use SSL for this. The other piece of information that has to be kept in mind, is the last value in the command line: that is the JNDI name, the one you will use in your code to retrieve the JavaMail session. The command line is very simple, you can find it on github.

Now you need to retrieve the JavaMail session from Glassfish, so that it is possible to use it in a MimeMessage Java object. Code to do that is again very simple, and you can find an example here on github.

So, all in all, the situation is now better: we do not store values in the code, and the code is a little bit more flexible and can handle different JavaMail sessions in order to send emails with different accounts. We started with one Java class that handled everything, now we have four classes and one interface, and all the values that need to be retrieved (JNDI names) are stored in a separate Java properties file. Since I was at that, I added attachments support to the email creation, you never know when it might comes handy. 😉

Last step in this work: create HTML templates for sending nice email instead of boring black character emails, and handle internationalization and localization of the templates. Another funny task ahead. 🙂

Mobile Web & Internationalization

For my work, we are building the trending-trend for the mobile world: mobile web applications. Web applications, or whatever you prefer to call them, thought and optimized for being used through a mobile device. This is all great and cool, you can exploit your HTML5-CSS-JavaScript-fu, and you do not have to learn to program natively on the various mobile platform out there. It is more or less a win-win situation: write once, use on every device. There are drawbacks of course: no real power from your device, you are doomed by the Lord of the Internet Connections and offline access to the data is not really good, and you loose a little bit of that native feeling. Even with all of these, you are still able to create great mobile experiences: the available tool-kits are really well done and are actively developed (jQuery Mobile, Sencha, KendoUI), there are tools to help you building a “native” app converting your HTML5 code, and you are even able to access (with some tricks) some of the hardware resources. But there is always one problem that sometimes people forget to think about: provide users with content in their own language (or at least try to get close to that very language).

The problem we are facing now is exactly this: how to do it? How to provide users with localized content? How to better handle the localization process?

Since we are on the web, we can get language information from different sources, which one to trust is open to debate: should we trust the web browser? Should we get the language via the geolocation of the user or should we get, in some way or another, the information from the underlying operating system?

I usually consider my case: I’m Italian, I live in France, my desktop environment is in Italian, but I prefer, where possible, to read websites in English (that is because websites tend to be better in English if not properly translated).

OK, deciding that is a little bit tricky, you can get into nasty discussions about how to render time and dates, monetary currency, the direction of the text, plural forms, left aside cultural changes if you embrace a broader users base (colors, icons…).

But, if we know which source to trust, how can we “easily” extract the text to be localized, translate it, and reconstruct everything after? Our software stack is composed of HTML + PHP, JavaScript (that comes from jQuery Mobile and Sencha), and Java.

Java provides us our backend, and some messages comes from it too: error messages if something goes kaput, email messages for authenticating a user, plus other small things. But with Java we are more or less safe: there is support for gettext in Java or we can use the Java built-in features (message bundles and properties file, that I do not like much). PHP has gettext support, so even here we are safe.
JavaScript seems a little bit more problematic. Around the web the are a lot of different approaches one can take, even if they all share a small common idea. jQuery Mobile seems to have some sort of internationalization support, Sencha I wasn’t able to find any, but there is a JavaScript implementation of the gettext library (it is not clear if it supports MO file loading).

All these JavaScript approaches looks like they are made with the idea to load the translations dynamically (a-la-gettext), but what if we want to create the final translated page on the server, and send it already translated to the user? Caching the pages directly on server side and serving content a little bit faster? These, and probably others, are questions that I will have to find an answer in the coming months, and they look interesting.

Eclipse Tools

This is more a brain-dump kind of post for me, so I do not forget, every time I need to re-install Eclipse for whatever reasons, to install the plugins I need the most (and where to find them) or to tweak it a little bit. Since my daily work is more Java-based, the tools I use are Java-centric.

Bash Tool

I tend to write a lot of shell code too, maintenance scripts for the servers or for repetetive tasks, and since I’m mostly working with Java and Eclipse, I needed something that could decently handle bash files inside Eclipse.

ShellEd comes to the rescue (the website is pretty basic, with not a lot of info). I didn’t look deeply into other shell plugins for Eclipse, but this one works nicely.

To use it, it is necessary to install also the LinuxTools Eclipse plugin (from here). The only necessary part of that set of tools, if C/C++ development and the other Linux integrations are not necessary, is the man pages viewer. ShellEd is able to present you with man pages of the shell commands in a hoover-help fashion (even if I find it tad slow).

To install, download the zip file, and from Help → Install New Software → Add, use the Archive button to install it.

Editing Sessions

At work I might be working or looking on different projects, with different set of files each, and I tend to work focused on the task at hand, trying not to pollute the screen with other distractions, even in the terms of files from another project. I was looking for an easy way to save and restore the “editing session”, the open files.

I find the Extended VS Presentation plugin to be very useful in this (info here).

Pretty simple to install in Eclipse via the “Help → Install New Software“, and then pasting in the “Work with” text-box this URL:


From time to time I also write some Python code, and as for shell code, I prefer to not move from the environment I’m used. For Eclipse there is a powerful plugin for Python: PyDev. Installation instructions are very well written.

Static Analysis

Other tools that for me are a must have, are a set of static analysis tools. The more useful:

  • PMD: Eclipse instructions are here;
  • FindBugs: installable via the Eclipse Market;
  • CodePro Tools: comes from Google, and it is more than a static code analysis tool: can calculate code metrics, is a code coverage tool, has JUnit integration and can generate unit tests automatically. Can’t live without this.

Speed Eclipse a Little Bit

And this is my Eclipse configuration file, I find Eclipse a little bit faster at startup (make a copy of your old one if you want to use this!):