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 osrfLogError("Unable to load gateway config file...");
38 fprintf(stderr, "Bootstrapping %d\n", getpid() );
42 static int mod_ils_gateway_method_handler (request_rec *r) {
44 /* make sure we're needed first thing*/
45 if (strcmp(r->handler, MODULE_NAME ))
48 apr_pool_t *p = r->pool; /* memory pool */
49 char* arg = r->args; /* url query string */
51 char* service = NULL; /* service to connect to */
52 char* method = NULL; /* method to perform */
54 //json* exception = NULL; /* returned in error conditions */
55 jsonObject* exception = NULL; /* returned in error conditions */
56 string_array* sarray = init_string_array(12); /* method parameters */
58 growing_buffer* buffer = NULL; /* POST data */
59 growing_buffer* tmp_buf = NULL; /* temp buffer */
61 char* key = NULL; /* query item name */
62 char* val = NULL; /* query item value */
66 /* verify we are connected */
67 if(!osrf_system_get_transport_client()) {
68 osrfLogError("Bootstrap Failed, no transport client");
69 return HTTP_INTERNAL_SERVER_ERROR;
74 /* gather the post args and append them to the url query string */
75 if( !strcmp(r->method,"POST") ) {
77 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
79 if(! ap_should_client_block(r)) {
80 osrfLogWarning("No Post Body");
85 buffer = buffer_init(1025);
87 while(ap_get_client_block(r, body, 1024)) {
88 osrfLogDebug("Apache read POST block data: %s\n", body);
89 buffer_add( buffer, body );
94 tmp_buf = buffer_init(1024);
95 buffer_add(tmp_buf,arg);
96 buffer_add(tmp_buf,buffer->buf);
97 arg = (char*) apr_pstrdup(p, tmp_buf->buf);
100 arg = (char*) apr_pstrdup(p, buffer->buf);
106 osrfLogDebug("params args are %s", arg);
109 if( ! arg || !arg[0] ) { /* we received no request */
110 osrfLogWarning("No Args");
114 r->allowed |= (AP_METHOD_BIT << M_GET);
115 r->allowed |= (AP_METHOD_BIT << M_POST);
118 while( arg && (val = ap_getword(p, (const char**) &arg, '&'))) {
120 key = ap_getword(r->pool, (const char**) &val, '=');
124 ap_unescape_url((char*)key);
125 ap_unescape_url((char*)val);
127 if(!strcmp(key,"service"))
130 if(!strcmp(key,"method"))
133 if(!strcmp(key,"param"))
134 string_array_add(sarray, val);
138 osrfLogInfo("Performing(%d): service %s | method %s | \n",
139 getpid(), service, method );
142 for( k = 0; k!= sarray->size; k++ ) {
143 osrfLogInfo( "param %s", string_array_get_string(sarray,k));
146 osrf_app_session* session = osrf_app_client_session_init(service);
148 osrfLogDebug("MOD session service: %s", session->remote_service );
150 int req_id = osrf_app_session_make_req( session, NULL, method, 1, sarray );
151 string_array_destroy(sarray);
153 osrf_message* omsg = NULL;
155 growing_buffer* result_data = buffer_init(256);
156 buffer_add(result_data, "[");
158 /* gather result data */
159 while((omsg = osrf_app_session_request_recv( session, req_id, 60 ))) {
161 if( omsg->_result_content ) {
162 char* content = jsonObjectToJSON(omsg->_result_content);
163 buffer_add(result_data, content);
164 buffer_add( result_data, ",");
170 /* build the exception information */
171 growing_buffer* exc_buffer = buffer_init(256);
172 buffer_add( exc_buffer, "\nReceived Exception:\nName: " );
173 buffer_add( exc_buffer, omsg->status_name );
174 buffer_add( exc_buffer, "\nStatus: " );
175 buffer_add( exc_buffer, omsg->status_text );
176 buffer_add( exc_buffer, "\nStatus: " );
180 sprintf( code, "%d", omsg->status_code );
181 buffer_add( exc_buffer, code );
183 exception = json_parse_string("{}");
184 jsonObjectSetKey(exception, "is_err", json_parse_string("1"));
185 jsonObjectSetKey(exception, "err_msg", jsonNewObject(exc_buffer->buf) );
187 osrfLogWarning("*** Looks like we got a "
188 "server exception\n%s", exc_buffer->buf );
190 buffer_free(exc_buffer);
191 osrf_message_free(omsg);
195 osrf_message_free(omsg);
199 /* remove trailing comma */
200 if( result_data->buf[strlen(result_data->buf)-1] == ',') {
201 result_data->buf[strlen(result_data->buf)-1] = '\0';
202 result_data->n_used--;
205 buffer_add(result_data,"]");
207 char* content = NULL;
210 content = jsonObjectToJSON(exception);
211 jsonObjectFree(exception);
214 /* set content type to text/xml for passing around XML objects */
215 ap_set_content_type(r, "text/xml");
216 if(content) { /* exception... */
218 content = json_string_to_xml( tmp );
221 content = json_string_to_xml( result_data->buf );
224 buffer_free(result_data);
227 osrfLogDebug( "APACHE writing data to web client: %s", content );
232 osrf_app_session_request_finish( session, req_id );
233 osrfLogDebug("gateway process message successfully");
236 osrf_app_session_destroy(session);
241 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
242 ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
243 ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
247 module AP_MODULE_DECLARE_DATA ils_rest_gateway_module = {
248 STANDARD20_MODULE_STUFF,
251 ils_gateway_create_config,
254 mod_ils_gateway_register_hooks,