script/gen-wsdl.pl untested

filter: Statementstmt 0.0% 0 / 148 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 204 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
4 if 0; # not running under some shell
5  
6 use warnings;
7 use strict;
8  
9 # Copyright (c) 2006 Randy Smith
10 # $Id: gen-wsdl.pl,v 1.5 2007/01/15 21:43:50 perlstalker Exp $
11  
12 our $VERSION = "0.1.0";
13  
14 our $DEBUG = 0;
15 my $c_sec = 'vsoapd';
16  
17 use FindBin;
18  
19 BEGIN {
20  
21 our @etc_dirs = (
22 "$FindBin::Bin/../etc",
23 "$FindBin::Bin",
24 "$FindBin::Bin/..",
25 "$FindBin::Bin/vuser",
26 "$FindBin::Bin/../vuser",
27 "$FindBin::Bin/../etc/vuser",
28 '/usr/local/etc',
29 '/usr/local/etc/vuser',
30 '/etc',
31 '/etc/vuser',
32 );
33 }
34  
35 use vars qw(@etc_dirs);
36  
37 use Config::IniFiles;
38 use Getopt::Long;
39  
40 use lib (map { "$_/extensions" } @etc_dirs);
41 use lib (map { "$_/lib" } @etc_dirs);
42  
43 use VUser::ExtLib qw(:config);
44 use VUser::ExtHandler;
45 use VUser::Log qw(:levels);
46 use VUser::Meta;
47  
48 my $config_file;
49 my $debug = 0;
50 my @keywords = ();
51 my $result = GetOptions( "config=s" => \$config_file,
52 "debug|d+" => \$debug,
53 "keywords=s" => \@keywords
54 );
55  
56 if( defined $config_file )
57 {
58 die "FATAL: config file: $config_file not found" unless( -e $config_file );
59 }
60 else
61 {
62 for my $etc_dir (@etc_dirs)
63 {
64 if (-e "$etc_dir/vuser.conf") {
65 $config_file = "$etc_dir/vuser.conf";
66 last;
67 }
68 }
69 }
70  
71 if (not defined $config_file) {
72 die "Unable to find a vuser.conf file in ".join (", ", @etc_dirs).".\n";
73 }
74  
75 my %cfg;
76 tie %cfg, 'Config::IniFiles', (-file => $config_file);
77  
78 our $log = VUser::Log->new(\%cfg, 'vsoapd/wsdl');
79  
80 $log->log(LOG_DEBUG, "Config loaded from $config_file");
81  
82 if (not $debug) {
83 $DEBUG = VUser::ExtLib::strip_ws($cfg{'vuser'}{'debug'}) || 0;
84 $DEBUG = VUser::ExtLib::check_bool($DEBUG) unless $DEBUG =~ /^\d+$/;
85 $debug = $DEBUG;
86 }
87  
88 my $eh = new VUser::ExtHandler (\%cfg);
89  
90 ## Build giant data structure first.
91 my %event_tree = ();
92  
93 @keywords = $eh->get_keywords() unless @keywords;
94  
95 # Skip a few special keywords.
96 foreach my $key (@keywords) {
97 if ($key eq 'config'
98 or $key eq 'help'
99 or $key eq 'man'
100 or $key eq 'version'
101 or not $eh->is_keyword($key)
102 ){
103 next;
104 }
105
106 $event_tree{$key} = {descr => $eh->get_description($key),
107 actions => {}};
108  
109 my @actions = $eh->get_actions($key);
110 foreach my $act (@actions) {
111 $event_tree{$key}{actions}{$act} = {descr => $eh->get_description($key, $act),
112 opts => {}};
113
114 my @opts = $eh->get_options ($key, $act);
115 foreach my $opt (@opts) {
116 my @meta = $eh->get_meta($key, $opt);
117 $event_tree{$key}{actions}{$act}{opts}{$opt}
118 = { descr => $eh->get_description($key, $act, $opt),
119 required => $eh->is_required($key, $act, $opt),
120 type => $meta[0]->type()
121 };
122 }
123 }
124 }
125  
126 ## Write the WSDL
127 print <<'HEAD';
128 <?xml version="1.0"?>
129 <wsdl:definitions name="VUser"
130 targetNamespace="urn:/VUser"
131 xmlns:tns="urn:/VUser"
132 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
133 xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/"
134 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
135 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
136
137 <wsdl:types>
138 <xsd:schema targetNamespace="urn:/VUser">
139 <xsd:complexType name="ColumnArray">
140 <xsd:complexContent>
141 <xsd:restriction base="enc:Array">
142 <xsd:attribute ref="enc:arrayType" wsdl:arrayType="xsd:string[]" />
143 </xsd:restriction>
144 </xsd:complexContent>
145 </xsd:complexType>
146 <xsd:complexType name="TypeArray">
147 <xsd:complexContent>
148 <xsd:restriction base="enc:Array">
149 <xsd:attribute ref="enc:arrayType" wsdl:arrayType="xsd:string[]" />
150 </xsd:restriction>
151 </xsd:complexContent>
152 </xsd:complexType>
153 <xsd:complexType name="ValueArray">
154 <xsd:complexContent>
155 <xsd:restriction base="enc:Array">
156 <xsd:attribute ref="enc:arrayType" wsdl:arrayType="xsd:string[]" />
157 </xsd:restriction>
158 </xsd:complexContent>
159 </xsd:complexType>
160 <xsd:complexType name="DataArray">
161 <xsd:complexContent>
162 <xsd:restriction base="enc:Array">
163 <xsd:attribute ref="enc:arrayType" wsdl:arrayType="tns:ValueArray" />
164 </xsd:restriction>
165 </xsd:complexContent>
166 </xsd:complexType>
167 <xsd:complexType name="ResultSet">
168 <xsd:all>
169 <xsd:element name="columns" type="tns:ColumnArray" minOccurs="1" maxOccurs="1" />
170 <xsd:element name="types" type="tns:TypeArray" minOccurs="1" maxOccurs="1" />
171 <xsd:element name="values" type="tns:DataArray" minOccurs="1" maxOccurs="1" />
172 </xsd:all>
173 </xsd:complexType>
174 <xsd:complexType name="Record">
175 <xsd:complexContent>
176 <xsd:restriction base="enc:Array">
177 <xsd:attribute ref="enc:arrayType" wsdl:arrayType="tns:ResultSet" />
178 </xsd:restriction>
179 </xsd:complexContent>
180 </xsd:complexType>
181 <xsd:complexType name="RecordArray">
182 <xsd:complexContent>
183 <xsd:restriction base="enc:Array">
184 <xsd:attribute ref="enc:arrayType" wsdl:arrayType="tns:Record" />
185 </xsd:restriction>
186 </xsd:complexContent>
187 </xsd:complexType>
188 <xsd:complexType name="StringArray">
189 <xsd:complexContent>
190 <xsd:restriction base="enc:Array">
191 <xsd:attribute ref="enc:arrayType" wsdl:arrayType="xsd:string[]" />
192 </xsd:restriction>
193 </xsd:complexContent>
194 </xsd:complexType>
195 </xsd:schema>
196 </wsdl:types>
197
198 HEAD
199  
200 ## Now for the <messages>
201  
202 print <<'MSGS';
203 <wsdl:message name="loginRequest">
204 <wsdl:part name="username" type="xsd:string" />
205 <wsdl:part name="password" type="xsd:string" />
206 </wsdl:message>
207  
208 <wsdl:message name="loginResponse">
209 <wsdl:part name="authinfo" type="xsd:string" />
210 </wsdl:message>
211  
212 <wsdl:message name="get_keywordsRequest" >
213 <wsdl:part name="authinfo" type="xsd:string" />
214 </wsdl:message>
215  
216 <wsdl:message name="get_keywordsResponse">
217 <wsdl:part name="keywords" type="tns:StringArray" />
218 </wsdl:message>
219
220 <wsdl:message name="get_actionsRequest" >
221 <wsdl:part name="authinfo" type="xsd:string" />
222 <wsdl:part name="keyword" type="xsd:string" />
223 </wsdl:message>
224
225 <wsdl:message name="get_actionsResponse">
226 <wsdl:part name="actions" type="tns:StringArray" />
227 </wsdl:message>
228
229 <wsdl:message name="get_optionsRequest" >
230 <wsdl:part name="authinfo" type="xsd:string" />
231 <wsdl:part name="keyword" type="xsd:string" />
232 <wsdl:part name="action" type="xsd:string" />
233 </wsdl:message>
234  
235 <wsdl:message name="get_optionsResponse">
236 <wsdl:part name="options" type="tns:StringArray" />
237 </wsdl:message>
238
239 <wsdl:message name="ResultsResponse">
240 <wsdl:part name="results" type="tns:RecordArray" />
241 </wsdl:message>
242 MSGS
243  
244 foreach my $key (sort keys %event_tree) {
245 foreach my $act (sort keys %{$event_tree{$key}{actions}}) {
246 printf(" <wsdl:message name=\"%s_%sRequest\">\n", $key, $act);
247 print " <wsdl:part name=\"authinfo\" type=\"xsd:string\" />\n";
248 foreach my $opt (sort keys %{$event_tree{$key}{actions}{$act}{opts}}) {
249 my $wsdl_type = 'xsd:string';
250 # Build switch to map VUser::Meta types to WSDL types
251 printf(" <wsdl:part name=\"%s\" type=\"%s\">\n", $opt, $wsdl_type);
252 printf(" <wsdl:documentation>%s</wsdl:documentation>\n",
253 $event_tree{$key}{actions}{$act}{opts}{$opt}{descr});
254 print(" </wsdl:part>\n");
255 }
256 print " </wsdl:message>\n";
257
258 printf(" <wsdl:message name=\"%s_%sResponse\">\n", $key, $act);
259 print " <wsdl:part name=\"results\" type=\"tns:RecordArray\" />\n";
260 print " </wsdl:message>\n";
261 }
262 }
263  
264 ## <portType>
265 # default port that included login and get_* operations
266 print <<'PORT';
267 <wsdl:portType name="DefaultPort">
268 <wsdl:documentation>
269 login() returns an authentication ticket that is passed in the headers
270 to the other operations.
271 </wsdl:documentation>
272 <wsdl:operation name="login">
273 <wsdl:input message="tns:loginRequest" />
274 <wsdl:output message="tns:loginResponse" />
275 </wsdl:operation>
276 <wsdl:operation name="get_keywords">
277 <wsdl:input message="tns:get_keywordsRequest" />
278 <wsdl:output message="tns:get_keywordsResponse" />
279 </wsdl:operation>
280 <wsdl:operation name="get_actions">
281 <wsdl:input message="tns:get_actionsRequest" />
282 <wsdl:output message="tns:get_actionsResponse" />
283 </wsdl:operation>
284 <wsdl:operation name="get_options">
285 <wsdl:input message="tns:get_optionsRequest" />
286 <wsdl:output message="tns:get_optionsResponse" />
287 </wsdl:operation>
288 </wsdl:portType>
289 PORT
290  
291 foreach my $key (sort keys %event_tree) {
292 my $uc_key = ucfirst $key;
293 # Documentation? keyword description
294 printf(" <wsdl:portType name=\"%sPort\">\n", $uc_key);
295 printf(" <wsdl:documentation>%s</wsdl:documentation>\n", $event_tree{$key}{descr});
296
297 foreach my $act (sort keys %{$event_tree{$key}{actions}}) {
298 my $uc_act = ucfirst($act);
299 # Documentation? action description
300 printf (" <wsdl:operation name=\"%s_%s\">\n", $key, $act);
301 printf(" <wsdl:documentation>%s</wsdl:documentation>\n",
302 $event_tree{$key}{actions}{$act}{descr});
303 printf (" <wsdl:input message=\"tns:%s_%sRequest\" />\n", $key, $act);
304 printf (" <wsdl:output message=\"tns:%s_%sResponse\" />\n", $key, $act);
305 #printf (" <wsdl:output message=\"tns:ResultsResponse\" />\n");
306 print " </wsdl:operation>\n";
307 }
308
309 print(" </wsdl:portType>\n");
310 }
311  
312 ## <binding>
313 print <<'BINDING';
314 <wsdl:binding name="DefaultSOAP" type="tns:DefaultPort">
315 <soap:binding style="rpc"
316 transport="http://schemas.xmlsoap.org/soap/http" />
317 <wsdl:operation name="login">
318 <soap:operation soapAction="urn:/VUser#login" />
319 <wsdl:input>
320 <soap:body use="encoded" parts="username password"
321 namespace="urn:/VUser"
322 encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
323 </wsdl:input>
324 <wsdl:output>
325 <soap:body use="encoded" namespace="urn:/VUser"
326 encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
327 </wsdl:output>
328 </wsdl:operation>
329 <wsdl:operation name="get_keywords">
330 <soap:operation soapAction="urn:/VUser#get_keywords" />
331 <wsdl:input>
332 <soap:body use="encoded"
333 namespace="urn:/VUser"
334 encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
335 <soap:header use="encoded" part="authinfo"
336 namespace="urn:/VUser"
337 message="tns:get_keywordsRequest" wsdl:required="1"
338 encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
339 </wsdl:input>
340 <wsdl:output>
341 <soap:body use="encoded" namespace="urn:/VUser"
342 encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
343 </wsdl:output>
344 </wsdl:operation>
345 <wsdl:operation name="get_actions">
346 <soap:operation soapAction="urn:/VUser#get_actions" />
347 <wsdl:input>
348 <soap:body use="encoded" part="keyword"
349 namespace="urn:/VUser"
350 encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
351 <soap:header use="encoded" part="authinfo"
352 namespace="urn:/VUser"
353 message="tns:get_actionsRequest" wsdl:required="1"
354 encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
355 </wsdl:input>
356 <wsdl:output>
357 <soap:body use="encoded" namespace="urn:/VUser"
358 encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
359 </wsdl:output>
360 </wsdl:operation>
361 <wsdl:operation name="get_options">
362 <soap:operation soapAction="urn:/VUser#get_options" />
363 <wsdl:input>
364 <soap:body use="encoded" parts="keyword action"
365 namespace="urn:/VUser"
366 encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
367 <soap:header use="encoded" part="authinfo"
368 namespace="urn:/VUser"
369 message="tns:get_optionsRequest" wsdl:required="1"
370 encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
371 </wsdl:input>
372 <wsdl:output>
373 <soap:body use="encoded" namespace="urn:/VUser"
374 encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
375 </wsdl:output>
376 </wsdl:operation>
377 </wsdl:binding>
378 BINDING
379  
380 foreach my $key (sort keys %event_tree) {
381 my $uc_key = ucfirst($key);
382
383 printf (" <wsdl:binding name=\"%sSOAP\" type=\"tns:%sPort\">\n", $uc_key, $uc_key);
384 # Test here for transport
385 print " <soap:binding style=\"rpc\" transport=\"http://schemas.xmlsoap.org/soap/http\" />\n";
386
387 foreach my $act (sort keys %{ $event_tree{$key}{actions} }) {
388 my $uc_act = ucfirst($act);
389 # Documentation? action description?
390 printf(" <wsdl:operation name=\"%s_%s\">\n", $key, $act);
391 printf( " <soap:operation soapAction=\"urn:/VUser#%s_%s\" />\n", $key, $act);
392 print " <wsdl:input>\n";
393 printf(" <soap:body use=\"encoded\"");
394 if (keys %{ $event_tree{$key}{actions}{$act}{opts} }) {
395 printf(" parts=\"%s\"",
396 join " ", sort keys %{ $event_tree{$key}{actions}{$act}{opts} });
397 }
398 print " namespace=\"urn:/VUser\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n";
399 print " <soap:header use=\"encoded\" part=\"authinfo\"";
400 #printf (" message=\"tns:%s%sRequest\" wsdl:required=\"1\"", $uc_key, $uc_act);
401 printf (" message=\"tns:%s_%sRequest\" wsdl:required=\"1\"", $key, $act);
402 print " namespace=\"urn:/VUser\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n";
403 print " </wsdl:input>\n";
404 print " <wsdl:output>\n";
405 print " <soap:body use=\"encoded\" namespace=\"urn:/VUser\"";
406 print " encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n";
407 print " </wsdl:output>\n";
408 print " </wsdl:operation>\n";
409 }
410 print " </wsdl:binding>\n";
411 }
412  
413 ## <service>
414 print <<'SERVICE';
415 <wsdl:service name="VUser">
416 <wsdl:port name="DefaultPort" binding="tns:DefaultSOAP">
417 SERVICE
418 # Location set in vuser.conf
419 my $location = strip_ws($cfg{$c_sec}{location});
420 $location = 'http://localhost:8000' unless $location;
421 printf (" <soap:address location=\"%s\" />\n", $location);
422 print " </wsdl:port>\n";
423  
424 foreach my $key (sort keys %event_tree) {
425 my $uc_key = ucfirst($key);
426 printf (" <wsdl:port name=\"%sPort\" binding=\"tns:%sSOAP\">\n", $uc_key, $uc_key);
427 printf (" <soap:address location=\"%s\" />\n", $location);
428 print(" </wsdl:port>\n");
429 }
430 print " </wsdl:service>\n";
431 print "</wsdl:definitions>\n";
432  
433 1;
434  
435 __END__