LS/Authority/WSDL.pm untested

filter: Statementstmt 0.0% 0 / 227 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 44 Podpod 0.0% 0 / 35 totaltotal 0.0% 0 / 409 CC 38 SCAR 22.5
CC
38
Cov
0%
new
30.0
new
30.0
add_service
12.0
CRAP 9.5
1 # $Id: WSDL.pm 1814 2007-11-05 19:22:21Z edwardkawas $
2 # ====================================================================
3 # Copyright (c) 2002,2003 IBM Corporation
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Common Public License v1.0
6 # which accompanies this distribution, and is available at
7 # http://www.opensource.org/licenses/cpl.php
8 #
9 # =====================================================================
10 package LS::Authority::WSDL;
11  
12 use strict;
13 use warnings;
14  
15 use vars qw( $METHODS %URI );
16  
17 use LS;
18 use LS::ID;
19 use LS::Authority::WSDL::Constants;
20 use LS::Authority::WSDL::Bindings;
21  
22 use base 'LS::Base';
23  
24  
25 %URI = (
26 wsdl=> 'http://schemas.xmlsoap.org/wsdl/',
27 http=> 'http://schemas.xmlsoap.org/wsdl/http/',
28 soap=> 'http://schemas.xmlsoap.org/wsdl/soap/',
29 );
30  
31  
32  
33 sub BEGIN {
34  
35 $METHODS = [
36 'bindings',
37 'services',
38 'namespaces',
39 'xmlImports',
40 'targetNamespace',
41 'defaultXMLNamespace',
42 ];
43
44 LS::makeAccessorMethods($METHODS, __PACKAGE__);
45 }
46  
47  
48  
49 #
50 # new( %options ) -
51 #
52 sub new {
53 my $self = shift;
54  
55 unless(ref $self) {
56
57 $self = bless {}, $self;
58 }
59
60 # Initialize this object
61 $self->services( {} );
62 $self->namespaces( {} );
63 $self->xmlImports( {} );
64
65 $self->bindings([]);
66  
67 $self->add_namespace(prefix=> 'wsdl', uri=> $URI{'wsdl'});
68
69 return $self;
70 }
71  
72  
73  
74 #
75 # bindings( ) -
76 #
77  
78  
79 #
80 # services( )
81 # Gets or sets the hash of services
82 #
83  
84  
85 #
86 # getService( $name )
87 # Gets the specified service by name
88 #
89 sub getService {
90  
91 my $self = shift;
92 my ($name) = @_;
93  
94 return (exists($self->services()->{ $name })) ? $self->services()->{ $name } : undef
95 }
96  
97  
98 #
99 # authority - Gets or Sets the authority name for this WSDL document
100 #
101  
102  
103 #
104 # targetNamespace( $tns ) -
105 #
106  
107  
108 #
109 # add_binding( $binding ) -
110 #
111 sub add_binding {
112 my $self = shift;
113 my ($binding) = @_;
114
115 push(@{ $self->bindings() }, $binding);
116 }
117  
118  
119 #
120 # add_service( $service ) -
121 #
122 sub add_service {
123
124 my $self = shift;
125 my $newService = shift;
126
127 unless(UNIVERSAL::isa($newService, 'LS::Authority::WSDL::ServiceDefinition')) {
128 return undef;
129 }
130
131 # FIXME: Should this be a strict check?
132 if(exists($self->services()->{ $newService->name() })) {
133 print STDERR "Adding duplicate service: " . $newService->name() . "\n";
134 }
135
136 $self->services()->{ $newService->name() } = $newService;
137 }
138  
139  
140 #
141 # add_xml_import( %options ) -
142 #
143 sub add_xml_import {
144  
145 my ($self, %options) = @_;
146  
147 $self->xmlImports()->{ $options{'location'} } = $options{'namespace'};
148 }
149  
150  
151 #
152 # add_namespace( %options ) -
153 #
154 sub add_namespace {
155  
156 my ($self, %options) = @_;
157  
158 if(exists($options{'defaultNamespace'})) {
159
160 $self->defaultXMLNamespace($options{'defaultNamespace'});
161 }
162 elsif( exists($options{'prefix'}) && exists($options{'uri'})) {
163
164 $self->namespaces()->{ $options{'prefix'} } = $options{'uri'};
165 }
166 else {
167
168 # TODO: Error message?
169 }
170 }
171  
172  
173 sub inline_std_defs {
174 my $self = shift;
175
176 @_ ? $self->{'_inline_std_defs'} = $_[0] : $self->{'_inline_std_defs'};
177 }
178  
179 #
180 # XML Manipulation / Display methods
181 #
182 #
183 # xml( ) - Returns the entire document as XML if no parameters are specified.
184 # Otherwise, builds a WSDL object based on the pass parameter in XML
185 #
186 sub xml {
187 my $self = shift;
188
189 return (@_ ? $self->from_xml(@_) : $self->to_xml());
190 }
191  
192 #
193 # imports_xml( ) - Returns the imports of this WSDL document in XML
194 #
195 sub imports_xml {
196  
197 my $self = shift;
198  
199 my $imports_xml;
200 my $imports_ref = $self->xmlImports();
201  
202 foreach my $location (keys(%{ $imports_ref })) {
203  
204 $imports_xml .= '<wsdl:import namespace="' . $imports_ref->{ $location } . '" location="' . $location . '"/>' . "\n";
205 }
206  
207 return $imports_xml;
208 }
209  
210 #
211 # namespaces_xml( ) - Returns the namespaces of this WSDL document in XML
212 #
213 sub namespaces_xml {
214  
215 my $self = shift;
216  
217 my $ns_xml;
218 my $ns_ref = $self->namespaces();
219  
220 $ns_xml = ' xmlns="' . $self->defaultXMLNamespace() . "\"\n"
221 if($self->defaultXMLNamespace());
222
223 $ns_xml .= ' xmlns:tns="' . $self->targetNamespace() . "\"\n";
224  
225 foreach my $prefix (keys(%{ $ns_ref })) {
226  
227 $ns_xml .= ' xmlns:' . $prefix . '="' . $ns_ref->{$prefix} . '"' . "\n";
228 }
229  
230 return $ns_xml;
231 }
232  
233  
234 #
235 # services_xml( ) - Returns the services contained in this WSDL object as XML
236 #
237 sub services_xml {
238  
239 my $self = shift;
240  
241 my $service_xml;
242  
243 foreach my $service (values(%{ $self->services() })) {
244  
245 foreach my $port (@{ $service->ports() }) {
246  
247 # Add the port's protocol's namespace
248 if($port->implementation()) {
249  
250 my $protocolURI = $port->implementation()->protocol();
251 my $uri = LS::Authority::WSDL::Constants::Protocols::protocolToURI( $protocolURI );
252  
253 $self->add_namespace(prefix=> $port->implementation()->protocol(),
254 uri=>$uri)
255 if($uri);
256 }
257 }
258
259 $service_xml .= $service->xml() . "\n";
260 }
261  
262 return $service_xml;
263 }
264  
265  
266 #
267 # to_xml( ) - Converts the entire WSDL object to XML
268 #
269 sub to_xml {
270  
271 my $self = shift;
272
273 # This adds each port's protocol namespace to the list of namespace
274 my $services_xml = $self->services_xml();
275  
276 my $ns_xml = $self->namespaces_xml();
277
278 my $target_ns_xml = ' targetNamespace="' . $self->targetNamespace() . '"';
279  
280 return
281 '<?xml version="1.0" ?>' . "\n" .
282 '<wsdl:definitions' . $target_ns_xml . $ns_xml . ">\n" .
283 $self->imports_xml() .
284 $services_xml .
285 '</wsdl:definitions>';
286 }
287  
288 #
289 # from_xml( ) - Creates a WSDL object based on the input XML
290 #
291 sub from_xml {
292  
293 my $self = shift->new();
294 my ($xml) = @_;
295  
296 $self->services({});
297  
298 require XML::XPath;
299  
300 my $xpath = XML::XPath->new(xml => $xml);
301
302 # Setup namespace mappings for the XPath queries
303 $xpath->set_namespace('wsdl', $URI{'wsdl'});
304  
305 my $service_nodes = $xpath->find("wsdl:definitions/wsdl:service");
306  
307 foreach my $service_node ($service_nodes->get_nodelist()) {
308 my $service = LS::Authority::WSDL::ServiceDefinition->from_xpath_node($service_node, $xpath);
309 next unless $service;
310  
311 $self->add_service($service);
312 }
313  
314 # Record the targetNamespace for future use
315 my $targetns_nodes = $xpath->find('wsdl:definitions/@targetNamespace');
316
317 if ($targetns_nodes->size() == 1) {
318 my $targetns_node = $targetns_nodes->get_node(1);
319 $self->targetNamespace( $targetns_node->getNodeValue() )
320 }
321
322 return $self;
323 }
324  
325  
326 sub _escape {
327 my $string = $_[0];
328
329 $string =~ s/&/&amp;/g;
330 $string =~ s/</&lt;/g;
331 $string =~ s/>/&gt;/g;
332  
333 return $string;
334 }
335  
336  
337 package LS::Authority::WSDL::ServiceDefinition;
338  
339 sub new {
340 my $self = shift;
341 my %params = @_;
342  
343 unless (ref $self) {
344 $self = bless [
345 undef, # name,
346 [] # ports
347 ], $self;
348
349 $self->name($params{'name'})
350 if($params{'name'});
351 }
352
353 return $self;
354 }
355  
356 sub name {
357 my $self = shift;
358
359 @_ ? $self->[0] = $_[0] : $self->[0];
360 }
361  
362 sub ports {
363 my $self = shift;
364
365 return $self->[1];
366 }
367  
368 sub add_port {
369 my $self = shift;
370
371 my $port = shift;
372
373 unless(UNIVERSAL::isa($port, 'LS::Authority::WSDL::Port')) {
374
375 return undef;
376 }
377  
378 push(@{ $self->ports() }, $port);
379 }
380  
381 sub xml {
382
383 my $self = shift;
384
385 my $ports_xml = '';
386
387 foreach my $port (@{ $self->ports() }) {
388
389 $ports_xml .= $port->xml();
390 }
391
392 return
393 '<wsdl:service name="' . $self->name() . '">' . "\n" .
394 $ports_xml .
395 '</wsdl:service>';
396 }
397  
398 sub from_xpath_node {
399  
400 my $self = shift->new();
401 my ($node, $xpath) = @_;
402
403 my $name = $node->getAttribute('name') || return;
404 $self->name($name);
405  
406 my $port_nodes = $xpath->find('wsdl:port', $node);
407  
408 foreach my $port_node ($port_nodes->get_nodelist()) {
409 my $port = LS::Authority::WSDL::Port->from_xpath_node($port_node, $xpath);
410 next unless $port;
411  
412 $self->add_port($port);
413 }
414
415 return $self;
416 }
417  
418  
419 package LS::Authority::WSDL::Port;
420  
421 sub new {
422
423 my ($self, %params) = @_;
424  
425 unless(ref $self) {
426 $self = bless [
427 undef, # name,
428 undef, # binding,
429 undef # implementation
430 ], $self;
431
432 $self->name($params{'name'})
433 if(exists($params{'name'}) && defined($params{'name'}));
434
435 $self->binding($params{'binding'})
436 if (exists($params{'binding'}) && defined($params{'binding'}));
437
438 $self->implementation($params{'implementation'})
439 if (exists($params{'implementation'}) && defined($params{'implementation'}));
440 }
441
442 return $self;
443 }
444  
445 sub name {
446
447 my $self = shift;
448  
449 @_ ? $self->[0] = $_[0] : $self->[0];
450 }
451  
452 sub binding {
453
454 my $self = shift;
455  
456 @_ ? $self->[1] = $_[0] : $self->[1];
457 }
458  
459 sub implementation {
460
461 my $self = shift;
462  
463 @_ ? $self->[2] = $_[0] : $self->[2];
464 }
465  
466 sub xml {
467 my $self = shift;
468
469 return
470 '<wsdl:port name="' . $self->name() . '"' . ($self->binding() ? ' binding="' . $self->binding() . '"' : '') . '>' . "\n" .
471 ($self->implementation() ? $self->implementation->xml() : '') . "\n" .
472 "</wsdl:port>\n";
473 }
474  
475 sub from_xpath_node {
476
477 my $self = shift->new();
478
479 my ($node, $xpath) = @_;
480  
481 my $name = $node->getAttribute('name') || return undef;
482 $self->name($name);
483  
484 my $binding = $node->getAttribute('binding') || 'http://www.omg.org/LSID/2003/LSIDDataServiceUnknownBinding';
485 $self->binding($binding);
486  
487 my $imp_nodes = $xpath->find('*', $node);
488 return undef if($imp_nodes->size() != 1);
489  
490 my $imp_node = $imp_nodes->get_node(1);
491 my $imp = LS::Authority::WSDL::Implementation->from_xpath_node($imp_node, $xpath);
492 return undef unless($imp);
493  
494 $self->implementation($imp);
495  
496 return $self;
497 }
498  
499  
500 package LS::Authority::WSDL::Implementation;
501  
502 sub new {
503 my $self = shift;
504 my %params = @_;
505  
506 unless(ref $self) {
507 $self = bless [
508 undef, # protocol,
509 undef, # name,
510 {} # attributes
511 ], $self;
512  
513 $self->protocol($params{'protocol'}) if defined $params{'protocol'};
514 $self->name($params{'name'}) if defined $params{'name'};
515 $self->attr($params{'attr'}) if defined $params{'attr'};
516 }
517
518 return $self;
519 }
520  
521 sub protocol {
522
523 my $self = shift;
524  
525 @_ ? $self->[0] = $_[0] : $self->[0];
526 }
527  
528 sub name {
529
530 my $self = shift;
531  
532 @_ ? $self->[1] = $_[0] : $self->[1];
533 }
534  
535 sub attr {
536
537 my $self = shift;
538  
539 @_ ? $self->[2] = $_[0] : $self->[2];
540 }
541  
542 sub get_attr {
543
544 my $self = shift;
545
546 my ($name) = @_;
547  
548 return $self->attr()->{ $name };
549 }
550  
551 sub set_attr {
552
553 my $self = shift;
554
555 my ($name, $value) = @_;
556  
557 $self->attr()->{ $name } = $value;
558 }
559  
560 sub xml {
561
562 my $self = shift;
563
564 my %attr = %{ $self->attr() };
565  
566 my $attr_string = '';
567  
568 foreach my $name (keys(%attr)) {
569
570 $attr_string .= ' ' . $name . '="' . &LS::Authority::WSDL::_escape($attr{ $name }) . '"';
571 }
572  
573 return '<' . $self->protocol() . ':' . $self->name() . $attr_string . '/>';
574 }
575  
576 sub from_xpath_node {
577
578 my $self = shift->new();
579
580 my ($node, $xpath) = @_;
581  
582 my $protocolURI = $node->getNamespace($node->getPrefix())->getExpanded();
583 my $protocol = LS::Authority::WSDL::Constants::Protocols::uriToProtocol( $protocolURI );
584 unless($protocol) {
585 die("Could not map protocol URI to a supported LSID Protocol, URI: $protocolURI");
586 }
587  
588 $self->name($node->getLocalName());
589 $self->protocol($protocol);
590  
591 my %attrs = ();
592 my @attr_nodes = $node->getAttributes();
593
594 foreach my $attr_node (@attr_nodes) {
595
596 $attrs{ $attr_node->getName() } = $attr_node->getNodeValue();
597 }
598  
599 $self->attr(\%attrs);
600  
601 return $self;
602 }
603  
604 1;
605  
606 __END__