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