From ec45e84e04df20e47525b6443b940d51689d2856 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Fri, 18 May 2018 14:34:16 -0400 Subject: [PATCH] bolt on support for simple JSON-returning services by EGWeb This patch adds a way for EGWeb context loaders to specify that a response should be emitted as JSON rather than rendering an HTML page using Template::Toolkit. Specifically, if the context as munged by the context loader contains a json_response key, the contents of that key will to provide a JSON reponse. The json_response_cookie key, if present, can be used to set a cookie as part of the response. Template Toolkit processing is bypassed entirely when emitting a JSON response, so the context loader would be entirely reponsible for localization of strings in the response meant for direct human consumption. Signed-off-by: Galen Charlton --- Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm index 8dc09cb8d2..0d2fe261b4 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm @@ -81,6 +81,19 @@ sub handler_guts { $stat = Apache2::Const::OK; } return $stat unless $stat == Apache2::Const::OK; + + # emit context as JSON if handler requests + if ($ctx->{json_response}) { + $r->content_type("application/json; charset=utf-8"); + $r->headers_out->add("cache-control" => "no-store, no-cache, must-revalidate"); + $r->headers_out->add("expires" => "-1"); + if ($ctx->{json_reponse_cookie}) { + $r->headers_out->add('Set-Cookie' => $ctx->{json_reponse_cookie}) + } + $r->print(OpenSRF::Utils::JSON->perl2JSON($ctx->{json_response})); + return Apache2::Const::OK; + } + return Apache2::Const::DECLINED unless $template; my $text_handler = set_text_handler($ctx, $r); -- 2.11.0