066378981efa2eb002d4e40a1ba315f4344933b7
[OpenSRF.git] / src / gateway / osrf_json_gateway.c
1 #include "apachetools.h"
2 #include "opensrf/osrf_app_session.h"
3 #include "opensrf/osrf_system.h"
4 #include "opensrf/osrfConfig.h"
5 #include <opensrf/osrf_json.h>
6 #include <opensrf/osrf_json_xml.h>
7 #include <opensrf/osrf_legacy_json.h>
8 #include <sys/time.h>
9 #include <sys/resource.h>
10 #include <unistd.h>
11 #include <strings.h>
12
13
14 #define MODULE_NAME "osrf_json_gateway_module"
15 #define GATEWAY_CONFIG "OSRFGatewayConfig"
16 #define DEFAULT_LOCALE "OSRFDefaultLocale"
17 #define CONFIG_CONTEXT "gateway"
18 #define JSON_PROTOCOL "OSRFGatewayLegacyJSON"
19 #define GATEWAY_USE_LEGACY_JSON 1
20
21 #define GATEWAY_DEFAULT_CONFIG "/openils/conf/opensrf_core.xml"
22
23
24 /* our config structure */
25 typedef struct { 
26         char* configfile;  /* our bootstrap config file */
27 } osrf_json_gateway_config;
28
29 typedef struct { 
30         int legacyJSON;
31 } osrf_json_gateway_dir_config;
32
33
34 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module;
35
36 char* osrf_json_default_locale = "en-US";
37 char* osrf_json_gateway_config_file = NULL;
38 int bootstrapped = 0;
39 int numserved = 0;
40 osrfStringArray* allowedServices = NULL;
41
42 static const char* osrf_json_gateway_set_default_locale(cmd_parms *parms, void *config, const char *arg) {
43         if (arg)
44                 osrf_json_default_locale = (char*) arg;
45         return NULL;
46 }
47
48 static const char* osrf_json_gateway_set_config(cmd_parms *parms, void *config, const char *arg) {
49         osrf_json_gateway_config  *cfg;
50         cfg = ap_get_module_config(parms->server->module_config, &osrf_json_gateway_module);
51         cfg->configfile = (char*) arg;
52         osrf_json_gateway_config_file = (char*) arg;
53         return NULL;
54 }
55
56 static const char* osrf_json_gateway_set_json_proto(cmd_parms *parms, void *config, const char *arg) {
57         osrf_json_gateway_dir_config* cfg = (osrf_json_gateway_dir_config*) config;
58         cfg->legacyJSON = (!strcasecmp((char*) arg, "false")) ? 0 : 1;
59         return NULL;
60 }
61
62 /* tell apache about our commands */
63 static const command_rec osrf_json_gateway_cmds[] = {
64         AP_INIT_TAKE1( GATEWAY_CONFIG, osrf_json_gateway_set_config, 
65                         NULL, RSRC_CONF, "osrf json gateway config file"),
66         AP_INIT_TAKE1( DEFAULT_LOCALE, osrf_json_gateway_set_default_locale, 
67                         NULL, RSRC_CONF, "osrf json gateway default locale"),
68         AP_INIT_TAKE1( JSON_PROTOCOL, osrf_json_gateway_set_json_proto,
69                         NULL, ACCESS_CONF, "osrf json gateway config file"),
70         {NULL}
71 };
72
73 /* build the config object */
74 static void* osrf_json_gateway_create_config( apr_pool_t* p, server_rec* s) {
75         osrf_json_gateway_config* cfg = (osrf_json_gateway_config*) 
76                         apr_palloc(p, sizeof(osrf_json_gateway_config));
77         cfg->configfile = GATEWAY_DEFAULT_CONFIG;
78         return (void*) cfg;
79 }
80
81 static void* osrf_json_gateway_create_dir_config( apr_pool_t* p, char* dir) {
82         osrf_json_gateway_dir_config* cfg = (osrf_json_gateway_dir_config*) 
83                         apr_palloc(p, sizeof(osrf_json_gateway_dir_config));
84         cfg->legacyJSON = GATEWAY_USE_LEGACY_JSON;
85         return (void*) cfg;
86 }
87
88
89 static void osrf_json_gateway_child_init(apr_pool_t *p, server_rec *s) {
90
91         char* cfg = osrf_json_gateway_config_file;
92         char buf[32];
93         memset(buf, 0x0, 32);
94         int t = time(NULL);
95         snprintf(buf, 32, "%d", t);
96
97         if( ! osrfSystemBootstrapClientResc( cfg, CONFIG_CONTEXT, buf ) ) {
98                 ap_log_error( APLOG_MARK, APLOG_ERR, 0, s, 
99                         "Unable to Bootstrap OpenSRF Client with config %s..", cfg);
100                 return;
101         }
102
103         bootstrapped = 1;
104         allowedServices = osrfNewStringArray(8);
105         osrfLogInfo(OSRF_LOG_MARK, "Bootstrapping gateway child for requests");
106         osrfConfigGetValueList( NULL, allowedServices, "/services/service" );
107
108         int i;
109         for( i = 0; i < allowedServices->size; i++ ) {
110                 ap_log_error( APLOG_MARK, APLOG_DEBUG, 0, s, 
111                         "allowed service: %s\n", osrfStringArrayGetString(allowedServices, i));
112         }
113 }
114
115 static int osrf_json_gateway_method_handler (request_rec *r) {
116
117         /* make sure we're needed first thing*/
118         if (strcmp(r->handler, MODULE_NAME )) return DECLINED;
119
120
121         osrf_json_gateway_dir_config* dir_conf =  
122                 ap_get_module_config(r->per_dir_config, &osrf_json_gateway_module);
123
124
125         /* provide 2 different JSON parsers and serializers to support legacy JSON */
126         jsonObject* (*parseJSONFunc) (char*) = legacy_jsonParseString;
127         char* (*jsonToStringFunc) (const jsonObject*) = legacy_jsonObjectToJSON;
128
129         if(dir_conf->legacyJSON) {
130                 ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r, "Using legacy JSON");
131
132         } else {
133                 ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r, "Not using legacy JSON");
134                 parseJSONFunc = jsonParseString;
135                 jsonToStringFunc = jsonObjectToJSON;
136         }
137
138
139         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: entered request handler");
140
141         /* verify we are connected */
142         if( !bootstrapped || !osrf_system_get_transport_client()) {
143                 ap_log_rerror( APLOG_MARK, APLOG_ERR, 0, r, "Cannot process request "
144                                 "because the OpenSRF JSON gateway has not been bootstrapped...");
145                 usleep( 100000 ); /* 100 milliseconds */
146                 exit(1);
147         }
148
149         osrfLogSetAppname("osrf_json_gw");
150
151         char* osrf_locale       = NULL;
152         char* param_locale      = NULL; /* locale for this call */
153         char* service           = NULL; /* service to connect to */
154         char* method            = NULL; /* method to perform */
155         char* format            = NULL; /* method to perform */
156         char* a_l               = NULL; /* request api level */
157         char* input_format      = NULL; /* POST data format, defaults to 'format' */
158         int   isXML             = 0;
159         int   api_level         = 1;
160
161         r->allowed |= (AP_METHOD_BIT << M_GET);
162         r->allowed |= (AP_METHOD_BIT << M_POST);
163
164         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: parsing URL params");
165         string_array* mparams   = NULL;
166         string_array* params    = apacheParseParms(r); /* free me */
167         param_locale            = apacheGetFirstParamValue( params, "locale" );
168         service                 = apacheGetFirstParamValue( params, "service" );
169         method                  = apacheGetFirstParamValue( params, "method" ); 
170         format                  = apacheGetFirstParamValue( params, "format" ); 
171         input_format            = apacheGetFirstParamValue( params, "input_format" ); 
172         a_l                     = apacheGetFirstParamValue( params, "api_level" ); 
173         mparams                 = apacheGetParamValues( params, "param" ); /* free me */
174
175         if(format == NULL)
176                 format = "json";
177         if(input_format == NULL)
178                 input_format = format;
179
180         /* set the user defined timeout value */
181         int timeout = 60;
182         char* tout = apacheGetFirstParamValue( params, "timeout" ); /* request timeout in seconds */
183         if( tout ) {
184                 timeout = atoi(tout);
185                 osrfLogDebug(OSRF_LOG_MARK, "Client supplied timeout of %d", timeout);
186         }
187
188         if (a_l)
189                 api_level = atoi(a_l);
190
191         if (!strcasecmp(format, "xml")) {
192                 isXML = 1;
193                 ap_set_content_type(r, "application/xml");
194         } else {
195                 ap_set_content_type(r, "text/plain");
196         }
197
198         int ret = OK;
199
200         /* ----------------------------------------------------------------- */
201         /* Grab the requested locale using the Accept-Language header*/
202
203
204         if ( !param_locale ) {
205                 if ( apr_table_get(r->headers_in, "X-OpenSRF-Language") ) {
206                         param_locale = strdup( apr_table_get(r->headers_in, "X-OpenSRF-Language") );
207                 } else if ( apr_table_get(r->headers_in, "Accept-Language") ) {
208                         param_locale = strdup( apr_table_get(r->headers_in, "Accept-Language") );
209                 }
210         }
211
212
213         if (param_locale) {
214                 growing_buffer* osrf_locale_buf = buffer_init(16);      
215                 if (index(param_locale, ',')) {
216                         int ind = index(param_locale, ',') - param_locale;
217                         int i;
218                         for ( i = 0; i < ind && i < 128; i++ )
219                                 buffer_add_char( osrf_locale_buf, param_locale[i] );
220                 } else {
221                         buffer_add( osrf_locale_buf, param_locale );
222                 }
223
224                 free(param_locale);
225                 osrf_locale = buffer_release( osrf_locale_buf );
226         } else {
227                 osrf_locale = strdup( osrf_json_default_locale );
228         }
229         /* ----------------------------------------------------------------- */
230
231
232         if(!(service && method) || 
233                 !osrfStringArrayContains(allowedServices, service)) {
234
235                 osrfLogError(OSRF_LOG_MARK, 
236                         "Service [%s] not found or not allowed", service);
237                 ret = HTTP_NOT_FOUND;
238
239         } else {
240
241                 /* This will log all heaers to the apache error log 
242                 const apr_array_header_t* arr = apr_table_elts(r->headers_in);
243                 const void* ptr;
244
245                 while( (ptr = apr_array_pop(arr)) ) {
246                         apr_table_entry_t* e = (apr_table_entry_t*) ptr;
247                         fprintf(stderr, "Table entry: %s : %s\n", e->key, e->val );
248                 }
249                 fflush(stderr);
250                 */
251
252                 osrfAppSession* session = osrf_app_client_session_init(service);
253                 osrf_app_session_set_locale(session, osrf_locale);
254
255                 double starttime = get_timestamp_millis();
256                 int req_id = -1;
257
258                 if(!strcasecmp(input_format, "json")) {
259                         jsonObject * arr = jsonNewObject(NULL);
260
261                         char* str;
262                         int i = 0;
263
264                         while( (str = osrfStringArrayGetString(mparams, i++)) ) 
265                                 jsonObjectPush(arr, parseJSONFunc(str));
266
267                         req_id = osrf_app_session_make_req( session, arr, method, api_level, NULL );
268                 } else {
269
270                         /**
271                         * If we receive XML method params, convert each param to a JSON object
272                         * and pass the array of JSON object params to the method */
273                         if(!strcasecmp(input_format, "xml")) {
274                                 jsonObject* jsonParams = jsonNewObject(NULL);
275
276                                 char* str;
277                                 int i = 0;
278                                 while( (str = osrfStringArrayGetString(mparams, i++)) ) {
279                                         jsonObjectPush(jsonParams, jsonXMLToJSONObject(str));
280                                 }
281
282                                 req_id = osrf_app_session_make_req( session, jsonParams, method, api_level, NULL );
283                                 jsonObjectFree(jsonParams);
284                         }
285                 }
286
287
288                 if( req_id == -1 ) {
289                         osrfLogError(OSRF_LOG_MARK, "I am unable to communcate with opensrf..going away...");
290                         /* we don't want to spawn an intense re-forking storm 
291                          * if there is no jabber server.. so give it some time before we die */
292                         usleep( 100000 ); /* 100 milliseconds */
293                         exit(1);
294                 }
295
296
297                 /* ----------------------------------------------------------------- */
298                 /* log all requests to the activity log */
299                 const char* authtoken = apr_table_get(r->headers_in, "X-OILS-Authtoken");
300                 if(!authtoken) authtoken = "";
301                 growing_buffer* act = buffer_init(128); 
302                 buffer_fadd(act, "[%s] [%s] %s %s", r->connection->remote_ip, authtoken, service, method );
303                 char* str; int i = 0;
304                 while( (str = osrfStringArrayGetString(mparams, i++)) ) {
305                         if( i == 1 ) {
306                                 OSRF_BUFFER_ADD(act, " ");
307                                 OSRF_BUFFER_ADD(act, str);
308                         } else {
309                                 OSRF_BUFFER_ADD(act, ", ");
310                                 OSRF_BUFFER_ADD(act, str);
311                         }
312                 }
313
314                 osrfLogActivity( OSRF_LOG_MARK, act->buf );
315                 buffer_free(act);
316                 /* ----------------------------------------------------------------- */
317
318
319                 osrf_message* omsg = NULL;
320
321                 int statuscode = 200;
322
323                 /* kick off the object */
324                 if (isXML)
325                         ap_rputs("<response xmlns=\"http://opensrf.org/-/namespaces/gateway/v1\"><payload>", r);
326                 else
327                         ap_rputs("{\"payload\":[", r);
328
329                 int morethan1           = 0;
330                 char* statusname        = NULL;
331                 char* statustext        = NULL;
332                 char* output            = NULL;
333
334                 while((omsg = osrfAppSessionRequestRecv( session, req_id, timeout ))) {
335         
336                         statuscode = omsg->status_code;
337                         jsonObject* res;        
338
339                         if( ( res = osrfMessageGetResult(omsg)) ) {
340
341                                 if (isXML) {
342                                         output = jsonObjectToXML( res );
343                                 } else {
344                                         output = jsonToStringFunc( res );
345                                         if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */
346                                 }
347                                 ap_rputs(output, r);
348                                 free(output);
349                                 morethan1 = 1;
350                 
351                         } else {
352         
353                                 if( statuscode > 299 ) { /* the request returned a low level error */
354                                         statusname = omsg->status_name ? strdup(omsg->status_name) : strdup("Unknown Error");
355                                         statustext = omsg->status_text ? strdup(omsg->status_text) : strdup("No Error Message");
356                                         osrfLogError( OSRF_LOG_MARK,  "Gateway received error: %s", statustext );
357                                 }
358                         }
359         
360                         osrf_message_free(omsg);
361                         if(statusname) break;
362                 }
363
364                 double duration = get_timestamp_millis() - starttime;
365                 osrfLogDebug(OSRF_LOG_MARK, "gateway request took %f seconds", duration);
366
367
368                 if (isXML)
369                         ap_rputs("</payload>", r);
370                 else
371                         ap_rputs("]",r); /* finish off the payload array */
372
373                 if(statusname) {
374
375                         /* add a debug field if the request died */
376                         ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r, 
377                                         "OpenSRF JSON Request returned error: %s -> %s", statusname, statustext );
378                         int l = strlen(statusname) + strlen(statustext) + 32;
379                         char buf[l];
380                         bzero(buf,l);
381
382                         if (isXML)
383                                 snprintf( buf, l, "<debug>\"%s : %s\"</debug>", statusname, statustext );
384
385                         else {
386                                 char bb[l];
387                                 bzero(bb, l);
388                                 snprintf(bb, l,  "%s : %s", statusname, statustext);
389                                 jsonObject* tmp = jsonNewObject(bb);
390                                 char* j = jsonToStringFunc(tmp);
391                                 snprintf( buf, l, ",\"debug\": %s", j);
392                                 free(j);
393                                 jsonObjectFree(tmp);
394                         }
395
396                         ap_rputs(buf, r);
397
398                         free(statusname);
399                         free(statustext);
400                 }
401
402                 /* insert the status code */
403                 char buf[32];
404                 bzero(buf,32);
405
406                 if (isXML)
407                         snprintf(buf, 32, "<status>%d</status>", statuscode );
408                 else
409                         snprintf(buf, 32, ",\"status\":%d", statuscode );
410
411                 ap_rputs( buf, r );
412
413                 if (isXML)
414                         ap_rputs("</response>", r);
415                 else
416                         ap_rputs( "}", r ); /* finish off the object */
417
418                 osrf_app_session_destroy(session);
419         }
420
421         osrfLogInfo(OSRF_LOG_MARK, "Completed processing service=%s, method=%s", service, method);
422         string_array_destroy(params);
423         string_array_destroy(mparams);
424
425         osrfLogDebug(OSRF_LOG_MARK, "Gateway served %d requests", ++numserved);
426         osrfLogClearXid();
427
428         return ret;
429 }
430
431
432
433 static void osrf_json_gateway_register_hooks (apr_pool_t *p) {
434         ap_hook_handler(osrf_json_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
435         ap_hook_child_init(osrf_json_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
436 }
437
438
439 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
440         STANDARD20_MODULE_STUFF,
441         osrf_json_gateway_create_dir_config,
442         NULL,
443         osrf_json_gateway_create_config,
444         NULL,
445         osrf_json_gateway_cmds,
446         osrf_json_gateway_register_hooks,
447 };
448
449
450
451