LS/SOAP/Service.pm untested

filter: Statementstmt 0.0% 0 / 245 Branchbran 0.0% 0 / 120 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 42 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 482 CC 61 SCAR 33.7
CC
61
Cov
0%
getMetadata
90.0
getAvailableServices
56.0
handle_http
42.0
CRAP 29.1
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  
10 package LS::SOAP::Service;
11  
12 use strict;
13 use warnings;
14  
15 use vars qw(@ISA $METHOD_PREFIX);
16  
17 use SOAP::Lite;
18  
19 use MIME::Entity;
20  
21 use HTTP::Status;
22 use HTTP::Headers;
23 use HTTP::Request;
24 use HTTP::Response;
25  
26  
27 use LS::Service;
28 use LS::Service::Fault;
29 use LS::Service::Response;
30  
31 use LS::SOAP::Fault;
32 use LS::SOAP::Serializer;
33  
34 use LS::Authority::WSDL::Constants;
35  
36  
37 use base 'LS::Service';
38  
39  
40  
41 $METHOD_PREFIX = '';
42  
43 # A SOAP::Server object of the appropriate subclass is created, and given
44 # the following settings:
45 #
46 # A custom serializer is used to allow setting/retrieval of the namespace
47 # prefix used on the method element, instead of using an internally generated
48 # one. This allows us to use the same known prefix when we generate elements
49 # in the response message. This serializer also turns autotyping off, and
50 # removes extraneous attributes and namespace declarations from the envelope.
51 #
52 # The default on_action handler for HTTP transport checks the SOAPAction HTTP
53 # header, and dies if it doesn't match the <namespace uri>#<name> of the
54 # method element in the envelope. Since the WS-I basic profile says that
55 # SOAPAction may contain any string (it's just a hint), we override with a noop.
56 #
57 # Custom HTTP Basic authentication processing is done via redefining the handle
58 # method. If authentication is enabled, the system will look for the appropriate
59 # headers and pass the credentials to a user defined routine.
60 #
61 #
62 # All of the authority, metadata, and data operations are defined in the
63 # namespace http://www.ibm.com/LSID/Standard/WSDL/SchemaTypes, so we dispatch
64 # that URI to this instance.
65 #
66 # This is no longer true, we must accept operations in the OMG spec.
67 #
68  
69  
70  
71 #
72 # new -
73 #
74 sub new {
75  
76 my $self = shift;
77  
78 unless (ref $self) {
79 $self = $self->SUPER::new(@_);
80  
81 $self->{'_mappings'} = {};
82 $self->{'_http_server'} = undef;
83  
84 $self->mappings('http://www.ibm.com/LSID/Standard/WSDL/SchemaTypes'=> $self,
85 'http://www.omg.org/LSID/2003/AuthorityServiceSOAPBindings'=> $self,
86 'http://www.omg.org/LSID/2003/DataServiceSOAPBindings'=> $self,
87 'http://www.omg.org/LSID/2003/DataServiceHTTPBindings'=> $self,
88 );
89  
90 $self -> serializer(LS::SOAP::Serializer->method_prefix($METHOD_PREFIX))
91 -> on_action(sub {})
92 -> dispatch_with($self->mappings());
93 }
94  
95 return $self;
96 }
97  
98  
99 #
100 # mappings -
101 #
102 sub mappings {
103  
104 my $self = shift;
105  
106 if(scalar(@_) > 1) {
107  
108 # 2 or more params indicates a new key
109 while(@_) {
110  
111 my $key = shift;
112 my $value = shift;
113  
114 $self->{'_mappings'}->{$key} = $value;
115 }
116  
117 if(@_) {
118  
119 $self->recordError('Odd number of parameters in hash');
120 $self->addStackTrace();
121  
122 return undef;
123 }
124 }
125 elsif(scalar(@_) == 1) {
126  
127 my $param = shift;
128 if(ref $param eq 'HASH') {
129  
130 #
131 # Copy the hash
132 #
133 %{$self->{'_mappings'}} = %{$param};
134  
135 return;
136 }
137  
138 #
139 # Non-hash return that key's value
140 #
141 return $self->{'_mappings'}->{$param};
142 }
143  
144 return $self->{'_mappings'};
145 }
146  
147  
148 #
149 # httpServer -
150 #
151 sub httpServer {
152  
153 my $self = shift;
154 @_ ? $self->{'_http_server'} = shift : return $self->{'_http_server'};
155 }
156  
157  
158 #
159 # http_server - Synonym for httpServer.
160 #
161 sub http_server {
162  
163 my $self = shift;
164 return $self->httpServer(@_);
165 }
166  
167  
168 sub dispatch_authority_to {
169  
170 my $self = shift->new();
171  
172 @_ ? ($self->{'_dispatch_authority_to'} = $_[0], return $self)
173 : return $self->{'_dispatch_authority_to'};
174 }
175  
176  
177 sub dispatch_metadata_to {
178  
179 my $self = shift->new();
180  
181 @_ ? ($self->{'_dispatch_metadata_to'} = $_[0], return $self)
182 : return $self->{'_dispatch_metadata_to'};
183 }
184  
185  
186 sub dispatch_data_to {
187  
188 my $self = shift->new();
189  
190 @_ ? ($self->{'_dispatch_data_to'} = $_[0], return $self)
191 : return $self->{'_dispatch_data_to'};
192 }
193  
194 sub dispatch {
195  
196 my $self = shift;
197  
198 $self->dispatch_authority_to($self->authorityService())
199 ->dispatch_data_to($self->dataService())
200 ->dispatch_metadata_to($self->metadataService())
201 ->handle();
202 }
203  
204  
205 #
206 # handle -
207 #
208 sub handle {
209
210 my $self = shift->new();
211
212 # This only works for HTTP Transportss
213 if(grep(/HTTP/, @ISA)) {
214  
215 if($self->auth_handler()) {
216  
217 # If the user has provided authorization information
218 # in the request, attempt to authorize them
219 return if(!$self->do_auth());
220 }
221  
222 #
223 # Handle HTTP requests to the base authority (we just return
224 # getAvailbleServices) - OMG spec
225 #
226 return $self->handle_http()
227 unless($ENV{'CONTENT_LENGTH'});
228 }
229
230 $self->SUPER::handle(@_);
231 }
232  
233  
234 #
235 # handle_http -
236 #
237 sub handle_http {
238  
239 my $self = shift;
240  
241 # FIXME: This is not a good way to do this.
242 # Ideally, the LS::HTTP::Service would handle different
243 # SOAP backends accordingly (Apache, CGI, Daemon).
244  
245 if($ENV{'REQUEST_URI'} && $ENV{'REQUEST_URI'} =~ /^\/authority(?:\/){0,1}$/) {
246  
247 #
248 # All services must accept a request for the service ports WSDL
249 # at http:/srvhost:srvport/authority/
250 #
251 my $r = new HTTP::Response(HTTP::Status::RC_OK);
252
253 $r->content($self->getServices());
254 $r->header('Content-Type'=> 'text/xml');
255 $r->header('Content-Length'=> length($r->content()));
256  
257 binmode(STDOUT);
258 print STDOUT "Status: " . $r->code() . ' ' . HTTP::Status::status_message($r->code()) . "\r\n";
259 print STDOUT $r->headers_as_string();
260 print STDOUT "\r\n";
261 print STDOUT $r->content() . "\r\n";
262 }
263  
264 if($self->http_server()) {
265  
266 #
267 # SOAP servers may optionally accept plain HTTP requests
268 #
269  
270 if($ENV{'REQUEST_URI'} && ($ENV{'REQUEST_URI'} =~ /^\/authority\/\?lsid=(.*)/)) {
271  
272 # Authority communication
273 $self->httpServer()->do_authority($1);
274 }
275 elsif($ENV{'REQUEST_URI'} && $ENV{'REQUEST_URI'} =~ /^\/authority\/metadata\/?(.*)/) {
276  
277 # Metadata communication
278 $self->httpServer()->do_metadata($1);
279 }
280 elsif($ENV{'REQUEST_URI'} && $ENV{'REQUEST_URI'} =~ /^\/authority\/data\/?(.*)/) {
281  
282 # Data communication
283 $self->httpServer()->do_data($1);
284 }
285 else {
286  
287 # Fault
288 $self->httpServer()->do_fault();
289 }
290  
291 }
292 }
293  
294 #
295 # FIXME: The authentication code is a mess, do something about it
296 #
297 sub do_auth {
298
299 my $self = shift;
300  
301 if($self->auth_type eq 'Basic') {
302  
303 $self->do_basic_auth();
304 }
305  
306 $self->auth_fail();
307
308 return undef;
309 }
310  
311 sub do_basic_auth {
312  
313 my $self = shift;
314  
315 #
316 # FIXME: A better authentication code base would be nice
317 #
318 # HTTP Basic Authentication
319 #
320 if($ENV{HTTP_CGI_AUTHORIZATION}) {
321
322 my $base64creds = (split(/ /, $ENV{HTTP_CGI_AUTHORIZATION}))[1];
323
324 $base64creds = MIME::Base64::decode_base64($base64creds);
325
326 my ($user, $pass) = split(/\:/, $base64creds);
327
328 return $self->auth_handler->($user, $pass);
329 }
330  
331 return undef;
332 }
333  
334 sub auth_fail {
335  
336 my $self = shift;
337
338 # All else fails, send the 401
339 my $resp = HTTP::Response->new(401);
340 $resp->content_type('text/html');
341
342 print STDOUT "WWW-Authenticate: Basic realm=\"LSID\"\n";
343 print STDOUT "Status: " . $resp->as_string() . "\n";
344 print STDOUT $resp->error_as_HTML();
345 }
346  
347  
348 #
349 # import -
350 #
351 sub import {
352  
353 shift;
354 my %params = @_;
355  
356 # The 'transport' parameter will be used to determine the superclass
357 # of LS::SOAP::Service objects.
358  
359 my $transport = $params{transport};
360 $transport =~ s/^\s+|\s+$//g;
361  
362 my $parent_class;
363  
364 if ($transport) {
365  
366 $transport =~ s|/|::|g;
367 my ($protocol) = split('::', $transport, 2);
368  
369 my $imp_file = "SOAP::Transport::$protocol";
370 eval "require $imp_file";
371 die $@ if $@;
372  
373 $parent_class = "SOAP::Transport::$transport";
374 }
375 else {
376 $parent_class = "SOAP::Server";
377 }
378  
379 unshift @ISA, $parent_class;
380 }
381  
382  
383  
384 #
385 #
386 # LSID Resolution Service Methods
387 #
388 #
389  
390 sub getServices {
391  
392 my ($self, %options) = @_;
393  
394 my $handler = $self->dispatch_authority_to();
395  
396 unless ($handler) {
397  
398 die LS::SOAP::Fault->faultcode('Server')
399 ->faultstring('Internal server error')
400 ->errorcode(500)
401 ->description('Unable to locate handler in getServices.');
402 }
403  
404 my $services;
405  
406 if ($handler->can('getServices')) {
407  
408 my @protocols = ( ${ LS::Authority::WSDL::Constants::Protocols::SOAP } );
409  
410 push @protocols, ${ LS::Authority::WSDL::Constants::Protocols::HTTP }
411 if($self->http_server());
412  
413 $services = $handler->getServices(protocols=> \@protocols);
414 }
415 else {
416  
417 die LS::SOAP::Fault->faultcode('Server')
418 ->faultstring('Method Not implemented')
419 ->errorcode(501)
420 ->description('getServices is not implemented by this authority service.');
421 }
422  
423 unless(UNIVERSAL::isa($services, 'LS::Service::Response') ||
424 UNIVERSAL::isa($services, 'LS::Service::Fault') ) {
425  
426 #die LS::SOAP::Fault->faultcode('Server')
427 #->faultstring('Internal processing error: returned object was not correct type')
428 #->errorcode(500)
429 #->description(
430 #$method_name . ' in package ' .
431 #(ref $handler ? ref $handler : $handler) .
432 #' did not return an LS::Service::Response object.'
433 #);
434 }
435  
436 return $services->response if($services->isa('LS::Service::Response'));
437  
438 # Must be a fault at this point
439 bless $services, 'LS::SOAP::Fault';
440 die $services->fault();
441 }
442  
443  
444 sub getAvailableServices {
445  
446 my $self = shift;
447  
448 my $handler = $self->dispatch_authority_to();
449  
450 unless ($handler) {
451  
452 die LS::SOAP::Fault->faultcode('Client')
453 ->faultstring('Unknown method')
454 ->errorcode(101)
455 ->description('A call was made to an unknown method getAvailableServices.');
456 }
457  
458 my $services;
459 my $method_name;
460
461 if ($handler->can('getAvailableServices')) {
462  
463 $method_name = 'getAvailableServices';
464 $services = $handler->getAvailableServices(@_);
465 }
466 else {
467  
468 die LS::SOAP::Fault->faultcode('Server')
469 ->faultstring('Not implemented')
470 ->errorcode(501)
471 ->description('getAvailableServices is not implemented by this authority service.');
472 }
473  
474 unless(UNIVERSAL::isa($services, 'LS::Service::Fault') ||
475 UNIVERSAL::isa($services, 'LS::Service::Response') ) {
476  
477 die LS::SOAP::Fault->faultcode('Server')
478 ->faultstring('Internal processing error')
479 ->errorcode(500)
480 ->description(
481 $method_name . ' in package ' .
482 (ref $handler ? ref $handler : $handler) .
483 ' did not return a scalar or an LS::Service::Response object.'
484 );
485 }
486  
487 if(UNIVERSAL::isa($services, 'LS::Service::Fault')) {
488  
489 bless $services, 'LS::SOAP::Fault';
490 die $services->fault();
491 }
492  
493 my @ret;
494  
495 if ($services->expiration()) {
496  
497 push(
498 @ret,
499 SOAP::Header->name(expiration=> $services->expiration())
500 ->prefix($METHOD_PREFIX)
501 ->uri(${LS::Authority::WSDL::Constants::SCHEMA_TYPES})
502 );
503 }
504  
505 return (build MIME::Entity(Data=> $services->response()), @ret);
506 }
507  
508  
509 sub getMetadata {
510  
511 my $self = shift;
512 my $handler = $self->dispatch_metadata_to();
513  
514 unless ($handler) {
515  
516 die LS::SOAP::Fault->faultcode('Client')
517 ->faultstring('Unknown method')
518 ->errorcode(101)
519 ->description('A call was made to an unknown method getMetadata.');
520 }
521  
522 unless ($handler->can('getMetadata')) {
523  
524 die LS::SOAP::Fault->faultcode('Server')
525 ->faultstring('Not implemented')
526 ->errorcode(501)
527 ->description('getMetadata is not implemented by this metadata service.');
528 }
529  
530 my $metadata = $handler->getMetadata(@_);
531  
532 unless(UNIVERSAL::isa($metadata, 'LS::Service::Fault') ||
533 UNIVERSAL::isa($metadata, 'LS::Service::Response') ) {
534  
535 die LS::SOAP::Fault->faultcode('Server')
536 ->faultstring('Internal processing error')
537 ->errorcode(500)
538 ->description(
539 'getMetadata in package ' .
540 (ref $handler ? ref $handler : $handler) .
541 ' did not return a LS::Service::Response object.'
542 );
543 }
544  
545 if($metadata->isa('LS::Service::Fault')) {
546  
547 bless $metadata, 'LS::SOAP::Fault';
548 die $metadata->fault();
549 }
550  
551 my @ret;
552  
553 #
554 # Expiration is optional
555 #
556 if ($metadata->expiration()) {
557 push(
558 @ret,
559 SOAP::Header->name(expiration=> $metadata->expiration())
560 ->prefix($METHOD_PREFIX)
561 ->uri(${LS::Authority::WSDL::Constants::SCHEMA_TYPES})
562 );
563 }
564  
565 #
566 # Metadata needs a type
567 #
568 unless($metadata->format()) {
569  
570 die LS::SOAP::Fault->faultcode('Server')
571 ->faultstring('Internal processing error')
572 ->errorcode(500)
573 ->description(
574 'getMetadata in package ' .
575 (ref $handler ? ref $handler : $handler) .
576 ' is missing metadata response format.'
577 );
578 }
579  
580 push(
581 @ret,
582 SOAP::Data->name(format=> $metadata->format())
583 );
584  
585 return (build MIME::Entity(Data=> $metadata->response()), @ret);
586 }
587  
588  
589 sub getData {
590  
591 my $self = shift;
592  
593 my $handler = $self->dispatch_data_to();
594  
595 unless ($handler) {
596  
597 die LS::SOAP::Fault->faultcode('Client')
598 ->faultstring('Unknown method')
599 ->errorcode(101)
600 ->description('A call was made to an unknown method getData.');
601 }
602  
603 unless ($handler->can('getData')) {
604  
605 die LS::SOAP::Fault->faultcode('Server')
606 ->faultstring('Not implemented')
607 ->errorcode(501)
608 ->description('getData is not implemented by this data service.');
609 }
610  
611 my $data = $handler->getData(@_);
612  
613 unless(UNIVERSAL::isa($data, 'LS::Service::Fault') ||
614 UNIVERSAL::isa($data, 'LS::Service::Response') ) {
615  
616 die LS::SOAP::Fault->faultcode('Server')
617 ->faultstring('Internal processing error')
618 ->errorcode(500)
619 ->description(
620 'getData in package ' .
621 (ref $handler ? ref $handler : $handler) .
622 ' did not return a LS::Service::Response.'
623 );
624 }
625  
626 if(UNIVERSAL::isa($data, 'LS::Service::Fault')) {
627  
628 bless $data, 'LS::SOAP::Fault';
629 die $data->fault();
630 }
631  
632 return build MIME::Entity(Data=> $data->response());
633 }
634  
635  
636 sub getDataByRange {
637  
638 my $self = shift;
639  
640 my $handler = $self->dispatch_data_to();
641  
642 unless ($handler) {
643  
644 die LS::SOAP::Fault->faultcode('Client')
645 ->faultstring('Unknown method')
646 ->errorcode(101)
647 ->description('A call was made to an unknown method getDataByRange.');
648 }
649  
650 unless ($handler->can('getDataByRange')) {
651  
652 die LS::SOAP::Fault->faultcode('Server')
653 ->faultstring('Not implemented')
654 ->errorcode(501)
655 ->description('getDataByRange is not implemented by this data service.');
656 }
657  
658 my $data = $handler->getDataByRange(@_);
659  
660 unless($data &&
661 ($data->isa('LS::Service::Fault') || $data->isa('LS::Service::Response')) ) {
662  
663 die LS::SOAP::Fault->faultcode('Server')
664 ->faultstring('Internal processing error')
665 ->errorcode(500)
666 ->description(
667 'getDataByRange in package ' .
668 (ref $handler ? ref $handler : $handler) .
669 ' did not return a LS::Service::Response object.'
670 );
671 }
672  
673 if($data->isa('LS::Service::Fault')) {
674  
675 bless $data, 'LS::SOAP::Fault';
676 die $data->fault();
677 }
678  
679 return build MIME::Entity(Data=> $data->response());
680 }
681  
682  
683 package LS::SOAP::Response;
684  
685 use strict;
686  
687 sub new {
688 my ($self, %params) = @_;
689  
690 unless (ref $self) {
691  
692 $self = bless {
693 _value=> undef,
694 _expires=> undef,
695 _attachment=> undef,
696 _att_type=> undef,
697 }, $self;
698  
699 $self->value($params{'value'}) if defined $params{'value'};
700 $self->expires($params{'expires'}) if defined $params{'expires'};
701  
702 $self->attachment($params{'attachment'}) if defined $params{'attachment'};
703 $self->expires($params{'type'}) if defined $params{'type'};
704 }
705  
706 return $self;
707 }
708  
709  
710 sub value {
711  
712 my $self = shift->new();
713  
714 $_[0] ? ($self->{'_value'} = $_[0], return $self) : $self->{'_value'};
715 }
716  
717  
718 sub expires {
719  
720 my $self = shift->new();
721  
722 $_[0] ? ($self->{'_expires'} = $_[0], return $self) : $self->{'_expires'};
723 }
724  
725 sub attachment {
726  
727 my $self = shift->new();
728  
729 $_[0] ? ($self->{'_attachment'} = $_[0], return $self) : $self->{'_attachment'};
730 }
731  
732 sub type {
733  
734 my $self = shift->new();
735  
736 $_[0] ? ($self->{'_att_type'} = $_[0], return $self) : $self->{'_att_type'};
737 }
738  
739 sub mime {
740  
741 }
742  
743  
744 1;
745  
746  
747 __END__