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>
11 #define MODULE_NAME "osrf_json_gateway_module"
12 #define GATEWAY_CONFIG "OSRFGatewayConfig"
13 #define CONFIG_CONTEXT "gateway"
15 #define GATEWAY_DEFAULT_CONFIG "/openils/conf/opensrf_core.xml"
18 /* our config structure */
20 char* configfile; /* our bootstrap config file */
21 } osrf_json_gateway_config;
23 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module;
25 char* osrf_json_gateway_config_file = NULL;
28 osrfStringArray* allowedServices = NULL;
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;
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"),
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;
54 static void osrf_json_gateway_child_init(apr_pool_t *p, server_rec *s) {
56 char* cfg = osrf_json_gateway_config_file;
60 snprintf(buf, 32, "%d", t);
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);
69 allowedServices = osrfNewStringArray(8);
70 osrfLogInfo(OSRF_LOG_MARK, "Bootstrapping gateway child for requests");
71 osrfConfigGetValueList( NULL, allowedServices, "/services/service" );
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));
80 static int osrf_json_gateway_method_handler (request_rec *r) {
82 /* make sure we're needed first thing*/
83 if (strcmp(r->handler, MODULE_NAME )) return DECLINED;
85 osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: entered request handler");
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;
94 osrfLogSetAppname("osrf_json_gw");
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 */
103 r->allowed |= (AP_METHOD_BIT << M_GET);
104 r->allowed |= (AP_METHOD_BIT << M_POST);
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 */
116 api_level = atoi(a_l);
118 if (format && !strcasecmp(format, "xml" )) {
120 ap_set_content_type(r, "application/xml");
122 ap_set_content_type(r, "text/plain");
127 if(!(service && method) ||
128 !osrfStringArrayContains(allowedServices, service)) {
130 osrfLogError(OSRF_LOG_MARK,
131 "Service [%s] not found or not allowed", service);
132 ret = HTTP_NOT_FOUND;
136 /* This will log all heaers to the apache error log
137 const apr_array_header_t* arr = apr_table_elts(r->headers_in);
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 );
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);
159 osrfLogActivity( OSRF_LOG_MARK, act->buf );
161 /* ----------------------------------------------------------------- */
163 osrfAppSession* session = osrf_app_client_session_init(service);
165 double starttime = get_timestamp_millis();
166 int req_id = osrf_app_session_make_req( session, NULL, method, api_level, mparams );
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 */
177 osrf_message* omsg = NULL;
179 int statuscode = 200;
181 /* kick off the object */
183 ap_rputs("<response xmlns=\"http://opensrf.org/-/namespaces/gateway/v1\"><payload>", r);
185 ap_rputs("{\"payload\":[", r);
188 char* statusname = NULL;
189 char* statustext = NULL;
192 while((omsg = osrfAppSessionRequestRecv( session, req_id, 60 ))) {
194 statuscode = omsg->status_code;
197 if( ( res = osrfMessageGetResult(omsg)) ) {
200 output = jsonObjectToXML( res );
202 output = jsonObjectToJSON( res );
203 if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */
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 );
218 osrf_message_free(omsg);
219 if(statusname) break;
222 double duration = get_timestamp_millis() - starttime;
223 osrfLogDebug(OSRF_LOG_MARK, "gateway request took %lf seconds", duration);
227 ap_rputs("</payload>", r);
229 ap_rputs("]",r); /* finish off the payload array */
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;
241 snprintf( buf, l, "<debug>\"%s : %s\"</debug>", statusname, statustext );
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);
260 /* insert the status code */
265 snprintf(buf, 32, "<status>%d</status>", statuscode );
267 snprintf(buf, 32, ",\"status\":%d", statuscode );
272 ap_rputs("</response>", r);
274 ap_rputs( "}", r ); /* finish off the object */
276 osrf_app_session_destroy(session);
279 osrfLogInfo(OSRF_LOG_MARK, "Completed processing service=%s, method=%s", service, method);
280 string_array_destroy(params);
281 string_array_destroy(mparams);
283 osrfLogDebug(OSRF_LOG_MARK, "Gateway served %d requests", ++numserved);
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);
296 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
297 STANDARD20_MODULE_STUFF,
300 osrf_json_gateway_create_config,
302 osrf_json_gateway_cmds,
303 osrf_json_gateway_register_hooks,