1 #include "mod_rest_gateway.h"
3 char* ils_rest_gateway_config_file;
5 static const char* ils_gateway_set_config(cmd_parms *parms, void *config, const char *arg) {
6 ils_gateway_config *cfg;
8 cfg = ap_get_module_config(parms->server->module_config, &ils_rest_gateway_module);
10 cfg->configfile = (char*) arg;
11 ils_rest_gateway_config_file = (char*) arg;
16 /* tell apache about our commands */
17 static const command_rec ils_gateway_cmds[] = {
18 AP_INIT_TAKE1( GATEWAY_CONFIG, ils_gateway_set_config, NULL, RSRC_CONF, "gateway config file"),
22 /* build the config object */
23 static void* ils_gateway_create_config( apr_pool_t* p, server_rec* s) {
24 ils_gateway_config* cfg = (ils_gateway_config*) apr_palloc(p, sizeof(ils_gateway_config));
25 cfg->configfile = GATEWAY_DEFAULT_CONFIG;
30 static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) {
32 char* cfg = ils_rest_gateway_config_file;
34 if( ! osrf_system_bootstrap_client( cfg, CONFIG_CONTEXT) )
35 fatal_handler("Unable to load gateway config file...");
36 fprintf(stderr, "Bootstrapping %d\n", getpid() );
40 static int mod_ils_gateway_method_handler (request_rec *r) {
42 /* make sure we're needed first thing*/
43 if (strcmp(r->handler, MODULE_NAME ))
46 apr_pool_t *p = r->pool; /* memory pool */
47 char* arg = r->args; /* url query string */
49 char* service = NULL; /* service to connect to */
50 char* method = NULL; /* method to perform */
52 //json* exception = NULL; /* returned in error conditions */
53 jsonObject* exception = NULL; /* returned in error conditions */
54 string_array* sarray = init_string_array(12); /* method parameters */
56 growing_buffer* buffer = NULL; /* POST data */
57 growing_buffer* tmp_buf = NULL; /* temp buffer */
59 char* key = NULL; /* query item name */
60 char* val = NULL; /* query item value */
64 /* verify we are connected */
65 if(!osrf_system_get_transport_client()) {
66 fatal_handler("Bootstrap Failed, no transport client");
67 return HTTP_INTERNAL_SERVER_ERROR;
72 /* gather the post args and append them to the url query string */
73 if( !strcmp(r->method,"POST") ) {
75 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
77 if(! ap_should_client_block(r)) {
78 warning_handler("No Post Body");
83 buffer = buffer_init(1025);
85 while(ap_get_client_block(r, body, 1024)) {
86 debug_handler("Apache read POST block data: %s\n", body);
87 buffer_add( buffer, body );
92 tmp_buf = buffer_init(1024);
93 buffer_add(tmp_buf,arg);
94 buffer_add(tmp_buf,buffer->buf);
95 arg = (char*) apr_pstrdup(p, tmp_buf->buf);
98 arg = (char*) apr_pstrdup(p, buffer->buf);
104 debug_handler("params args are %s", arg);
107 if( ! arg || !arg[0] ) { /* we received no request */
108 warning_handler("No Args");
112 r->allowed |= (AP_METHOD_BIT << M_GET);
113 r->allowed |= (AP_METHOD_BIT << M_POST);
116 while( arg && (val = ap_getword(p, (const char**) &arg, '&'))) {
118 key = ap_getword(r->pool, (const char**) &val, '=');
122 ap_unescape_url((char*)key);
123 ap_unescape_url((char*)val);
125 if(!strcmp(key,"service"))
128 if(!strcmp(key,"method"))
131 if(!strcmp(key,"param"))
132 string_array_add(sarray, val);
136 info_handler("Performing(%d): service %s | method %s | \n",
137 getpid(), service, method );
140 for( k = 0; k!= sarray->size; k++ ) {
141 info_handler( "param %s", string_array_get_string(sarray,k));
144 osrf_app_session* session = osrf_app_client_session_init(service);
146 debug_handler("MOD session service: %s", session->remote_service );
148 int req_id = osrf_app_session_make_req( session, NULL, method, 1, sarray );
149 string_array_destroy(sarray);
151 osrf_message* omsg = NULL;
153 growing_buffer* result_data = buffer_init(256);
154 buffer_add(result_data, "[");
156 /* gather result data */
157 while((omsg = osrf_app_session_request_recv( session, req_id, 60 ))) {
159 if( omsg->_result_content ) {
160 char* content = jsonObjectToJSON(omsg->_result_content);
161 buffer_add(result_data, content);
162 buffer_add( result_data, ",");
168 /* build the exception information */
169 growing_buffer* exc_buffer = buffer_init(256);
170 buffer_add( exc_buffer, "\nReceived Exception:\nName: " );
171 buffer_add( exc_buffer, omsg->status_name );
172 buffer_add( exc_buffer, "\nStatus: " );
173 buffer_add( exc_buffer, omsg->status_text );
174 buffer_add( exc_buffer, "\nStatus: " );
178 sprintf( code, "%d", omsg->status_code );
179 buffer_add( exc_buffer, code );
181 exception = json_parse_string("{}");
182 jsonObjectSetKey(exception, "is_err", json_parse_string("1"));
183 jsonObjectSetKey(exception, "err_msg", jsonNewObject(exc_buffer->buf) );
185 warning_handler("*** Looks like we got a "
186 "server exception\n%s", exc_buffer->buf );
188 buffer_free(exc_buffer);
189 osrf_message_free(omsg);
193 osrf_message_free(omsg);
197 /* remove trailing comma */
198 if( result_data->buf[strlen(result_data->buf)-1] == ',') {
199 result_data->buf[strlen(result_data->buf)-1] = '\0';
200 result_data->n_used--;
203 buffer_add(result_data,"]");
205 char* content = NULL;
208 content = jsonObjectToJSON(exception);
209 jsonObjectFree(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 );
222 buffer_free(result_data);
225 debug_handler( "APACHE writing data to web client: %s", content );
230 osrf_app_session_request_finish( session, req_id );
231 debug_handler("gateway process message successfully");
234 osrf_app_session_destroy(session);
239 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
240 ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
241 ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
245 module AP_MODULE_DECLARE_DATA ils_rest_gateway_module = {
246 STANDARD20_MODULE_STUFF,
249 ils_gateway_create_config,
252 mod_ils_gateway_register_hooks,