[Opensrf-commits] r1008 - branches/new-json2/src/gateway

svn at svn.open-ils.org svn at svn.open-ils.org
Fri Jul 6 17:00:11 EDT 2007


Author: erickson
Date: 2007-07-06 16:56:08 -0400 (Fri, 06 Jul 2007)
New Revision: 1008

Modified:
   branches/new-json2/src/gateway/osrf_json_gateway.c
Log:
added compatibility layer to gateway for legacy json

Modified: branches/new-json2/src/gateway/osrf_json_gateway.c
===================================================================
--- branches/new-json2/src/gateway/osrf_json_gateway.c	2007-07-06 20:36:37 UTC (rev 1007)
+++ branches/new-json2/src/gateway/osrf_json_gateway.c	2007-07-06 20:56:08 UTC (rev 1008)
@@ -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 @@
 	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 @@
 	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,7 +67,14 @@
 	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) {
 
 	char* cfg = osrf_json_gateway_config_file;
@@ -84,6 +106,24 @@
 	/* 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,8 +209,14 @@
 		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 @@
 				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 @@
 				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,8 +392,8 @@
 
 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
 	STANDARD20_MODULE_STUFF,
+	osrf_json_gateway_create_dir_config,
 	NULL,
-	NULL,
 	osrf_json_gateway_create_config,
 	NULL,
 	osrf_json_gateway_cmds,



More information about the opensrf-commits mailing list