1 #include "opensrf/osrf_message.h"
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]: JabberDisconnected Exception This JabberClient instance is no longer connected to the server' name='status'/><oils:domainObjectAttr value='500' name='statusCode'/></oils:domainObject></oils:domainObject></oils:root>";
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>"
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>"
35 int ret = osrf_message_from_xml( x, arr );
36 fprintf(stderr, "RET: %d\n", ret );
38 fatal_handler( "none parsed" );
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 );
44 // printf("Message 2 status %s, statusCode %d\n", xml_msg->status_text, xml_msg->status_code );
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 );
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));
63 osrf_message_set_request_info( msg, "add", params );
64 //osrf_message_set_result_content( msg, params );
65 json_object_put( params );
67 char* xml = osrf_message_to_xml( msg );
68 printf( "\n\nMessage as XML\n%s", xml );
70 osrf_message* xml_msg = osrf_message_from_xml( xml );
72 printf( "Message stuff \n\ntype %d"
73 "\nthread_trace %d \nprotocol %d "
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)
86 osrf_message_free( msg );
87 osrf_message_free( xml_msg );
94 osrf_message* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol ) {
96 osrf_message* msg = safe_malloc(sizeof(osrf_message));
98 msg->thread_trace = thread_trace;
99 msg->protocol = protocol;
106 void osrf_message_set_request_info( osrf_message* msg, char* method_name, json* json_params ) {
107 if( msg == NULL || method_name == NULL )
108 fatal_handler( "Bad params to osrf_message_set_request_params()" );
110 if( json_params != NULL )
111 msg->params = json_tokener_parse(json_object_to_json_string(json_params));
113 msg->params = json_tokener_parse("[]");
115 msg->method_name = strdup( method_name );
120 void osrf_message_set_status_info(
121 osrf_message* msg, char* status_name, char* status_text, int status_code ) {
124 fatal_handler( "Bad params to osrf_message_set_status_info()" );
126 if( msg->m_type == STATUS )
127 if( status_name != NULL )
128 msg->status_name = strdup( status_name );
130 if( status_text != NULL )
131 msg->status_text = strdup( status_text );
133 msg->status_code = status_code;
137 void osrf_message_set_result_content( osrf_message* msg, json* result_content ) {
139 fatal_handler( "Bad params to osrf_message_set_result_content()" );
140 msg->result_content = json_tokener_parse(json_object_to_json_string(result_content));
145 void osrf_message_free( osrf_message* msg ) {
147 warning_handler( "Trying to delete NULL osrf_message" );
149 if( msg->status_name != NULL )
150 free(msg->status_name);
152 if( msg->status_text != NULL )
153 free(msg->status_text);
155 if( msg->result_content != NULL )
156 json_object_put( msg->result_content );
158 if( msg->method_name != NULL )
159 free(msg->method_name);
161 if( msg->params != NULL )
162 json_object_put( msg->params );
169 /* here's where things get interesting */
170 char* osrf_message_to_xml( osrf_message* msg ) {
179 xmlKeepBlanksDefault(0);
181 xmlNodePtr message_node;
182 xmlNodePtr type_node;
183 xmlNodePtr thread_trace_node;
184 xmlNodePtr protocol_node;
185 xmlNodePtr status_node;
186 xmlNodePtr status_text_node;
187 xmlNodePtr status_code_node;
188 xmlNodePtr method_node;
189 xmlNodePtr method_name_node;
190 xmlNodePtr params_node;
191 xmlNodePtr result_node;
192 xmlNodePtr content_node;
195 xmlDocPtr doc = xmlReadDoc(
196 BAD_CAST "<oils:root xmlns:oils='http://open-ils.org/xml/namespaces/oils_v1'>"
197 "<oils:domainObject name='oilsMessage'/></oils:root>",
198 NULL, NULL, XML_PARSE_NSCLEAN );
200 message_node = xmlDocGetRootElement(doc)->children; /* since it's the only child */
201 type_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObjectAttr", NULL );
202 thread_trace_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObjectAttr", NULL );
203 protocol_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObjectAttr", NULL );
207 sprintf(tt,"%d",msg->thread_trace);
208 xmlSetProp( thread_trace_node, BAD_CAST "name", BAD_CAST "threadTrace" );
209 xmlSetProp( thread_trace_node, BAD_CAST "value", BAD_CAST tt );
213 sprintf(prot,"%d",msg->protocol);
214 xmlSetProp( protocol_node, BAD_CAST "name", BAD_CAST "protocol" );
215 xmlSetProp( protocol_node, BAD_CAST "value", BAD_CAST prot );
217 switch(msg->m_type) {
220 xmlSetProp( type_node, BAD_CAST "name", BAD_CAST "type" );
221 xmlSetProp( type_node, BAD_CAST "value", BAD_CAST "CONNECT" );
225 xmlSetProp( type_node, BAD_CAST "type", "DISCONNECT" );
230 xmlSetProp( type_node, BAD_CAST "name", BAD_CAST "type" );
231 xmlSetProp( type_node, BAD_CAST "value", BAD_CAST "STATUS" );
232 status_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObject", NULL );
233 xmlSetProp( status_node, BAD_CAST "name", BAD_CAST msg->status_name );
235 status_text_node = xmlNewChild( status_node, NULL, BAD_CAST "domainObjectAttr", NULL );
236 xmlSetProp( status_text_node, BAD_CAST "name", BAD_CAST "status" );
237 xmlSetProp( status_text_node, BAD_CAST "value", BAD_CAST msg->status_text);
239 status_code_node = xmlNewChild( status_node, NULL, BAD_CAST "domainObjectAttr", NULL );
240 xmlSetProp( status_code_node, BAD_CAST "name", BAD_CAST "statusCode" );
244 sprintf(sc,"%d",msg->status_code);
245 xmlSetProp( status_code_node, BAD_CAST "value", BAD_CAST sc);
251 xmlSetProp( type_node, BAD_CAST "name", "type" );
252 xmlSetProp( type_node, BAD_CAST "value", "REQUEST" );
253 method_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObject", NULL );
254 xmlSetProp( method_node, BAD_CAST "name", BAD_CAST "oilsMethod" );
256 if( msg->method_name != NULL ) {
258 method_name_node = xmlNewChild( method_node, NULL, BAD_CAST "domainObjectAttr", NULL );
259 xmlSetProp( method_name_node, BAD_CAST "name", BAD_CAST "method" );
260 xmlSetProp( method_name_node, BAD_CAST "value", BAD_CAST msg->method_name );
262 if( msg->params != NULL ) {
263 params_node = xmlNewChild( method_node, NULL,
264 BAD_CAST "params", BAD_CAST json_object_to_json_string( msg->params ) );
272 xmlSetProp( type_node, BAD_CAST "name", BAD_CAST "type" );
273 xmlSetProp( type_node, BAD_CAST "value", BAD_CAST "RESULT" );
274 result_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObject", NULL );
275 xmlSetProp( result_node, BAD_CAST "name", BAD_CAST "oilsResult" );
277 status_text_node = xmlNewChild( result_node, NULL, BAD_CAST "domainObjectAttr", NULL );
278 xmlSetProp( status_text_node, BAD_CAST "name", BAD_CAST "status" );
279 xmlSetProp( status_text_node, BAD_CAST "value", BAD_CAST msg->status_text);
281 status_code_node = xmlNewChild( result_node, NULL, BAD_CAST "domainObjectAttr", NULL );
282 xmlSetProp( status_code_node, BAD_CAST "name", BAD_CAST "statusCode" );
286 sprintf(stc,"%d",msg->status_code);
287 xmlSetProp( status_code_node, BAD_CAST "value", BAD_CAST stc);
289 content_node = xmlNewChild( result_node, NULL,
290 BAD_CAST "domainObject", BAD_CAST json_object_to_json_string( msg->result_content ) );
291 xmlSetProp( content_node, BAD_CAST "name", BAD_CAST "oilsScalar" );
296 warning_handler( "Recieved bogus message type" );
301 // -----------------------------------------------------
302 // Dump the XML doc to a string and remove the
304 // -----------------------------------------------------
306 /* passing in a '1' means we want to retain the formatting */
307 xmlDocDumpFormatMemory( doc, &xmlbuf, &bufsize, 0 );
308 encoded_msg = strdup( (char*) xmlbuf );
310 if( encoded_msg == NULL )
311 fatal_handler("message_to_xml(): Out of Memory");
318 /*** remove the XML declaration */
319 int len = strlen(encoded_msg);
321 memset( tmp, 0, len );
325 /* when we reach the first >, take everything after it */
326 for( i = 0; i!= len; i++ ) {
327 if( encoded_msg[i] == 62) { /* ascii > */
329 /* found_at holds the starting index of the rest of the doc*/
336 /* move the shortened doc into the tmp buffer */
337 strncpy( tmp, encoded_msg + found_at, len - found_at );
338 /* move the tmp buffer back into the allocated space */
339 memset( encoded_msg, 0, len );
340 strcpy( encoded_msg, tmp );
348 int osrf_message_from_xml( char* xml, osrf_message* msgs[] ) {
352 xmlKeepBlanksDefault(0);
354 xmlNodePtr message_node;
355 xmlDocPtr doc = xmlReadDoc(
356 BAD_CAST xml, NULL, NULL, XML_PARSE_NSCLEAN );
358 xmlNodePtr root =xmlDocGetRootElement(doc);
360 warning_handler( "Attempt to build message from incomplete xml %s", xml );
365 message_node = root->children; /* since it's the only child */
368 warning_handler( "Attempt to build message from incomplete xml %s", xml );
372 while( message_node != NULL ) {
374 xmlNodePtr cur_node = message_node->children;
375 osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
380 xmlChar* name = NULL;
381 xmlChar* value = NULL;
383 /* we're a domainObjectAttr */
384 if( !strcmp((char*)cur_node->name,"domainObjectAttr" )) {
385 name = xmlGetProp( cur_node, BAD_CAST "name");
389 value = xmlGetProp( cur_node, BAD_CAST "value" );
392 if( (!strcmp((char*)name, "type")) ) { /* what type are we? */
394 if(!strcmp((char*)value, "CONNECT"))
395 new_msg->m_type = CONNECT;
397 if(!strcmp((char*)value, "DISCONNECT"))
398 new_msg->m_type = DISCONNECT;
400 if(!strcmp((char*)value, "STATUS"))
401 new_msg->m_type = STATUS;
403 if(!strcmp((char*)value, "REQUEST"))
404 new_msg->m_type = REQUEST;
406 if(!strcmp((char*)value, "RESULT"))
407 new_msg->m_type = RESULT;
409 } else if((!strcmp((char*)name, "threadTrace"))) {
410 new_msg->thread_trace = atoi( (char*) value );
412 } else if((!strcmp((char*)name, "protocol"))) {
413 new_msg->protocol = atoi( (char*) value );
422 /* we're a domainObject */
423 if( !strcmp((char*)cur_node->name,"domainObject" )) {
425 name = xmlGetProp( cur_node, BAD_CAST "name");
429 if( !strcmp(name,"oilsMethod") ) {
431 xmlNodePtr meth_node = cur_node->children;
433 while( meth_node != NULL ) {
435 if( !strcmp((char*)meth_node->name,"domainObjectAttr" )) {
436 char* meth_name = xmlGetProp( meth_node, BAD_CAST "value" );
438 new_msg->method_name = strdup(meth_name);
443 if( !strcmp((char*)meth_node->name,"params" ) && meth_node->children->content )
444 new_msg->params = json_object_new_string( meth_node->children->content );
445 //new_msg->params = json_tokener_parse(ng(json_params));
447 meth_node = meth_node->next;
451 if( !strcmp(name,"oilsResult") || new_msg->m_type == STATUS ) {
453 xmlNodePtr result_nodes = cur_node->children;
455 while( result_nodes ) {
457 if(!strcmp(result_nodes->name,"domainObjectAttr")) {
459 xmlChar* result_attr_name = xmlGetProp( result_nodes, BAD_CAST "name");
460 if(result_attr_name) {
461 xmlChar* result_attr_value = xmlGetProp( result_nodes, BAD_CAST "value" );
463 if( result_attr_value ) {
464 if((!strcmp((char*)result_attr_name, "status")))
465 new_msg->status_text = strdup((char*) result_attr_value );
467 else if((!strcmp((char*)result_attr_name, "statusCode")))
468 new_msg->status_code = atoi((char*) result_attr_value );
469 xmlFree(result_attr_value);
472 xmlFree(result_attr_name);
478 if(!strcmp(result_nodes->name,"domainObject")) {
479 xmlChar* r_name = xmlGetProp( result_nodes, BAD_CAST "name" );
481 if( !strcmp((char*)r_name,"oilsScalar") && result_nodes->children->content )
482 new_msg->result_content = json_object_new_string( result_nodes->children->content );
486 result_nodes = result_nodes->next;
490 if( new_msg->m_type == STATUS ) { new_msg->status_name = strdup(name); }
495 /* we're a params node */
496 if( !strcmp((char*)cur_node->name,"params" )) {
500 cur_node = cur_node->next;
503 msgs[msg_index] = new_msg;
505 message_node = message_node->next;
507 } // while message_node != null
509 xmlCleanupCharEncodingHandlers();