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