]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/gateway/mod_ils_gateway.c
setting the logger app name for debugging
[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         osrfLogSetAppname("osrf_json_gw");
36         fprintf(stderr, "Bootstrapping %d\n", getpid() );
37         fflush(stderr);
38 }
39
40 static int mod_ils_gateway_method_handler (request_rec *r) {
41
42         /* make sure we're needed first thing*/
43         if (strcmp(r->handler, MODULE_NAME )) 
44                 return DECLINED;
45
46         apr_pool_t *p = r->pool;        /* memory pool */
47         char* arg = r->args;                    /* url query string */
48
49         char* service                                   = NULL; /* service to connect to */
50         char* method                                    = NULL; /* method to perform */
51
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 */
55
56         growing_buffer* buffer          = NULL; /* POST data */
57         growing_buffer* tmp_buf         = NULL; /* temp buffer */
58
59         char* key                                               = NULL; /* query item name */
60         char* val                                               = NULL; /* query item value */
61
62         jsonObject* response                    = jsonParseString("{\"status\":0,\"debug\":\"\"}");
63         jsonObject* payload                     = jsonParseString("[]");
64         jsonObjectSetKey(response, "payload", payload );
65
66
67
68         /* verify we are connected */
69         if(!osrf_system_get_transport_client()) {
70                 osrfLogError("Bootstrap Failed, no transport client");
71                 return HTTP_INTERNAL_SERVER_ERROR;
72         }
73
74
75         ap_set_content_type(r, "text/plain");
76
77         /* gather the post args and append them to the url query string */
78         if( !strcmp(r->method,"POST") ) {
79
80                 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
81
82                 if(! ap_should_client_block(r)) {
83                         osrfLogWarning("No Post Body");
84                 }
85
86                 char body[1025];
87                 memset(body,0,1025);
88                 buffer = buffer_init(1025);
89
90                 while(ap_get_client_block(r, body, 1024)) {
91                         osrfLogDebug("Apache read POST block data: %s\n", body);
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         osrfLogDebug("params args are %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("\nPerforming(%d):  service %s "
142                         "| method %s |", 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         if(content) {
181                 osrfLogInfo( "Gateway responding with:  %s", 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