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