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 char* context = "gateway";
29 context = "rest_gateway";
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() );
37 static int mod_ils_gateway_method_handler (request_rec *r) {
40 /* make sure we're needed first thing*/
41 if (strcmp(r->handler, MODULE_NAME ))
45 apr_pool_t *p = r->pool; /* memory pool */
46 char* arg = r->args; /* url query string */
48 char* service = NULL; /* service to connect to */
49 char* method = NULL; /* method to perform */
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 */
55 growing_buffer* buffer = NULL; /* POST data */
56 growing_buffer* tmp_buf = NULL; /* temp buffer */
58 char* key = NULL; /* query item name */
59 char* val = NULL; /* query item value */
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;
71 /* gather the post args and append them to the url query string */
72 if( !strcmp(r->method,"POST") ) {
74 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
76 if(! ap_should_client_block(r)) {
77 warning_handler("No Post Body");
82 buffer = buffer_init(1025);
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 );
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);
97 arg = (char*) apr_pstrdup(p, buffer->buf);
103 debug_handler("params args are %s", arg);
106 if( ! arg || !arg[0] ) { /* we received no request */
107 warning_handler("No Args");
111 r->allowed |= (AP_METHOD_BIT << M_GET);
112 r->allowed |= (AP_METHOD_BIT << M_POST);
115 while( arg && (val = ap_getword(p, (const char**) &arg, '&'))) {
117 key = ap_getword(r->pool, (const char**) &val, '=');
121 ap_unescape_url((char*)key);
122 ap_unescape_url((char*)val);
124 if(!strcmp(key,"service"))
127 if(!strcmp(key,"method"))
130 if(!strcmp(key,"param"))
131 string_array_add(sarray, val);
135 info_handler("Performing(%d): service %s | method %s | \n",
136 getpid(), service, method );
139 for( k = 0; k!= sarray->size; k++ ) {
140 info_handler( "param %s", string_array_get_string(sarray,k));
143 osrf_app_session* session = osrf_app_client_session_init(service);
145 debug_handler("MOD session service: %s", session->remote_service );
147 int req_id = osrf_app_session_make_req( session, NULL, method, 1, sarray );
148 string_array_destroy(sarray);
150 osrf_message* omsg = NULL;
152 growing_buffer* result_data = buffer_init(256);
153 buffer_add(result_data, "[");
155 /* gather result data */
156 while((omsg = osrf_app_session_request_recv( session, req_id, 60 ))) {
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, ",");
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: " );
177 sprintf( code, "%d", omsg->status_code );
178 buffer_add( exc_buffer, code );
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) );
184 warning_handler("*** Looks like we got a "
185 "server exception\n%s", exc_buffer->buf );
187 buffer_free(exc_buffer);
188 osrf_message_free(omsg);
192 osrf_message_free(omsg);
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--;
202 buffer_add(result_data,"]");
204 char* content = NULL;
207 content = exception->to_json(exception);
208 free_object(exception);
212 /* set content type to text/xml for passing around XML objects */
213 ap_set_content_type(r, "text/xml");
214 if(content) { /* exception... */
216 content = json_string_to_xml( tmp );
219 content = json_string_to_xml( result_data->buf );
223 /* set content type to text/plain for passing around JSON objects */
225 ap_set_content_type(r, "text/plain");
226 content = buffer_data(result_data);
231 buffer_free(result_data);
234 debug_handler( "APACHE writing data to web client: %s", content );
239 osrf_app_session_request_finish( session, req_id );
240 debug_handler("gateway process message successfully");
243 osrf_app_session_destroy(session);
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
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);
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.
269 module AP_MODULE_DECLARE_DATA ils_rest_gateway_module =
271 STANDARD20_MODULE_STUFF,
277 mod_ils_gateway_register_hooks,
282 module AP_MODULE_DECLARE_DATA ils_gateway_module =
284 STANDARD20_MODULE_STUFF,
290 mod_ils_gateway_register_hooks,