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"
8 #include <sys/resource.h>
12 #define MODULE_NAME "osrf_json_gateway_module"
13 #define GATEWAY_CONFIG "OSRFGatewayConfig"
14 #define CONFIG_CONTEXT "gateway"
16 #define GATEWAY_DEFAULT_CONFIG "/openils/conf/opensrf_core.xml"
19 /* our config structure */
21 char* configfile; /* our bootstrap config file */
22 } osrf_json_gateway_config;
24 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module;
26 char* osrf_json_gateway_config_file = NULL;
29 osrfStringArray* allowedServices = NULL;
31 static const char* osrf_json_gateway_set_config(cmd_parms *parms, void *config, const char *arg) {
32 osrf_json_gateway_config *cfg;
33 cfg = ap_get_module_config(parms->server->module_config, &osrf_json_gateway_module);
34 cfg->configfile = (char*) arg;
35 osrf_json_gateway_config_file = (char*) arg;
39 /* tell apache about our commands */
40 static const command_rec osrf_json_gateway_cmds[] = {
41 AP_INIT_TAKE1( GATEWAY_CONFIG, osrf_json_gateway_set_config,
42 NULL, RSRC_CONF, "osrf json gateway config file"),
46 /* build the config object */
47 static void* osrf_json_gateway_create_config( apr_pool_t* p, server_rec* s) {
48 osrf_json_gateway_config* cfg = (osrf_json_gateway_config*)
49 apr_palloc(p, sizeof(osrf_json_gateway_config));
50 cfg->configfile = GATEWAY_DEFAULT_CONFIG;
55 static void osrf_json_gateway_child_init(apr_pool_t *p, server_rec *s) {
57 char* cfg = osrf_json_gateway_config_file;
61 snprintf(buf, 32, "%d", t);
63 if( ! osrfSystemBootstrapClientResc( cfg, CONFIG_CONTEXT, buf ) ) {
64 ap_log_error( APLOG_MARK, APLOG_ERR, 0, s,
65 "Unable to Bootstrap OpenSRF Client with config %s..", cfg);
70 allowedServices = osrfNewStringArray(8);
71 osrfLogInfo(OSRF_LOG_MARK, "Bootstrapping gateway child for requests");
72 osrfConfigGetValueList( NULL, allowedServices, "/services/service" );
75 for( i = 0; i < allowedServices->size; i++ ) {
76 ap_log_error( APLOG_MARK, APLOG_DEBUG, 0, s,
77 "allowed service: %s\n", osrfStringArrayGetString(allowedServices, i));
81 static int osrf_json_gateway_method_handler (request_rec *r) {
83 /* make sure we're needed first thing*/
84 if (strcmp(r->handler, MODULE_NAME )) return DECLINED;
86 osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: entered request handler");
88 /* verify we are connected */
89 if( !bootstrapped || !osrf_system_get_transport_client()) {
90 ap_log_rerror( APLOG_MARK, APLOG_ERR, 0, r, "Cannot process request "
91 "because the OpenSRF JSON gateway has not been bootstrapped...");
92 usleep( 100000 ); /* 100 milliseconds */
96 osrfLogSetAppname("osrf_json_gw");
98 char* service = NULL; /* service to connect to */
99 char* method = NULL; /* method to perform */
100 char* format = NULL; /* method to perform */
101 char* a_l = NULL; /* request api level */
105 r->allowed |= (AP_METHOD_BIT << M_GET);
106 r->allowed |= (AP_METHOD_BIT << M_POST);
108 osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: parsing URL params");
109 string_array* mparams = NULL;
110 string_array* params = apacheParseParms(r); /* free me */
111 service = apacheGetFirstParamValue( params, "service" );
112 method = apacheGetFirstParamValue( params, "method" );
113 format = apacheGetFirstParamValue( params, "format" );
114 a_l = apacheGetFirstParamValue( params, "api_level" );
115 mparams = apacheGetParamValues( params, "param" ); /* free me */
118 api_level = atoi(a_l);
120 if (format && !strcasecmp(format, "xml" )) {
122 ap_set_content_type(r, "application/xml");
124 ap_set_content_type(r, "text/plain");
129 if(!(service && method) ||
130 !osrfStringArrayContains(allowedServices, service)) {
132 osrfLogError(OSRF_LOG_MARK,
133 "Service [%s] not found or not allowed", service);
134 ret = HTTP_NOT_FOUND;
138 /* This will log all heaers to the apache error log
139 const apr_array_header_t* arr = apr_table_elts(r->headers_in);
142 while( (ptr = apr_array_pop(arr)) ) {
143 apr_table_entry_t* e = (apr_table_entry_t*) ptr;
144 fprintf(stderr, "Table entry: %s : %s\n", e->key, e->val );
150 /* ----------------------------------------------------------------- */
151 /* log all requests to the activity log */
152 const char* authtoken = apr_table_get(r->headers_in, "X-OILS-Authtoken");
153 if(!authtoken) authtoken = "";
154 growing_buffer* act = buffer_init(128);
155 buffer_fadd(act, "[%s] [%s] %s %s", r->connection->remote_ip, authtoken, service, method );
156 char* str; int i = 0;
157 while( (str = osrfStringArrayGetString(mparams, i++)) )
158 if( i == 1 ) buffer_fadd(act, " %s", str);
159 else buffer_fadd(act, ", %s", str);
161 osrfLogActivity( OSRF_LOG_MARK, act->buf );
163 /* ----------------------------------------------------------------- */
165 osrfAppSession* session = osrf_app_client_session_init(service);
167 double starttime = get_timestamp_millis();
168 int req_id = osrf_app_session_make_req( session, NULL, method, api_level, mparams );
172 osrfLogError(OSRF_LOG_MARK, "I am unable to communcate with opensrf..going away...");
173 /* we don't want to spawn an intense re-forking storm
174 * if there is no jabber server.. so give it some time before we die */
175 usleep( 100000 ); /* 100 milliseconds */
179 osrf_message* omsg = NULL;
181 int statuscode = 200;
183 /* kick off the object */
185 ap_rputs("<response xmlns=\"http://opensrf.org/-/namespaces/gateway/v1\"><payload>", r);
187 ap_rputs("{\"payload\":[", r);
190 char* statusname = NULL;
191 char* statustext = NULL;
194 while((omsg = osrfAppSessionRequestRecv( session, req_id, 60 ))) {
196 statuscode = omsg->status_code;
199 if( ( res = osrfMessageGetResult(omsg)) ) {
202 output = jsonObjectToXML( res );
204 output = jsonObjectToJSON( res );
205 if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */
213 if( statuscode > 299 ) { /* the request returned a low level error */
214 statusname = omsg->status_name ? strdup(omsg->status_name) : strdup("Unknown Error");
215 statustext = omsg->status_text ? strdup(omsg->status_text) : strdup("No Error Message");
216 osrfLogError( OSRF_LOG_MARK, "Gateway received error: %s", statustext );
220 osrf_message_free(omsg);
221 if(statusname) break;
224 double duration = get_timestamp_millis() - starttime;
225 osrfLogDebug(OSRF_LOG_MARK, "gateway request took %lf seconds", duration);
229 ap_rputs("</payload>", r);
231 ap_rputs("]",r); /* finish off the payload array */
235 /* add a debug field if the request died */
236 ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r,
237 "OpenSRF JSON Request returned error: %s -> %s", statusname, statustext );
238 int l = strlen(statusname) + strlen(statustext) + 32;
243 snprintf( buf, l, "<debug>\"%s : %s\"</debug>", statusname, statustext );
248 snprintf(bb, l, "%s : %s", statusname, statustext);
249 jsonObject* tmp = jsonNewObject(bb);
250 char* j = jsonObjectToJSON(tmp);
251 snprintf( buf, l, ",\"debug\": %s", j);
262 /* insert the status code */
267 snprintf(buf, 32, "<status>%d</status>", statuscode );
269 snprintf(buf, 32, ",\"status\":%d", statuscode );
274 ap_rputs("</response>", r);
276 ap_rputs( "}", r ); /* finish off the object */
278 osrf_app_session_destroy(session);
281 osrfLogInfo(OSRF_LOG_MARK, "Completed processing service=%s, method=%s", service, method);
282 string_array_destroy(params);
283 string_array_destroy(mparams);
285 osrfLogDebug(OSRF_LOG_MARK, "Gateway served %d requests", ++numserved);
292 static void osrf_json_gateway_register_hooks (apr_pool_t *p) {
293 ap_hook_handler(osrf_json_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
294 ap_hook_child_init(osrf_json_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
298 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
299 STANDARD20_MODULE_STUFF,
302 osrf_json_gateway_create_config,
304 osrf_json_gateway_cmds,
305 osrf_json_gateway_register_hooks,