blib/lib/XML/Compile/SOAP11/Operation.pm untested

filter: Statementstmt 0.0% 0 / 239 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 134 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 473 SCAR 69.0
CC
31
Cov
0%
explain
380.0
_msg_parts
56.0
addHeader
30.0
CRAP 992.0
1 # Copyrights 2007-2021 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-SOAP. 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::SOAP11::Operation;
10 use vars '$VERSION';
11 $VERSION = '3.27';
12  
13 use base 'XML::Compile::SOAP::Operation';
14  
15 use warnings;
16 use strict;
17  
18 use Log::Report 'xml-compile-soap';
19  
20 use List::Util 'first';
21  
22 use XML::Compile::Util qw/pack_type unpack_type/;
23 use XML::Compile::SOAP::Util qw/:soap11/;
24 use XML::Compile::SOAP11::Client;
25 use XML::Compile::SOAP11::Server;
26 use XML::Compile::SOAP::Extension;
27  
28 use vars '$VERSION'; # OODoc adds $VERSION to the script
29 $VERSION ||= '(devel)';
30  
31 # client/server object per schema class, because initiation options
32 # can be different. Class reference is key.
33 my (%soap11_client, %soap11_server);
34  
35  
36 sub init($)
37 { my ($self, $args) = @_;
38  
39 $self->SUPER::init($args);
40  
41 $self->{$_} = $args->{$_} || {}
42 for qw/input_def output_def fault_def/;
43  
44 $self->{style} = $args->{style} || 'document';
45  
46 XML::Compile::SOAP::Extension->soap11OperationInit($self, $args);
47 $self;
48 }
49  
50 sub _fromWSDL11(@)
51 { my ($class, %args) = @_;
52  
53 # Extract the SOAP11 specific information from a WSDL11 file. There are
54 # half a zillion parameters.
55 my ($p_op, $b_op, $wsdl)
56 = @args{ qw/port_op bind_op wsdl/ };
57  
58 $args{schemas} = $wsdl;
59 $args{endpoints} = $args{serv_port}{soap_address}{location};
60  
61 my $sop = $b_op->{soap_operation} || {};
62 $args{action} ||= $sop->{soapAction};
63  
64 my $sb = $args{binding}{soap_binding} || {};
65 $args{transport} = $sb->{transport} || 'HTTP';
66 $args{style} = $sb->{style} || 'document';
67  
68 $args{input_def} = $class->_msg_parts($wsdl, $args{name}, $args{style}
69 , $p_op->{wsdl_input}, $b_op->{wsdl_input});
70  
71 $args{output_def} = $class->_msg_parts($wsdl, $args{name}.'Response'
72 , $args{style}, $p_op->{wsdl_output}, $b_op->{wsdl_output});
73  
74 $args{fault_def}
75 = $class->_fault_parts($wsdl, $p_op->{wsdl_fault}, $b_op->{wsdl_fault});
76  
77 $class->SUPER::new(%args);
78 }
79  
80 sub _msg_parts($$$$$)
81 { my ($class, $wsdl, $opname, $style, $port_op, $bind_op) = @_;
82 my %parts;
83  
84 defined $port_op # communication not in two directions
85 or return ({}, {});
86  
87 if(my $body = $bind_op->{soap_body})
88 { my $msgname = $port_op->{message};
89 my @parts = $class->_select_parts($wsdl, $msgname, $body->{parts});
90  
91 my ($ns, $local) = unpack_type $msgname;
92 my $rpc_ns = $body->{namespace} // '';
93 $wsdl->addNicePrefix(call => $rpc_ns) if $rpc_ns;
94  
95 my $procedure
96 = $style eq 'rpc' ? pack_type($rpc_ns, $opname)
97 : @parts==1 && $parts[0]{type} ? $msgname
98 : $local;
99  
100 $parts{body} = { procedure => $procedure, %$port_op, use => 'literal'
101 , %$body, parts => \@parts };
102 }
103 elsif($port_op->{message})
104 { # missing <soap:body use="literal"> in <wsdl:input> or :output
105 error __x"operation {opname} has a message in its portType but no encoding in the binding", opname => $opname;
106 }
107  
108 my $bsh = $bind_op->{soap_header} || [];
109 foreach my $header (ref $bsh eq 'ARRAY' ? @$bsh : $bsh)
110 { my $msgname = $header->{message};
111 my @parts = $class->_select_parts($wsdl, $msgname, $header->{part});
112 push @{$parts{header}}, { %$header, parts => \@parts };
113  
114 foreach my $fault ( @{$header->{headerfault} || []} )
115 { $msgname = $fault->{message};
116 my @hf = $class->_select_parts($wsdl, $msgname, $fault->{part});
117 push @{$parts{headerfault}}, { %$fault, parts => \@hf };
118 }
119 }
120 \%parts;
121 }
122  
123 sub _select_parts($$$)
124 { my ($class, $wsdl, $msgname, $need_parts) = @_;
125 my $msg = $wsdl->findDef(message => $msgname)
126 or error __x"cannot find message {name}", name => $msgname;
127  
128 my @need
129 = ref $need_parts ? @$need_parts
130 : defined $need_parts ? $need_parts
131 : ();
132  
133 my $parts = $msg->{wsdl_part} || [];
134 @need or return @$parts;
135  
136 my @sel;
137 my %parts = map +($_->{name} => $_), @$parts;
138 foreach my $name (@need)
139 { my $part = $parts{$name}
140 or error __x"message {msg} does not have a part named {part}"
141 , msg => $msg->{name}, part => $name;
142  
143 push @sel, $part;
144 }
145  
146 @sel;
147 }
148  
149 sub _fault_parts($$$)
150 { my ($class, $wsdl, $portop, $bind) = @_;
151  
152 my $port_faults = $portop || [];
153 my %faults;
154  
155 foreach my $fault (@$bind)
156 { $fault or next;
157 my $name = $fault->{name};
158  
159 my $port = first {$_->{name} eq $name} @$port_faults;
160 defined $port
161 or error __x"cannot find port for fault {name}", name => $name;
162  
163 my $msgname = $port->{message}
164 or error __x"no fault message name in portOperation";
165  
166 my $message = $wsdl->findDef(message => $msgname)
167 or error __x"cannot find fault message {name}", name => $msgname;
168  
169 @{$message->{wsdl_part} || []}==1
170 or error __x"fault message {name} must have one part exactly"
171 , name => $msgname;
172  
173 $faults{$name} =
174 { part => $message->{wsdl_part}[0]
175 , use => ($fault->{use} || 'literal')
176 };
177 }
178  
179 +{ faults => \%faults };
180 }
181  
182 #-------------------------------------------
183  
184  
185 sub style() {shift->{style}}
186 sub version() { 'SOAP11' }
187 sub serverClass { 'XML::Compile::SOAP11::Server' }
188 sub clientClass { 'XML::Compile::SOAP11::Client' }
189  
190 #-------------------------------------------
191  
192  
193 sub addHeader($$$%)
194 { my ($self, $dir, $label, $el, %opts) = @_;
195 my $elem = $self->schemas->findName($el);
196 my $defs
197 = $dir eq 'INPUT' ? 'input_def'
198 : $dir eq 'OUTPUT' ? 'output_def'
199 : $dir eq 'FAULT' ? 'fault_def'
200 : panic "addHeader $dir";
201 my $headers = $self->{$defs}{header} ||= [];
202  
203 if(my $already = first {$_->{part} eq $label} @$headers)
204 { # the header is already defined, ignore second declaration
205 my $other_type = $already->{parts}[0]{element};
206 $other_type eq $elem
207 or error __x"header {label} already defined with type {type}"
208 , label => $label, type => $other_type;
209 return $already;
210 }
211  
212 my %part =
213 ( part => $label, use => 'literal'
214 , parts => [
215 { name => $label, element => $elem
216 , mustUnderstand => $opts{mustUnderstand}
217 , destination => $opts{destination}
218 } ]);
219  
220 push @$headers, \%part;
221 \%part;
222 }
223  
224 #-------------------------------------------
225  
226  
227 sub compileHandler(@)
228 { my ($self, %args) = @_;
229  
230 my $soap = $soap11_server{$self->{schemas}}
231 ||= XML::Compile::SOAP11::Server->new(schemas => $self->{schemas});
232 my $style = $args{style} ||= $self->style;
233  
234 my @ro = (%{$self->{input_def}}, %{$self->{fault_def}});
235 my @so = (%{$self->{output_def}}, %{$self->{fault_def}});
236  
237 $args{encode} ||= $soap->_sender(@so, %args);
238 $args{decode} ||= $soap->_receiver(@ro, %args);
239 $args{kind} ||= $self->kind;
240 $args{name} = $self->name;
241 $args{selector} ||= $soap->compileFilter(%{$self->{input_def}},
242 style => $style);
243  
244 $args{callback} = XML::Compile::SOAP::Extension
245 ->soap11HandlerWrapper($self, $args{callback}, \%args);
246  
247 $soap->compileHandler(%args);
248 }
249  
250  
251 sub compileClient(@)
252 { my ($self, %args) = @_;
253  
254 my $client = $soap11_client{$self->{schemas}}
255 ||= XML::Compile::SOAP11::Client->new(schemas => $self->{schemas});
256 my $style = $args{style} ||= $self->style;
257 my $kind = $args{kind} ||= $self->kind;
258  
259 my @so = (%{$self->{input_def}}, %{$self->{fault_def}});
260 my @ro = (%{$self->{output_def}}, %{$self->{fault_def}});
261  
262 my $call = $client->compileClient
263 ( name => $self->name
264 , kind => $kind
265 , encode => $client->_sender(@so, %args)
266 , decode => $client->_receiver(@ro, %args)
267 , transport => $self->compileTransporter(%args)
268 , async => $args{async}
269 , soap => $args{soap}
270 );
271  
272 XML::Compile::SOAP::Extension->soap11ClientWrapper($self, $call, \%args);
273 }
274  
275 #--------------------------
276  
277  
278 my $sep = '#--------------------------------------------------------------';
279  
280 sub explain($$$@)
281 { my ($self, $schema, $format, $dir, %args) = @_;
282  
283 # $schema has to be passed as argument, because we do not want operation
284 # objects to be glued to a schema object after compile time.
285  
286 UNIVERSAL::isa($schema, 'XML::Compile::Schema')
287 or error __x"explain() requires first element to be a schema";
288  
289 $format eq 'PERL'
290 or error __x"only PERL template supported for the moment, not {got}"
291 , got => $format;
292  
293 my $style = $self->style;
294 my $opname = $self->name;
295 my $skip_header = delete $args{skip_header} || 0;
296 my $recurse = delete $args{recurse} || 0;
297  
298 my $def = $dir eq 'INPUT' ? $self->{input_def} : $self->{output_def};
299 my $faults = $self->{fault_def}{faults};
300  
301 my (@struct, @postproc, @attach);
302 my @main = $recurse
303 ? "# The details of the types and elements are attached below."
304 : "# To explore the HASHes for each part, use recurse option.";
305  
306 HEAD_PART:
307 foreach my $header (@{$def->{header} || []})
308 { foreach my $part ( @{$header->{parts} || []} )
309 { my $name = $part->{name};
310 my ($kind, $value) = $part->{type} ? (type => $part->{type})
311 : (element => $part->{element});
312
313 my $type = $schema->prefixed($value) || $value;
314 push @main, ''
315 , "# Header part '$name' is $kind $type"
316 , ($kind eq 'type' && $recurse ? "# See fake element '$name'" : ())
317 , "my \$$name = {};";
318 push @struct, " $name => \$$name,";
319
320 $recurse or next HEAD_PART;
321
322 my $elem = $value;
323 if($kind eq 'type')
324 { # generate element with part name, because template requires elem
325 $schema->compileType(READER => $value, element => $name);
326 $elem = $name;
327 }
328
329 push @attach, '', $sep, "\$$name ="
330 , $schema->template(PERL => $elem, skip_header => 1, %args), ';';
331 }
332 }
333  
334 BODY_PART:
335 foreach my $part ( @{$def->{body}{parts} || []} )
336 { my $name = $part->{name};
337 my ($kind, $value) = $part->{type} ? (type => $part->{type})
338 : (element => $part->{element});
339  
340 my $type = $schema->prefixed($value) || $value;
341 push @main, ''
342 , "# Body part '$name' is content for $kind $type"
343 , ($kind eq 'type' && $recurse ? "# See fake element '$name'" : ())
344 , "my \$$name = {};";
345 push @struct, " $name => \$$name,";
346  
347 $recurse or next BODY_PART;
348  
349 my $elem = $value;
350 if($kind eq 'type')
351 { # generate element with part name, because template requires elem
352 $schema->compileType(READER => $value, element => $name);
353 $elem = $name;
354 }
355  
356 push @attach, '', $sep, "\$$name ="
357 , $schema->template(PERL => $elem, skip_header => 1, %args), ';';
358 }
359  
360 foreach my $fault (sort keys %$faults)
361 { my $part = $faults->{$fault}{part}; # fault msgs have only one part
362 my ($kind, $value) = $part->{type} ? (type => $part->{type})
363 : (element => $part->{element});
364  
365 my $type = $schema->prefixFor($value)
366 ? $schema->prefixed($value) : $value;
367  
368 if($dir eq 'OUTPUT')
369 { push @main, ''
370 , "# ... or fault $fault is $kind"
371 , "my \$$fault = {}; # $type"
372 , ($kind eq 'type' && $recurse ? "# See fake element '$fault'" : ())
373 , "my \$fault ="
374 , " { code => pack_type(\$myns, 'Open.NoSuchFile')"
375 , " , reason => 'because I can'"
376 , " , detail => \$$fault"
377 , ' };';
378 push @struct, " $fault => \$fault,";
379 }
380 else
381 { my $nice = $schema->prefixed($type) || $type;
382 push @postproc
383 , " elsif(\$errname eq '$fault')"
384 , " { # \$details is a $nice"
385 , " }";
386 }
387  
388 $recurse or next;
389  
390 my $elem = $value;
391 if($kind eq 'type')
392 { # generate element with part name, because template requires elem
393 $schema->compileType(READER => $value, element => $fault);
394 $elem = $fault;
395 }
396  
397 push @attach, '', $sep, "# FAULT", "\$$fault ="
398 , $schema->template(PERL => $elem, skip_header => 1, %args), ';';
399 }
400  
401 if($dir eq 'INPUT')
402 { push @main, ''
403 , '# Call with the combination of parts.'
404 , 'my @params = (', @struct, ');'
405 , 'my ($answer, $trace) = $call->(@params);', ''
406 , '# @params will become %$data_in in the server handler.'
407 , '# $answer is a HASH, an operation OUTPUT or Fault.'
408 , '# $trace is an XML::Compile::SOAP::Trace object.';
409  
410 unshift @postproc, ''
411 , '# You may get an error back from the server'
412 , 'if(my $f = $answer->{Fault})'
413 , '{ my $errname = $f->{_NAME};'
414 , ' my $error = $answer->{$errname};'
415 , ' print "$error->{code}\n";', ''
416 , ' my $details = $error->{detail};'
417 , ' if(not $details)'
418 , ' { # system error, no $details'
419 , ' }';
420
421 push @postproc
422 , ' exit 1;'
423 , '}';
424 }
425 elsif($dir eq 'OUTPUT')
426 { s/^/ / for @main, @struct;
427 unshift @main, ''
428 , "sub handle_$opname(\$)"
429 , '{ my ($server, $data_in) = @_;'
430 , ' # process $data_in, structured as INPUT message.'
431 , ' # Hint: use "print Dumper $data_in"';
432  
433 push @main, ''
434 , ' # This will end-up as $answer at client-side'
435 , ' return # optional keyword'
436 , " +{", @struct, " };", "}";
437 }
438 else
439 { error __x"template for direction INPUT or OUTPUT, not {got}"
440 , got => $dir;
441 }
442  
443 my @header;
444 if(my $how = $def->{body})
445 { my $use = $how->{use} || 'literal';
446 push @header
447 , "# Operation ". ($how->{procedure} || '(unnamed)')
448 , "# $dir, $style $use";
449 }
450 else
451 { push @header,
452 , "# Operation $opname has no $dir";
453 }
454  
455 foreach my $fault (sort keys %$faults)
456 { my $usage = $faults->{$fault};
457 push @header
458 , "# FAULT $fault, $style $usage->{use}" # $style?
459 }
460  
461 push @header
462 , "# Produced by ".__PACKAGE__." version $VERSION"
463 , "# on ".localtime()
464 , "#"
465 , "# The output below is only an example: it cannot be used"
466 , "# without interpretation, although very close to real code."
467 , ""
468 unless $args{skip_header};
469  
470 if($dir eq 'INPUT')
471 { push @header
472 , '# Compile only once in your code, usually during initiation:'
473 , "# my \$call = \$wsdl->compileClient('$opname');"
474 , '# then call it as often as you need. Alternatively'
475 , '# $wsdl->compileCalls(); # once'
476 , "# \$response = \$wsdl->call('$opname', \$request);";
477 }
478 else #OUTPUT
479 { push @header
480 , '# As part of the initiation phase of your server:'
481 , 'my $daemon = XML::Compile::SOAP::HTTPDaemon->new;'
482 , '$daemon->operationsFromWSDL'
483 , ' ( $wsdl'
484 , ' , callbacks =>'
485 , " { $opname => \\&handle_$opname}"
486 , ' );'
487 }
488  
489 join "\n", @header, @main, @postproc, @attach, '';
490 }
491  
492 sub parsedWSDL()
493 { my $self = shift;
494 +{ input => $self->{input_def}{body}
495 , output => $self->{output_def}{body}
496 , faults => $self->{fault_def}{faults}
497 , style => $self->style
498 };
499 }
500  
501 1;