| 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::Service::Authority; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use vars qw( $METHODS ); | ||
| 15 | |||
| 16 | use LS; | ||
| 17 | use LS::ID; | ||
| 18 | |||
| 19 | use LS::Authority::WSDL::Simple; | ||
| 20 | use LS::Authority::WSDL::Services; | ||
| 21 | use LS::Authority::WSDL::Constants; | ||
| 22 | |||
| 23 | use base 'LS::Base'; | ||
| 24 | |||
| 25 | sub BEGIN { | ||
| 26 | |||
| 27 | $METHODS = [ | ||
| 28 | 'name', | ||
| 29 | 'location', | ||
| 30 | 'authorityID', | ||
| 31 | |||
| 32 | 'services', | ||
| 33 | 'handlers', | ||
| 34 | ]; | ||
| 35 | |||
| 36 | LS::makeAccessorMethods($METHODS, __PACKAGE__); | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | # | ||
| 41 | # new( %options ) - | ||
| 42 | # | ||
| 43 | sub new { | ||
| 44 | |||
| 45 | my $self = shift; | ||
| 46 | my (%options) = @_; | ||
| 47 | |||
| 48 | unless (ref $self) { | ||
| 49 | |||
| 50 | $self = bless { | ||
| 51 | _curID=> 1, | ||
| 52 | }, $self; | ||
| 53 | |||
| 54 | $self->services( {} ); | ||
| 55 | |||
| 56 | $self->name( $options{'name'} ); | ||
| 57 | $self->location( $options{'location'}); | ||
| 58 | |||
| 59 | # Accept either hash parameter name for the authority ID | ||
| 60 | $self->authorityID( ($options{'authority'} || $options{'authorityID'}) ); | ||
| 61 | |||
| 62 | $self->addService($self->defaultName()); | ||
| 63 | |||
| 64 | # Setup the handlers for implementation specific code | ||
| 65 | # These are used instead of subclassing the LS::Service::Authority object | ||
| 66 | $self->handlers( LS::Service::Authority::Handlers->new() ); | ||
| 67 | # Accept either hash parameter name for the authentication handler | ||
| 68 | $self->handlers()->authenticate( ($options{'auth_handler'} || $options{'authenticate'}) ); | ||
| 69 | $self->handlers()->getAvailableServices( $options{'getAvailableServices'} ); | ||
| 70 | $self->handlers()->notifyForeignAuthority( $options{'notifyForeignAuthority'} ); | ||
| 71 | $self->handlers()->revokeNotificationForeignAuthority( $options{'revokeNotificationForeignAuthority'} ); | ||
| 72 | } | ||
| 73 | |||
| 74 | return $self; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | # | ||
| 79 | # nextID( ) | ||
| 80 | # | ||
| 81 | sub nextID { | ||
| 82 | my $self = shift; | ||
| 83 | return $self->{'_curID'}++; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | # | ||
| 88 | # defaultName( ) | ||
| 89 | # Returns the default name for this authority | ||
| 90 | # | ||
| 91 | sub defaultName { | ||
| 92 | |||
| 93 | my $self = shift; | ||
| 94 | return $self->name(); | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | # | ||
| 99 | # addService( $serviceName | serviceName=>$serviceName ) | ||
| 100 | # | ||
| 101 | sub addService { | ||
| 102 | |||
| 103 | my $self = shift; | ||
| 104 | |||
| 105 | my $serviceName; | ||
| 106 | |||
| 107 | if(scalar(@_) == 1) { | ||
| 108 | $serviceName = shift; | ||
| 109 | } | ||
| 110 | elsif(scalar(@_) % 2 == 0) { | ||
| 111 | my (%options) = @_; | ||
| 112 | $serviceName = $options{'serviceName'}; | ||
| 113 | } | ||
| 114 | else { | ||
| 115 | |||
| 116 | $self->recordError('Too many parameters'); | ||
| 117 | $self->addStackTrace(); | ||
| 118 | return undef; | ||
| 119 | } | ||
| 120 | |||
| 121 | unless($serviceName) { | ||
| 122 | |||
| 123 | $self->recordError('Service name not specified'); | ||
| 124 | $self->addStackTrace(); | ||
| 125 | return undef; | ||
| 126 | } | ||
| 127 | |||
| 128 | # Only setup the service if it does not already exist | ||
| 129 | $self->services()->{ $serviceName } = [ ] | ||
| 130 | unless(exists($self->services()->{ $serviceName })); | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | # | ||
| 135 | # addPort( %options ) - | ||
| 136 | # | ||
| 137 | sub addPort { | ||
| 138 | |||
| 139 | my $self = shift; | ||
| 140 | |||
| 141 | my (%options) = @_; | ||
| 142 | |||
| 143 | unless(exists($options{'port'}) && | ||
| 144 | UNIVERSAL::isa($options{'port'}, 'LS::Authority::WSDL::Port') ) { | ||
| 145 | |||
| 146 | $self->recordError('Must specifiy a port parameter of type LS::Authority::WSDL::Port'); | ||
| 147 | $self->addStackTrace(); | ||
| 148 | |||
| 149 | return undef; | ||
| 150 | } | ||
| 151 | |||
| 152 | # Get the default service name if unspecified | ||
| 153 | $options{'serviceName'} = $self->defaultName() | ||
| 154 | unless($options{'serviceName'}); | ||
| 155 | |||
| 156 | unless(exists($self->services()->{ $options{'serviceName'} }) ) { | ||
| 157 | $self->addService(serviceName=> $options{'serviceName'}); | ||
| 158 | } | ||
| 159 | |||
| 160 | push( @{ $self->services()->{ $options{'serviceName'} }}, $options{'port'} ); | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | # | ||
| 165 | # addDataPort( %options ) - | ||
| 166 | # | ||
| 167 | sub addDataPort { | ||
| 168 | |||
| 169 | my $self = shift; | ||
| 170 | |||
| 171 | my (%options) = @_; | ||
| 172 | |||
| 173 | unless($options{'endpoint'} && | ||
| 174 | $options{'protocol'}) { | ||
| 175 | |||
| 176 | $self->recordError('Missing endpoint or protocol parameter'); | ||
| 177 | $self->addStackTrace(); | ||
| 178 | |||
| 179 | return undef; | ||
| 180 | } | ||
| 181 | |||
| 182 | # Naming a port isn't important, people probably don't want to do it | ||
| 183 | # unless they absolutely have to. | ||
| 184 | unless($options{'portName'}) { | ||
| 185 | $options{'portName'} = $options{'protocol'} . 'Data' . $self->nextID(); | ||
| 186 | } | ||
| 187 | |||
| 188 | $options{'port'} = LS::Authority::WSDL::Simple::DataPort->newData( %options ); | ||
| 189 | |||
| 190 | return $self->addPort( %options ); | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | # | ||
| 195 | # addMetadataPort( %options ) - | ||
| 196 | # | ||
| 197 | sub addMetadataPort { | ||
| 198 | |||
| 199 | my $self = shift; | ||
| 200 | |||
| 201 | my %options = @_; | ||
| 202 | |||
| 203 | unless($options{'endpoint'} && | ||
| 204 | $options{'protocol'}) { | ||
| 205 | |||
| 206 | $self->recordError('Missing endpoint or protocol parameter'); | ||
| 207 | $self->addStackTrace(); | ||
| 208 | |||
| 209 | return undef; | ||
| 210 | } | ||
| 211 | |||
| 212 | unless($options{'portName'}) { | ||
| 213 | $options{'portName'} = $options{'protocol'} . 'Metadata' . $self->nextID(); | ||
| 214 | } | ||
| 215 | |||
| 216 | $options{'port'} = LS::Authority::WSDL::Simple::MetadataPort->newMetadata( %options ); | ||
| 217 | |||
| 218 | return $self->addPort( %options ); | ||
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | # | ||
| 224 | # Authority stubs - These stubs do minimal input checking from the SOAP Layer | ||
| 225 | # and then pass any parameters to custom definitions if present. | ||
| 226 | # The default behavior should be sufficient for most implementations. | ||
| 227 | # | ||
| 228 | # | ||
| 229 | # | ||
| 230 | |||
| 231 | # | ||
| 232 | # authenticate - | ||
| 233 | # | ||
| 234 | sub authenticate { | ||
| 235 | |||
| 236 | my $self = shift; | ||
| 237 | |||
| 238 | if($self->handlers()->authenticate()) { | ||
| 239 | |||
| 240 | my @credentials = shift; | ||
| 241 | # FIXME: CREDENTIAL OBJECT | ||
| 242 | # FIXME: DO WE NEED THIS? | ||
| 243 | return $self->authenticate()->(@credentials); | ||
| 244 | } | ||
| 245 | |||
| 246 | return undef; | ||
| 247 | } | ||
| 248 | |||
| 249 | |||
| 250 | # | ||
| 251 | # getServices( %options ) - | ||
| 252 | # | ||
| 253 | sub getServices { | ||
| 254 | |||
| 255 | my $self = shift; | ||
| 256 | my (%options) = @_; | ||
| 257 | |||
| 258 | my $servicesWSDL = LS::Authority::WSDL::Services->new(name=> 'AuthorityService'); | ||
| 259 | |||
| 260 | return LS::Service::Fault->fault('Internal Server Error') | ||
| 261 | unless(UNIVERSAL::isa($servicesWSDL, 'LS::Authority::WSDL::Services')); | ||
| 262 | |||
| 263 | my $location = $self->location(); | ||
| 264 | |||
| 265 | return LS::Service::Fault->fault('Internal Server Error') | ||
| 266 | unless($location); | ||
| 267 | |||
| 268 | my $protocolsProvided = $options{'protocols'}; | ||
| 269 | |||
| 270 | return LS::Service::Fault->fault('Internal Server Error') | ||
| 271 | unless($protocolsProvided && ref $protocolsProvided eq 'ARRAY'); | ||
| 272 | |||
| 273 | foreach(@{ $protocolsProvided }) { | ||
| 274 | |||
| 275 | my $loc = $location; | ||
| 276 | |||
| 277 | # For HTTP GET ports, we always add /authority/ | ||
| 278 | $loc .= '/authority/' | ||
| 279 | if($_ ne ${ LS::Authority::WSDL::Constants::Protocols::HTTP }); | ||
| 280 | |||
| 281 | $servicesWSDL->add_port(name=> $_ . 'Port', | ||
| 282 | protocol=> $_, | ||
| 283 | location=> $loc); | ||
| 284 | } | ||
| 285 | |||
| 286 | return LS::Service::Response->new(response=> $servicesWSDL->xml(), | ||
| 287 | format=> 'application/xml'); | ||
| 288 | } | ||
| 289 | |||
| 290 | |||
| 291 | # | ||
| 292 | # getAvailableServices( $lsid ) - | ||
| 293 | # | ||
| 294 | sub getAvailableServices { | ||
| 295 | |||
| 296 | my $self = shift; | ||
| 297 | my ($lsid) = @_; | ||
| 298 | |||
| 299 | return LS::Service::Fault->fault('Malformed LSID') | ||
| 300 | unless($lsid); | ||
| 301 | |||
| 302 | $lsid = LS::ID->new($lsid); | ||
| 303 | |||
| 304 | return LS::Service::Fault->fault('Malformed LSID') | ||
| 305 | unless($lsid); | ||
| 306 | |||
| 307 | my $wsdl = LS::Authority::WSDL::Simple->new( | ||
| 308 | authority=> $self->authorityID(), | ||
| 309 | name=> $self->name(), | ||
| 310 | lsid=> $lsid->as_string(), | ||
| 311 | ); | ||
| 312 | |||
| 313 | return LS::Service::Fault->fault('Internal Server Error') | ||
| 314 | unless(UNIVERSAL::isa($wsdl, 'LS::Authority::WSDL::Simple')); | ||
| 315 | |||
| 316 | # Iterate over the services and their ports | ||
| 317 | foreach my $svc (keys(%{ $self->services() })) { | ||
| 318 | |||
| 319 | my @ports = @{ $self->services()->{ $svc } }; | ||
| 320 | |||
| 321 | foreach my $p (@ports) { | ||
| 322 | |||
| 323 | $wsdl->addPort(port=> $p, | ||
| 324 | serviceName=> $svc); | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | # | ||
| 329 | # Call out to the custom getAvailableServices method | ||
| 330 | # | ||
| 331 | if($self->handlers()->getAvailableServices()) { | ||
| 332 | |||
| 333 | my $r; | ||
| 334 | |||
| 335 | # Protect against die() | ||
| 336 | eval { | ||
| 337 | $r = $self->handlers()->getAvailableServices()->($lsid, $wsdl); | ||
| 338 | }; | ||
| 339 | |||
| 340 | # Return a fault if the external method issues a die() or similar request | ||
| 341 | $r = LS::Service::Fault->serverFault($@, 500) | ||
| 342 | if($@); | ||
| 343 | |||
| 344 | return $r if(UNIVERSAL::isa($r, 'LS::Service::Response') || | ||
| 345 | UNIVERSAL::isa($r, 'LS::Service::Fault') ); | ||
| 346 | } | ||
| 347 | |||
| 348 | return LS::Service::Response->new(response=> $wsdl->xml(), | ||
| 349 | format=> 'application/xml'); | ||
| 350 | } | ||
| 351 | |||
| 352 | |||
| 353 | |||
| 354 | # | ||
| 355 | # FAN framework | ||
| 356 | # NOTE: This could change | ||
| 357 | # | ||
| 358 | |||
| 359 | |||
| 360 | # | ||
| 361 | # notifyForeignAuthority( $lsid, $authorityName ) - | ||
| 362 | # | ||
| 363 | sub notifyForeignAuthority { | ||
| 364 | |||
| 365 | my $self = shift; | ||
| 366 | my ($lsid, $authorityName) = @_; | ||
| 367 | |||
| 368 | return LS::Service::Fault->fault('Malformed LSID') | ||
| 369 | unless($lsid); | ||
| 370 | |||
| 371 | return LS::Service::Fault->fault('Invalid Method Call') | ||
| 372 | unless($authorityName); | ||
| 373 | |||
| 374 | $lsid = LS::ID->new($lsid); | ||
| 375 | |||
| 376 | return LS::Service::Fault->fault('Malformed LSID') | ||
| 377 | unless($lsid); | ||
| 378 | |||
| 379 | unless($self->handlers()->notifyForeignAuthority()) { | ||
| 380 | return LS::Service::Fault->fault('Method Not Implemented'); | ||
| 381 | } | ||
| 382 | |||
| 383 | return $self->handlers()->notifyForeignAuthority()->($lsid, $authorityName); | ||
| 384 | } | ||
| 385 | |||
| 386 | |||
| 387 | # | ||
| 388 | # revokeNotificationForeignAuthority( $lsid, $authorityName ) - | ||
| 389 | # | ||
| 390 | sub revokeNotificationForeignAuthority { | ||
| 391 | |||
| 392 | my $self = shift; | ||
| 393 | my ($lsid, $authorityName) = @_; | ||
| 394 | |||
| 395 | return LS::Service::Fault->fault('Malformed LSID') | ||
| 396 | unless($lsid); | ||
| 397 | |||
| 398 | return LS::Service::Fault->fault('Invalid Method Call') | ||
| 399 | unless($authorityName); | ||
| 400 | |||
| 401 | $lsid = LS::ID->new($lsid); | ||
| 402 | |||
| 403 | return LS::Service::Fault->fault('Malformed LSID') | ||
| 404 | unless($lsid); | ||
| 405 | |||
| 406 | unless($self->handlers()->revokeNotificationForeignAuthority()) { | ||
| 407 | return LS::Service::Fault->fault('Method Not Implemented'); | ||
| 408 | } | ||
| 409 | |||
| 410 | return $self->handlers()->revokeNotificationForeignAuthority()->($lsid, $authorityName); | ||
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | package LS::Service::Authority::Handlers; | ||
| 415 | |||
| 416 | use strict; | ||
| 417 | use warnings; | ||
| 418 | |||
| 419 | use vars qw( $METHODS ); | ||
| 420 | |||
| 421 | use LS; | ||
| 422 | |||
| 423 | use base 'LS::Base'; | ||
| 424 | |||
| 425 | sub BEGIN { | ||
| 426 | |||
| 427 | $METHODS = [ | ||
| 428 | 'authenticate', | ||
| 429 | 'getAvailableServices', | ||
| 430 | 'notifyForeignAuthority', | ||
| 431 | 'revokeNotificationForeignAuthority', | ||
| 432 | ]; | ||
| 433 | |||
| 434 | LS::makeAccessorMethods($METHODS, __PACKAGE__); | ||
| 435 | } | ||
| 436 | |||
| 437 | sub new { | ||
| 438 | my $self = shift; | ||
| 439 | return bless {}, $self; | ||
| 440 | } | ||
| 441 | |||
| 442 | 1; | ||
| 443 | |||
| 444 | __END__ |