1 #include "osrf_message.h"
4 int parse_json_result = 1;
5 int parse_json_params = 1;
7 /* utility function for debugging a DOM doc */
8 static void recurse_doc( xmlNodePtr node ) {
9 if( node == NULL ) return;
10 debug_handler("Recurse: %s => %s", node->name, node->content );
11 xmlNodePtr t = node->children;
20 osrf_message* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol ) {
22 osrf_message* msg = (osrf_message*) safe_malloc(sizeof(osrf_message));
24 msg->thread_trace = thread_trace;
25 msg->protocol = protocol;
27 msg->is_exception = 0;
28 msg->parse_json_result = parse_json_result;
29 msg->parse_json_params = parse_json_params;
30 msg->parray = init_string_array(16); /* start out with a slot for 16 params. can grow */
37 void osrf_message_set_json_parse_result( int ibool ) {
38 parse_json_result = ibool;
41 void osrf_message_set_json_parse_params( int ibool ) {
42 parse_json_params = ibool;
45 void osrf_message_set_request_info(
46 osrf_message* msg, char* method_name, json* json_params ) {
48 if( msg == NULL || method_name == NULL )
49 fatal_handler( "Bad params to osrf_message_set_request_params()" );
51 if(msg->parse_json_params) {
52 if( json_params != NULL )
53 msg->params = json_tokener_parse(json_object_to_json_string(json_params));
55 msg->params = json_tokener_parse("[]");
58 msg->method_name = strdup( method_name );
62 /* only works of parse_json_params is false */
63 void osrf_message_add_param( osrf_message* msg, char* param_string ) {
64 if(msg == NULL || param_string == NULL)
66 if(!msg->parse_json_params)
67 string_array_add(msg->parray, param_string);
71 void osrf_message_set_status_info(
72 osrf_message* msg, char* status_name, char* status_text, int status_code ) {
75 fatal_handler( "Bad params to osrf_message_set_status_info()" );
77 if( status_name != NULL )
78 msg->status_name = strdup( status_name );
80 if( status_text != NULL )
81 msg->status_text = strdup( status_text );
83 msg->status_code = status_code;
87 void osrf_message_set_result_content( osrf_message* msg, char* json_string ) {
88 if( msg == NULL || json_string == NULL)
89 warning_handler( "Bad params to osrf_message_set_result_content()" );
91 msg->result_string = strdup(json_string);
93 /* ----------------------------------------------------- */
94 object* o = json_parse_string(json_string);
95 char* string = o->to_json(o);
96 debug_handler("---------------------------------------------------");
97 debug_handler("Parsed JSON string \n%s\n", string);
99 debug_handler("Class is %s\n", o->classname);
100 debug_handler("---------------------------------------------------");
103 /* ----------------------------------------------------- */
105 debug_handler( "Message Parse JSON is set to: %d", msg->parse_json_result );
107 if(msg->parse_json_result)
108 msg->result_content = json_tokener_parse(msg->result_string);
113 void osrf_message_free( osrf_message* msg ) {
117 if( msg->status_name != NULL )
118 free(msg->status_name);
120 if( msg->status_text != NULL )
121 free(msg->status_text);
123 if( msg->result_content != NULL )
124 json_object_put( msg->result_content );
126 if( msg->result_string != NULL )
127 free( msg->result_string);
129 if( msg->method_name != NULL )
130 free(msg->method_name);
132 if( msg->params != NULL )
133 json_object_put( msg->params );
135 string_array_destroy(msg->parray);
142 /* here's where things get interesting */
143 char* osrf_message_to_xml( osrf_message* msg ) {
152 xmlKeepBlanksDefault(0);
154 xmlNodePtr message_node;
155 xmlNodePtr type_node;
156 xmlNodePtr thread_trace_node;
157 xmlNodePtr protocol_node;
158 xmlNodePtr status_node;
159 xmlNodePtr status_text_node;
160 xmlNodePtr status_code_node;
161 xmlNodePtr method_node = NULL;
162 xmlNodePtr method_name_node;
163 xmlNodePtr params_node = NULL;
164 xmlNodePtr result_node;
165 xmlNodePtr content_node;
168 xmlDocPtr doc = xmlReadDoc(
169 BAD_CAST "<oils:root xmlns:oils='http://open-ils.org/xml/namespaces/oils_v1'>"
170 "<oils:domainObject name='oilsMessage'/></oils:root>",
171 NULL, NULL, XML_PARSE_NSCLEAN );
173 message_node = xmlDocGetRootElement(doc)->children; /* since it's the only child */
174 type_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObjectAttr", NULL );
175 thread_trace_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObjectAttr", NULL );
176 protocol_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObjectAttr", NULL );
180 sprintf(tt,"%d",msg->thread_trace);
181 xmlSetProp( thread_trace_node, BAD_CAST "name", BAD_CAST "threadTrace" );
182 xmlSetProp( thread_trace_node, BAD_CAST "value", BAD_CAST tt );
186 sprintf(prot,"%d",msg->protocol);
187 xmlSetProp( protocol_node, BAD_CAST "name", BAD_CAST "protocol" );
188 xmlSetProp( protocol_node, BAD_CAST "value", BAD_CAST prot );
190 switch(msg->m_type) {
193 xmlSetProp( type_node, BAD_CAST "name", BAD_CAST "type" );
194 xmlSetProp( type_node, BAD_CAST "value", BAD_CAST "CONNECT" );
198 xmlSetProp( type_node, BAD_CAST "name", BAD_CAST "type" );
199 xmlSetProp( type_node, BAD_CAST "value", BAD_CAST "DISCONNECT" );
204 xmlSetProp( type_node, BAD_CAST "name", BAD_CAST "type" );
205 xmlSetProp( type_node, BAD_CAST "value", BAD_CAST "STATUS" );
206 status_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObject", NULL );
207 xmlSetProp( status_node, BAD_CAST "name", BAD_CAST msg->status_name );
209 status_text_node = xmlNewChild( status_node, NULL, BAD_CAST "domainObjectAttr", NULL );
210 xmlSetProp( status_text_node, BAD_CAST "name", BAD_CAST "status" );
211 xmlSetProp( status_text_node, BAD_CAST "value", BAD_CAST msg->status_text);
213 status_code_node = xmlNewChild( status_node, NULL, BAD_CAST "domainObjectAttr", NULL );
214 xmlSetProp( status_code_node, BAD_CAST "name", BAD_CAST "statusCode" );
218 sprintf(sc,"%d",msg->status_code);
219 xmlSetProp( status_code_node, BAD_CAST "value", BAD_CAST sc);
225 xmlSetProp( type_node, BAD_CAST "name", "type" );
226 xmlSetProp( type_node, BAD_CAST "value", "REQUEST" );
227 method_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObject", NULL );
228 xmlSetProp( method_node, BAD_CAST "name", BAD_CAST "oilsMethod" );
230 if( msg->method_name != NULL ) {
232 method_name_node = xmlNewChild( method_node, NULL, BAD_CAST "domainObjectAttr", NULL );
233 xmlSetProp( method_name_node, BAD_CAST "name", BAD_CAST "method" );
234 xmlSetProp( method_name_node, BAD_CAST "value", BAD_CAST msg->method_name );
236 if( msg->parse_json_params ) {
237 if( msg->params != NULL ) {
239 char* jj = json_object_to_json_string( msg->params );
240 params_node = xmlNewChild( method_node, NULL, BAD_CAST "params", NULL );
241 xmlNodePtr tt = xmlNewDocTextLen( doc, BAD_CAST jj, strlen(jj) );
242 xmlAddChild(params_node, tt);
246 if( msg->parray != NULL ) {
248 /* construct the json array for the params */
249 growing_buffer* buf = buffer_init(128);
250 buffer_add( buf, "[");
252 for( k=0; k!= msg->parray->size; k++) {
253 buffer_add( buf, string_array_get_string(msg->parray, k) );
254 if(string_array_get_string(msg->parray, k+1))
255 buffer_add( buf, "," );
258 buffer_add( buf, "]");
260 char* tmp = safe_malloc( (buf->n_used + 1) * sizeof(char));
261 memcpy(tmp, buf->buf, buf->n_used);
263 params_node = xmlNewChild( method_node, NULL,
264 BAD_CAST "params", NULL );
266 xmlNodePtr tt = xmlNewDocTextLen( doc, BAD_CAST tmp, strlen(tmp) );
267 xmlAddChild(params_node, tt);
278 xmlSetProp( type_node, BAD_CAST "name", BAD_CAST "type" );
279 xmlSetProp( type_node, BAD_CAST "value", BAD_CAST "RESULT" );
280 result_node = xmlNewChild( message_node, NULL, BAD_CAST "domainObject", NULL );
281 xmlSetProp( result_node, BAD_CAST "name", BAD_CAST "oilsResult" );
283 status_text_node = xmlNewChild( result_node, NULL, BAD_CAST "domainObjectAttr", NULL );
284 xmlSetProp( status_text_node, BAD_CAST "name", BAD_CAST "status" );
285 xmlSetProp( status_text_node, BAD_CAST "value", BAD_CAST msg->status_text);
287 status_code_node = xmlNewChild( result_node, NULL, BAD_CAST "domainObjectAttr", NULL );
288 xmlSetProp( status_code_node, BAD_CAST "name", BAD_CAST "statusCode" );
292 sprintf(stc,"%d",msg->status_code);
293 xmlSetProp( status_code_node, BAD_CAST "value", BAD_CAST stc);
295 content_node = xmlNewChild( result_node, NULL,
296 BAD_CAST "domainObject", BAD_CAST msg->result_string );
297 xmlSetProp( content_node, BAD_CAST "name", BAD_CAST "oilsScalar" );
302 warning_handler( "Recieved bogus message type" );
307 // -----------------------------------------------------
308 // Dump the XML doc to a string and remove the
310 // -----------------------------------------------------
312 /* passing in a '1' means we want to retain the formatting */
314 //xmlDocDumpFormatMemory( doc, &xmlbuf, &bufsize, 0 );
315 //xmlDocDumpMemoryEnc( doc, &xmlbuf, &bufsize, "UTF-8" );
316 xmlDocDumpMemoryEnc( doc, &xmlbuf, &bufsize, "UTF-8" );
318 encoded_msg = strdup( (char*) xmlbuf );
321 if( encoded_msg == NULL )
322 fatal_handler("message_to_xml(): Out of Memory");
329 /*** remove the XML declaration */
330 int len = strlen(encoded_msg);
332 memset( tmp, 0, len );
336 /* when we reach the first >, take everything after it */
337 for( i = 0; i!= len; i++ ) {
338 if( encoded_msg[i] == 62) { /* ascii > */
340 /* found_at holds the starting index of the rest of the doc*/
347 /* move the shortened doc into the tmp buffer */
348 strncpy( tmp, encoded_msg + found_at, len - found_at );
349 /* move the tmp buffer back into the allocated space */
350 memset( encoded_msg, 0, len );
351 strcpy( encoded_msg, tmp );
361 int osrf_message_from_xml( char* xml, osrf_message* msgs[] ) {
365 xmlKeepBlanksDefault(0);
367 xmlNodePtr message_node;
368 xmlDocPtr doc = xmlReadDoc(
369 BAD_CAST xml, NULL, NULL, XML_PARSE_NSCLEAN );
371 xmlNodePtr root =xmlDocGetRootElement(doc);
373 warning_handler( "Attempt to build message from incomplete xml %s", xml );
378 message_node = root->children; /* since it's the only child */
381 warning_handler( "Attempt to build message from incomplete xml %s", xml );
385 while( message_node != NULL ) {
387 xmlNodePtr cur_node = message_node->children;
388 osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
389 new_msg->parse_json_result = parse_json_result;
394 xmlChar* name = NULL;
395 xmlChar* value = NULL;
397 /* we're a domainObjectAttr */
398 if( !strcmp((char*)cur_node->name,"domainObjectAttr" )) {
399 name = xmlGetProp( cur_node, BAD_CAST "name");
403 value = xmlGetProp( cur_node, BAD_CAST "value" );
406 if( (!strcmp((char*)name, "type")) ) { /* what type are we? */
408 if(!strcmp((char*)value, "CONNECT"))
409 new_msg->m_type = CONNECT;
411 if(!strcmp((char*)value, "DISCONNECT"))
412 new_msg->m_type = DISCONNECT;
414 if(!strcmp((char*)value, "STATUS"))
415 new_msg->m_type = STATUS;
417 if(!strcmp((char*)value, "REQUEST"))
418 new_msg->m_type = REQUEST;
420 if(!strcmp((char*)value, "RESULT"))
421 new_msg->m_type = RESULT;
423 } else if((!strcmp((char*)name, "threadTrace"))) {
424 new_msg->thread_trace = atoi( (char*) value );
426 } else if((!strcmp((char*)name, "protocol"))) {
427 new_msg->protocol = atoi( (char*) value );
436 /* we're a domainObject */
437 if( !strcmp((char*)cur_node->name,"domainObject" )) {
439 name = xmlGetProp( cur_node, BAD_CAST "name");
443 if( !strcmp(name,"oilsMethod") ) {
445 xmlNodePtr meth_node = cur_node->children;
447 while( meth_node != NULL ) {
449 if( !strcmp((char*)meth_node->name,"domainObjectAttr" )) {
450 char* meth_name = xmlGetProp( meth_node, BAD_CAST "value" );
452 new_msg->method_name = strdup(meth_name);
457 if( !strcmp((char*)meth_node->name,"params" ) && meth_node->children->content ) {
458 //new_msg->params = json_object_new_string( meth_node->children->content );
459 if( new_msg->parse_json_params) {
460 new_msg->params = json_tokener_parse(meth_node->children->content);
462 /* XXX this will have to parse the JSON to
463 grab the strings for full support! This should only be
464 necessary for server support of
465 non-json-param-parsing, though. Ugh. */
466 new_msg->params = json_tokener_parse(meth_node->children->content);
470 meth_node = meth_node->next;
474 if( !strcmp(name,"oilsResult") || new_msg->m_type == STATUS ) {
476 xmlNodePtr result_nodes = cur_node->children;
478 while( result_nodes ) {
480 if(!strcmp(result_nodes->name,"domainObjectAttr")) {
482 xmlChar* result_attr_name = xmlGetProp( result_nodes, BAD_CAST "name");
483 if(result_attr_name) {
484 xmlChar* result_attr_value = xmlGetProp( result_nodes, BAD_CAST "value" );
486 if( result_attr_value ) {
487 if((!strcmp((char*)result_attr_name, "status")))
488 new_msg->status_text = strdup((char*) result_attr_value );
490 else if((!strcmp((char*)result_attr_name, "statusCode")))
491 new_msg->status_code = atoi((char*) result_attr_value );
492 xmlFree(result_attr_value);
495 xmlFree(result_attr_name);
501 if(!strcmp(result_nodes->name,"domainObject")) {
502 xmlChar* r_name = xmlGetProp( result_nodes, BAD_CAST "name" );
504 if( !strcmp((char*)r_name,"oilsScalar") && result_nodes->children->content ) {
505 osrf_message_set_result_content( new_msg, result_nodes->children->content);
510 result_nodes = result_nodes->next;
514 if( new_msg->m_type == STATUS )
515 new_msg->status_name = strdup(name);
521 /* we're a params node */
522 if( !strcmp((char*)cur_node->name,"params" )) {
526 cur_node = cur_node->next;
529 msgs[msg_index] = new_msg;
531 message_node = message_node->next;
533 } // while message_node != null
535 xmlCleanupCharEncodingHandlers();