Write JSON response to a buffer instead of directly to the output stream (#11424, #11156)

* Allows safe abortion of JSON output and writing a critical notification instead
* Unifies json writing for init request and other UIDL requests
* Ensures headers are written before the response

Change-Id: Idd8acb672aac8716b727701d6c057bbe58f50993