]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/gateway/mod_ils_gateway.c
using our log stuff instead of stderr
[OpenSRF.git] / src / gateway / mod_ils_gateway.c
1 /*
2 Copyright 2002 Kevin O'Donnell
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19 /*
20  * Include the core server components.
21   */
22 #include "httpd.h"
23 #include "http_config.h"
24 #include "http_core.h"
25 #include "http_protocol.h"
26 #include "apr_compat.h"
27 #include "apr_strings.h"
28
29
30
31 /* our stuff */
32 #include "opensrf/transport_client.h"
33 #include "opensrf/generic_utils.h"
34 #include "opensrf/osrf_message.h"
35 #include "opensrf/osrf_app_session.h"
36 #include "md5.h"
37
38 /*
39  * This function is registered as a handler for HTTP methods and will
40   * therefore be invoked for all GET requests (and others).  Regardless
41    * of the request type, this function simply sends a message to
42  * STDERR (which httpd redirects to logs/error_log).  A real module
43   * would do *alot* more at this point.
44    */
45 #define MODULE_NAME "ils_gateway_module"
46
47
48 static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) {
49         if( ! osrf_system_bootstrap_client( 
50                 "/pines/cvs/ILS/OpenSRF/src/gateway/gateway.xml") ) { /* config option */
51         }
52         fprintf(stderr, "Bootstrapping %d\n", getpid() );
53         fflush(stderr);
54 }
55
56 static int mod_ils_gateway_method_handler (request_rec *r) {
57
58
59         /* make sure we're needed first thing*/
60         if (strcmp(r->handler, MODULE_NAME )) 
61                 return DECLINED;
62
63
64         apr_pool_t *p = r->pool;        /* memory pool */
65         char* arg = r->args;                    /* url query string */
66
67         char* service                                   = NULL; /* service to connect to */
68         char* method                                    = NULL; /* method to perform */
69
70         json* params                                    = NULL; /* method parameters */ 
71         json* exception                         = NULL; /* returned in error conditions */
72
73         growing_buffer* buffer          = NULL; /* POST data */
74         growing_buffer* tmp_buf         = NULL; /* temp buffer */
75
76         char* key                                               = NULL; /* query item name */
77         char* val                                               = NULL; /* query item value */
78
79
80
81         /* verify we are connected */
82         if(!osrf_system_get_transport_client()) {
83                 fatal_handler("Bootstrap Failed, no transport client");
84                 return HTTP_INTERNAL_SERVER_ERROR;
85         }
86
87         /* set content type to text/plain for passing around JSON objects */
88         ap_set_content_type(r, "text/plain");
89
90
91
92         /* gather the post args and append them to the url query string */
93         if( !strcmp(r->method,"POST") ) {
94
95                 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
96
97                 if(! ap_should_client_block(r)) {
98                         warning_handler("No Post Body");
99                 }
100
101                 char body[1024];
102                 memset(body,0,1024);
103                 buffer = buffer_init(1024);
104
105                 while(ap_get_client_block(r, body, 1024)) {
106                         buffer_add( buffer, body );
107                         memset(body,0,1024);
108                 }
109
110                 if(arg && arg[0]) {
111                         tmp_buf = buffer_init(1024);
112                         buffer_add(tmp_buf,arg);
113                         buffer_add(tmp_buf,buffer->buf);
114                         arg = (char*) apr_pstrdup(p, tmp_buf->buf);
115                         buffer_free(tmp_buf);
116                 } else {
117                         arg = (char*) apr_pstrdup(p, buffer->buf);
118                 }
119                 buffer_free(buffer);
120
121         } 
122
123
124         if( ! arg || !arg[0] ) { /* we received no request */
125                 warning_handler("No Args");
126                 return OK;
127         }
128
129         r->allowed |= (AP_METHOD_BIT << M_GET);
130         r->allowed |= (AP_METHOD_BIT << M_POST);
131
132         
133         char* argcopy = (char*) apr_pstrdup(p, arg);
134
135         params = json_object_new_array();;
136         while( argcopy && (val = ap_getword(p, &argcopy, '&'))) {
137
138                 key = ap_getword(r->pool,&val, '=');
139                 if(!key || !key[0])
140                         break;
141
142                 ap_unescape_url((char*)key);
143                 ap_unescape_url((char*)val);
144
145                 if(!strcmp(key,"service")) 
146                         service = val;
147
148                 if(!strcmp(key,"method"))
149                         method = val;
150
151                 if(!strcmp(key,"__param"))
152                         json_object_array_add( params, json_tokener_parse(val));
153         }
154
155         debug_handler("Performing(%d):  service %s | method %s | \nparams %s\n\n",
156                         getpid(), service, method, json_object_to_json_string(params));
157
158         osrf_app_session* session = osrf_app_client_session_init(service);
159
160         /* connect to the remote service */
161         if(!osrf_app_session_connect(session)) {
162                 exception = json_object_new_object();
163                 json_object_object_add( exception, "is_err", json_object_new_int(1));
164                 json_object_object_add( exception, 
165                                 "err_msg", json_object_new_string("Unable to connect to remote service"));
166
167                 ap_rputs(json_object_to_json_string(exception), r );
168                 json_object_put(exception);
169                 return OK;
170         }
171
172         int req_id = osrf_app_session_make_request( session, params, method, 1 );
173         json_object_put(params);
174
175         osrf_message* omsg = NULL;
176
177         growing_buffer* result_data = buffer_init(256);
178         buffer_add(result_data, "[");
179
180         /* gather result data */
181         while((omsg = osrf_app_session_request_recv( session, req_id, 20 ))) {
182
183                 if( omsg->result_string ) {
184                         buffer_add(result_data, omsg->result_string);
185                         debug_handler( "Received Response: %s", omsg->result_string );
186                         buffer_add( result_data, ",");
187
188                 } else {
189
190                         warning_handler("*** Looks like we got an exception\n" );
191
192                         /* build the exception information */
193                         growing_buffer* exc_buffer = buffer_init(256);
194                         buffer_add( exc_buffer, "\nReceived Exception:\nName: " );
195                         buffer_add( exc_buffer, omsg->status_name );
196                         buffer_add( exc_buffer, "\nStatus: " );
197                         buffer_add( exc_buffer, omsg->status_text );
198                         buffer_add( exc_buffer, "\nStatus: " );
199                         char code[16];
200                         memset(code, 0, 16);
201                         sprintf( code, "%d", omsg->status_code );
202                         buffer_add( exc_buffer, code );
203
204                         /* build the exception object */
205                         exception = json_object_new_object();
206                         json_object_object_add( exception, "is_err", json_object_new_int(1));
207                         json_object_object_add( exception, 
208                                         "err_msg", json_object_new_string(exc_buffer->buf));
209                         buffer_free(exc_buffer);
210                         osrf_message_free(omsg);
211                         break;
212                 }
213
214                 osrf_message_free(omsg);
215                 omsg = NULL;
216         }
217
218         /* remove trailing comma */
219         if( result_data->buf[strlen(result_data->buf)-1] == ',') {
220                 result_data->buf[strlen(result_data->buf)-1] = '\0';
221                 result_data->n_used--;
222         }
223
224         buffer_add(result_data,"]");
225
226         char* content = NULL;
227
228         /* round up our data */
229         if(exception) {
230                 content = strdup(json_object_to_json_string(exception));
231                 json_object_put(exception);
232         } else 
233                 content = buffer_data(result_data); 
234         
235
236         buffer_free(result_data);
237
238         if(content) {
239                 ap_rputs(content,r);
240                 free(content);
241         } 
242
243         osrf_app_session_request_finish( session, req_id );
244         osrf_app_session_disconnect( session );
245         osrf_app_session_destroy(session); //need to test removing this
246
247         return OK;
248
249 }
250
251 /*
252  * This function is a callback and it declares what other functions
253   * should be called for request processing and configuration requests.
254    * This callback function declares the Handlers for other events.  */
255 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
256 // I think this is the call to make to register a handler for method calls (GET PUT et. al.).
257 // We will ask to be last so that the comment has a higher tendency to
258 // go at the end.
259         ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
260         ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
261 }
262
263 /*
264  * Declare and populate the module's data structure.  The
265   * name of this structure ('tut1_module') is important - it
266    * must match the name of the module.  This structure is the
267  * only "glue" between the httpd core and the module.
268   */
269
270 module AP_MODULE_DECLARE_DATA ils_gateway_module =
271 {
272 STANDARD20_MODULE_STUFF,
273 NULL,
274 NULL,
275 NULL,
276 NULL,
277 NULL,
278 mod_ils_gateway_register_hooks,
279 };
280