LS/Authority/WSDL/Simple.pm untested

filter: Statementstmt 0.0% 0 / 253 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 39 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 393 CC 31 SCAR 37.2
CC
31
Cov
0%
buildLocations
182.0
new
30.0
addPort
20.0
CRAP 41.4
1 # ====================================================================
2 # Copyright (c) 2002,2003 IBM Corporation
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Common Public License v1.0
5 # which accompanies this distribution, and is available at
6 # http://www.opensource.org/licenses/cpl.php
7 #
8 # =====================================================================
9 package LS::Authority::WSDL::Simple;
10  
11 use strict;
12 use warnings;
13  
14 use vars qw( $METHODS
15 $METADATA_PREFIX
16 $METADATA_SUBSET_PREFIX
17 $DATA_PREFIX
18 );
19  
20 use URI;
21  
22 use LS;
23  
24 use LS::Authority::Mappings;
25  
26 use LS::Authority::WSDL;
27 use LS::Authority::WSDL::Location;
28 use LS::Authority::WSDL::Constants;
29  
30  
31 use base 'LS::Authority::WSDL';
32  
33  
34 #
35 # Constants for creating built-in bindings
36 #
37 $METADATA_PREFIX = 'LSIDMetadata';
38 $METADATA_SUBSET_PREFIX = "${METADATA_PREFIX}Subset";
39  
40 $DATA_PREFIX = 'LSIDData';
41  
42 sub BEGIN {
43  
44 $METHODS = [
45 'defaultServiceName',
46 'authority',
47
48 'metadataLocations',
49 'dataLocations',
50 'unknownLocations',
51 ];
52
53 LS::makeAccessorMethods($METHODS, __PACKAGE__);
54 }
55  
56  
57  
58 #
59 # new( %options ) -
60 #
61 sub new {
62
63 my $self = shift;
64 my %params = @_;
65  
66 $self = $self->SUPER::new(@_);
67  
68 $self->lsid($params{'lsid'})
69 if($params{'lsid'});
70
71 $self->authority($params{'authority'})
72 if($params{'authority'});
73
74  
75 $self->metadataLocations( {} );
76 $self->dataLocations( {} );
77 $self->unknownLocations( {} );
78  
79 #
80 # Default imports
81 #
82 $self->add_xml_import(location=> 'LSIDDataServiceHTTPBindings.wsdl',
83 namespace=> 'http://www.omg.org/LSID/2003/DataServiceHTTPBindings');
84  
85 $self->add_xml_import(location=> 'LSIDDataServiceSOAPBindings.wsdl',
86 namespace=> 'http://www.omg.org/LSID/2003/DataServiceSOAPBindings');
87  
88  
89 #
90 # Default namespaces
91 #
92 $self->add_namespace(prefix=>'dhb',
93 uri=>'http://www.omg.org/LSID/2003/DataServiceHTTPBindings');
94  
95 $self->add_namespace(prefix=>'dsb',
96 uri=>'http://www.omg.org/LSID/2003/DataServiceSOAPBindings');
97 return $self;
98 }
99  
100  
101 #
102 # lsid( $lsid ) -
103 #
104 sub lsid {
105  
106 my ($self, $lsid) = @_;
107  
108 if ($lsid) {
109
110 $lsid = LS::ID->new($lsid);
111
112 unless ($lsid) {
113
114 $self->recordError("Invalid LSID");
115 $self->addStackTrace();
116
117 return undef;
118 }
119  
120 $self->{__PACKAGE__ . '__lsid'} = $lsid;
121 }
122
123 return $self->{__PACKAGE__ . '__lsid'};
124 }
125  
126  
127 #
128 # defaultServiceName( [ $name ] )
129 # Returns the name of the first service (the default service)
130 #
131  
132  
133 #
134 # addPort( %options )
135 #
136 # Adds a LS::Authority::WSDL::Port to the specified service
137 #
138 sub addPort {
139  
140 my ($self, %options) = @_;
141  
142 unless($options{'port'} &&
143 $options{'serviceName'}) {
144  
145 $self->recordError('Missing parameters');
146 $self->addStackTrace();
147
148 return undef;
149 }
150  
151 unless(UNIVERSAL::isa($options{'port'}, 'LS::Authority::WSDL::Port')) {
152  
153 $self->recordError('Parameter \'port\' is not an LS::Authority::WSDL::Port');
154 $self->addStackTrace();
155
156 return undef;
157 }
158  
159 # Attempt to locate the specified service. If it does not exist,
160 # create the service object and add it to the array.
161 my $svc = $self->getService($options{'serviceName'});
162  
163 unless($svc) {
164  
165 $svc = LS::Authority::WSDL::ServiceDefinition->new(name=> $options{'serviceName'});
166 $self->add_service($svc);
167 }
168  
169 return $svc->add_port($options{'port'});
170 }
171  
172  
173 #
174 # getMetadataLocations( $serviceName )
175 #
176 # Retreives the metadata ports for the specified service.
177 # If the service name is not specified, the ports of the first
178 # service will be returned.
179 #
180 sub getMetadataLocations {
181  
182 my $self = shift;
183 my $serviceName = shift;
184  
185 $serviceName = $self->defaultServiceName()
186 unless($serviceName);
187  
188 return $self->metadataLocations()->{ $serviceName };
189 }
190  
191  
192 #
193 # getAllMetadataLocations( )
194 #
195 # Retreives all known metadata locations for this WSDL
196 #
197 sub getAllMetadataLocations {
198  
199 my $self = shift;
200
201 my $locations = [];
202 # TODO: Finish this function
203 return $locations;
204 }
205  
206  
207 #
208 # getDataLocations( $serviceName )
209 #
210 # Retrieves the data ports for the specified service.
211 # If the service name is not specified, the ports of the
212 # first service will be returned.
213 #
214 sub getDataLocations {
215  
216 my $self = shift;
217 my $serviceName = shift;
218
219 $serviceName = $self->defaultServiceName()
220 unless($serviceName);
221  
222 return $self->dataLocations()->{ $serviceName };
223 }
224  
225  
226 #
227 # getAllDataLocations( )
228 #
229 # Retreives all known data locations for this WSDL
230 #
231 sub getAllDataLocations {
232  
233 my $self = shift;
234
235 my $locations = [];
236 # TODO: Finish this function
237 return $locations;
238 }
239  
240  
241 #
242 # to_xml( ) - Returns the WSDL data structure as a WSDL XML document
243 #
244 sub to_xml {
245  
246 my $self = shift;
247  
248 $self->targetNamespace('http://' . $self->authority() . '/availableServices?' . ($self->lsid() ? $self->lsid()->as_string() : ''));
249  
250 return $self->SUPER::to_xml();
251 }
252  
253  
254 #
255 # from_xml( $xml ) - Builds on object based on the XML parameter
256 #
257 sub from_xml {
258  
259 my $self = shift->new();
260
261 $self = $self->SUPER::from_xml(@_);
262 unless(UNIVERSAL::isa($self, 'LS::Authority::WSDL::Simple')) {
263 return undef;
264 }
265  
266 # Parse the target namespace for the LSID and authority
267 if ($self->targetNamespace() =~ m|^http://([^/]*)/availableServices\?(.*)$|) {
268
269 $self->authority($1);
270 $self->lsid($2);
271 }
272
273 # Get the first service's name and use that as a default
274 $self->defaultServiceName((values(%{ $self->services() }))[0]->name())
275 if(scalar(values(%{ $self->services() })) > 0);
276  
277  
278 # Build the metadata and data location structures
279 $self->buildLocations();
280
281 return $self;
282 }
283  
284  
285 sub buildLocations {
286  
287 my $self = shift;
288
289 my $metadataLocations = $self->metadataLocations();
290 my $dataLocations = $self->dataLocations();
291 my $unknownLocations = $self->unknownLocations();
292
293 foreach my $service (values(%{ $self->services() })) {
294  
295 # Initialize the location hashes for this service if necessary
296 $metadataLocations->{ $service->name() } = []
297 unless(UNIVERSAL::isa($metadataLocations->{ $service->name() }, 'ARRAY'));
298
299 $dataLocations->{ $service->name() } = []
300 unless(UNIVERSAL::isa($dataLocations->{ $service->name() }, 'ARRAY'));
301
302 $unknownLocations->{ $service->name() } = []
303 unless(UNIVERSAL::isa($unknownLocations->{ $service->name() }, 'ARRAY'));
304
305 foreach my $port (@{ $service->ports() }) {
306
307 my $binding = $port->binding();
308 my $protocol = $port->implementation()->protocol();
309
310 my $location;
311  
312 # Figure out which kind of port this is: Data or Metadata
313 if($binding =~ /$LS::Authority::WSDL::Simple::METADATA_PREFIX.*/) {
314  
315 $location = $metadataLocations->{ $service->name() };
316 }
317 elsif($binding =~ /$LS::Authority::WSDL::Simple::DATA_PREFIX.*/) {
318  
319 $location = $dataLocations->{ $service->name() };
320 }
321 else {
322  
323 $location = $unknownLocations->{ $service->name() };
324 }
325  
326 my $method = '';
327 my $url = $port->implementation->get_attr('location');
328
329 if ($protocol eq ${LS::Authority::WSDL::Constants::Protocols::HTTP}) {
330  
331 # Setup the METHOD item for HTTP
332 $method = (uc($port->implementation->get_attr('verb')) ||
333 ${LS::Authority::WSDL::Constants::Protocols::HTTP_GET});
334 }
335 elsif ($protocol eq ${LS::Authority::WSDL::Constants::Protocols::SOAP}) {
336  
337 # Nothing specific for SOAP
338 }
339 elsif (0 && $protocol eq ${LS::Authority::WSDL::Constants::Protocols::FTP}) {
340 # Disabled code block
341 # my $out_imp = $op->output();
342 my $out_imp;
343 next unless $protocol eq $out_imp->protocol();
344 next unless 'get' eq $out_imp->name();
345  
346 my $path = $out_imp->get_attr('filepath');
347 $path = '/' . $path unless($path =~ m|^/|);
348  
349 my $server = $port->implementation()->get_attr('server');
350 $server =~ s|/+$||;
351  
352 my $username = $port->implementation()->get_attr('user');
353 my $password = $port->implementation()->get_attr('password');
354  
355 $url = 'ftp://' . (($username || $password) ? ($username. ':' . $password . '@') : '') . $server . $path;
356  
357 push @{ $location },
358 LS::Authority::WSDL::Location->new(
359 protocol => $protocol,
360 url => $url
361 );
362
363 }
364 else {
365
366 # TODO: Error message for unknown protocols?
367 }
368  
369  
370 # Store the new LS::Authority::WSDL::Location in the
371 # previously determined metadata, data or unknown location hash
372 push @{ $location },
373 LS::Authority::WSDL::Location->new(
374 protocol=> $protocol,
375 url=> $url,
376 binding=> $binding,
377 name=> $port->name(),
378 parentName=> $service->name(),
379 method=> $method,
380 );
381  
382
383 } # End ports processing
384 } # End services processing
385 }
386  
387  
388 package LS::Authority::WSDL::Simple::MetadataPort;
389  
390 use strict;
391 use warnings;
392  
393 use LS::Authority::WSDL;
394 use LS::Authority::WSDL::Constants;
395  
396  
397 #
398 # new( %options ) -
399 #
400 sub new {
401  
402 shift; # Throw away
403  
404 my (%options) = @_;
405  
406 my $portName = $options{'portName'};
407  
408 my $binding = $options{'binding'};
409 my $protocol = lc($options{'protocol'});
410 my $endpoint = $options{'endpoint'};
411  
412 # Validate the parameters
413  
414 my $port_impl = LS::Authority::WSDL::Implementation->new(
415 protocol => $protocol,
416 name => 'address',
417 attr => {
418 location => $endpoint,
419 }
420 );
421  
422 # FIXME: Error strings!
423 return undef unless($port_impl);
424  
425 my $port = LS::Authority::WSDL::Port->new(
426 name=> $portName,
427 binding=> $binding,
428 implementation=> $port_impl
429 );
430  
431 return $port;
432 }
433  
434  
435 #
436 # newMetadata( %options ) -
437 #
438 sub newMetadata {
439  
440 shift;
441  
442 my (%options) = @_;
443  
444 my $binding = ${LS::Authority::WSDL::Simple::METADATA_PREFIX} . uc($options{'protocol'}) . 'Binding';
445  
446 $binding = LS::Authority::WSDL::Mappings->bindingToPrefix($binding) . ":$binding";
447  
448 return LS::Authority::WSDL::Simple::MetadataPort->new(
449 %options,
450 binding=> $binding
451 );
452 }
453  
454  
455 #
456 # newMetadataSubset( %options ) -
457 #
458 sub newMetadataSubset {
459  
460 shift;
461  
462 my (%options) = @_;
463  
464 my $binding = ${LS::Authority::WSDL::Simple::METADATA_SUBSET_PREFIX} . uc($options{'protocol'}) . 'Binding';
465  
466 $binding = LS::Authority::WSDL::Mappings->bindingToPrefix($binding) . ":$binding";
467  
468 return LS::Authority::WSDL::Simple::MetadataPort->new(
469 %options,
470 binding=> $binding
471 );
472 }
473  
474  
475 #
476 # newMetadataDirect( %options ) -
477 #
478 sub newMetadataDirect {
479  
480 shift;
481  
482 my (%options) = @_;
483  
484 $options{'protocol'} = ${LS::Authority::WSDL::Protocols::HTTP};
485  
486 my $binding = ${LS::Authority::WSDL::Simple::METADATA_PREFIX} . uc($options{'protocol'}) . 'BindingDirect';
487  
488 $binding = LS::Authority::WSDL::Mappings->bindingToPrefix($binding) . ":$binding";
489  
490 return LS::Authority::WSDL::Simple::MetadataPort->new(
491 %options,
492 binding=> $binding
493 );
494 }
495  
496  
497 #
498 # newMetadataSubsetDirect( %options ) -
499 #
500 sub newMetadataSubsetDirect {
501  
502 shift;
503  
504 my (%options) = @_;
505  
506 $options{'protocol'} = ${LS::Authority::WSDL::Protocols::HTTP};
507  
508 my $binding = ${LS::Authority::WSDL::Simple::METADATA_SUBSET_PREFIX} . uc($options{'protocol'}) . 'BindingDirect';
509  
510 $binding = LS::Authority::WSDL::Mappings->bindingToPrefix($binding) . ":$binding";
511  
512 return LS::Authority::WSDL::Simple::MetadataPort->new(
513 %options,
514 binding=> $binding
515 );
516 }
517  
518  
519  
520  
521 package LS::Authority::WSDL::Simple::DataPort;
522  
523 use strict;
524 use warnings;
525  
526 use LS::Authority::WSDL;
527 use LS::Authority::WSDL::Constants;
528  
529  
530 #
531 # new( %options ) -
532 #
533 sub new {
534  
535 shift;
536  
537 my (%options) = @_;
538  
539 my $portName = $options{'portName'};
540  
541 my $binding = $options{'binding'};
542 my $protocol = lc $options{'protocol'};
543 my $endpoint = $options{'endpoint'};
544  
545 # Validate the parameters
546  
547 my $port_impl;
548  
549 if($protocol eq ${LS::Authority::WSDL::Constants::Protocols::FTP}) {
550  
551 my $username = $options{'username'};
552 my $password = $options{'password'};
553  
554 $port_impl = LS::Authority::WSDL::Implementation->new(
555 protocol => $protocol,
556 name => 'location',
557 attr => {
558 server => $endpoint,
559 $username ? (user => $username) : (),
560 $password ? (password => $password) : (),
561 }
562 );
563 }
564 else {
565  
566 $port_impl = LS::Authority::WSDL::Implementation->new(
567 protocol => $protocol,
568 name => 'address',
569 attr => {
570 location => $endpoint,
571 }
572 );
573 }
574  
575 # FIXME: Error strings!
576 return undef unless($port_impl);
577  
578 my $port = LS::Authority::WSDL::Port->new(
579 name=> $portName,
580 binding=> $binding,
581 implementation=> $port_impl
582 );
583  
584 return $port;
585 }
586  
587  
588 #
589 # newData( %options ) -
590 #
591 sub newData {
592  
593 shift;
594  
595 my (%options) = @_;
596  
597 my $binding = ${LS::Authority::WSDL::Simple::DATA_PREFIX} . uc($options{'protocol'}) . 'Binding';
598  
599 $binding = LS::Authority::WSDL::Mappings->bindingToPrefix($binding) . ":$binding";
600  
601 return LS::Authority::WSDL::Simple::DataPort->new(
602 %options,
603 binding=> $binding
604 );
605 }
606  
607  
608 #
609 # newDataByRange( %options ) -
610 #
611 sub newDataByRange {
612  
613 shift;
614  
615 my (%options) = @_;
616  
617 my $binding = ${LS::Authority::WSDL::Simple::DATA_PREFIX} . uc($options{'protocol'}) . 'Binding';
618  
619 $binding = LS::Authority::WSDL::Mappings->bindingToPrefix($binding) . ":$binding";
620  
621 return LS::Authority::WSDL::Simple::DataPort->new(
622 %options,
623 binding=> $binding
624 );
625 }
626  
627  
628 #
629 # newDataDirect( %options ) -
630 #
631 sub newDataDirect {
632  
633 shift;
634  
635 my (%options) = @_;
636  
637 $options{'protocol'} = ${LS::Authority::WSDL::Protocols::HTTP};
638  
639 my $binding = ${LS::Authority::WSDL::Simple::DATA_PREFIX} . uc($options{'protocol'}) . 'BindingDirect';
640  
641 $binding = LS::Authority::WSDL::Mappings->bindingToPrefix($binding) . ":$binding";
642  
643 return LS::Authority::WSDL::Simple::DataPort->new(
644 %options,
645 binding=> $binding
646 );
647 }
648  
649  
650 #
651 # newDataByRangeDirect( %options ) -
652 #
653 sub newDataByRangeDirect {
654  
655 shift;
656  
657 my (%options) = @_;
658  
659 $options{'protocol'} = ${LS::Authority::WSDL::Protocols::HTTP};
660  
661 my $binding = ${LS::Authority::WSDL::Simple::DATA_PREFIX} . uc($options{'protocol'}) . 'BindingDirect';
662  
663 $binding = LS::Authority::WSDL::Mappings->bindingToPrefix($binding) . ":$binding";
664  
665 return LS::Authority::WSDL::Simple::DataPort->new(
666 %options,
667 binding=> $binding
668 );
669 }
670  
671  
672  
673 1;
674  
675 __END__