]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/gateway/osrf_json_gateway.c
doing better job of propogating network errors up the stack
[Evergreen.git] / OpenSRF / 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 "objson/object.h"
6 #include "objson/json2xml.h"
7 #include <sys/time.h>
8 #include <sys/resource.h>
9
10
11 #define MODULE_NAME "osrf_json_gateway_module"
12 #define GATEWAY_CONFIG "OSRFGatewayConfig"
13 #define CONFIG_CONTEXT "gateway"
14
15 #define GATEWAY_DEFAULT_CONFIG "/openils/conf/opensrf_core.xml"
16
17
18 /* our config structure */
19 typedef struct { 
20         char* configfile;  /* our bootstrap config file */
21 } osrf_json_gateway_config;
22
23 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module;
24
25 char* osrf_json_gateway_config_file = NULL;
26 int bootstrapped = 0;
27 int numserved = 0;
28 osrfStringArray* allowedServices = NULL;
29
30 static const char* osrf_json_gateway_set_config(cmd_parms *parms, void *config, const char *arg) {
31         osrf_json_gateway_config  *cfg;
32         cfg = ap_get_module_config(parms->server->module_config, &osrf_json_gateway_module);
33         cfg->configfile = (char*) arg;
34         osrf_json_gateway_config_file = (char*) arg;
35         return NULL;
36 }
37
38 /* tell apache about our commands */
39 static const command_rec osrf_json_gateway_cmds[] = {
40         AP_INIT_TAKE1( GATEWAY_CONFIG, osrf_json_gateway_set_config, 
41                         NULL, RSRC_CONF, "osrf json gateway config file"),
42         {NULL}
43 };
44
45 /* build the config object */
46 static void* osrf_json_gateway_create_config( apr_pool_t* p, server_rec* s) {
47         osrf_json_gateway_config* cfg = (osrf_json_gateway_config*) 
48                         apr_palloc(p, sizeof(osrf_json_gateway_config));
49         cfg->configfile = GATEWAY_DEFAULT_CONFIG;
50         return (void*) cfg;
51 }
52
53
54 static void osrf_json_gateway_child_init(apr_pool_t *p, server_rec *s) {
55
56         char* cfg = osrf_json_gateway_config_file;
57         char buf[32];
58         memset(buf, 0x0, 32);
59         int t = time(NULL);
60         snprintf(buf, 32, "%d", t);
61
62         if( ! osrfSystemBootstrapClientResc( cfg, CONFIG_CONTEXT, buf ) ) {
63                 ap_log_error( APLOG_MARK, APLOG_ERR, 0, s, 
64                         "Unable to Bootstrap OpenSRF Client with config %s..", cfg);
65                 return;
66         }
67
68         bootstrapped = 1;
69         allowedServices = osrfNewStringArray(8);
70         osrfLogInfo(OSRF_LOG_MARK, "Bootstrapping gateway child for requests");
71         osrfConfigGetValueList( NULL, allowedServices, "/services/service" );
72
73         int i;
74         for( i = 0; i < allowedServices->size; i++ ) {
75                 ap_log_error( APLOG_MARK, APLOG_DEBUG, 0, s, 
76                         "allowed service: %s\n", osrfStringArrayGetString(allowedServices, i));
77         }
78 }
79
80 static int osrf_json_gateway_method_handler (request_rec *r) {
81
82         /* make sure we're needed first thing*/
83         if (strcmp(r->handler, MODULE_NAME )) return DECLINED;
84
85         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: entered request handler");
86
87         /* verify we are connected */
88         if( !bootstrapped || !osrf_system_get_transport_client()) {
89                 ap_log_rerror( APLOG_MARK, APLOG_ERR, 0, r, "Cannot process request "
90                                 "because the OpenSRF JSON gateway has not been bootstrapped...");
91                 return HTTP_INTERNAL_SERVER_ERROR;
92         }
93
94         osrfLogSetAppname("osrf_json_gw");
95
96         char* service           = NULL; /* service to connect to */
97         char* method            = NULL; /* method to perform */
98         char* format            = NULL; /* method to perform */
99         char* a_l                       = NULL; /* request api level */
100         int   isXML                     = 0;
101         int   api_level = 1;
102
103         r->allowed |= (AP_METHOD_BIT << M_GET);
104         r->allowed |= (AP_METHOD_BIT << M_POST);
105
106         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: parsing URL params");
107         string_array* mparams   = NULL;
108         string_array* params            = apacheParseParms(r); /* free me */
109         service         = apacheGetFirstParamValue( params, "service" );
110         method          = apacheGetFirstParamValue( params, "method" ); 
111         format          = apacheGetFirstParamValue( params, "format" ); 
112         a_l                     = apacheGetFirstParamValue( params, "api_level" ); 
113         mparams         = apacheGetParamValues( params, "param" ); /* free me */
114
115         if (a_l)
116                 api_level = atoi(a_l);
117
118         if (format && !strcasecmp(format, "xml" )) {
119                 isXML = 1;
120                 ap_set_content_type(r, "application/xml");
121         } else {
122                 ap_set_content_type(r, "text/plain");
123         }
124
125         int ret = OK;
126
127         if(!(service && method) || 
128                 !osrfStringArrayContains(allowedServices, service)) {
129
130                 osrfLogError(OSRF_LOG_MARK, 
131                         "Service [%s] not found or not allowed", service);
132                 ret = HTTP_NOT_FOUND;
133
134         } else {
135
136                 /* This will log all heaers to the apache error log 
137                 const apr_array_header_t* arr = apr_table_elts(r->headers_in);
138                 const void* ptr;
139
140                 while( (ptr = apr_array_pop(arr)) ) {
141                         apr_table_entry_t* e = (apr_table_entry_t*) ptr;
142                         fprintf(stderr, "Table entry: %s : %s\n", e->key, e->val );
143                 }
144                 fflush(stderr);
145                 */
146
147
148                 /* ----------------------------------------------------------------- */
149                 /* log all requests to the activity log */
150                 const char* authtoken = apr_table_get(r->headers_in, "X-OILS-Authtoken");
151                 if(!authtoken) authtoken = "";
152                 growing_buffer* act = buffer_init(128); 
153                 buffer_fadd(act, "[%s] [%s] %s %s", r->connection->remote_ip, authtoken, service, method );
154                 char* str; int i = 0;
155                 while( (str = osrfStringArrayGetString(mparams, i++)) ) 
156                         if( i == 1 ) buffer_fadd(act, " %s", str);
157                         else buffer_fadd(act, ", %s", str);
158
159                 osrfLogActivity( OSRF_LOG_MARK, act->buf );
160                 buffer_free(act);
161                 /* ----------------------------------------------------------------- */
162
163                 osrfAppSession* session = osrf_app_client_session_init(service);
164
165                 double starttime = get_timestamp_millis();
166                 int req_id = osrf_app_session_make_req( session, NULL, method, api_level, mparams );
167
168
169                 if( req_id == -1 ) {
170                         osrfLogError(OSRF_LOG_MARK, "I am unable to communcate with opensrf..going away...");
171                         /* we don't want to spawn an intense re-forking storm 
172                          * if there is no jabber server.. so give it some time before we die */
173                         sleep(1); 
174                         exit(1);
175                 }
176
177                 osrf_message* omsg = NULL;
178
179                 int statuscode = 200;
180
181                 /* kick off the object */
182                 if (isXML)
183                         ap_rputs("<response xmlns=\"http://opensrf.org/-/namespaces/gateway/v1\"><payload>", r);
184                 else
185                         ap_rputs("{\"payload\":[", r);
186
187                 int morethan1           = 0;
188                 char* statusname        = NULL;
189                 char* statustext        = NULL;
190                 char* output            = NULL;
191
192                 while((omsg = osrfAppSessionRequestRecv( session, req_id, 60 ))) {
193         
194                         statuscode = omsg->status_code;
195                         jsonObject* res;        
196
197                         if( ( res = osrfMessageGetResult(omsg)) ) {
198
199                                 if (isXML) {
200                                         output = jsonObjectToXML( res );
201                                 } else {
202                                         output = jsonObjectToJSON( res );
203                                         if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */
204                                 }
205                                 ap_rputs(output, r);
206                                 free(output);
207                                 morethan1 = 1;
208                 
209                         } else {
210         
211                                 if( statuscode > 299 ) { /* the request returned a low level error */
212                                         statusname = omsg->status_name ? strdup(omsg->status_name) : strdup("Unknown Error");
213                                         statustext = omsg->status_text ? strdup(omsg->status_text) : strdup("No Error Message");
214                                         osrfLogError( OSRF_LOG_MARK,  "Gateway received error: %s", statustext );
215                                 }
216                         }
217         
218                         osrf_message_free(omsg);
219                         if(statusname) break;
220                 }
221
222                 double duration = get_timestamp_millis() - starttime;
223                 osrfLogDebug(OSRF_LOG_MARK, "gateway request took %lf seconds", duration);
224
225
226                 if (isXML)
227                         ap_rputs("</payload>", r);
228                 else
229                         ap_rputs("]",r); /* finish off the payload array */
230
231                 if(statusname) {
232
233                         /* add a debug field if the request died */
234                         ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r, 
235                                         "OpenSRF JSON Request returned error: %s -> %s", statusname, statustext );
236                         int l = strlen(statusname) + strlen(statustext) + 32;
237                         char buf[l];
238                         bzero(buf,l);
239
240                         if (isXML)
241                                 snprintf( buf, l, "<debug>\"%s : %s\"</debug>", statusname, statustext );
242
243                         else {
244                                 char bb[l];
245                                 bzero(bb, l);
246                                 snprintf(bb, l,  "%s : %s", statusname, statustext);
247                                 jsonObject* tmp = jsonNewObject(bb);
248                                 char* j = jsonObjectToJSON(tmp);
249                                 snprintf( buf, l, ",\"debug\": %s", j);
250                                 free(j);
251                                 jsonObjectFree(tmp);
252                         }
253
254                         ap_rputs(buf, r);
255
256                         free(statusname);
257                         free(statustext);
258                 }
259
260                 /* insert the status code */
261                 char buf[32];
262                 bzero(buf,32);
263
264                 if (isXML)
265                         snprintf(buf, 32, "<status>%d</status>", statuscode );
266                 else
267                         snprintf(buf, 32, ",\"status\":%d", statuscode );
268
269                 ap_rputs( buf, r );
270
271                 if (isXML)
272                         ap_rputs("</response>", r);
273                 else
274                         ap_rputs( "}", r ); /* finish off the object */
275
276                 osrf_app_session_destroy(session);
277         }
278
279         osrfLogInfo(OSRF_LOG_MARK, "Completed processing service=%s, method=%s", service, method);
280         string_array_destroy(params);
281         string_array_destroy(mparams);
282
283         osrfLogDebug(OSRF_LOG_MARK, "Gateway served %d requests", ++numserved);
284
285         return ret;
286 }
287
288
289
290 static void osrf_json_gateway_register_hooks (apr_pool_t *p) {
291         ap_hook_handler(osrf_json_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
292         ap_hook_child_init(osrf_json_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
293 }
294
295
296 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
297         STANDARD20_MODULE_STUFF,
298         NULL,
299         NULL,
300         osrf_json_gateway_create_config,
301         NULL,
302         osrf_json_gateway_cmds,
303         osrf_json_gateway_register_hooks,
304 };
305
306
307
308