1 /* zxidwsctool.c  -  Command line Web Services Client, a tool for making ID-WSF calls
2  * Copyright (c) 2010 Sampo Kellomaki (sampo@iki.fi), All Rights Reserved.
3  * Copyright (c) 2007 Symlabs (symlabs@symlabs.com), All Rights Reserved.
4  * Author: Sampo Kellomaki (sampo@iki.fi)
5  * This is confidential unpublished proprietary source code of the author.
6  * NO WARRANTY, not even implied warranties. Contains trade secrets.
7  * Distribution prohibited unless authorized in writing.
8  * Licensed under Apache License 2.0, see file COPYING.
9  * $Id: zxidwsctool.c,v 1.10 2010-01-08 02:10:09 sampo Exp $
10  *
11  * 25.1.2007, created --Sampo
12  *
13  * See also: http://hoohoo.ncsa.uiuc.edu/cgi/interface.html (CGI specification)
14  *           README-zxid, section 10 "zxid_simple() API"
15  */
16 
17 #include <string.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include <zx/platform.h>
22 #include <zx/errmac.h>
23 #include <zx/zxid.h>      /* ZXID main API, including zxid_simple(). */
24 #include <zx/zxidpriv.h>
25 #include <zx/zxidconf.h>  /* Default and compile-time configuration options. */
26 #include <zx/wsf.h>
27 #include <zx/c/zxidvers.h>
28 #include <zx/c/zx-ns.h>
29 #include <zx/c/zx-e-data.h>
30 
31 char* help =
32 "zxidwsctool  - Command Line ID-WSF WSC - R" ZXID_REL "\n\
33 Copyright (c) 2007 Symlabs (symlabs@symlabs.com), All Rights Reserved.\n\
34 Author: Sampo Kellomaki (sampo@iki.fi)\n\
35 NO WARRANTY, not even implied warranties. Licensed under Apache License v2.0\n\
36 See http://www.apache.org/licenses/LICENSE-2.0\n\
37 Send well-researched bug reports to the author. Home: zxid.org\n\
38 \n\
39 Usage: zxidwsctool [options] sesid svc\n\
40   sesid            Session ID referring to a directory in /var/zxid/ses\n\
41                    Use zxidhlo to do SSO and then cut and paste from there.\n\
42   svc              Service type or abbreviation (di = discovery 2.0, dap = ID-DAP)\n\
43   -d               Increase debug prints\n\
44   -s               Simple API\n\
45   -h               This help message\n\
46   --               End of options\n";
47 
48 /* ============== M A I N ============== */
49 
50 #if 0
51 #define ZXIDHLO "zxidhlowsf"
52 #define CONF "PATH=/var/zxid/&URL=https://sp1.zxidsp.org:8443/" ZXIDHLO
53 #define ZXIDHLO "zxidhlo"
54 #define CONF "PATH=/var/zxid/&URL=https://sp1.zxidsp.org:8443/" ZXIDHLO
55 #else
56 #define ZXIDHLO "zxidhlowsf"
57 #define CONF "PATH=/var/zxid/&URL=https://sampo:8443/" ZXIDHLO
58 #endif
59 
60 /* Called by: */
main(int argc,char ** argv)61 int main(int argc, char** argv)
62 {
63   zxid_ses sess;
64   zxid_conf* cf;
65   zxid_ses* ses;
66   //struct zx_root_s* r;
67   struct zx_e_Envelope_s* env;
68   zxid_epr* epr;
69   int simple = 0;
70   char* sid;
71   char* svc;
72 
73   --argc; ++argv;
74   while (argc) {
75     if (argv[0][0] != '-') break;
76     switch (argv[0][1]) {
77     case 'd': ++errmac_debug; break;
78     case 's': ++simple; break;
79     default:  fprintf(stderr, "Unknown option(%s)\n%s", argv[0], help); exit(1);
80     }
81     --argc; ++argv;
82   }
83   if (argc != 2) { fprintf(stderr, "Wrong number of args(%d), (!= 2)\n%s", argc, help); exit(1); }
84   sid = argv[0];
85   svc = argv[1];
86 
87   if (!strcmp(svc, "di"))  svc = XMLNS_DISCO_2_0;
88   if (!strcmp(svc, "dap")) svc = XMLNS_DAP;
89   if (!strcmp(svc, "demomed")) svc = zx_xmlns_demomed;
90 
91 #if 0
92   if (simple) {
93     char* res = zxid_dap_call(sid, );
94     D("Result(%s)", res);
95     exit(1);
96   }
97 #endif
98 
99   ses = &sess;
100   cf = zxid_new_conf_to_cf(CONF);
101   zxid_get_ses(cf, ses, sid);
102 
103   if (!strcmp(svc, XMLNS_DISCO_2_0)) {
104     /* Discover service (call discovery service using bootstrap) */
105 
106     env = zx_NEW_e_Envelope(cf->ctx,0);
107     env->Header = zx_NEW_e_Header(cf->ctx, &env->gg);
108     env->Body = zx_NEW_e_Body(cf->ctx, &env->gg);
109     env->Body->Query = zxid_mk_di_query(cf, &env->Body->gg, svc, 0,0,0);
110     epr = zxid_find_epr(cf, ses, XMLNS_DISCO_2_0, 0,0,0, 1);
111     env = zxid_wsc_call(cf, ses, epr, env, 0);
112     if (env || env != (void*)ZXID_REDIR_OK && env->Body && env->Body->QueryResponse)
113       for (epr = env->Body->QueryResponse->EndpointReference;
114 	   epr;
115 	   epr = (void*)ZX_NEXT(epr)) {
116 	if (epr->gg.g.tok != zx_a_EndpointReference_ELEM)
117 	  continue;
118 	zxid_cache_epr(cf, ses, epr, 1);
119       }
120 
121   } else if (!strcmp(svc, XMLNS_DAP)) {
122 
123     epr = zxid_get_epr(cf, ses, XMLNS_DAP, 0,0,0, 1);
124     if (epr) {
125       env = zx_NEW_e_Envelope(cf->ctx,0);
126       env->Header = zx_NEW_e_Header(cf->ctx, &env->gg);
127       env->Body = zx_NEW_e_Body(cf->ctx, &env->gg);
128 #if 1
129       env->Body->dap_Query
130 	= zxid_mk_dap_query(cf, &env->Body->gg,
131 			    0,   /* No tests */
132 			    zxid_mk_dap_query_item(cf, 0,
133 						   zxid_mk_dap_select(cf, 0,
134 								      0,  /* DN from ID-WSF */
135 								      "objecttype=svcprofile",
136 								      0,  /* all attributes */
137 								      1,  /* chase symlinks */
138 								      ZXID_DAP_SCOPE_SUBTREE,
139 								      0,  /* no size limit */
140 								      0,  /* no time limit */
141 								      0), /* return data */
142 						   0,  /* regular data entries */
143 						   0,  /* No predefined operation */
144 						   0,  /* No sorting. */
145 						   0,  /* No changed since specification. */
146 						   0,  /* Do not include LDAP common attributes. */
147 						   0,  /* Start from first result (offset == 0) */
148 						   0,  /* Return all results (count == 0) */
149 						   0,  /* Do not request snapshot */
150 						   0,  /* Do not refer to snapshot */
151 						   0), /* No contingent item ID reference */
152 			    0);  /* No subscriptions */
153       env->Body->dap_Query
154 	= zxid_mk_dap_query(cf, &env->Body->gg, 0,
155 			    zxid_mk_dap_query_item(cf, 0,
156 						   zxid_mk_dap_select(cf, 0, 0,
157 								      "objecttype=svcprofile",
158 								      0, 1,
159 								      ZXID_DAP_SCOPE_SUBTREE,
160 								      0, 0, 0),
161 						   0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
162 			    0);  /* No subscriptions */
163 #else
164       env->Body->dap_Query
165 	= zxid_mk_dap_query(cf, &env->Body->gg,
166 			    zxid_mk_dap_test_item(cf, 0,
167 						  zxid_mk_dap_testop(cf, 0,
168 								     0,  /* DN from ID-WSF */
169 								     "objecttype=svcprofile",
170 								     0,  /* all attributes */
171 								     1,  /* chase symlinks */
172 								     ZXID_DAP_SCOPE_SUBTREE,
173 								     0,  /* no size limit */
174 								     0,  /* no time limit */
175 								     0), /* return data */
176 						  0,   /* regular data entries */
177 						  0),  /* No predefined operation */
178 			    zxid_mk_dap_query_item(cf, 0,
179 						   zxid_mk_dap_select(cf, 0,
180 								      0,  /* DN from ID-WSF */
181 								      "objecttype=svcprofile",
182 								      0,  /* all attributes */
183 								      1,  /* chase symlinks */
184 								      ZXID_DAP_SCOPE_SUBTREE,
185 								      0,  /* no size limit */
186 								      0,  /* no time limit */
187 								      0), /* return data */
188 						   0,  /* regular data entries */
189 						   0,  /* No predefined operation */
190 						   0,  /* No sorting. */
191 						   0,  /* No changed since specification. */
192 						   0,  /* Do not include LDAP common attributes. */
193 						   0,  /* Start from first result (offset == 0) */
194 						   0,  /* Return all results (count == 0) */
195 						   0,  /* Do not request snapshot */
196 						   0,  /* Do not refer to snapshot */
197 						   0), /* No contingent item ID reference */
198 			    zxid_mk_dap_subscription(cf, 0,
199 						     "subsid",
200 						     0,  /* No item ID reference */
201 						     zxid_mk_dap_resquery(cf, 0,
202 									  zxid_mk_dap_select(cf, 0,
203 											     0,  /* DN from ID-WSF */
204 											     "objecttype=svcprofile",
205 											     0,  /* all attributes */
206 											     1,  /* chase symlinks */
207 											     ZXID_DAP_SCOPE_SUBTREE,
208 											     0,  /* no size limit */
209 											     0,  /* no time limit */
210 											     0), /* return data */
211 									  0,  /* regular data entries */
212 									  0,  /* No predefined operation */
213 									  0,  /* No sorting. */
214 									  0,  /* No changed since specification. */
215 									  0,  /* Do not include LDAP common attributes. */
216 									  0), /* No contingent item ID reference */
217 						     0, /* No notification aggregation spec. */
218 						     0, /* No notification trigger spec. */
219 						     0, /* Subscription starts immediately. */
220 						     0, /* Subscription never expires. */
221 						     1, /* Include changed data in the notifications. */
222 						     0, /* Use notification reference for administrative notifications. */
223 						     "http://host/notif_sink")
224 			    );
225 #endif
226       env = zxid_wsc_call(cf, ses, epr, env, 0);
227       if (env && env != (void*)ZXID_REDIR_OK && env->Body)
228 	if (env->Body->dap_QueryResponse)
229 	  D("Result is LDIF(%.*s)",
230 	    ZX_GET_CONTENT_LEN(env->Body->dap_QueryResponse->Data->LDIF),
231 	    ZX_GET_CONTENT_S(env->Body->dap_QueryResponse->Data->LDIF));
232 	else
233 	  ERR("There was no result %p", env->Body);
234       else
235 	ERR("Web services call failed %p", env);
236     } else
237       ERR("EPR could not be discovered svc(%s)", svc);
238 
239   } else if (!strcmp(svc, zx_xmlns_demomed)) {
240     struct zx_demomed_ObjectInfo_s* objinfo;
241     struct zx_demomed_ObjectInfo_s* first_objinfo;
242     struct zx_demomed_ObjectData_s* objdata;
243     struct zx_demomed_ObjectData_s* first_objdata;
244 
245     epr = zxid_get_epr(cf, ses, zx_xmlns_demomed, 0,0,0, 1);
246     if (epr) {
247 
248       /* Get list of media objects. */
249 
250       env = zx_NEW_e_Envelope(cf->ctx,0);
251       env->Header = zx_NEW_e_Header(cf->ctx, &env->gg);
252       env->Body = zx_NEW_e_Body(cf->ctx, &env->gg);
253       env->Body->GetObjectListRequest = zx_NEW_demomed_GetObjectListRequest(cf->ctx, &env->Body->gg);
254       env->Body->GetObjectListRequest->ObjectSearchParm = zx_NEW_demomed_ObjectSearchParm(cf->ctx, &env->Body->GetObjectListRequest->gg);
255 
256       env = zxid_wsc_call(cf, ses, epr, env, 0);
257 
258       if (env && env != (void*)ZXID_REDIR_OK && env->Body)
259 	if (env->Body->GetObjectListResponse) {
260 	  if (!memcmp(env->Body->GetObjectListResponse->Status->code->g.s, "OK", 2)) {
261 	    first_objinfo = env->Body->GetObjectListResponse->ObjectInfo;
262 	    D("Successful response %p", first_objinfo);
263 	    for (objinfo = first_objinfo;
264 		 objinfo;
265 		 objinfo = (struct zx_demomed_ObjectInfo_s*)objinfo->gg.g.n) {
266 	      if (objinfo->gg.g.tok != zx_demomed_ObjectInfo_ELEM)
267 		continue;
268 	      D("objectID(%.*s) Dir(%.*s) Name(%.*s) Type(%.*s) Created(%.*s) Comment(%.*s)",
269 		objinfo->objectID->g.len,       objinfo->objectID->g.s,
270 		ZX_GET_CONTENT_LEN(objinfo->Dir),     ZX_GET_CONTENT_S(objinfo->Dir),
271 		ZX_GET_CONTENT_LEN(objinfo->Name),    ZX_GET_CONTENT_S(objinfo->Name),
272 		ZX_GET_CONTENT_LEN(objinfo->Type),    ZX_GET_CONTENT_S(objinfo->Type),
273 		ZX_GET_CONTENT_LEN(objinfo->Created), ZX_GET_CONTENT_S(objinfo->Created),
274 		ZX_GET_CONTENT_LEN(objinfo->Comment), ZX_GET_CONTENT_S(objinfo->Comment));
275 	    }
276 	  } else {
277 	    D("Non OK status(%.*s)", env->Body->GetObjectListResponse->Status->code->g.len, env->Body->GetObjectListResponse->Status->code->g.s);
278 	  }
279 	} else
280 	  ERR("There was no result %p", env->Body);
281       else
282 	ERR("Web services call failed %p", env);
283 
284       /* Get first media object from the list */
285 
286       env = zx_NEW_e_Envelope(cf->ctx,0);
287       env->Header = zx_NEW_e_Header(cf->ctx, &env->gg);
288       env->Body = zx_NEW_e_Body(cf->ctx, &env->gg);
289       env->Body->GetObjectRequest = zx_NEW_demomed_GetObjectRequest(cf->ctx, &env->Body->gg);
290       env->Body->GetObjectRequest->ObjectID = zx_new_str_elem(cf->ctx, &env->Body->GetObjectRequest->gg, zx_demomed_ObjectID_ELEM, &first_objinfo->objectID->g);
291 
292       env = zxid_wsc_call(cf, ses, epr, env, 0);
293 
294       if (env && env != (void*)ZXID_REDIR_OK && env->Body)
295 	if (env->Body->GetObjectResponse) {
296 	  if (!memcmp(env->Body->GetObjectResponse->Status->code->g.s, "OK", 2)) {
297 	    first_objdata = env->Body->GetObjectResponse->ObjectData;
298 	    D("Successful response %p", first_objdata);
299 	    for (objdata = first_objdata;
300 		 objdata;
301 		 objdata = (struct zx_demomed_ObjectData_s*)objdata->gg.g.n) {
302 	      if (objdata->gg.g.tok != zx_demomed_ObjectData_ELEM)
303 		continue;
304 	      // show image
305 	    }
306 	  } else {
307 	    D("Non OK status(%.*s)", env->Body->GetObjectResponse->Status->code->g.len, env->Body->GetObjectListResponse->Status->code->g.s);
308 	  }
309 	} else
310 	  ERR("There was no result %p", env->Body);
311       else
312 	ERR("Web services call failed %p", env);
313 
314     } else
315       ERR("EPR could not be discovered svc(%s)", svc);
316 
317   } else
318     ERR("Unknown (and unimplemented) svc(%s)", svc);
319   return 0;
320 }
321 
322 /* EOF  --  zxidwsctool.c */
323