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