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