From 7c2673fb32667f9c5a5ffaaf75760fbf73ec8ae2 Mon Sep 17 00:00:00 2001 From: erickson Date: Fri, 6 Jul 2007 20:56:08 +0000 Subject: [PATCH] added compatibility layer to gateway for legacy json git-svn-id: svn://svn.open-ils.org/OpenSRF/branches/new-json2@1008 9efc2488-bf62-4759-914b-345cdb29e865 --- src/gateway/osrf_json_gateway.c | 56 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/gateway/osrf_json_gateway.c b/src/gateway/osrf_json_gateway.c index 9687989..fe3f789 100644 --- a/src/gateway/osrf_json_gateway.c +++ b/src/gateway/osrf_json_gateway.c @@ -13,8 +13,10 @@ #define MODULE_NAME "osrf_json_gateway_module" #define GATEWAY_CONFIG "OSRFGatewayConfig" #define CONFIG_CONTEXT "gateway" +#define JSON_PROTOCOL "OSRFGatewayJSONProtocol" #define GATEWAY_DEFAULT_CONFIG "/openils/conf/opensrf_core.xml" +#define GATEWAY_DEFAULT_PROTOCOL "wrapper" // other option is "classy" /* our config structure */ @@ -22,6 +24,11 @@ typedef struct { char* configfile; /* our bootstrap config file */ } osrf_json_gateway_config; +typedef struct { + char* JSONProtocol; +} osrf_json_gateway_dir_config; + + module AP_MODULE_DECLARE_DATA osrf_json_gateway_module; char* osrf_json_gateway_config_file = NULL; @@ -37,10 +44,18 @@ static const char* osrf_json_gateway_set_config(cmd_parms *parms, void *config, return NULL; } +static const char* osrf_json_gateway_set_json_proto(cmd_parms *parms, void *config, const char *arg) { + osrf_json_gateway_dir_config* cfg = (osrf_json_gateway_dir_config*) config; + cfg->JSONProtocol = (char*) arg; + return NULL; +} + /* tell apache about our commands */ static const command_rec osrf_json_gateway_cmds[] = { AP_INIT_TAKE1( GATEWAY_CONFIG, osrf_json_gateway_set_config, NULL, RSRC_CONF, "osrf json gateway config file"), + AP_INIT_TAKE1( JSON_PROTOCOL, osrf_json_gateway_set_json_proto, + NULL, ACCESS_CONF, "osrf json gateway config file"), {NULL} }; @@ -52,6 +67,13 @@ static void* osrf_json_gateway_create_config( apr_pool_t* p, server_rec* s) { return (void*) cfg; } +static void* osrf_json_gateway_create_dir_config( apr_pool_t* p, char* dir) { + osrf_json_gateway_dir_config* cfg = (osrf_json_gateway_dir_config*) + apr_palloc(p, sizeof(osrf_json_gateway_dir_config)); + cfg->JSONProtocol = GATEWAY_DEFAULT_PROTOCOL; + return (void*) cfg; +} + static void osrf_json_gateway_child_init(apr_pool_t *p, server_rec *s) { @@ -84,6 +106,24 @@ static int osrf_json_gateway_method_handler (request_rec *r) { /* make sure we're needed first thing*/ if (strcmp(r->handler, MODULE_NAME )) return DECLINED; + + osrf_json_gateway_dir_config* dir_conf = + ap_get_module_config(r->per_dir_config, &osrf_json_gateway_module); + + ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r, "JSON protocol = %s", dir_conf->JSONProtocol); + + /* provide 2 different JSON parsers and serializers to support legacy JSON */ + jsonObject* (*parseJSONFunc) (char*) = legacy_jsonParseString; + char* (*jsonToStringFunc) (const jsonObject*) = legacy_jsonObjectToJSON; + + if(dir_conf->JSONProtocol && !strcmp(dir_conf->JSONProtocol,"wrapper") ) { + /* if protocol is wrapper, use the new wrapper JSON code */ + ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r, "Using wrapper JSON"); + parseJSONFunc = jsonParseString; + jsonToStringFunc = jsonObjectToJSON; + } + + osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: entered request handler"); /* verify we are connected */ @@ -169,7 +209,13 @@ static int osrf_json_gateway_method_handler (request_rec *r) { int req_id = -1; if(!strcasecmp(input_format, "json")) { - req_id = osrf_app_session_make_req( session, NULL, method, api_level, mparams ); + jsonObject * arr = jsonNewObject(NULL); + char* str; + int i = 0; + while( (str = osrfStringArrayGetString(mparams, i++)) ) + jsonObjectPush(arr, parseJSONFunc(str)); + + req_id = osrf_app_session_make_req( session, arr, method, api_level, NULL ); } else { @@ -247,7 +293,8 @@ static int osrf_json_gateway_method_handler (request_rec *r) { if (isXML) { output = jsonObjectToXML( res ); } else { - output = jsonObjectToJSON( res ); + //output = jsonObjectToJSON( res ); + output = jsonToStringFunc( res ); if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */ } ap_rputs(output, r); @@ -293,7 +340,8 @@ static int osrf_json_gateway_method_handler (request_rec *r) { bzero(bb, l); snprintf(bb, l, "%s : %s", statusname, statustext); jsonObject* tmp = jsonNewObject(bb); - char* j = jsonObjectToJSON(tmp); + char* j = jsonToStringFunc(tmp); + //char* j = jsonObjectToJSON(tmp); snprintf( buf, l, ",\"debug\": %s", j); free(j); jsonObjectFree(tmp); @@ -344,7 +392,7 @@ static void osrf_json_gateway_register_hooks (apr_pool_t *p) { module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = { STANDARD20_MODULE_STUFF, - NULL, + osrf_json_gateway_create_dir_config, NULL, osrf_json_gateway_create_config, NULL, -- 2.11.0