1 #include "apachetools.h"
2 #include "opensrf/osrf_app_session.h"
3 #include "opensrf/osrf_system.h"
4 #include "objson/object.h"
5 #include "objson/json2xml.h"
7 #define MODULE_NAME "osrf_json_gateway_module"
8 #define GATEWAY_CONFIG "OSRFGatewayConfig"
9 #define CONFIG_CONTEXT "gateway"
11 #define GATEWAY_DEFAULT_CONFIG "/openils/conf/opensrf_core.xml"
14 /* our config structure */
16 char* configfile; /* our bootstrap config file */
17 } osrf_json_gateway_config;
19 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module;
21 char* osrf_json_gateway_config_file = NULL;
24 static const char* osrf_json_gateway_set_config(cmd_parms *parms, void *config, const char *arg) {
25 osrf_json_gateway_config *cfg;
26 cfg = ap_get_module_config(parms->server->module_config, &osrf_json_gateway_module);
27 cfg->configfile = (char*) arg;
28 osrf_json_gateway_config_file = (char*) arg;
32 /* tell apache about our commands */
33 static const command_rec osrf_json_gateway_cmds[] = {
34 AP_INIT_TAKE1( GATEWAY_CONFIG, osrf_json_gateway_set_config,
35 NULL, RSRC_CONF, "osrf json gateway config file"),
39 /* build the config object */
40 static void* osrf_json_gateway_create_config( apr_pool_t* p, server_rec* s) {
41 osrf_json_gateway_config* cfg = (osrf_json_gateway_config*)
42 apr_palloc(p, sizeof(osrf_json_gateway_config));
43 cfg->configfile = GATEWAY_DEFAULT_CONFIG;
48 static void osrf_json_gateway_child_init(apr_pool_t *p, server_rec *s) {
50 char* cfg = osrf_json_gateway_config_file;
51 if( ! osrf_system_bootstrap_client( cfg, CONFIG_CONTEXT) ) {
52 ap_log_error( APLOG_MARK, APLOG_ERR, 0, s, "Unable to Bootstrap OpenSRF Client..");
56 ap_log_error( APLOG_MARK, APLOG_DEBUG, 0, s, "Bootstrapping OpenSRF Client..");
59 static int osrf_json_gateway_method_handler (request_rec *r) {
61 /* make sure we're needed first thing*/
62 if (strcmp(r->handler, MODULE_NAME )) return DECLINED;
64 /* verify we are connected */
65 if( !bootstrapped || !osrf_system_get_transport_client()) {
66 ap_log_rerror( APLOG_MARK, APLOG_ERR, 0, r, "Cannot process request "
67 "because the OpenSRF JSON gateway has not been bootstrapped...");
68 return HTTP_INTERNAL_SERVER_ERROR;
72 osrfLogSetAppname("osrf_json_gw");
74 char* service = NULL; /* service to connect to */
75 char* method = NULL; /* method to perform */
76 char* format = NULL; /* method to perform */
77 char* a_l = NULL; /* request api level */
81 r->allowed |= (AP_METHOD_BIT << M_GET);
82 r->allowed |= (AP_METHOD_BIT << M_POST);
84 string_array* mparams = NULL;
85 string_array* params = apacheParseParms(r); /* free me */
86 service = apacheGetFirstParamValue( params, "service" );
87 method = apacheGetFirstParamValue( params, "method" );
88 format = apacheGetFirstParamValue( params, "format" );
89 a_l = apacheGetFirstParamValue( params, "api_level" );
90 mparams = apacheGetParamValues( params, "param" ); /* free me */
93 api_level = atoi(a_l);
95 if (format && !strcasecmp(format, "xml" )) {
97 ap_set_content_type(r, "application/xml");
99 ap_set_content_type(r, "text/plain");
103 if( service && method ) {
105 osrfLogInfo( OSRF_LOG_MARK, "service=%s, method=%s", service, method );
106 osrfAppSession* session = osrf_app_client_session_init(service);
107 int req_id = osrf_app_session_make_req( session, NULL, method, api_level, mparams );
108 osrf_message* omsg = NULL;
110 int statuscode = 200;
112 /* kick off the object */
114 ap_rputs("<response xmlns=\"http://opensrf.org/-/namespaces/gateway/v1\"><payload>", r);
116 ap_rputs("{\"payload\":[", r);
119 char* statusname = NULL;
120 char* statustext = NULL;
123 while((omsg = osrfAppSessionRequestRecv( session, req_id, 60 ))) {
125 statuscode = omsg->status_code;
128 if( ( res = osrfMessageGetResult(omsg)) ) {
131 output = jsonObjectToXML( res );
133 output = jsonObjectToJSON( res );
134 if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */
142 if( statuscode > 299 ) { /* the request returned a low level error */
143 statusname = omsg->status_name ? strdup(omsg->status_name) : strdup("Unknown Error");
144 statustext = omsg->status_text ? strdup(omsg->status_text) : strdup("No Error Message");
145 osrfLogError( OSRF_LOG_MARK, "Gateway received error: %s", statustext );
149 osrf_message_free(omsg);
150 if(statusname) break;
154 ap_rputs("</payload>", r);
156 ap_rputs("]",r); /* finish off the payload array */
160 /* add a debug field if the request died */
161 ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r,
162 "OpenSRF JSON Request returned error: %s -> %s", statusname, statustext );
163 int l = strlen(statusname) + strlen(statustext) + 32;
168 snprintf( buf, l, "<debug>\"%s : %s\"</debug>", statusname, statustext );
172 snprintf(bb, l, "%s : %s", statusname, statustext);
173 jsonObject* tmp = jsonNewObject(bb);
174 char* j = jsonObjectToJSON(tmp);
175 snprintf( buf, l, ",\"debug\": %s", j);
186 /* insert the status code */
191 snprintf(buf, 32, "<status>%d</status>", statuscode );
193 snprintf(buf, 32, ",\"status\":%d", statuscode );
198 ap_rputs("</response>", r);
200 ap_rputs( "}", r ); /* finish off the object */
202 osrf_app_session_destroy(session);
205 string_array_destroy(params);
206 string_array_destroy(mparams);
211 static void osrf_json_gateway_register_hooks (apr_pool_t *p) {
212 ap_hook_handler(osrf_json_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
213 ap_hook_child_init(osrf_json_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
217 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
218 STANDARD20_MODULE_STUFF,
221 osrf_json_gateway_create_config,
223 osrf_json_gateway_cmds,
224 osrf_json_gateway_register_hooks,