2 #include "http_config.h"
4 #include "http_protocol.h"
5 #include "apr_compat.h"
6 #include "apr_strings.h"
9 #include "opensrf/transport_client.h"
10 #include "opensrf/osrf_message.h"
11 #include "opensrf/osrf_app_session.h"
12 #include "string_array.h"
14 #include "objson/object.h"
15 #include "objson/json_parser.h"
20 #define MODULE_NAME "ils_rest_gateway_module"
22 #define MODULE_NAME "ils_gateway_module"
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() );
33 static int mod_ils_gateway_method_handler (request_rec *r) {
36 /* make sure we're needed first thing*/
37 if (strcmp(r->handler, MODULE_NAME ))
41 apr_pool_t *p = r->pool; /* memory pool */
42 char* arg = r->args; /* url query string */
44 char* service = NULL; /* service to connect to */
45 char* method = NULL; /* method to perform */
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 */
51 growing_buffer* buffer = NULL; /* POST data */
52 growing_buffer* tmp_buf = NULL; /* temp buffer */
54 char* key = NULL; /* query item name */
55 char* val = NULL; /* query item value */
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;
67 /* gather the post args and append them to the url query string */
68 if( !strcmp(r->method,"POST") ) {
70 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
72 if(! ap_should_client_block(r)) {
73 warning_handler("No Post Body");
78 buffer = buffer_init(1025);
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 );
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);
93 arg = (char*) apr_pstrdup(p, buffer->buf);
99 debug_handler("params args are %s", arg);
102 if( ! arg || !arg[0] ) { /* we received no request */
103 warning_handler("No Args");
107 r->allowed |= (AP_METHOD_BIT << M_GET);
108 r->allowed |= (AP_METHOD_BIT << M_POST);
111 while( arg && (val = ap_getword(p, (const char**) &arg, '&'))) {
113 key = ap_getword(r->pool, (const char**) &val, '=');
117 ap_unescape_url((char*)key);
118 ap_unescape_url((char*)val);
120 if(!strcmp(key,"service"))
123 if(!strcmp(key,"method"))
126 if(!strcmp(key,"param"))
127 string_array_add(sarray, val);
131 info_handler("Performing(%d): service %s | method %s | \n",
132 getpid(), service, method );
135 for( k = 0; k!= sarray->size; k++ ) {
136 info_handler( "param %s", string_array_get_string(sarray,k));
139 osrf_app_session* session = osrf_app_client_session_init(service);
141 debug_handler("MOD session service: %s", session->remote_service );
143 int req_id = osrf_app_session_make_req( session, NULL, method, 1, sarray );
144 string_array_destroy(sarray);
146 osrf_message* omsg = NULL;
148 growing_buffer* result_data = buffer_init(256);
149 buffer_add(result_data, "[");
151 /* gather result data */
152 while((omsg = osrf_app_session_request_recv( session, req_id, 60 ))) {
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, ",");
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: " );
173 sprintf( code, "%d", omsg->status_code );
174 buffer_add( exc_buffer, code );
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) );
180 warning_handler("*** Looks like we got a "
181 "server exception\n%s", exc_buffer->buf );
183 buffer_free(exc_buffer);
184 osrf_message_free(omsg);
188 osrf_message_free(omsg);
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--;
198 buffer_add(result_data,"]");
200 char* content = NULL;
203 content = exception->to_json(exception);
204 free_object(exception);
208 /* set content type to text/xml for passing around XML objects */
209 ap_set_content_type(r, "text/xml");
210 if(content) { /* exception... */
212 content = json_string_to_xml( tmp );
215 content = json_string_to_xml( result_data->buf );
219 /* set content type to text/plain for passing around JSON objects */
221 ap_set_content_type(r, "text/plain");
222 content = buffer_data(result_data);
227 buffer_free(result_data);
230 debug_handler( "APACHE writing data to web client: %s", content );
235 osrf_app_session_request_finish( session, req_id );
236 debug_handler("gateway process message successfully");
239 osrf_app_session_destroy(session);
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
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);
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.
265 module AP_MODULE_DECLARE_DATA ils_rest_gateway_module =
267 STANDARD20_MODULE_STUFF,
273 mod_ils_gateway_register_hooks,
278 module AP_MODULE_DECLARE_DATA ils_gateway_module =
280 STANDARD20_MODULE_STUFF,
286 mod_ils_gateway_register_hooks,