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