lib/SOAP/Clean/Client.pm untested

filter: Statementstmt 0.0% 0 / 265 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 391 CC 28 SCAR 37.3
CC
28
Cov
0%
invoke
132.0
xsd_type_to_sc
56.0
usage
12.0
CRAP 41.6
1 # Copyright (c) 2003, Cornell University
2 # See the file COPYING for the status of this software
3  
4 package SOAP::Clean::Client;
5  
6 use strict;
7 use warnings;
8  
9 use LWP::UserAgent;
10 use Data::Dumper;
11  
12 use SOAP::Clean::Misc;
13 use SOAP::Clean::XML;
14 use SOAP::Clean::Internal;
15 use SOAP::Clean::WSDL;
16  
17 # Inheritance
18 our @ISA = qw(SOAP::Clean::Internal::Actor);
19 our $AUTOLOAD; # it's a package global
20  
21 sub initialize {
22 my ($self,$wsdl_url) = @_;
23 $self->SUPER::initialize();
24 $self->{wsdl_url} = $wsdl_url;
25 LWP::Protocol::implementor('cgifile', 'my_cgifile_handler');
26 }
27  
28 sub DESTROY {
29 my ($self) = @_;
30 }
31  
32 ########################################################################
33  
34 # Set params using WSDL
35 sub _ensure_wsdl {
36 my ($self) = @_;
37  
38 if ( defined($self->{port}) ) {
39 return;
40 }
41  
42 # if $self->{port} is not set, then ask the server.
43 assert(defined($self->{wsdl_url}), "URL for WSDL not set");
44  
45 my $response = $self->_comm(0,$self->{wsdl_url},"GET");
46  
47 ($response->code == 200)
48 || die("WSDL request failed. Response follows:\n".
49 $response->as_string);
50  
51 ($response->content_type eq "text/xml")
52 || die("WSDL response was not XML. Response follows:\n".
53 $response->as_string);
54  
55 my $wsdl = xml_from_string($response->content);
56  
57 # Parse the WSDL
58 my $wp = new SOAP::Clean::WSDL::Parser;
59 my $service_defs = $wp->parse($wsdl);
60 $self->{port} = generate_services($service_defs);
61 }
62  
63 ########################################################################
64  
65 sub usage {
66 my ($self) = @_;
67  
68 $self->_ensure_wsdl();
69  
70 my $port = $self->{port};
71 my $operations = $$port{operations};
72  
73 my $results = {};
74 foreach my $method_name (keys %$operations) {
75 my $operation = $$operations{$method_name};
76 my ($input_args,$output_args);
77 if ($$operation{style} eq "rpc") {
78 $input_args = $self->usage_rpc_flit($$operation{input});
79 $output_args = $self->usage_rpc_flit($$operation{output});
80 } elsif ($$operation{style} eq "document") {
81 $input_args = $self->usage_document_flit($$operation{input});
82 $output_args = $self->usage_document_flit($$operation{output});
83 } else {
84 die;
85 }
86 $$results{$method_name} = { input => $input_args,
87 output => $output_args };
88 }
89 return $results;
90 }
91  
92 sub usage_rpc_flit {
93 my ($self,$flit) = @_;
94 my $results = {};
95 my $flit_parts = $$flit{parts};
96 foreach my $part_name (keys %$flit_parts) {
97 my $part_type = $$flit_parts{$part_name};
98 $$results{$part_name} = $$part_type{kind};
99 }
100 return $results;
101 }
102  
103  
104 sub usage_document_flit {
105 my ($self,$flit) = @_;
106 my $results = {};
107  
108 assert($$flit{use} eq "literal", "document with non-literal part");
109  
110 my $flit_parts = $$flit{parts};
111 my @flit_parts_names = keys %$flit_parts;
112 assert($#flit_parts_names == 0, "Wrong number of flit parts");
113 my $flit_part_name = $flit_parts_names[0];
114 my $flit_type = $$flit_parts{$flit_part_name};
115 assert($$flit_type{kind} eq "element");
116 $flit_type = $$flit_type{basetype};
117 assert($$flit_type{kind} eq "sequence");
118  
119 foreach my $type ( @{$$flit_type{list}} ) {
120 assert($$type{kind} eq "element");
121 assert($$type{nillable} eq "false");
122 assert($$type{max_occurs} eq "1");
123 $$results{$$type{name}} = xsd_type_to_sc($$type{basetype});
124 my $optional = ($$type{min_occurs} eq "0");
125 if ($optional) {
126 $$results{$$type{name}} = "optional ".$$results{$$type{name}};
127 }
128 }
129  
130 return $results;
131 }
132  
133 ########################################################################
134  
135 sub AUTOLOAD {
136 my $self = shift;
137 my $method_name;
138 if ( $AUTOLOAD =~ '.*::([^:]+)$' ) {
139 $method_name = $1;
140 } else {
141 $method_name = $AUTOLOAD;
142 }
143 return $self->invoke($method_name,@_);
144 }
145  
146 sub invoke {
147 my $self = shift;
148 my $method_name = shift;
149 my $args = shift;
150 # $args is either a hash from arg names to values, or the first
151 # argument of the argument list.
152 if (ref $args ne "HASH") {
153 $args = [ $args, @_ ];
154 }
155  
156 $self->_ensure_wsdl();
157  
158 my $port = $self->{port};
159 my $location = $$port{location};
160 my $operations = $$port{operations};
161 my $operation = $$operations{$method_name};
162 assert(defined($operation),"No such method, $method_name");
163  
164 # The base envelope
165 my ($request_method,$d);
166 if ($$operation{style} eq "rpc") {
167 ($request_method,$d) =
168 $self->request_rpc($location,$method_name,$operation,$args);
169 } elsif ($$operation{style} eq "document") {
170 ($request_method,$d) =
171 $self->request_document($location,$method_name,$operation,$args);
172 } else {
173 die;
174 }
175  
176 # Encrypt the body and just add it in below, if the enc option was
177 # specified
178 if (defined($self->{enc})) {
179 SOAP::Clean::Security::encrypt_body($d,
180 $self->{privkeyenc},
181 $self->{pubkeyenc},
182 $self->{enctmpl},
183 $self->{appl});
184 }
185  
186 # Sign the envelope
187 if (defined($self->{dsig})) { $self->_dsign_envelope($d); }
188  
189 # Send it the request envelope and receive the response envelope
190 my $request_str = xml_to_string($d,1);
191 my $response =
192 $self->_comm($method_name,$location,$request_method,
193 { 'Content-Type' => "text/xml",
194 'SOAPAction' => $$operation{soapAction} },
195 $request_str);
196 ($response->code == 200)
197 || die("SOAP request failed. Response follows:\n".
198 $response->as_string);
199  
200 ($response->content_type eq "text/xml")
201 || die("SOAP response was not XML. Response follows:\n".
202 $response->as_string);
203  
204 $d = xml_from_string($response->content);
205  
206 #### DECRYPTION HERE #########
207  
208 if (defined($self->{enc})) { $self->_decrypt_body($d); }
209  
210  
211 # Extract the results from the response
212  
213 my @raw_results;
214 if ($$operation{style} eq "rpc") {
215 @raw_results =
216 $self->response_rpc($location,$method_name,$operation,$d);
217 } elsif ($$operation{style} eq "document") {
218 @raw_results =
219 $self->response_document($location,$method_name,$operation,$d);
220 } else {
221 die;
222 }
223  
224 if (ref $args eq "HASH") {
225 my $results = {};
226 while ( $#raw_results >= 0 ) {
227 my $k = shift @raw_results;
228 my $v = shift @raw_results;
229 $$results{$k} = $v;
230 }
231 return $results;
232 } else {
233 my @results = ();
234 while ( $#raw_results >= 0 ) {
235 my $k = shift @raw_results;
236 my $v = shift @raw_results;
237 push @results,$v;
238 }
239 return wantarray ? @results : $results[0] ;
240 }
241  
242 }
243  
244 #########################################################################
245  
246 sub request_rpc {
247 my ($self,$location,$method_name,$operation,$args) = @_;
248  
249 my $input = $$operation{input};
250 assert($$input{kind} eq "input");
251 assert($$input{use} eq "encoded");
252  
253 my $input_parts = $$input{parts};
254 my @parts = ();
255 foreach my $part_name (keys %$input_parts) {
256 my $part_type = $$input_parts{$part_name};
257 my $part_value;
258 if (ref $args eq "HASH") {
259 $part_value = $$args{$part_name};
260 } else {
261 $part_value = shift @$args;
262 }
263 assert(defined($part_value),"$method_name: $part_name missing");
264 push @parts, arg_encode(xsd_type_to_sc($part_type),
265 "server:".$part_name,$part_value);
266 }
267  
268 # The request
269 my $d =
270 document
271 (element("SOAP-ENV:Envelope",
272 namespace("SOAP-ENV",$SOAP_ENV),
273 namespace("xsi",$xsi),
274 namespace("xsd",$xsd),
275 namespace("server",$$input{namespace}),
276 element("SOAP-ENV:Body",
277 element("server:".$method_name,
278 ($$input{encodingStyle} &&
279 attr("SOAP-ENV:encodingStyle",$SOAP_ENC)),
280 @parts))));
281  
282 return ('POST',$d);
283 }
284  
285 sub response_rpc {
286 my ($self,$location,$method_name,$operation,$d) = @_;
287  
288 my $output_flit = $$operation{output};
289 my $output_parts = $$output_flit{parts};
290  
291 my $envelope = xml_get_child($d,$SOAP_ENV,"Envelope");
292 assert($envelope);
293 my $body = xml_get_child($envelope,$SOAP_ENV,"Body");
294 assert($body);
295 my @body_kids = xml_get_children($body);
296 assert($#body_kids == 0);
297 my $container = $body_kids[0];
298 assert($container);
299  
300 my @results = ();
301  
302 foreach my $result ( xml_get_children($container) ) {
303 my ($urn,$local) = xml_get_name($result);
304 push @results, $local;
305 my $part_type = $$output_parts{$local};
306 assert(defined($part_type),"Argh! Where is the part-type");
307 if ($part_type eq "xml") {
308 push @results, arg_decode_to_xml($result);
309 } else {
310 push @results, arg_decode_to_string($result);
311 }
312 }
313 return wantarray ? @results : $results[0] ;
314 }
315  
316 ########################################################################
317  
318 sub request_document {
319 my ($self,$location,$method_name,$operation,$args) = @_;
320  
321 my $input = $$operation{input};
322 assert($$input{kind} eq "input");
323 assert($$input{use} eq "literal");
324  
325 my $input_parts = $$input{parts};
326 my @input_parts_names = keys %$input_parts;
327 assert($#input_parts_names == 0, "Wrong number of input parts");
328 my $input_part_name = $input_parts_names[0];
329 my $input_type = $$input_parts{$input_part_name};
330 assert($$input_type{kind} eq "element");
331 my $input_name = $$input_type{name};
332 my $input_ns = $$input_type{namespace};
333 $input_type = $$input_type{basetype};
334 assert($$input_type{kind} eq "sequence");
335  
336 my @input_formals = ();
337 foreach my $input_formal ( @{$$input_type{list}} ) {
338 assert($$input_formal{kind} eq "element");
339 my $input_formal_name = $$input_formal{name};
340 assert($$input_formal{namespace} eq $input_ns,
341 "Arg not in input namespace");
342 my $input_formal_value;
343 assert($$input_formal{nillable} eq "false");
344 assert($$input_formal{max_occurs} eq "1");
345 my $optional = ($$input_formal{min_occurs} eq "0");
346 if (ref $args eq "HASH") {
347 $input_formal_value = $$args{$input_formal_name};
348 } else {
349 $input_formal_value = shift @$args;
350 }
351 assert($optional || defined($input_formal_value),
352 "$method_name: $input_formal_name missing");
353 if (defined($input_formal_value)) {
354 my $input_formal_type = $$input_formal{basetype};
355 push @input_formals, arg_encode(xsd_type_to_sc($input_formal_type),
356 "server:".$input_formal_name,
357 $input_formal_value);
358 }
359 }
360  
361 # The request
362 my $d =
363 document
364 (element("SOAP-ENV:Envelope",
365 namespace("SOAP-ENV",$SOAP_ENV),
366 namespace("xsi",$xsi),
367 namespace("xsd",$xsd),
368 namespace("server",$input_ns),
369 element("SOAP-ENV:Body",
370 element("server:".$input_name,
371 ($$input{encodingStyle} &&
372 attr("SOAP-ENV:encodingStyle",$SOAP_ENC)),
373 @input_formals))));
374  
375 return ('POST',$d);
376 }
377  
378 sub response_document {
379 my ($self,$location,$method_name,$operation,$d) = @_;
380  
381 my $output = $$operation{output};
382 assert($$output{kind} eq "output");
383 assert($$output{use} eq "literal");
384  
385 my $envelope = xml_get_child($d,$SOAP_ENV,"Envelope");
386 assert($envelope);
387 my $body = xml_get_child($envelope,$SOAP_ENV,"Body");
388 assert($body);
389  
390 my @body_kids = xml_get_children($body);
391 assert($#body_kids == 0);
392 my $container = $body_kids[0];
393 assert($container);
394  
395 my $output_parts = $$output{parts};
396 my @output_parts_names = keys %$output_parts;
397 assert($#output_parts_names == 0,"Wrong number of output parts");
398 my $output_part_name = $output_parts_names[0];
399 my $output_type = $$output_parts{$output_part_name};
400 assert($$output_type{kind} eq "element");
401 my $output_name = $$output_type{name};
402 my $output_ns = $$output_type{namespace};
403 $output_type = $$output_type{basetype};
404 assert($$output_type{kind} eq "sequence");
405  
406 my @results = ();
407 foreach my $output_formal ( @{$$output_type{list}} ) {
408 assert($$output_formal{kind} eq "element");
409 my $output_formal_name = $$output_formal{name};
410 assert($$output_formal{namespace} eq $output_ns, "Arg not in output namespace");
411 my $output_formal_type = $$output_formal{basetype};
412 my $result = xml_get_child($container,$output_ns,$output_formal_name);
413 assert(defined($result),"$method_name: $output_formal_name missing");
414 my $value;
415 if (xsd_type_to_sc($output_formal_type) eq 'xml') {
416 $value = arg_decode_to_xml($result);
417 } else {
418 $value = arg_decode_to_string($result);
419 }
420 push @results, $output_formal_name;
421 push @results, $value;
422 }
423 return @results;
424 }
425  
426 ########################################################################
427  
428 sub xsd_type_to_sc {
429 my ($type) = @_;
430 my $kind = $$type{kind};
431  
432 return "int" if ($kind eq "int");
433 return "bool" if ($kind eq "boolean");
434 return "float" if ($kind eq "float");
435 return "raw" if ($kind eq "base64Binary");
436 return "string" if ($kind eq "string");
437 return "xml" if (is_any($type));
438  
439 print Dumper($type);
440 return "*unknown*";
441 }
442  
443 sub is_any {
444 my ($type) = @_;
445  
446 ($$type{kind} eq "sequence") || return 0;
447  
448 my @list = @{$$type{list}};
449 ($#list == 0) || return 0;
450  
451 my $x = $list[0];
452 ($$x{kind} eq "any") || return 0;
453  
454 return 1;
455 }
456  
457  
458 ########################################################################
459  
460 1;
461