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