added content-type back into gateway
[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         ap_set_content_type(r, "text/plain");
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("No Post Body");
83                 }
84
85                 char body[1025];
86                 memset(body,0,1025);
87                 buffer = buffer_init(1025);
88
89                 while(ap_get_client_block(r, body, 1024)) {
90                         osrfLogDebug("Apache read POST block data: %s\n", body);
91                         buffer_add( buffer, body );
92                         memset(body,0,1025);
93                 }
94
95                 if(arg && arg[0]) {
96                         tmp_buf = buffer_init(1024);
97                         buffer_add(tmp_buf,arg);
98                         buffer_add(tmp_buf,buffer->buf);
99                         arg = (char*) apr_pstrdup(p, tmp_buf->buf);
100                         buffer_free(tmp_buf);
101                 } else {
102                         arg = (char*) apr_pstrdup(p, buffer->buf);
103                 }
104                 buffer_free(buffer);
105
106         } 
107
108         osrfLogDebug("params args are %s", arg);
109
110
111         if( ! arg || !arg[0] ) { /* we received no request */
112                 osrfLogWarning("No Args");
113                 return OK;
114         }
115
116         r->allowed |= (AP_METHOD_BIT << M_GET);
117         r->allowed |= (AP_METHOD_BIT << M_POST);
118
119         
120         while( arg && (val = ap_getword(p, (const char**) &arg, '&'))) {
121
122                 key = ap_getword(r->pool, (const char**) &val, '=');
123                 if(!key || !key[0])
124                         break;
125
126                 ap_unescape_url((char*)key);
127                 ap_unescape_url((char*)val);
128
129                 if(!strcmp(key,"service")) 
130                         service = val;
131
132                 if(!strcmp(key,"method"))
133                         method = val;
134
135                 if(!strcmp(key,"param"))
136                         string_array_add(sarray, val);
137
138         }
139
140         osrfLogInfo("\nPerforming(%d):  service %s "
141                         "| method %s |", getpid(), service, method );
142
143         int k;
144         for( k = 0; k!= sarray->size; k++ ) {
145                 osrfLogInfo( "param %s", string_array_get_string(sarray,k));
146         }
147
148         osrf_app_session* session = osrf_app_client_session_init(service);
149
150         osrfLogDebug("MOD session service: %s", session->remote_service );
151
152         int req_id = osrf_app_session_make_req( session, NULL, method, 1, sarray );
153         string_array_destroy(sarray);
154
155         osrf_message* omsg = NULL;
156
157         while((omsg = osrf_app_session_request_recv( session, req_id, 60 ))) {
158
159                 jsonObjectSetKey(response, "status", jsonNewNumberObject(omsg->status_code));
160
161                 if( omsg->_result_content ) {
162                         jsonObjectPush( payload, jsonObjectClone(omsg->_result_content));
163         
164                 } else {
165
166                         char* s = omsg->status_name ? omsg->status_name : "Unknown Error";
167                         char* t = omsg->status_text ? omsg->status_text : "No Error Message";
168                         jsonObjectSetKey(response, "debug", jsonNewObject("\n\n%s:\n%s\n", s, t));
169                         osrfLogError( "Gateway received error: %s", 
170                                         jsonObjectGetString(jsonObjectGetKey(response, "debug")));
171                         break;
172                 }
173
174                 osrf_message_free(omsg);
175                 omsg = NULL;
176         }
177
178         char* content = jsonObjectToJSON(response);
179         if(content) {
180                 osrfLogInfo( "Gateway responding with:  %s", content );
181                 ap_rputs(content,r);
182                 free(content);
183         } 
184         jsonObjectFree(response);
185
186         osrf_app_session_request_finish( session, req_id );
187         osrfLogDebug("gateway processed message successfully");
188
189         osrf_app_session_destroy(session);
190         return OK;
191 }
192
193 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
194         ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
195         ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
196 }
197
198
199 module AP_MODULE_DECLARE_DATA ils_gateway_module = {
200         STANDARD20_MODULE_STUFF,
201         NULL,
202         NULL,
203         ils_gateway_create_config,
204         NULL,
205         ils_gateway_cmds,
206         mod_ils_gateway_register_hooks,
207 };
208
209
210
211