Move common code to one place.
Add inital test and implementation of json rest handler.