]> git.evergreen-ils.org Git - working/Evergreen.git/blob - OpenSRF/src/gateway/mod_ils_gateway.c
e8dd01b98b8e7add2b28fc5ca819b0ec1eed0f55
[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         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         if(exception) {
203                 content = exception->to_json(exception);
204                 free_object(exception);
205         } 
206
207 #ifdef RESTGATEWAY
208         /* set content type to text/xml for passing around XML objects */
209         ap_set_content_type(r, "text/xml");
210         if(content) { /* exception... */
211                 char* tmp = content;
212                 content = json_string_to_xml( tmp );
213                 free(tmp);
214         } else {
215                 content = json_string_to_xml( result_data->buf );
216         }
217
218 #else
219         /* set content type to text/plain for passing around JSON objects */
220         if(!content) {
221                 ap_set_content_type(r, "text/plain");
222                 content = buffer_data(result_data); 
223         }
224 #endif
225         
226
227         buffer_free(result_data);
228
229         if(content) {
230                 debug_handler( "APACHE writing data to web client: %s", content );
231                 ap_rputs(content,r);
232                 free(content);
233         } 
234
235         osrf_app_session_request_finish( session, req_id );
236         debug_handler("gateway process message successfully");
237
238
239         osrf_app_session_destroy(session);
240         return OK;
241
242 }
243
244 /*
245  * This function is a callback and it declares what other functions
246   * should be called for request processing and configuration requests.
247    * This callback function declares the Handlers for other events.  */
248 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
249 // I think this is the call to make to register a handler for method calls (GET PUT et. al.).
250 // We will ask to be last so that the comment has a higher tendency to
251 // go at the end.
252         ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
253         ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
254 }
255
256 /*
257  * Declare and populate the module's data structure.  The
258   * name of this structure ('tut1_module') is important - it
259    * must match the name of the module.  This structure is the
260  * only "glue" between the httpd core and the module.
261   */
262
263 #ifdef RESTGATEWAY
264
265 module AP_MODULE_DECLARE_DATA ils_rest_gateway_module =
266 {
267 STANDARD20_MODULE_STUFF,
268 NULL,
269 NULL,
270 NULL,
271 NULL,
272 NULL,
273 mod_ils_gateway_register_hooks,
274 };
275
276 #else
277
278 module AP_MODULE_DECLARE_DATA ils_gateway_module =
279 {
280 STANDARD20_MODULE_STUFF,
281 NULL,
282 NULL,
283 NULL,
284 NULL,
285 NULL,
286 mod_ils_gateway_register_hooks,
287 };
288
289 #endif
290