9fafbd20cf158141b2b2692b8654735136b03b27
[working/Evergreen.git] / OpenSRF / src / gateway / mod_ils_gateway.c
1 #include "httpd.h"
2 #include "http_config.h"
3 #include "http_core.h"
4 #include "http_protocol.h"
5 #include "apr_compat.h"
6 #include "apr_strings.h"
7
8 /* our stuff */
9 #include "opensrf/transport_client.h"
10 #include "opensrf/osrf_message.h"
11 #include "opensrf/osrf_app_session.h"
12 #include "string_array.h"
13 #include "md5.h"
14 #include "objson/object.h"
15 #include "objson/json_parser.h"
16
17 #ifdef RESTGATEWAY
18 #include "rest_xml.h"
19
20 #define MODULE_NAME "ils_rest_gateway_module"
21 #else
22 #define MODULE_NAME "ils_gateway_module"
23 #endif
24
25
26 static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) {
27         char* context = "gateway";
28         #ifdef RESTGATEWAY
29         context = "rest_gateway";
30         #endif
31         if( ! osrf_system_bootstrap_client( "/home/erickson/sandbox/openils/conf/opensrf_core.xml", context) ) 
32                 fatal_handler("Unable to load gateway config file...");
33         fprintf(stderr, "Bootstrapping %d\n", getpid() );
34         fflush(stderr);
35 }
36
37 static int mod_ils_gateway_method_handler (request_rec *r) {
38
39
40         /* make sure we're needed first thing*/
41         if (strcmp(r->handler, MODULE_NAME )) 
42                 return DECLINED;
43
44
45         apr_pool_t *p = r->pool;        /* memory pool */
46         char* arg = r->args;                    /* url query string */
47
48         char* service                                   = NULL; /* service to connect to */
49         char* method                                    = NULL; /* method to perform */
50
51         //json* exception                               = NULL; /* returned in error conditions */
52         object* exception                               = NULL; /* returned in error conditions */
53         string_array* sarray                    = init_string_array(12); /* method parameters */
54
55         growing_buffer* buffer          = NULL; /* POST data */
56         growing_buffer* tmp_buf         = NULL; /* temp buffer */
57
58         char* key                                               = NULL; /* query item name */
59         char* val                                               = NULL; /* query item value */
60
61
62
63         /* verify we are connected */
64         if(!osrf_system_get_transport_client()) {
65                 fatal_handler("Bootstrap Failed, no transport client");
66                 return HTTP_INTERNAL_SERVER_ERROR;
67         }
68
69
70
71         /* gather the post args and append them to the url query string */
72         if( !strcmp(r->method,"POST") ) {
73
74                 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
75
76                 if(! ap_should_client_block(r)) {
77                         warning_handler("No Post Body");
78                 }
79
80                 char body[1025];
81                 memset(body,0,1025);
82                 buffer = buffer_init(1025);
83
84                 while(ap_get_client_block(r, body, 1024)) {
85                         debug_handler("Apache read POST block data: %s\n", body);
86                         buffer_add( buffer, body );
87                         memset(body,0,1025);
88                 }
89
90                 if(arg && arg[0]) {
91                         tmp_buf = buffer_init(1024);
92                         buffer_add(tmp_buf,arg);
93                         buffer_add(tmp_buf,buffer->buf);
94                         arg = (char*) apr_pstrdup(p, tmp_buf->buf);
95                         buffer_free(tmp_buf);
96                 } else {
97                         arg = (char*) apr_pstrdup(p, buffer->buf);
98                 }
99                 buffer_free(buffer);
100
101         } 
102
103         debug_handler("params args are %s", arg);
104
105
106         if( ! arg || !arg[0] ) { /* we received no request */
107                 warning_handler("No Args");
108                 return OK;
109         }
110
111         r->allowed |= (AP_METHOD_BIT << M_GET);
112         r->allowed |= (AP_METHOD_BIT << M_POST);
113
114         
115         while( arg && (val = ap_getword(p, (const char**) &arg, '&'))) {
116
117                 key = ap_getword(r->pool, (const char**) &val, '=');
118                 if(!key || !key[0])
119                         break;
120
121                 ap_unescape_url((char*)key);
122                 ap_unescape_url((char*)val);
123
124                 if(!strcmp(key,"service")) 
125                         service = val;
126
127                 if(!strcmp(key,"method"))
128                         method = val;
129
130                 if(!strcmp(key,"param"))
131                         string_array_add(sarray, val);
132
133         }
134
135         info_handler("Performing(%d):  service %s | method %s | \n",
136                         getpid(), service, method );
137
138         int k;
139         for( k = 0; k!= sarray->size; k++ ) {
140                 info_handler( "param %s", string_array_get_string(sarray,k));
141         }
142
143         osrf_app_session* session = osrf_app_client_session_init(service);
144
145         debug_handler("MOD session service: %s", session->remote_service );
146
147         int req_id = osrf_app_session_make_req( session, NULL, method, 1, sarray );
148         string_array_destroy(sarray);
149
150         osrf_message* omsg = NULL;
151
152         growing_buffer* result_data = buffer_init(256);
153         buffer_add(result_data, "[");
154
155         /* gather result data */
156         while((omsg = osrf_app_session_request_recv( session, req_id, 60 ))) {
157
158                 if( omsg->_result_content ) {
159                         char* content = object_to_json(omsg->_result_content);
160                         buffer_add(result_data, content);
161                         buffer_add( result_data, ",");
162                         free(content);
163
164                 } else {
165
166
167                         /* build the exception information */
168                         growing_buffer* exc_buffer = buffer_init(256);
169                         buffer_add( exc_buffer, "\nReceived Exception:\nName: " );
170                         buffer_add( exc_buffer, omsg->status_name );
171                         buffer_add( exc_buffer, "\nStatus: " );
172                         buffer_add( exc_buffer, omsg->status_text );
173                         buffer_add( exc_buffer, "\nStatus: " );
174
175                         char code[16];
176                         memset(code, 0, 16);
177                         sprintf( code, "%d", omsg->status_code );
178                         buffer_add( exc_buffer, code );
179
180                         exception = json_parse_string("{}");
181                         exception->add_key(exception, "is_err", json_parse_string("1"));
182                         exception->add_key(exception, "err_msg", new_object(exc_buffer->buf) );
183
184                         warning_handler("*** Looks like we got a "
185                                         "server exception\n%s", exc_buffer->buf );
186
187                         buffer_free(exc_buffer);
188                         osrf_message_free(omsg);
189                         break;
190                 }
191
192                 osrf_message_free(omsg);
193                 omsg = NULL;
194         }
195
196         /* remove trailing comma */
197         if( result_data->buf[strlen(result_data->buf)-1] == ',') {
198                 result_data->buf[strlen(result_data->buf)-1] = '\0';
199                 result_data->n_used--;
200         }
201
202         buffer_add(result_data,"]");
203
204         char* content = NULL;
205
206         if(exception) {
207                 content = exception->to_json(exception);
208                 free_object(exception);
209         } 
210
211 #ifdef RESTGATEWAY
212         /* set content type to text/xml for passing around XML objects */
213         ap_set_content_type(r, "text/xml");
214         if(content) { /* exception... */
215                 char* tmp = content;
216                 content = json_string_to_xml( tmp );
217                 free(tmp);
218         } else {
219                 content = json_string_to_xml( result_data->buf );
220         }
221
222 #else
223         /* set content type to text/plain for passing around JSON objects */
224         if(!content) {
225                 ap_set_content_type(r, "text/plain");
226                 content = buffer_data(result_data); 
227         }
228 #endif
229         
230
231         buffer_free(result_data);
232
233         if(content) {
234                 debug_handler( "APACHE writing data to web client: %s", content );
235                 ap_rputs(content,r);
236                 free(content);
237         } 
238
239         osrf_app_session_request_finish( session, req_id );
240         debug_handler("gateway process message successfully");
241
242
243         osrf_app_session_destroy(session);
244         return OK;
245
246 }
247
248 /*
249  * This function is a callback and it declares what other functions
250   * should be called for request processing and configuration requests.
251    * This callback function declares the Handlers for other events.  */
252 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
253 // I think this is the call to make to register a handler for method calls (GET PUT et. al.).
254 // We will ask to be last so that the comment has a higher tendency to
255 // go at the end.
256         ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
257         ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
258 }
259
260 /*
261  * Declare and populate the module's data structure.  The
262   * name of this structure ('tut1_module') is important - it
263    * must match the name of the module.  This structure is the
264  * only "glue" between the httpd core and the module.
265   */
266
267 #ifdef RESTGATEWAY
268
269 module AP_MODULE_DECLARE_DATA ils_rest_gateway_module =
270 {
271 STANDARD20_MODULE_STUFF,
272 NULL,
273 NULL,
274 NULL,
275 NULL,
276 NULL,
277 mod_ils_gateway_register_hooks,
278 };
279
280 #else
281
282 module AP_MODULE_DECLARE_DATA ils_gateway_module =
283 {
284 STANDARD20_MODULE_STUFF,
285 NULL,
286 NULL,
287 NULL,
288 NULL,
289 NULL,
290 mod_ils_gateway_register_hooks,
291 };
292
293 #endif
294