]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/gateway/osrf_json_gateway.c
adding xml format support to the gateway
[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 "objson/object.h"
5 #include "objson/json2xml.h"
6
7 #define MODULE_NAME "osrf_json_gateway_module"
8 #define GATEWAY_CONFIG "OSRFGatewayConfig"
9 #define CONFIG_CONTEXT "gateway"
10
11 #define GATEWAY_DEFAULT_CONFIG "/openils/conf/opensrf_core.xml"
12
13
14 /* our config structure */
15 typedef struct { 
16         char* configfile;  /* our bootstrap config file */
17 } osrf_json_gateway_config;
18
19 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module;
20
21 char* osrf_json_gateway_config_file = NULL;
22 int bootstrapped = 0;
23
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;
29         return NULL;
30 }
31
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"),
36         {NULL}
37 };
38
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;
44         return (void*) cfg;
45 }
46
47
48 static void osrf_json_gateway_child_init(apr_pool_t *p, server_rec *s) {
49
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..");
53                 return;
54         }
55         bootstrapped = 1;
56         ap_log_error( APLOG_MARK, APLOG_DEBUG, 0, s, "Bootstrapping OpenSRF Client..");
57 }
58
59 static int osrf_json_gateway_method_handler (request_rec *r) {
60
61         /* make sure we're needed first thing*/
62         if (strcmp(r->handler, MODULE_NAME )) return DECLINED;
63
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;
69         }
70
71
72         osrfLogSetAppname("osrf_json_gw");
73
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 */
78         int   isXML                             = 0;
79         int   api_level                         = 1;
80
81         r->allowed |= (AP_METHOD_BIT << M_GET);
82         r->allowed |= (AP_METHOD_BIT << M_POST);
83
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 */
91
92         if (a_l)
93                 api_level = atoi(a_l);
94
95         if (format && !strcasecmp(format, "xml" )) {
96                 isXML = 1;
97                 ap_set_content_type(r, "application/xml");
98         } else {
99                 ap_set_content_type(r, "text/plain");
100         }
101
102
103         if( service && method ) {
104
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;
109
110                 int statuscode = 200;
111
112                 /* kick off the object */
113                 if (isXML)
114                         ap_rputs("<response xmlns=\"http://opensrf.org/-/namespaces/gateway/v1\"><payload>", r);
115                 else
116                         ap_rputs("{\"payload\":[", r);
117
118                 int morethan1           = 0;
119                 char* statusname        = NULL;
120                 char* statustext        = NULL;
121                 char* output            = NULL;
122
123                 while((omsg = osrfAppSessionRequestRecv( session, req_id, 60 ))) {
124         
125                         statuscode = omsg->status_code;
126                         jsonObject* res;        
127
128                         if( ( res = osrfMessageGetResult(omsg)) ) {
129
130                                 if (isXML) {
131                                         output = jsonObjectToXML( res );
132                                 } else {
133                                         output = jsonObjectToJSON( res );
134                                         if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */
135                                 }
136                                 ap_rputs(output, r);
137                                 free(output);
138                                 morethan1 = 1;
139                 
140                         } else {
141         
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 );
146                                 }
147                         }
148         
149                         osrf_message_free(omsg);
150                         if(statusname) break;
151                 }
152
153                 if (isXML)
154                         ap_rputs("</payload>", r);
155                 else
156                         ap_rputs("]",r); /* finish off the payload array */
157
158                 if(statusname) {
159
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;
164                         char buf[l];
165                         bzero(buf,l);
166
167                         if (isXML)
168                                 snprintf( buf, l, "<debug>\"%s : %s\"</debug>", statusname, statustext );
169                         else
170                                 snprintf( buf, l, ",\"debug\":\"%s : %s\"", statusname, statustext );
171
172                         ap_rputs(buf, r);
173
174                         free(statusname);
175                         free(statustext);
176                 }
177
178                 /* insert the status code */
179                 char buf[32];
180                 bzero(buf,32);
181
182                 if (isXML)
183                         snprintf(buf, 32, "<status>%d</status>", statuscode );
184                 else
185                         snprintf(buf, 32, ",\"status\":%d", statuscode );
186
187                 ap_rputs( buf, r );
188
189                 if (isXML)
190                         ap_rputs("</response>", r);
191                 else
192                         ap_rputs( "}", r ); /* finish off the object */
193
194                 osrf_app_session_destroy(session);
195         }
196
197         string_array_destroy(params);
198         string_array_destroy(mparams);
199
200         return OK;
201 }
202
203 static void osrf_json_gateway_register_hooks (apr_pool_t *p) {
204         ap_hook_handler(osrf_json_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
205         ap_hook_child_init(osrf_json_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
206 }
207
208
209 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
210         STANDARD20_MODULE_STUFF,
211         NULL,
212         NULL,
213         osrf_json_gateway_create_config,
214         NULL,
215         osrf_json_gateway_cmds,
216         osrf_json_gateway_register_hooks,
217 };
218
219
220
221