client: Debug ndoeset in sendxmldoc
[libisds.git] / client / sendxmldoc.c
bloba7fa6a293bc9b4127b6fb21dae0961db2d25ced9
1 #define _XOPEN_SOURCE 500
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <locale.h>
5 #include <time.h>
6 #include <string.h>
7 #include <libgen.h>
8 #include <isds.h>
9 #include <libxml/parser.h>
10 #include <libxml/xpath.h>
11 #include <libxml/debugXML.h>
12 #include "common.h"
15 int main(int argc, char **argv) {
16 struct isds_ctx *ctx = NULL;
17 isds_error err;
18 char *recipient = NULL;
19 xmlDocPtr xml = NULL;
20 xmlNodePtr node_list = NULL;
22 setlocale(LC_ALL, "");
24 err = isds_init();
25 if (err) {
26 printf("isds_init() failed: %s\n", isds_strerror(err));
27 exit(EXIT_FAILURE);
30 isds_set_logging(ILF_ALL & ~ILF_HTTP, ILL_ALL);
32 ctx = isds_ctx_create();
33 if (!ctx) {
34 printf("isds_ctx_create() failed");
37 err = isds_set_timeout(ctx, 10000);
38 if (err) {
39 printf("isds_set_timeout() failed: %s\n", isds_strerror(err));
42 err = isds_login(ctx, url, username, password, NULL);
43 if (err) {
44 printf("isds_login() failed: %s: %s\n", isds_strerror(err),
45 isds_long_message(ctx));
46 } else {
47 printf("Logged in :)\n");
51 if (argv[1] && argv[1][0]) {
52 recipient = strdup(argv[1]);
53 } else {
54 /* Find a recipient */
55 struct isds_list *boxes = NULL;
56 struct isds_DbOwnerInfo criteria;
57 isds_DbType criteria_db_type = DBTYPE_OVM;
58 memset(&criteria, 0, sizeof(criteria));
59 criteria.firmName = "Místní";
60 criteria.dbType = &criteria_db_type;
62 printf("Searching box with firm name `%s':\n", criteria.firmName);
63 err = isds_FindDataBox(ctx, &criteria, &boxes);
64 if (err == IE_SUCCESS || err == IE_2BIG) {
65 printf("isds_FindDataBox() succeeded:\n");
67 if (boxes && boxes->data) {
68 printf("Selected recipient:\n");
69 print_DbOwnerInfo(boxes->data);
70 recipient = strdup(
71 ((struct isds_DbOwnerInfo *)(boxes->data))->dbID);
73 } else {
74 printf("isds_FindDataBox() failed: %s: %s\n",
75 isds_strerror(err), isds_long_message(ctx));
78 isds_list_free(&boxes);
83 xmlXPathContextPtr xpath_ctx = NULL;
84 xmlXPathObjectPtr result = NULL;
86 /* Get XML node list */
87 if (argc != 4) {
88 printf("Bad number of arguments\n");
89 printf("Usage: %s RECIPIENT XML_FILE XPATH_EXPRESSION\n"
90 "Send a message with XML document defined by XPATH_EXPRESSION on XML_FILE\n"
91 "to RECIPIENT. If RECIPIENT is empty, send to random foubd one.",
92 basename(argv[0]));
93 exit(EXIT_FAILURE);
96 xml = xmlParseFile(argv[2]);
97 if (!xml) {
98 printf("Error while parsing `%s'\n", argv[1]);
99 exit(EXIT_FAILURE);
102 xpath_ctx = xmlXPathNewContext(xml);
103 if (!xpath_ctx) {
104 printf("Error while creating XPath context\n");
105 exit(EXIT_FAILURE);
107 result = xmlXPathEvalExpression(BAD_CAST argv[3], xpath_ctx);
108 if (!result) {
109 printf("Error while evaluating XPath expression `%s'\n", argv[3]);
110 exit(EXIT_FAILURE);
112 if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
113 printf("Empty match, trying to send empty XML document\n");
114 node_list = NULL;
115 } else {
116 /* Convert node set to list of siblings */
117 for (int i = 0; i < result->nodesetval->nodeNr; i++) {
118 printf("* Embeding node #%d:\n", i);
119 /* FIXME */
120 node_list = result->nodesetval->nodeTab[i];
121 xmlDebugDumpNode(stdout, result->nodesetval->nodeTab[i], 2);
125 xmlXPathFreeObject(result);
126 xmlXPathFreeContext(xpath_ctx);
132 /* Send one message */
133 struct isds_message message;
134 memset(&message, 0, sizeof(message));
136 struct isds_envelope envelope;
137 memset(&envelope, 0, sizeof(envelope));
138 message.envelope = &envelope;
139 long int dmSenderOrgUnitNum = 42;
140 envelope.dmSenderOrgUnitNum = &dmSenderOrgUnitNum;
141 envelope.dmAnnotation = "XML documents";
142 envelope.dbIDRecipient = recipient;
145 struct isds_document main_document;
146 memset(&main_document, 0, sizeof(main_document));
147 main_document.is_xml = 0;
148 main_document.data = "Hello World!";
149 main_document.data_length = strlen(main_document.data);
150 main_document.dmMimeType = "text/plain";
151 main_document.dmFileMetaType = FILEMETATYPE_MAIN;
152 main_document.dmFileDescr = "standard_text.txt";
153 main_document.dmFileGuid = "1";
155 struct isds_document xml_document;
156 memset(&xml_document, 0, sizeof(xml_document));
157 xml_document.is_xml = 1;
158 xml_document.dmMimeType = "text/xml";
159 xml_document.dmFileMetaType = FILEMETATYPE_ENCLOSURE;
160 xml_document.dmFileDescr = "in-line.xml";
161 xml_document.dmFileGuid = "2";
163 struct isds_list documents_xml_item = {
164 .data = &xml_document,
165 .next = NULL,
166 .destructor = NULL
168 struct isds_list documents_main_item = {
169 .data = &main_document,
170 .next = &documents_xml_item,
171 .destructor = NULL
173 message.documents = &documents_main_item;
176 printf("Sending message to box ID `%s'\n", recipient);
177 err = isds_send_message(ctx, &message);
179 if (err == IE_SUCCESS){
180 printf("isds_send_message() succeeded: message ID = %s\n",
181 message.envelope->dmID);
182 } else
183 printf("isds_send_message() failed: "
184 "%s: %s\n", isds_strerror(err), isds_long_message(ctx));
188 free(recipient);
189 xmlFreeDoc(xml);
192 err = isds_logout(ctx);
193 if (err) {
194 printf("isds_logout() failed: %s\n", isds_strerror(err));
198 err = isds_ctx_free(&ctx);
199 if (err) {
200 printf("isds_ctx_free() failed: %s\n", isds_strerror(err));
204 err = isds_cleanup();
205 if (err) {
206 printf("isds_cleanup() failed: %s\n", isds_strerror(err));
209 exit (EXIT_SUCCESS);