blib/lib/XML/Compile/WSDL11.pm untested

filter: Statementstmt 0.0% 0 / 304 Branchbran 0.0% 0 / 110 Conditioncond 0.0% 0 / 148 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 612 CC 56 SCAR 47.4
CC
56
Cov
0%
operation
420.0
findDef
72.0
addWSDL
56.0
CRAP 114.7
1 # Copyrights 2014-2018 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution XML-Compile-WSDL11. Meta-POD processed
6 # with OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package XML::Compile::WSDL11;
10 use vars '$VERSION';
11 $VERSION = '3.07';
12  
13 use base 'XML::Compile::Cache';
14  
15 use warnings;
16 use strict;
17  
18 use Log::Report 'xml-compile-soap';
19  
20 use XML::Compile ();
21 use XML::Compile::Util qw/pack_type unpack_type/;
22 use XML::Compile::SOAP ();
23 use XML::Compile::SOAP::Util qw/:wsdl11/;
24 use XML::Compile::SOAP::Extension;
25  
26 use XML::Compile::SOAP::Operation ();
27 use XML::Compile::Transport ();
28  
29 use File::Spec ();
30 use List::Util qw/first/;
31 use Scalar::Util qw/blessed/;
32 use File::Basename qw/dirname/;
33  
34  
35 sub init($)
36 { my ($self, $args) = @_;
37 $args->{schemas} and panic "new(schemas) option removed in 0.78";
38 my $wsdl = delete $args->{top};
39  
40 local $args->{any_element} = 'ATTEMPT';
41 local $args->{any_attribute} = 'ATTEMPT'; # not implemented
42 local $args->{allow_undeclared} = 1;
43  
44 $self->SUPER::init($args);
45  
46 $self->{index} = {};
47  
48 $self->addPrefixes(wsdl => WSDL11, soap => WSDL11SOAP, http => WSDL11HTTP);
49  
50 # next modules should change into an extension as well...
51 $_->can('_initWSDL11') && $_->_initWSDL11($self)
52 for XML::Compile::SOAP->registered;
53  
54 XML::Compile::SOAP::Extension->wsdl11Init($self, $args);
55  
56 $self->declare
57 ( READER => 'wsdl:definitions'
58 , key_rewrite => 'PREFIXED(wsdl,soap,http)'
59 , hook => {type => 'wsdl:tOperation', after => 'ELEMENT_ORDER'}
60 );
61  
62 $self->{XCW_dcopts} = {};
63 $self->{XCW_server} = $args->{server_type};
64  
65 my @xsds = map File::Spec->catdir(dirname(__FILE__), 'WSDL11', 'xsd', $_)
66 , qw(wsdl.xsd wsdl-mime.xsd wsdl-http.xsd);
67  
68 $self->importDefinitions(\@xsds, element_form_default => 'qualified');
69  
70 $self->addWSDL($_) for ref $wsdl eq 'ARRAY' ? @$wsdl : $wsdl;
71 $self;
72 }
73  
74 sub schemas(@) { panic "schemas() removed in v2.00, not needed anymore" }
75  
76 #--------------------------
77  
78  
79 sub compileAll(;$$)
80 { my ($self, $need, $usens) = @_;
81 $self->SUPER::compileAll($need, $usens)
82 if !$need || $need ne 'CALLS';
83  
84 $self->compileCalls
85 if !$need || $need eq 'CALLS';
86 $self;
87 }
88  
89  
90 sub compileCalls(@)
91 { my ($self, %args) = @_;
92 my $long = $args{long_names};
93  
94 my @ops = $self->operations
95 ( service => delete $args{service}
96 , port => delete $args{port}
97 , binding => delete $args{binding}
98 );
99  
100 foreach my $op (@ops)
101 { my $alias = $long ? $op->longName : undef;
102 $self->compileCall($op, alias => $alias, %args);
103 }
104  
105 $self;
106 }
107  
108  
109 sub compileCall($@)
110 { my ($self, $oper, %opts) = @_;
111 my $alias = delete $opts{alias};
112 my $op = blessed $oper ? $oper : $self->operation($oper, %opts);
113  
114 my $name = $alias || $op->name;
115 error __x"a compiled call for {name} already exists", name => $name
116 if $self->{XCW_ccode}{$name};
117  
118 my $dopts = $self->{XCW_dcopts} || {};
119 my @opts = %opts;
120 push @opts, ref $dopts eq 'ARRAY' ? @$dopts : %$dopts;
121 trace "compiling call `$name'";
122 $self->{XCW_ccode}{$name} = $op->compileClient(@opts);
123 }
124  
125  
126 sub call($@)
127 { my ($self, $name) = (shift, shift);
128  
129 my $codes = $self->{XCW_ccode}
130 or error __x"you can only use call() after compileCalls()";
131  
132 my $call = $codes->{$name}
133 or error __x"operation `{name}' is not known", name => $name;
134
135 $call->(@_);
136 }
137  
138 #--------------------------
139  
140  
141 sub addWSDL($%)
142 { my ($self, $data, %args) = @_;
143 defined $data or return ();
144  
145 if(ref $data eq 'ARRAY')
146 { $self->addWSDL($_) for @$data;
147 return $self;
148 }
149  
150 my ($node, %details) = $self->dataToXML($data);
151 defined $node or return $self;
152  
153 $node->localName eq 'definitions' && $node->namespaceURI eq WSDL11
154 or error __x"root element for WSDL is not 'wsdl:definitions'";
155  
156 $self->importDefinitions($node, details => \%details);
157 $self->learnPrefixes($node);
158  
159 my $spec = $self->reader('wsdl:definitions')->($node);
160 my $tns = $spec->{targetNamespace}
161 or error __x"WSDL sets no targetNamespace";
162  
163 # WSDL 1.1 par 2.1.1 says: WSDL def types each in own name-space
164 my $index = $self->{index};
165  
166 # silly WSDL structure
167 my $toplevels = $spec->{gr_wsdl_anyTopLevelOptionalElement} || [];
168  
169 foreach my $toplevel (@$toplevels)
170 { my ($which, $def) = %$toplevel; # always only one
171 $which =~ s/^wsdl_(service|message|binding|portType)$/$1/
172 or next;
173  
174 $index->{$which}{pack_type $tns, $def->{name}} = $def;
175  
176 if($which eq 'service')
177 { foreach my $port ( @{$def->{wsdl_port} || []} )
178 { my $addr_label = first { /address$/ } keys %$port
179 or error __x"no address in port {port}"
180 , port => $port->{name};
181 my $first_addr = $port->{$addr_label};
182 $first_addr = $first_addr->[0] if ref $first_addr eq 'ARRAY';
183  
184 # Is XML::Compile::SOAP<version> loaded?
185 ref $first_addr eq 'HASH'
186 or error __x"wsdl namespace {ns} not loaded"
187 , ns => $first_addr->namespaceURI;
188  
189 $index->{port}{pack_type $tns, $port->{name}} = $port;
190 }
191 }
192 }
193  
194 # no service block when only one port
195 unless($index->{service})
196 { # only from this WSDL, cannot use collective $index
197 my @portTypes = map $_->{wsdl_portType}||(), @$toplevels;
198 @portTypes==1
199 or error __x"no service definition so needs 1 portType, found {nr}"
200 , nr => scalar @portTypes;
201  
202 my @bindings = map $_->{wsdl_binding}||(), @$toplevels;
203 @bindings==1
204 or error __x"no service definition so needs 1 binding, found {nr}"
205 , nr => scalar @bindings;
206  
207 my $binding = pack_type $tns, $bindings[0]->{name};
208 my $portname = $portTypes[0]->{name};
209 my $servname = $portname;
210 $servname =~ s/Service$|(?:Service)?Port(?:Type)?$/Service/i
211 or $servname .= 'Service';
212  
213 my $addr
214 = $bindings[0]->{soap_binding} ? 'soap_address'
215 : $bindings[0]->{soap12_binding} ? 'soap12_address'
216 : error __x"unrecognized binding type for wsdl without service block";
217  
218 my %port = (name => $portname, binding => $binding
219 , $addr => {location => 'http://localhost'} );
220  
221 $index->{service}{pack_type $tns, $servname}
222 = { name => $servname, wsdl_port => [ \%port ] };
223 $index->{port}{pack_type $tns, $portname} = \%port;
224 }
225 #warn "INDEX: ",Dumper $index;
226 $self;
227 }
228  
229  
230 sub namesFor($)
231 { my ($self, $class) = @_;
232 keys %{shift->index($class) || {}};
233 }
234  
235  
236 # new options, then also add them to the list in compileClient()
237  
238 sub operation(@)
239 { my $self = shift;
240 my $name = @_ % 2 ? shift : undef;
241 my %args = (name => $name, @_);
242  
243 #
244 ## Service structure
245 #
246  
247 my $service = $self->findDef(service => delete $args{service});
248  
249 my $port;
250 my @ports = @{$service->{wsdl_port} || []};
251 if(my $not = first {blessed $_} @ports)
252 { error __x"not all name-spaces loaded, {ns} not parsed in port"
253 , ns => $not->namespaceURI;
254 }
255  
256 my @portnames = map $_->{name}, @ports;
257 if(my $portname = delete $args{port})
258 { $port = first {$_->{name} eq $portname} @ports;
259 error __x"cannot find port `{portname}', pick from {ports}"
260 , portname => $portname, ports => join("\n ", '', @portnames)
261 unless $port;
262 }
263 elsif(@ports==1)
264 { $port = shift @ports;
265 }
266 else
267 { error __x"specify port explicitly, pick from {portnames}"
268 , portnames => join("\n ", '', @portnames);
269 }
270  
271 # get plugin for operation
272 my $address = first { /address$/ && $port->{$_}{location}} keys %$port
273 or error __x"no address provided in service {service} port {port}"
274 , service => $service->{name}, port => $port->{name};
275  
276 if($address =~ m/^{/) # }
277 { my ($ns) = unpack_type $address;
278  
279 warning __"Since v2.00 you have to require XML::Compile::SOAP11 explicitly"
280 if $ns eq WSDL11SOAP;
281  
282 error __x"ports of type {ns} not supported (not loaded?)", ns => $ns;
283 }
284  
285 #use Data::Dumper;
286 #warn Dumper $port, $self->prefixes;
287 my ($prefix) = $address =~ m/(\w+)_address$/;
288 $prefix
289 or error __x"port address not prefixed; probably need to add a plugin XML::Compile::SOAP12";
290  
291 my $opns = $self->findName("$prefix:");
292 my $protocol = XML::Compile::SOAP->plugin($opns);
293 unless($protocol)
294 { my $pkg = $opns eq WSDL11SOAP ? 'SOAP11'
295 : $opns eq WSDL11SOAP12 ? 'SOAP12'
296 : undef;
297  
298 if($pkg)
299 { error __x"add 'use XML::Compile::{pkg}' to your script", pkg=>$pkg;
300 }
301 else
302 { notice __x"ignoring unsupported namespace {ns}", ns => $opns;
303 return;
304 }
305 }
306  
307 my $opclass = $protocol.'::Operation';
308 $opclass->can('_fromWSDL11')
309 or error __x"WSDL11 not supported by {class}", class => $opclass;
310  
311 #
312 ## Binding
313 #
314  
315 my $bindtype = $port->{binding}
316 or error __x"no binding defined in port '{name}'"
317 , name => $port->{name};
318  
319 my $binding = $self->findDef(binding => $bindtype);
320  
321 my $type = $binding->{type} # get portTypeType
322 or error __x"no type defined with binding `{name}'"
323 , name => $bindtype;
324  
325 my $portType = $self->findDef(portType => $type);
326 my $types = $portType->{wsdl_operation}
327 or error __x"no operations defined for portType `{name}'"
328 , name => $type;
329  
330 my @port_ops = map $_->{name}, @$types;
331  
332 $name ||= delete $args{operation};
333 my $port_op;
334 if(defined $name)
335 { $port_op = first {$_->{name} eq $name} @$types;
336 error __x"no operation `{op}' for portType {pt}, pick from{ops}"
337 , op => $name, pt => $type, ops => join("\n ", '', @port_ops)
338 unless $port_op;
339 }
340 elsif(@port_ops==1)
341 { $port_op = shift @$types;
342 $name = $port_op->{name};
343 }
344 else
345 { error __x"multiple operations in portType `{pt}', pick from {ops}"
346 , pt => $type, ops => join("\n ", '', @port_ops)
347 }
348  
349 my @bindops = @{$binding->{wsdl_operation} || []};
350 my $bind_op = first {$_->{name} eq $name} @bindops;
351 $bind_op
352 or error __x"cannot find bind operation for {name}", name => $name;
353  
354 # This should be detected while parsing the WSDL because the order of
355 # input and output is significant (and lost), but WSDL 1.1 simplifies
356 # our life by saying that only 2 out-of 4 predefined types can actually
357 # be used at present.
358  
359 my @order = map +(unpack_type $_)[1], @{$port_op->{_ELEMENT_ORDER}};
360  
361 my ($first_in, $first_out);
362 for(my $i = 0; $i<@order; $i++)
363 { $first_in = $i if !defined $first_in && $order[$i] eq 'input';
364 $first_out = $i if !defined $first_out && $order[$i] eq 'output';
365 }
366  
367 my $kind
368 = !defined $first_in ? 'notification-operation'
369 : !defined $first_out ? 'one-way'
370 : $first_in < $first_out ? 'request-response'
371 : 'solicit-response';
372  
373 #
374 ### message components
375 #
376  
377 my $operation = $opclass->_fromWSDL11
378 ( name => $name,
379 , kind => $kind
380  
381 , service => $service
382 , serv_port => $port
383 , binding => $binding
384 , bind_op => $bind_op
385 , portType => $portType
386 , port_op => $port_op
387  
388 , wsdl => $self
389 , action => $args{action}
390  
391 , server_type => $args{server_type} || $self->{XCW_server}
392 );
393
394 $operation;
395 }
396  
397  
398 sub compileClient(@)
399 { my $self = shift;
400 unshift @_, 'operation' if @_ % 2;
401 my $op = $self->operation(@_) or return ();
402  
403 my $dopts = $self->{XCW_dcopts} || {};
404 $op->compileClient(@_, (ref $dopts eq 'ARRAY' ? @$dopts : %$dopts));
405 }
406  
407 #---------------------
408  
409  
410 sub declare($$@)
411 { my ($self, $need, $names, @opts) = @_;
412 my $opts = @opts==1 ? shift @opts : \@opts;
413 $opts = [ %$opts ] if ref $opts eq 'HASH';
414  
415 $need eq 'OPERATION'
416 or $self->SUPER::declare($need, $names, @opts);
417  
418 foreach my $name (ref $names eq 'ARRAY' ? @$names : $names)
419 { # checking existence of opname is expensive here
420 # and may be problematic with multiple bindings.
421 $self->{XCW_dcopts}{$name} = $opts;
422 }
423  
424 $self;
425 }
426  
427 #--------------------------
428  
429  
430 sub index(;$$)
431 { my $index = shift->{index};
432 @_ or return $index;
433  
434 my $class = $index->{ (shift) }
435 or return ();
436  
437 @_ ? $class->{ (shift) } : $class;
438 }
439  
440  
441 sub findDef($;$)
442 { my ($self, $class, $name) = @_;
443 my $group = $self->index($class)
444 or error __x"no definitions for `{class}' found", class => $class;
445  
446 if(defined $name)
447 { return $group->{$name} if exists $group->{$name}; # QNAME
448  
449 if($name =~ m/\:/) # PREFIXED
450 { my $qname = $self->findName($name);
451 return $group->{$qname} if exists $group->{$qname};
452 }
453  
454 if(my $q = first { (unpack_type $_)[1] eq $name } keys %$group)
455 { return $group->{$q};
456 }
457  
458 error __x"no definition for `{name}' as {class}, pick from:{groups}"
459 , name => $name, class => $class
460 , groups => join("\n ", '', sort keys %$group);
461 }
462  
463 return values %$group
464 if wantarray;
465  
466 return (values %$group)[0]
467 if keys %$group==1;
468 my @alts = map $self->prefixed($_), sort keys %$group;
469 error __x"explicit selection required: pick one {class} from {alts}"
470 , class => $class, alts => join("\n ", '', @alts);
471 }
472  
473  
474 sub operations(@)
475 { my ($self, %args) = @_;
476 $args{produce} and die "produce option removed in 0.81";
477  
478 my @ops;
479 my @services = $self->findDef('service');
480 foreach my $service (@services)
481 { my $sname = $service->{name};
482 next if $args{service} && $args{service} ne $sname;
483  
484 my @ports = @{$service->{wsdl_port} || []};
485 foreach my $port (@ports)
486 {
487 next if $args{port} && $args{port} ne $port->{name};
488 my $bindtype = $port->{binding}
489 or error __x"no binding defined in port '{name}'"
490 , name => $port->{name};
491 my $binding = $self->findDef(binding => $bindtype);
492  
493 next if $args{binding} && $args{binding} ne $binding->{name};
494  
495 my $type = $binding->{type}
496 or error __x"no type defined with binding `{name}'"
497 , name => $bindtype;
498  
499 my %all_ops;
500 foreach my $operation ( @{$binding->{wsdl_operation}||[]} )
501 { my $opname = $operation->{name};
502  
503 if(my $has = $all_ops{$opname})
504 { error __x"operation {name} found again; choose service {has} or {also}"
505 , name => $opname, has => $has->serviceName
506 , also => $sname
507 if @services > 1 && !$args{service};
508  
509 error __x"need one set of operations, pick port from {ports}"
510 , ports => [ map $_->{name}, @ports ], _join => ', ';
511 }
512
513 my $op = $all_ops{$opname} = $self->operation
514 ( service => $sname
515 , port => $port->{name}
516 , binding => $bindtype
517 , operation => $opname
518 , portType => $type
519 , server_type => $args{server_type}
520 );
521  
522 push @ops, $op;
523 }
524 }
525 }
526  
527 @ops;
528 }
529  
530  
531 sub endPoint(@)
532 { my ($self, %args) = @_;
533 my $service = $self->findDef(service => delete $args{service});
534  
535 my $port;
536 my @ports = @{$service->{wsdl_port} || []};
537 my @portnames = map $_->{name}, @ports;
538 if(my $portname = delete $args{port})
539 { $port = first {$_->{name} eq $portname} @ports;
540 error __x"cannot find port `{portname}', pick from {ports}"
541 , portname => $portname, ports => join("\n ", '', @portnames)
542 unless $port;
543 }
544 elsif(@ports==1)
545 { $port = shift @ports;
546 }
547 else
548 { error __x"specify port explicitly, pick from {portnames}"
549 , portnames => join("\n ", '', @portnames);
550 }
551  
552 foreach my $k (keys %$port)
553 { return $port->{$k}{location} if $k =~ m/address$/;
554 }
555  
556 ();
557 }
558  
559  
560 sub printIndex(@)
561 { my $self = shift;
562 my $fh = @_ % 2 ? shift : select;
563 my @args = @_;
564  
565 my %tree;
566 foreach my $op ($self->operations(@args))
567 { my $port = $op->version.' port '.$op->portName;
568 my $bind = '(binding '.$op->bindingName.')';
569 $tree{'service '.$op->serviceName}{"$port $bind"}{$op->name} = $_;
570 }
571  
572 foreach my $service (sort keys %tree)
573 { $fh->print("$service\n");
574 foreach my $port (sort keys %{$tree{$service}})
575 { $fh->print(" $port\n");
576 foreach my $op (sort keys %{$tree{$service}{$port}})
577 { $fh->print(" $op\n");
578 }
579 }
580 }
581 }
582  
583  
584 sub explain($$$@)
585 { my ($self, $opname, $format, $direction, @opts) = @_;
586 my $op = $self->operation($opname, @opts)
587 or error __x"explain operation {name} not found", name => $opname;
588 $op->explain($self, $format, $direction, @opts);
589 }
590  
591 #--------------------------------
592  
593  
594 1;