freeing json response object
[Evergreen.git] / OpenSRF / src / gateway / mod_ils_gateway.c
1 #include "mod_ils_gateway.h"
2
3 char* ils_gateway_config_file;
4 char* ils_rest_gateway_config_file;
5
6 static const char* ils_gateway_set_config(cmd_parms *parms, void *config, const char *arg) {
7         ils_gateway_config  *cfg;
8         cfg = ap_get_module_config(parms->server->module_config, &ils_gateway_module);
9         cfg->configfile = (char*) arg;
10         ils_gateway_config_file = (char*) arg;
11         return NULL;
12 }
13
14 /* tell apache about our commands */
15 static const command_rec ils_gateway_cmds[] = {
16         AP_INIT_TAKE1( GATEWAY_CONFIG, ils_gateway_set_config, NULL, RSRC_CONF, "gateway config file"),
17         {NULL}
18 };
19
20 /* build the config object */
21 static void* ils_gateway_create_config( apr_pool_t* p, server_rec* s) {
22         ils_gateway_config* cfg = (ils_gateway_config*) apr_palloc(p, sizeof(ils_gateway_config));
23         cfg->configfile = GATEWAY_DEFAULT_CONFIG;
24         return (void*) cfg;
25 }
26
27
28 static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) {
29
30         char* cfg = ils_gateway_config_file;
31         if( ! osrf_system_bootstrap_client( cfg, CONFIG_CONTEXT) ) {
32                 osrfLogError("Unable to load gateway config file...");
33                 return;
34         }
35         fprintf(stderr, "Bootstrapping %d\n", getpid() );
36         fflush(stderr);
37 }
38
39 static int mod_ils_gateway_method_handler (request_rec *r) {
40
41         /* make sure we're needed first thing*/
42         if (strcmp(r->handler, MODULE_NAME )) 
43                 return DECLINED;
44
45         apr_pool_t *p = r->pool;        /* memory pool */
46         char* arg = r->args;                    /* url query string */
47
48         char* service                                   = NULL; /* service to connect to */
49         char* method                                    = NULL; /* method to perform */
50
51         //json* exception                               = NULL; /* returned in error conditions */
52         //jsonObject* exception         = NULL; /* returned in error conditions */
53         string_array* sarray                    = init_string_array(12); /* method parameters */
54
55         growing_buffer* buffer          = NULL; /* POST data */
56         growing_buffer* tmp_buf         = NULL; /* temp buffer */
57
58         char* key                                               = NULL; /* query item name */
59         char* val                                               = NULL; /* query item value */
60
61         jsonObject* response                    = jsonParseString("{\"status\":0,\"debug\":\"\"}");
62         jsonObject* payload                     = jsonParseString("[]");
63         jsonObjectSetKey(response, "payload", payload );
64
65
66
67         /* verify we are connected */
68         if(!osrf_system_get_transport_client()) {
69                 osrfLogError("Bootstrap Failed, no transport client");
70                 return HTTP_INTERNAL_SERVER_ERROR;
71         }
72
73
74
75         /* gather the post args and append them to the url query string */
76         if( !strcmp(r->method,"POST") ) {
77
78                 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
79
80                 if(! ap_should_client_block(r)) {
81                         osrfLogWarning("No Post Body");
82                 }
83
84                 char body[1025];
85                 memset(body,0,1025);
86                 buffer = buffer_init(1025);
87
88                 while(ap_get_client_block(r, body, 1024)) {
89                         osrfLogDebug("Apache read POST block data: %s\n", body);
90                         buffer_add( buffer, body );
91                         memset(body,0,1025);
92                 }
93
94                 if(arg && arg[0]) {
95                         tmp_buf = buffer_init(1024);
96                         buffer_add(tmp_buf,arg);
97                         buffer_add(tmp_buf,buffer->buf);
98                         arg = (char*) apr_pstrdup(p, tmp_buf->buf);
99                         buffer_free(tmp_buf);
100                 } else {
101                         arg = (char*) apr_pstrdup(p, buffer->buf);
102                 }
103                 buffer_free(buffer);
104
105         } 
106
107         osrfLogDebug("params args are %s", arg);
108
109
110         if( ! arg || !arg[0] ) { /* we received no request */
111                 osrfLogWarning("No Args");
112                 return OK;
113         }
114
115         r->allowed |= (AP_METHOD_BIT << M_GET);
116         r->allowed |= (AP_METHOD_BIT << M_POST);
117
118         
119         while( arg && (val = ap_getword(p, (const char**) &arg, '&'))) {
120
121                 key = ap_getword(r->pool, (const char**) &val, '=');
122                 if(!key || !key[0])
123                         break;
124
125                 ap_unescape_url((char*)key);
126                 ap_unescape_url((char*)val);
127
128                 if(!strcmp(key,"service")) 
129                         service = val;
130
131                 if(!strcmp(key,"method"))
132                         method = val;
133
134                 if(!strcmp(key,"param"))
135                         string_array_add(sarray, val);
136
137         }
138
139         osrfLogInfo("\nPerforming(%d):  service %s "
140                         "| method %s |", getpid(), service, method );
141
142         int k;
143         for( k = 0; k!= sarray->size; k++ ) {
144                 osrfLogInfo( "param %s", string_array_get_string(sarray,k));
145         }
146
147         osrf_app_session* session = osrf_app_client_session_init(service);
148
149         osrfLogDebug("MOD session service: %s", session->remote_service );
150
151         int req_id = osrf_app_session_make_req( session, NULL, method, 1, sarray );
152         string_array_destroy(sarray);
153
154         osrf_message* omsg = NULL;
155
156         while((omsg = osrf_app_session_request_recv( session, req_id, 60 ))) {
157
158                 jsonObjectSetKey(response, "status", jsonNewNumberObject(omsg->status_code));
159
160                 if( omsg->_result_content ) {
161                         jsonObjectPush( payload, jsonObjectClone(omsg->_result_content));
162         
163                 } else {
164
165                         char* s = omsg->status_name ? omsg->status_name : "Unknown Error";
166                         char* t = omsg->status_text ? omsg->status_text : "No Error Message";
167                         jsonObjectSetKey(response, "debug", jsonNewObject("\n\n%s:\n%s\n", s, t));
168                         osrfLogError( "Gateway received error: %s", 
169                                         jsonObjectGetString(jsonObjectGetKey(response, "debug")));
170                         break;
171                 }
172
173                 osrf_message_free(omsg);
174                 omsg = NULL;
175         }
176
177         char* content = jsonObjectToJSON(response);
178         if(content) {
179                 osrfLogInfo( "----------------------------------------------\n"
180                         "Gateway responding with:\n%s\n"
181                         "----------------------------------------------", content );
182                 ap_rputs(content,r);
183                 free(content);
184         } 
185         jsonObjectFree(response);
186
187         osrf_app_session_request_finish( session, req_id );
188         osrfLogDebug("gateway processed message successfully");
189
190         osrf_app_session_destroy(session);
191         return OK;
192 }
193
194 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
195         ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
196         ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
197 }
198
199
200 module AP_MODULE_DECLARE_DATA ils_gateway_module = {
201         STANDARD20_MODULE_STUFF,
202         NULL,
203         NULL,
204         ils_gateway_create_config,
205         NULL,
206         ils_gateway_cmds,
207         mod_ils_gateway_register_hooks,
208 };
209
210
211
212