]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/gateway/mod_ils_gateway.c
moved C code to a unified logging framework which currently supports syslogging and...
[Evergreen.git] / OpenSRF / src / gateway / mod_ils_gateway.c
1 #include "mod_ils_gateway.h"
2
3 char* ils_gateway_config_file;
4 char* ils_rest_gateway_config_file;
5
6 static const char* ils_gateway_set_config(cmd_parms *parms, void *config, const char *arg) {
7         ils_gateway_config  *cfg;
8
9         #ifdef RESTGATEWAY
10         cfg = ap_get_module_config(parms->server->module_config, &ils_rest_gateway_module);
11         #else
12         cfg = ap_get_module_config(parms->server->module_config, &ils_gateway_module);
13         #endif
14
15         cfg->configfile = (char*) arg;
16         #ifdef RESTGATEWAY
17         ils_rest_gateway_config_file = (char*) arg;
18         #else
19         ils_gateway_config_file = (char*) arg;
20         #endif
21
22         return NULL;
23 }
24
25 /* tell apache about our commands */
26 static const command_rec ils_gateway_cmds[] = {
27         AP_INIT_TAKE1( GATEWAY_CONFIG, ils_gateway_set_config, NULL, RSRC_CONF, "gateway config file"),
28         {NULL}
29 };
30
31 /* build the config object */
32 static void* ils_gateway_create_config( apr_pool_t* p, server_rec* s) {
33         ils_gateway_config* cfg = (ils_gateway_config*) apr_palloc(p, sizeof(ils_gateway_config));
34         cfg->configfile = GATEWAY_DEFAULT_CONFIG;
35         return (void*) cfg;
36 }
37
38
39 static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) {
40
41         char* cfg = ils_gateway_config_file;
42         #ifdef RESTGATEWAY
43         cfg = ils_gateway_config_file;
44         #endif
45
46         if( ! osrf_system_bootstrap_client( cfg, CONFIG_CONTEXT) ) {
47                 osrfLogError("Unable to load gateway config file...");
48                 return;
49         }
50         fprintf(stderr, "Bootstrapping %d\n", getpid() );
51         fflush(stderr);
52 }
53
54 static int mod_ils_gateway_method_handler (request_rec *r) {
55
56         /* make sure we're needed first thing*/
57         if (strcmp(r->handler, MODULE_NAME )) 
58                 return DECLINED;
59
60         apr_pool_t *p = r->pool;        /* memory pool */
61         char* arg = r->args;                    /* url query string */
62
63         char* service                                   = NULL; /* service to connect to */
64         char* method                                    = NULL; /* method to perform */
65
66         //json* exception                               = NULL; /* returned in error conditions */
67         jsonObject* exception                           = NULL; /* returned in error conditions */
68         string_array* sarray                    = init_string_array(12); /* method parameters */
69
70         growing_buffer* buffer          = NULL; /* POST data */
71         growing_buffer* tmp_buf         = NULL; /* temp buffer */
72
73         char* key                                               = NULL; /* query item name */
74         char* val                                               = NULL; /* query item value */
75
76
77
78         /* verify we are connected */
79         if(!osrf_system_get_transport_client()) {
80                 osrfLogError("Bootstrap Failed, no transport client");
81                 return HTTP_INTERNAL_SERVER_ERROR;
82         }
83
84
85
86         /* gather the post args and append them to the url query string */
87         if( !strcmp(r->method,"POST") ) {
88
89                 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
90
91                 if(! ap_should_client_block(r)) {
92                         osrfLogWarning("No Post Body");
93                 }
94
95                 char body[1025];
96                 memset(body,0,1025);
97                 buffer = buffer_init(1025);
98
99                 while(ap_get_client_block(r, body, 1024)) {
100                         osrfLogDebug("Apache read POST block data: %s\n", body);
101                         buffer_add( buffer, body );
102                         memset(body,0,1025);
103                 }
104
105                 if(arg && arg[0]) {
106                         tmp_buf = buffer_init(1024);
107                         buffer_add(tmp_buf,arg);
108                         buffer_add(tmp_buf,buffer->buf);
109                         arg = (char*) apr_pstrdup(p, tmp_buf->buf);
110                         buffer_free(tmp_buf);
111                 } else {
112                         arg = (char*) apr_pstrdup(p, buffer->buf);
113                 }
114                 buffer_free(buffer);
115
116         } 
117
118         osrfLogDebug("params args are %s", arg);
119
120
121         if( ! arg || !arg[0] ) { /* we received no request */
122                 osrfLogWarning("No Args");
123                 return OK;
124         }
125
126         r->allowed |= (AP_METHOD_BIT << M_GET);
127         r->allowed |= (AP_METHOD_BIT << M_POST);
128
129         
130         while( arg && (val = ap_getword(p, (const char**) &arg, '&'))) {
131
132                 key = ap_getword(r->pool, (const char**) &val, '=');
133                 if(!key || !key[0])
134                         break;
135
136                 ap_unescape_url((char*)key);
137                 ap_unescape_url((char*)val);
138
139                 if(!strcmp(key,"service")) 
140                         service = val;
141
142                 if(!strcmp(key,"method"))
143                         method = val;
144
145                 if(!strcmp(key,"param"))
146                         string_array_add(sarray, val);
147
148         }
149
150         osrfLogInfo("\nPerforming(%d):  service %s | method %s |",
151                         getpid(), service, method );
152
153         int k;
154         for( k = 0; k!= sarray->size; k++ ) {
155                 osrfLogInfo( "param %s", string_array_get_string(sarray,k));
156         }
157
158         osrf_app_session* session = osrf_app_client_session_init(service);
159
160         osrfLogDebug("MOD session service: %s", session->remote_service );
161
162         int req_id = osrf_app_session_make_req( session, NULL, method, 1, sarray );
163         string_array_destroy(sarray);
164
165         osrf_message* omsg = NULL;
166
167         growing_buffer* result_data = buffer_init(256);
168         buffer_add(result_data, "[");
169
170         /* gather result data */
171         while((omsg = osrf_app_session_request_recv( session, req_id, 60 ))) {
172
173                 if( omsg->_result_content ) {
174                         char* content = jsonObjectToJSON(omsg->_result_content);
175                         buffer_add(result_data, content);
176                         buffer_add( result_data, ",");
177                         free(content);
178
179                 } else {
180
181
182                         /* build the exception information */
183                         growing_buffer* exc_buffer = buffer_init(256);
184                         buffer_add( exc_buffer, "\nReceived Exception:\nName: " );
185                         buffer_add( exc_buffer, omsg->status_name );
186                         buffer_add( exc_buffer, "\nStatus: " );
187                         buffer_add( exc_buffer, omsg->status_text );
188                         buffer_add( exc_buffer, "\nStatus: " );
189
190                         char code[16];
191                         memset(code, 0, 16);
192                         sprintf( code, "%d", omsg->status_code );
193                         buffer_add( exc_buffer, code );
194
195                         exception = json_parse_string("{}");
196                         jsonObjectSetKey(exception, "is_err", json_parse_string("1"));
197                         jsonObjectSetKey(exception, "err_msg", jsonNewObject(exc_buffer->buf) );
198
199                         osrfLogWarning("*** Looks like we got a "
200                                         "server exception\n%s", exc_buffer->buf );
201
202                         buffer_free(exc_buffer);
203                         osrf_message_free(omsg);
204                         break;
205                 }
206
207                 osrf_message_free(omsg);
208                 omsg = NULL;
209         }
210
211         /* remove trailing comma */
212         if( result_data->buf[strlen(result_data->buf)-1] == ',') {
213                 result_data->buf[strlen(result_data->buf)-1] = '\0';
214                 result_data->n_used--;
215         }
216
217         buffer_add(result_data,"]");
218
219         char* content = NULL;
220
221         if(exception) {
222                 content = jsonObjectToJSON(exception);
223                 jsonObjectFree(exception);
224         } 
225
226 #ifdef RESTGATEWAY
227         /* set content type to text/xml for passing around XML objects */
228         ap_set_content_type(r, "text/xml");
229         if(content) { /* exception... */
230                 char* tmp = content;
231                 content = json_string_to_xml( tmp );
232                 free(tmp);
233         } else {
234                 content = json_string_to_xml( result_data->buf );
235         }
236
237 #else
238         /* set content type to text/plain for passing around JSON objects */
239         if(!content) {
240                 ap_set_content_type(r, "text/plain");
241                 content = buffer_data(result_data); 
242         }
243 #endif
244         
245
246         buffer_free(result_data);
247
248         if(content) {
249                 osrfLogInfo( "APACHE writing data to web client: %s", content );
250                 ap_rputs(content,r);
251                 free(content);
252         } 
253
254         osrf_app_session_request_finish( session, req_id );
255         osrfLogDebug("gateway process message successfully");
256
257         osrf_app_session_destroy(session);
258         return OK;
259
260 }
261
262 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
263         ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
264         ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
265 }
266
267
268 #ifdef RESTGATEWAY
269 module AP_MODULE_DECLARE_DATA ils_rest_gateway_module = {
270 #else
271 module AP_MODULE_DECLARE_DATA ils_gateway_module = {
272 #endif
273         STANDARD20_MODULE_STUFF,
274         NULL,
275         NULL,
276         ils_gateway_create_config,
277         NULL,
278         ils_gateway_cmds,
279         mod_ils_gateway_register_hooks,
280 };
281
282
283 /*
284 #ifdef RESTGATEWAY
285
286 module AP_MODULE_DECLARE_DATA ils_rest_gateway_module =
287 {
288 STANDARD20_MODULE_STUFF,
289 NULL,
290 NULL,
291 ils_gateway_create_config,
292 NULL,
293 ils_gateway_cmds,
294 mod_ils_gateway_register_hooks,
295 };
296
297 #else
298
299 module AP_MODULE_DECLARE_DATA ils_gateway_module =
300 {
301 STANDARD20_MODULE_STUFF,
302 NULL,
303 NULL,
304 ils_gateway_create_config,
305 NULL,
306 ils_gateway_cmds,
307 mod_ils_gateway_register_hooks,
308 };
309
310 #endif
311 */
312