1 #include "mod_ils_gateway.h"
3 char* ils_gateway_config_file;
4 char* ils_rest_gateway_config_file;
6 static const char* ils_gateway_set_config(cmd_parms *parms, void *config, const char *arg) {
7 ils_gateway_config *cfg;
10 cfg = ap_get_module_config(parms->server->module_config, &ils_rest_gateway_module);
12 cfg = ap_get_module_config(parms->server->module_config, &ils_gateway_module);
15 cfg->configfile = (char*) arg;
17 ils_rest_gateway_config_file = (char*) arg;
19 ils_gateway_config_file = (char*) arg;
25 /* tell apache about our commands */
26 static const command_rec ils_gateway_cmds[] = {
27 AP_INIT_TAKE1( GATEWAY_CONFIG, ils_gateway_set_config, NULL, RSRC_CONF, "gateway config file"),
31 /* build the config object */
32 static void* ils_gateway_create_config( apr_pool_t* p, server_rec* s) {
33 ils_gateway_config* cfg = (ils_gateway_config*) apr_palloc(p, sizeof(ils_gateway_config));
34 cfg->configfile = GATEWAY_DEFAULT_CONFIG;
39 static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) {
41 char* cfg = ils_gateway_config_file;
43 cfg = ils_gateway_config_file;
46 if( ! osrf_system_bootstrap_client( cfg, CONFIG_CONTEXT) ) {
47 osrfLogError("Unable to load gateway config file...");
50 fprintf(stderr, "Bootstrapping %d\n", getpid() );
54 static int mod_ils_gateway_method_handler (request_rec *r) {
56 /* make sure we're needed first thing*/
57 if (strcmp(r->handler, MODULE_NAME ))
60 apr_pool_t *p = r->pool; /* memory pool */
61 char* arg = r->args; /* url query string */
63 char* service = NULL; /* service to connect to */
64 char* method = NULL; /* method to perform */
66 //json* exception = NULL; /* returned in error conditions */
67 jsonObject* exception = NULL; /* returned in error conditions */
68 string_array* sarray = init_string_array(12); /* method parameters */
70 growing_buffer* buffer = NULL; /* POST data */
71 growing_buffer* tmp_buf = NULL; /* temp buffer */
73 char* key = NULL; /* query item name */
74 char* val = NULL; /* query item value */
78 /* verify we are connected */
79 if(!osrf_system_get_transport_client()) {
80 osrfLogError("Bootstrap Failed, no transport client");
81 return HTTP_INTERNAL_SERVER_ERROR;
86 /* gather the post args and append them to the url query string */
87 if( !strcmp(r->method,"POST") ) {
89 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
91 if(! ap_should_client_block(r)) {
92 osrfLogWarning("No Post Body");
97 buffer = buffer_init(1025);
99 while(ap_get_client_block(r, body, 1024)) {
100 osrfLogDebug("Apache read POST block data: %s\n", body);
101 buffer_add( buffer, body );
106 tmp_buf = buffer_init(1024);
107 buffer_add(tmp_buf,arg);
108 buffer_add(tmp_buf,buffer->buf);
109 arg = (char*) apr_pstrdup(p, tmp_buf->buf);
110 buffer_free(tmp_buf);
112 arg = (char*) apr_pstrdup(p, buffer->buf);
118 osrfLogDebug("params args are %s", arg);
121 if( ! arg || !arg[0] ) { /* we received no request */
122 osrfLogWarning("No Args");
126 r->allowed |= (AP_METHOD_BIT << M_GET);
127 r->allowed |= (AP_METHOD_BIT << M_POST);
130 while( arg && (val = ap_getword(p, (const char**) &arg, '&'))) {
132 key = ap_getword(r->pool, (const char**) &val, '=');
136 ap_unescape_url((char*)key);
137 ap_unescape_url((char*)val);
139 if(!strcmp(key,"service"))
142 if(!strcmp(key,"method"))
145 if(!strcmp(key,"param"))
146 string_array_add(sarray, val);
150 osrfLogInfo("\nPerforming(%d): service %s | method %s |",
151 getpid(), service, method );
154 for( k = 0; k!= sarray->size; k++ ) {
155 osrfLogInfo( "param %s", string_array_get_string(sarray,k));
158 osrf_app_session* session = osrf_app_client_session_init(service);
160 osrfLogDebug("MOD session service: %s", session->remote_service );
162 int req_id = osrf_app_session_make_req( session, NULL, method, 1, sarray );
163 string_array_destroy(sarray);
165 osrf_message* omsg = NULL;
167 growing_buffer* result_data = buffer_init(256);
168 buffer_add(result_data, "[");
170 /* gather result data */
171 while((omsg = osrf_app_session_request_recv( session, req_id, 60 ))) {
173 if( omsg->_result_content ) {
174 char* content = jsonObjectToJSON(omsg->_result_content);
175 buffer_add(result_data, content);
176 buffer_add( result_data, ",");
182 /* build the exception information */
183 growing_buffer* exc_buffer = buffer_init(256);
184 buffer_add( exc_buffer, "\nReceived Exception:\nName: " );
185 buffer_add( exc_buffer, omsg->status_name );
186 buffer_add( exc_buffer, "\nStatus: " );
187 buffer_add( exc_buffer, omsg->status_text );
188 buffer_add( exc_buffer, "\nStatus: " );
192 sprintf( code, "%d", omsg->status_code );
193 buffer_add( exc_buffer, code );
195 exception = json_parse_string("{}");
196 jsonObjectSetKey(exception, "is_err", json_parse_string("1"));
197 jsonObjectSetKey(exception, "err_msg", jsonNewObject(exc_buffer->buf) );
199 osrfLogWarning("*** Looks like we got a "
200 "server exception\n%s", exc_buffer->buf );
202 buffer_free(exc_buffer);
203 osrf_message_free(omsg);
207 osrf_message_free(omsg);
211 /* remove trailing comma */
212 if( result_data->buf[strlen(result_data->buf)-1] == ',') {
213 result_data->buf[strlen(result_data->buf)-1] = '\0';
214 result_data->n_used--;
217 buffer_add(result_data,"]");
219 char* content = NULL;
222 content = jsonObjectToJSON(exception);
223 jsonObjectFree(exception);
227 /* set content type to text/xml for passing around XML objects */
228 ap_set_content_type(r, "text/xml");
229 if(content) { /* exception... */
231 content = json_string_to_xml( tmp );
234 content = json_string_to_xml( result_data->buf );
238 /* set content type to text/plain for passing around JSON objects */
240 ap_set_content_type(r, "text/plain");
241 content = buffer_data(result_data);
246 buffer_free(result_data);
249 osrfLogInfo( "APACHE writing data to web client: %s", content );
254 osrf_app_session_request_finish( session, req_id );
255 osrfLogDebug("gateway process message successfully");
257 osrf_app_session_destroy(session);
262 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
263 ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
264 ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
269 module AP_MODULE_DECLARE_DATA ils_rest_gateway_module = {
271 module AP_MODULE_DECLARE_DATA ils_gateway_module = {
273 STANDARD20_MODULE_STUFF,
276 ils_gateway_create_config,
279 mod_ils_gateway_register_hooks,
286 module AP_MODULE_DECLARE_DATA ils_rest_gateway_module =
288 STANDARD20_MODULE_STUFF,
291 ils_gateway_create_config,
294 mod_ils_gateway_register_hooks,
299 module AP_MODULE_DECLARE_DATA ils_gateway_module =
301 STANDARD20_MODULE_STUFF,
304 ils_gateway_create_config,
307 mod_ils_gateway_register_hooks,