]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_message.c
OK. This is the first early C version of the OpenSRF stack. It's highly
[OpenSRF.git] / src / libstack / osrf_message.c
1 #include "opensrf/osrf_message.h"
2
3
4
5 /*
6 int main() {
7
8         char* x = "<oils:root xmlns:oils='http://open-ils.org/xml/namespaces/oils_v1'><oils:domainObject name='oilsMessage'><oils:domainObjectAttr value='STATUS' name='type'/><oils:domainObjectAttr value='12' name='threadTrace'/><oils:domainObject name='oilsMethodException'><oils:domainObjectAttr value=' *** Call to [div] failed for session [1351200643.110915057523738], thread trace [12]:&#10;JabberDisconnected Exception &#10;This JabberClient instance is no longer connected to the server' name='status'/><oils:domainObjectAttr value='500' name='statusCode'/></oils:domainObject></oils:domainObject></oils:root>";
9         */
10
11         /*
12         char* x = "<oils:root xmlns:oils='http://open-ils.org/xml/namespaces/oils_v1'>"
13                                 "<oils:domainObject name='oilsMessage'>"
14                                 "<oils:domainObjectAttr value='STATUS' name='type'/>"
15                                 "<oils:domainObjectAttr value='1' name='threadTrace'/>"
16                                 "<oils:domainObject name='oilsConnectStatus'>"
17                                 "<oils:domainObjectAttr value='Connection Successful' name='status'/>"
18                                 "<oils:domainObjectAttr value='200' name='statusCode'/>"
19                                 "</oils:domainObject></oils:domainObject>"
20
21                                 "<oils:domainObject name='oilsMessage'>"
22                                 "<oils:domainObjectAttr value='STATUS' name='type'/>"
23                                 "<oils:domainObjectAttr value='1' name='threadTrace'/>"
24                                 "<oils:domainObject name='oilsConnectStatus'>"
25                                 "<oils:domainObjectAttr value='Request Complete' name='status'/>"
26                                 "<oils:domainObjectAttr value='205' name='statusCode'/>"
27                                 "</oils:domainObject></oils:domainObject>"
28                                 
29                                 "</oils:root>";
30                                 */
31
32 /*
33         osrf_message* arr[4];
34         memset(arr, 0, 4);
35         int ret = osrf_message_from_xml( x, arr );
36         fprintf(stderr, "RET: %d\n", ret );
37         if(ret<=0)
38                 fatal_handler( "none parsed" );
39
40         osrf_message* xml_msg = arr[0];
41         printf("Message name: %s\nstatus %s, \nstatusCode %d\n", xml_msg->status_name, xml_msg->status_text, xml_msg->status_code );
42
43 //      xml_msg = arr[1];
44 //      printf("Message 2 status %s, statusCode %d\n", xml_msg->status_text, xml_msg->status_code );
45
46
47         return 0;
48 }
49 */
50
51
52
53         /*
54         osrf_message* msg = osrf_message_init( STATUS, 1, 1 );
55 //      osrf_message* msg = osrf_message_init( CONNECT, 1, 1 );
56         //osrf_message* msg = osrf_message_init( REQUEST, 1, 1 );
57         osrf_message_set_status_info( msg, "oilsConnectStatus", "Connection Succsesful", 200 );
58
59         json* params = json_object_new_array();
60         json_object_array_add(params, json_object_new_int(1));
61         json_object_array_add(params, json_object_new_int(2));
62
63         osrf_message_set_request_info( msg, "add", params );
64         //osrf_message_set_result_content( msg, params );
65         json_object_put( params );
66
67         char* xml =  osrf_message_to_xml( msg );
68         printf( "\n\nMessage as XML\n%s", xml );
69
70         osrf_message* xml_msg = osrf_message_from_xml( xml );
71
72         printf( "Message stuff \n\ntype %d"
73                         "\nthread_trace %d \nprotocol %d "
74                         "\nstatus_name %s"
75                         "\nstatus_text %s\nstatus_code %d" 
76                         "\nresult_content %s \nparams %s"
77                         "\n", xml_msg->m_type, 
78                         xml_msg->thread_trace, xml_msg->protocol, xml_msg->status_name, 
79                         xml_msg->status_text, xml_msg->status_code, 
80                         json_object_to_json_string( xml_msg->result_content),
81                         json_object_to_json_string(xml_msg->params) 
82                         );
83
84
85         free(xml);
86         osrf_message_free( msg );
87         osrf_message_free( xml_msg );
88         return 0;
89         
90 }
91 */
92
93
94 osrf_message* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol ) {
95
96         osrf_message* msg = safe_malloc(sizeof(osrf_message));
97         msg->m_type = type;
98         msg->thread_trace = thread_trace;
99         msg->protocol = protocol;
100         msg->next = NULL;
101
102         return msg;
103 }
104
105
106 void osrf_message_set_request_info( osrf_message* msg, char* method_name, json* json_params ) {
107         if( msg == NULL || method_name == NULL || json_params == NULL )
108                 fatal_handler( "Bad params to osrf_message_set_request_params()" );
109
110         msg->params = json_tokener_parse(json_object_to_json_string(json_params));
111         msg->method_name = strdup( method_name );
112 }
113
114
115
116 void osrf_message_set_status_info( 
117                 osrf_message* msg, char* status_name, char* status_text, int status_code ) {
118
119         if( msg == NULL )
120                 fatal_handler( "Bad params to osrf_message_set_status_info()" );
121
122         if( msg->m_type == STATUS ) 
123                 if( status_name != NULL ) 
124                         msg->status_name = strdup( status_name );
125
126         if( status_text != NULL )
127                 msg->status_text = strdup( status_text );
128
129         msg->status_code = status_code;
130 }
131
132
133 void osrf_message_set_result_content( osrf_message* msg, json* result_content ) {
134         if( msg == NULL )
135                 fatal_handler( "Bad params to osrf_message_set_result_content()" );
136         msg->result_content = json_tokener_parse(json_object_to_json_string(result_content));
137 }
138
139
140
141 void osrf_message_free( osrf_message* msg ) {
142         if( msg == NULL )
143                 warning_handler( "Trying to delete NULL osrf_message" );
144
145         if( msg->status_name != NULL )
146                 free(msg->status_name);
147
148         if( msg->status_text != NULL )
149                 free(msg->status_text);
150
151         if( msg->result_content != NULL )
152                 json_object_put( msg->result_content );
153
154         if( msg->method_name != NULL )
155                 free(msg->method_name);
156
157         if( msg->params != NULL )
158                 json_object_put( msg->params );
159
160         free(msg);
161 }
162
163
164                 
165 /* here's where things get interesting */
166 char* osrf_message_to_xml( osrf_message* msg ) {
167
168         if( msg == NULL )
169                 return NULL;
170
171         int                     bufsize;
172         xmlChar*                xmlbuf;
173         char*                   encoded_msg;
174
175         xmlKeepBlanksDefault(0);
176
177         xmlNodePtr      message_node;
178         xmlNodePtr      type_node;
179         xmlNodePtr      thread_trace_node;
180         xmlNodePtr      protocol_node;
181         xmlNodePtr      status_node;
182         xmlNodePtr      status_text_node;
183         xmlNodePtr      status_code_node;
184         xmlNodePtr      method_node;
185         xmlNodePtr      method_name_node;
186         xmlNodePtr      params_node;
187         xmlNodePtr      result_node;
188         xmlNodePtr      content_node;
189
190
191         xmlDocPtr       doc = xmlReadDoc( 
192                         BAD_CAST "<oils:root xmlns:oils='http://open-ils.org/xml/namespaces/oils_v1'>"
193                         "<oils:domainObject name='oilsMessage'/></oils:root>", 
194                         NULL, NULL, XML_PARSE_NSCLEAN );
195
196         message_node = xmlDocGetRootElement(doc)->children; /* since it's the only child */
197         type_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObjectAttr", NULL );
198         thread_trace_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObjectAttr", NULL );
199         protocol_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObjectAttr", NULL );
200
201         char tt[64];
202         memset(tt,0,64);
203         sprintf(tt,"%d",msg->thread_trace);
204         xmlSetProp( thread_trace_node, BAD_CAST "name", BAD_CAST "threadTrace" );
205         xmlSetProp( thread_trace_node, BAD_CAST "value", BAD_CAST tt );
206
207         char prot[64];
208         memset(prot,0,64);
209         sprintf(prot,"%d",msg->protocol);
210         xmlSetProp( protocol_node, BAD_CAST "name", BAD_CAST "protocol" );
211         xmlSetProp( protocol_node, BAD_CAST "value", BAD_CAST prot );
212
213         switch(msg->m_type) {
214
215                 case CONNECT: 
216                         xmlSetProp( type_node, BAD_CAST "name", BAD_CAST "type" );
217                         xmlSetProp( type_node, BAD_CAST "value", BAD_CAST "CONNECT" );
218                         break;
219
220                 case DISCONNECT:
221                         xmlSetProp( type_node, BAD_CAST "type", "DISCONNECT" );
222                         break;
223
224                 case STATUS:
225
226                         xmlSetProp( type_node, BAD_CAST "name", BAD_CAST "type" );
227                         xmlSetProp( type_node, BAD_CAST "value", BAD_CAST "STATUS" );
228                         status_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObject", NULL );
229                         xmlSetProp( status_node, BAD_CAST "name", BAD_CAST msg->status_name );
230
231                         status_text_node = xmlNewChild( status_node, NULL, BAD_CAST "domainObjectAttr", NULL );
232                         xmlSetProp( status_text_node, BAD_CAST "name", BAD_CAST "status" );
233                         xmlSetProp( status_text_node, BAD_CAST "value", BAD_CAST msg->status_text);
234
235                         status_code_node = xmlNewChild( status_node, NULL, BAD_CAST "domainObjectAttr", NULL );
236                         xmlSetProp( status_code_node, BAD_CAST "name", BAD_CAST "statusCode" );
237
238                         char sc[64];
239                         memset(sc,0,64);
240                         sprintf(sc,"%d",msg->status_code);
241                         xmlSetProp( status_code_node, BAD_CAST "value", BAD_CAST sc);
242                         
243                         break;
244
245                 case REQUEST:
246
247                         xmlSetProp( type_node, BAD_CAST "name", "type" );
248                         xmlSetProp( type_node, BAD_CAST "value", "REQUEST" );
249                         method_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObject", NULL );
250                         xmlSetProp( method_node, BAD_CAST "name", BAD_CAST "oilsMethod" );
251
252                         if( msg->method_name != NULL ) {
253
254                                 method_name_node = xmlNewChild( method_node, NULL, BAD_CAST "domainObjectAttr", NULL );
255                                 xmlSetProp( method_name_node, BAD_CAST "name", BAD_CAST "method" );
256                                 xmlSetProp( method_name_node, BAD_CAST "value", BAD_CAST msg->method_name );
257
258                                 if( msg->params != NULL ) {
259                                         params_node = xmlNewChild( method_node, NULL, 
260                                                 BAD_CAST "params", BAD_CAST json_object_to_json_string( msg->params ) );
261                                 }
262                         }
263
264                         break;
265
266                 case RESULT:
267
268                         xmlSetProp( type_node, BAD_CAST "name", BAD_CAST "type" );
269                         xmlSetProp( type_node, BAD_CAST "value", BAD_CAST "RESULT" );
270                         result_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObject", NULL );
271                         xmlSetProp( result_node, BAD_CAST "name", BAD_CAST "oilsResult" );
272
273                         status_text_node = xmlNewChild( result_node, NULL, BAD_CAST "domainObjectAttr", NULL );
274                         xmlSetProp( status_text_node, BAD_CAST "name", BAD_CAST "status" );
275                         xmlSetProp( status_text_node, BAD_CAST "value", BAD_CAST msg->status_text);
276
277                         status_code_node = xmlNewChild( result_node, NULL, BAD_CAST "domainObjectAttr", NULL );
278                         xmlSetProp( status_code_node, BAD_CAST "name", BAD_CAST "statusCode" );
279
280                         char stc[64];
281                         memset(stc,0,64);
282                         sprintf(stc,"%d",msg->status_code);
283                         xmlSetProp( status_code_node, BAD_CAST "value", BAD_CAST stc);
284
285                         content_node = xmlNewChild( result_node, NULL, 
286                                         BAD_CAST "domainObject", BAD_CAST json_object_to_json_string( msg->result_content ) );
287                         xmlSetProp( content_node, BAD_CAST "name", BAD_CAST "oilsScalar" );
288
289                         break;
290
291                 default:
292                         warning_handler( "Recieved bogus message type" );
293                         return NULL;
294         }
295
296
297         // -----------------------------------------------------
298         // Dump the XML doc to a string and remove the 
299         // xml declaration
300         // -----------------------------------------------------
301
302         /* passing in a '1' means we want to retain the formatting */
303         xmlDocDumpFormatMemory( doc, &xmlbuf, &bufsize, 0 );
304         encoded_msg = strdup( (char*) xmlbuf );
305
306         if( encoded_msg == NULL ) 
307                 fatal_handler("message_to_xml(): Out of Memory");
308
309         xmlFree(xmlbuf);
310         xmlFreeDoc( doc );
311         xmlCleanupParser();
312
313
314         /*** remove the XML declaration */
315         int len = strlen(encoded_msg);
316         char tmp[len];
317         memset( tmp, 0, len );
318         int i;
319         int found_at = 0;
320
321         /* when we reach the first >, take everything after it */
322         for( i = 0; i!= len; i++ ) {
323                 if( encoded_msg[i] == 62) { /* ascii > */
324
325                         /* found_at holds the starting index of the rest of the doc*/
326                         found_at = i + 1; 
327                         break;
328                 }
329         }
330
331         if( found_at ) {
332                 /* move the shortened doc into the tmp buffer */
333                 strncpy( tmp, encoded_msg + found_at, len - found_at );
334                 /* move the tmp buffer back into the allocated space */
335                 memset( encoded_msg, 0, len );
336                 strcpy( encoded_msg, tmp );
337         }
338
339         return encoded_msg;
340
341 }
342
343
344 int osrf_message_from_xml( char* xml, osrf_message* msgs[] ) {
345
346         if(!xml) return 0;
347
348         xmlKeepBlanksDefault(0);
349
350         xmlNodePtr      message_node;
351         xmlDocPtr       doc = xmlReadDoc( 
352                         BAD_CAST xml, NULL, NULL, XML_PARSE_NSCLEAN );
353
354         xmlNodePtr root =xmlDocGetRootElement(doc);
355         if(!root) {
356                 warning_handler( "Attempt to build message from incomplete xml %s", xml );
357                 return 0;
358         }
359
360         int msg_index = 0;
361         message_node = root->children; /* since it's the only child */
362
363         if(!message_node) {
364                 warning_handler( "Attempt to build message from incomplete xml %s", xml );
365                 return 0;
366         }
367
368         while( message_node != NULL ) {
369
370                 xmlNodePtr cur_node = message_node->children;
371                 osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
372         
373
374                 while( cur_node ) {
375
376                         xmlChar* name = NULL; 
377                         xmlChar* value = NULL;
378                         
379                         /* we're a domainObjectAttr */
380                         if( !strcmp((char*)cur_node->name,"domainObjectAttr" )) {
381                                 name = xmlGetProp( cur_node, BAD_CAST "name");
382         
383                                 if(name) {
384         
385                                         value = xmlGetProp( cur_node, BAD_CAST "value" );
386                                         if(value) {
387         
388                                                 if( (!strcmp((char*)name, "type")) ) { /* what type are we? */
389         
390                                                         if(!strcmp((char*)value, "CONNECT"))
391                                                                 new_msg->m_type = CONNECT;
392         
393                                                         if(!strcmp((char*)value, "DISCONNECT"))
394                                                                 new_msg->m_type = DISCONNECT;
395                 
396                                                         if(!strcmp((char*)value, "STATUS"))
397                                                                 new_msg->m_type = STATUS;
398                 
399                                                         if(!strcmp((char*)value, "REQUEST"))
400                                                                 new_msg->m_type = REQUEST;
401                                                         
402                                                         if(!strcmp((char*)value, "RESULT"))
403                                                                 new_msg->m_type = RESULT;
404                         
405                                                 } else if((!strcmp((char*)name, "threadTrace"))) {
406                                                         new_msg->thread_trace = atoi( (char*) value );
407                         
408                                                 } else if((!strcmp((char*)name, "protocol"))) {
409                                                         new_msg->protocol = atoi( (char*) value );
410                                                 }
411         
412                                                 xmlFree(value);
413                                         }
414                                         xmlFree(name);
415                                 }
416                         }
417         
418                         /* we're a domainObject */
419                         if( !strcmp((char*)cur_node->name,"domainObject" )) {
420         
421                                 name = xmlGetProp( cur_node, BAD_CAST "name");
422         
423                                 if(name) {
424         
425                                         if( !strcmp(name,"oilsMethod") ) {
426         
427                                                 xmlNodePtr meth_node = cur_node->children;
428         
429                                                 while( meth_node != NULL ) {
430         
431                                                         if( !strcmp((char*)meth_node->name,"domainObjectAttr" )) {
432                                                                 char* meth_name = xmlGetProp( meth_node, BAD_CAST "value" );
433                                                                 if(meth_name) {
434                                                                         new_msg->method_name = strdup(meth_name);
435                                                                         xmlFree(meth_name);
436                                                                 }
437                                                         }
438         
439                                                         if( !strcmp((char*)meth_node->name,"params" ) && meth_node->children->content ) 
440                                                                 new_msg->params = json_object_new_string( meth_node->children->content );
441                                                                 //new_msg->params = json_tokener_parse(ng(json_params));
442         
443                                                         meth_node = meth_node->next;
444                                                 }
445                                         } //oilsMethod
446         
447                                         if( !strcmp(name,"oilsResult") || new_msg->m_type == STATUS ) {
448         
449                                                 xmlNodePtr result_nodes = cur_node->children;
450         
451                                                 while( result_nodes ) {
452         
453                                                         if(!strcmp(result_nodes->name,"domainObjectAttr")) {
454         
455                                                                 xmlChar* result_attr_name = xmlGetProp( result_nodes, BAD_CAST "name");
456                                                                 if(result_attr_name) {
457                                                                         xmlChar* result_attr_value = xmlGetProp( result_nodes, BAD_CAST "value" );
458         
459                                                                         if( result_attr_value ) {
460                                                                                 if((!strcmp((char*)result_attr_name, "status"))) 
461                                                                                         new_msg->status_text = strdup((char*) result_attr_value );
462         
463                                                                                 else if((!strcmp((char*)result_attr_name, "statusCode"))) 
464                                                                                         new_msg->status_code = atoi((char*) result_attr_value );
465                                                                                 xmlFree(result_attr_value);
466                                                                         }
467         
468                                                                         xmlFree(result_attr_name);
469                                                                 }
470         
471                                                         }
472                                                 
473         
474                                                         if(!strcmp(result_nodes->name,"domainObject")) {
475                                                                 xmlChar* r_name = xmlGetProp( result_nodes, BAD_CAST "name" );
476                                                                 if(r_name) {
477                                                                         if( !strcmp((char*)r_name,"oilsScalar") && result_nodes->children->content ) 
478                                                                                 new_msg->result_content = json_object_new_string( result_nodes->children->content );
479                                                                         xmlFree(r_name);
480                                                                 }
481                                                         }
482                                                         result_nodes = result_nodes->next;
483                                                 }
484                                         }
485                                         
486                                         if( new_msg->m_type == STATUS ) { new_msg->status_name = strdup(name); }
487                                         xmlFree(name);
488                                 }
489                         }
490         
491                         /* we're a params node */
492                         if( !strcmp((char*)cur_node->name,"params" )) {
493         
494                         }
495         
496                         cur_node = cur_node->next;
497                 }
498         
499                 msgs[msg_index] = new_msg;
500                 msg_index++;
501                 message_node = message_node->next;
502
503         } // while message_node != null
504
505         xmlCleanupCharEncodingHandlers();
506         xmlFreeDoc( doc );
507         xmlCleanupParser();
508
509         return msg_index;
510
511 }
512
513