| 1 | #----------------------------------------------------------------- | ||
| 2 | # MOSES::MOBY::Async | ||
| 3 | # Author: Edward Kawas <edward.kawas@gmail.com>, | ||
| 4 | # | ||
| 5 | # For copyright and disclaimer see below. | ||
| 6 | # | ||
| 7 | # $Id: Async.pm,v 1.3 2009/05/06 13:47:31 kawas Exp $ | ||
| 8 | #----------------------------------------------------------------- | ||
| 9 | |||
| 10 | package MOSES::MOBY::Async; | ||
| 11 | use base ("MOSES::MOBY::Base"); | ||
| 12 | |||
| 13 | use POSIX; | ||
| 14 | use MOBY::Async::LSAE; | ||
| 15 | use MOBY::Async::WSRF; | ||
| 16 | use HTTP::Date; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | |||
| 20 | # add versioning to this module | ||
| 21 | use vars qw /$VERSION/; | ||
| 22 | $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/; | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | MOSES::MOBY::Async - A module for interacting with HTTP POST WSRF asynchronous services | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use MOSES::MOBY::Async; | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | This module is the main module used by asynchronous HTTP POST biomoby services. In most situations, it would be incorrect for you to instantiate a reference to this module. | ||
| 35 | |||
| 36 | =head1 AUTHORS | ||
| 37 | |||
| 38 | Edward Kawas (edward.kawas [at] gmail [dot] com) | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | #----------------------------------------------------------------- | ||
| 43 | # A list of allowed attribute names. See MOSES::MOBY::Base for details. | ||
| 44 | #----------------------------------------------------------------- | ||
| 45 | |||
| 46 | =head1 ACCESSIBLE ATTRIBUTES | ||
| 47 | |||
| 48 | Details are in L<MOSES::MOBY::Base>. Here just a list of them (additionally | ||
| 49 | to the attributes from the parent classes) | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | { | ||
| 54 | my %_allowed = ( | ||
| 55 | |||
| 56 | ); | ||
| 57 | |||
| 58 | sub _accessible { | ||
| 59 | my ( $self, $attr ) = @_; | ||
| 60 | exists $_allowed{$attr} or $self->SUPER::_accessible($attr); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _attr_prop { | ||
| 64 | my ( $self, $attr_name, $prop_name ) = @_; | ||
| 65 | my $attr = $_allowed{$attr_name}; | ||
| 66 | return ref($attr) ? $attr->{$prop_name} : $attr if $attr; | ||
| 67 | return $self->SUPER::_attr_prop( $attr_name, $prop_name ); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | #----------------------------------------------------------------- | ||
| 72 | # some FAULTS; their names, and descriptions | ||
| 73 | #----------------------------------------------------------------- | ||
| 74 | my %FAULT_DESCRIPTIONS = (); | ||
| 75 | my %FAULT_NAMES = (); | ||
| 76 | |||
| 77 | =head2 | ||
| 78 | |||
| 79 | The following are constants that refer to faults. | ||
| 80 | |||
| 81 | I<none are exported> | ||
| 82 | |||
| 83 | =over | ||
| 84 | |||
| 85 | =item C<RESOURCE_UNKNOWN_FAULT> | ||
| 86 | |||
| 87 | =item C<RESOURCE_UNAVAILABLE_FAULT> | ||
| 88 | |||
| 89 | =item C<RESOURCE_NOT_DESTROYED_FAULT> | ||
| 90 | |||
| 91 | =item C<INVALID_RESOURCE_PROPERTY_QNAME_FAULT> | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | use constant RESOURCE_UNKNOWN_FAULT => 400; | ||
| 98 | use constant RESOURCE_UNAVAILABLE_FAULT => 401; | ||
| 99 | use constant RESOURCE_NOT_DESTROYED_FAULT => 402; | ||
| 100 | use constant INVALID_RESOURCE_PROPERTY_QNAME_FAULT => 403; | ||
| 101 | |||
| 102 | BEGIN { | ||
| 103 | |||
| 104 | # set the fault descriptions | ||
| 105 | $FAULT_DESCRIPTIONS{RESOURCE_UNKNOWN_FAULT} = "Resource unknown"; | ||
| 106 | $FAULT_DESCRIPTIONS{RESOURCE_UNAVAILABLE_FAULT} = "Resource unavailable"; | ||
| 107 | $FAULT_DESCRIPTIONS{RESOURCE_NOT_DESTROYED_FAULT} = "Resource not destroyed"; | ||
| 108 | $FAULT_DESCRIPTIONS{INVALID_RESOURCE_PROPERTY_QNAME_FAULT} = | ||
| 109 | "Invalid resource property QName"; | ||
| 110 | |||
| 111 | # set the fault names | ||
| 112 | $FAULT_NAMES{RESOURCE_UNKNOWN_FAULT} = "ResourceUnknownFault"; | ||
| 113 | $FAULT_NAMES{RESOURCE_UNAVAILABLE_FAULT} = "ResourceUnavailableFault"; | ||
| 114 | $FAULT_NAMES{RESOURCE_NOT_DESTROYED_FAULT} = "ResourceNotDestroyedFault"; | ||
| 115 | $FAULT_NAMES{INVALID_RESOURCE_PROPERTY_QNAME_FAULT} = | ||
| 116 | "InvalidResourcePropertyQNameFault"; | ||
| 117 | } | ||
| 118 | |||
| 119 | #----------------------------------------------------------------- | ||
| 120 | # init | ||
| 121 | #----------------------------------------------------------------- | ||
| 122 | sub init { | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | #----------------------------------------------------------------- | ||
| 127 | # submit | ||
| 128 | # takes in the Service, the service invocation id, the current Job, | ||
| 129 | # the incoming Package and the outgoing Package. | ||
| 130 | # invokes the Service and creates a WSRF resource for it. | ||
| 131 | # return: nothing | ||
| 132 | #----------------------------------------------------------------- | ||
| 133 | |||
| 134 | =head2 | ||
| 135 | |||
| 136 | Name : submit | ||
| 137 | Function : submits an asynchronous job | ||
| 138 | Usage : $async->submit($obj, $id, $job, $in_package, $out_package); | ||
| 139 | Args : $obj - a child of MOSES::MOBY::Service::ServiceBase | ||
| 140 | $id - a scalar string representing the service invocation id | ||
| 141 | $job - a MOSES::MOBY::Job, representing the current job | ||
| 142 | $in_package - a MOSES::MOBY::Package, representing the incoming message | ||
| 143 | $out_package - a MOSES::MOBY::Package, representing the outgoing message | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub submit { | ||
| 148 | my ( $self, $obj, $ID, $job, $in_package, $out_package ) = @_; | ||
| 149 | |||
| 150 | my $queryID = $job->jid; | ||
| 151 | my $property_pid = "pid_$queryID"; | ||
| 152 | my $property_input = "input_$queryID"; | ||
| 153 | my $property_status = "status_$queryID"; | ||
| 154 | my $property_result = "result_$queryID"; | ||
| 155 | |||
| 156 | my $lock; | ||
| 157 | |||
| 158 | # Fork | ||
| 159 | my $pid = fork(); | ||
| 160 | do { | ||
| 161 | |||
| 162 | #-----------------# | ||
| 163 | # Fork has failed # | ||
| 164 | #-----------------# | ||
| 165 | |||
| 166 | # Status | ||
| 167 | my $status = LSAE::AnalysisEventBlock->new(); | ||
| 168 | $status->type(LSAE_STATE_CHANGED_EVENT); | ||
| 169 | $status->previous_state('created'); | ||
| 170 | $status->new_state('terminated_by_error'); | ||
| 171 | $status->id($queryID); | ||
| 172 | |||
| 173 | # Result | ||
| 174 | $out_package->record_error( | ||
| 175 | { code => 701, msg => 'Unable to submit the job' } ); | ||
| 176 | $lock = WSRF::MobyFile->new( undef, $ID ); | ||
| 177 | |||
| 178 | # New properties values | ||
| 179 | $WSRF::WSRP::Private{$property_pid} = ''; | ||
| 180 | $WSRF::WSRP::ResourceProperties{$property_status} = $status->XML(); | ||
| 181 | $WSRF::WSRP::ResourceProperties{$property_result} = | ||
| 182 | $out_package->job_by_id( $job->jid )->toXML(); | ||
| 183 | $lock->toFile(); | ||
| 184 | |||
| 185 | } unless defined($pid); | ||
| 186 | |||
| 187 | if ($pid) { | ||
| 188 | |||
| 189 | #-----------------------------------------------------# | ||
| 190 | # Fork has had success and this is the parent process # | ||
| 191 | #-----------------------------------------------------# | ||
| 192 | |||
| 193 | # Status | ||
| 194 | my $status; | ||
| 195 | if ( $WSRF::WSRP::ResourceProperties{$property_status} ) { | ||
| 196 | |||
| 197 | # This is not the first execution of the service for this queryID | ||
| 198 | # Previous state is the new state of the previous execution | ||
| 199 | my $old_status = LSAE::AnalysisEventBlock->new( | ||
| 200 | $WSRF::WSRP::ResourceProperties{$property_status} ); | ||
| 201 | $status = LSAE::AnalysisEventBlock->new(); | ||
| 202 | $status->type(LSAE_STATE_CHANGED_EVENT); | ||
| 203 | $status->previous_state( $old_status->new_state() ); | ||
| 204 | $status->new_state('created'); | ||
| 205 | $status->id($queryID); | ||
| 206 | |||
| 207 | } else { | ||
| 208 | |||
| 209 | # This is the first execution of the service for this queryID | ||
| 210 | $status = LSAE::AnalysisEventBlock->new(); | ||
| 211 | $status->type(LSAE_STATE_CHANGED_EVENT); | ||
| 212 | $status->previous_state('created'); | ||
| 213 | $status->new_state('created'); | ||
| 214 | $status->id($queryID); | ||
| 215 | } | ||
| 216 | |||
| 217 | # New properties values | ||
| 218 | $lock = WSRF::MobyFile->new( undef, $ID ); | ||
| 219 | $WSRF::WSRP::Private{$property_pid} = undef; | ||
| 220 | $WSRF::WSRP::ResourceProperties{$property_status} = $status->XML(); | ||
| 221 | $WSRF::WSRP::ResourceProperties{$property_result} = ''; | ||
| 222 | $lock->toFile(); | ||
| 223 | |||
| 224 | } elsif ( $pid == 0 ) { | ||
| 225 | |||
| 226 | #----------------------------------------------------# | ||
| 227 | # Fork has had success and this is the child process # | ||
| 228 | #----------------------------------------------------# | ||
| 229 | |||
| 230 | # Daemonize | ||
| 231 | open STDIN, "/dev/null"; | ||
| 232 | open STDOUT, ">/dev/null"; | ||
| 233 | open STDERR, ">/dev/null"; | ||
| 234 | setsid; | ||
| 235 | |||
| 236 | # Status | ||
| 237 | my $status = LSAE::AnalysisEventBlock->new(); | ||
| 238 | $status->type(LSAE_STATE_CHANGED_EVENT); | ||
| 239 | $status->previous_state('created'); | ||
| 240 | $status->new_state('running'); | ||
| 241 | $status->id($queryID); | ||
| 242 | |||
| 243 | # New properties values | ||
| 244 | $lock = WSRF::MobyFile->new( undef, $ID ); | ||
| 245 | $WSRF::WSRP::Private{$property_pid} = $$; | ||
| 246 | $WSRF::WSRP::ResourceProperties{$property_status} = $status->XML(); | ||
| 247 | $WSRF::WSRP::ResourceProperties{$property_result} = ''; | ||
| 248 | $lock->toFile(); | ||
| 249 | |||
| 250 | # Run service | ||
| 251 | eval { | ||
| 252 | $obj->process_it( $job, $out_package->job_by_id( $job->jid ), | ||
| 253 | $out_package ); | ||
| 254 | }; | ||
| 255 | |||
| 256 | my $result; | ||
| 257 | |||
| 258 | # Check if there has been errors during service execution | ||
| 259 | |||
| 260 | unless ($@) { | ||
| 261 | |||
| 262 | # Service has been executed successfully | ||
| 263 | # Status | ||
| 264 | $status = LSAE::AnalysisEventBlock->new(); | ||
| 265 | $status->type(LSAE_STATE_CHANGED_EVENT); | ||
| 266 | $status->previous_state('running'); | ||
| 267 | $status->new_state('completed'); | ||
| 268 | $status->id($queryID); | ||
| 269 | } else { | ||
| 270 | |||
| 271 | # Service execution has failed | ||
| 272 | # Status | ||
| 273 | $status = LSAE::AnalysisEventBlock->new(); | ||
| 274 | $status->type(LSAE_STATE_CHANGED_EVENT); | ||
| 275 | $status->previous_state('running'); | ||
| 276 | $status->new_state('terminated_by_error'); | ||
| 277 | $status->id($queryID); | ||
| 278 | |||
| 279 | # Result INTERNAL_PROCESSING_ERROR | ||
| 280 | $out_package->job_by_id( $job->jid ) | ||
| 281 | ->record_error( | ||
| 282 | { code => 701, msg => "Error while executing job: '$@'" } ); | ||
| 283 | } | ||
| 284 | # now lets create the MOBY XML for this job | ||
| 285 | my ($out_package_for_this_job) = new MOSES::MOBY::Package; | ||
| 286 | $out_package_for_this_job->add_jobs($out_package->job_by_id( $job->jid )); | ||
| 287 | $out_package->job_by_id( $job->jid )->_context($out_package_for_this_job); | ||
| 288 | |||
| 289 | # any service notes? | ||
| 290 | $out_package_for_this_job->serviceNotes($out_package->serviceNotes) if $out_package->serviceNotes; | ||
| 291 | # any exceptions? | ||
| 292 | $out_package_for_this_job->exceptions(@{$out_package->exceptions}) if $out_package->exceptions and scalar @{$out_package->exceptions}; | ||
| 293 | |||
| 294 | # New properties values | ||
| 295 | $lock = WSRF::MobyFile->new( undef, $ID ); | ||
| 296 | $WSRF::WSRP::Private{$property_pid} = ''; | ||
| 297 | $WSRF::WSRP::ResourceProperties{$property_status} = $status->XML(); | ||
| 298 | $WSRF::WSRP::ResourceProperties{$property_result} = | ||
| 299 | #$out_package_for_this_job->job_by_id( $job->jid )->toXML->toString(0); | ||
| 300 | $out_package_for_this_job->toXML->toString(0); | ||
| 301 | $lock->toFile(); | ||
| 302 | |||
| 303 | # Exits the child process | ||
| 304 | exit; | ||
| 305 | } | ||
| 306 | |||
| 307 | } | ||
| 308 | |||
| 309 | #----------------------------------------------------------------- | ||
| 310 | # destroy | ||
| 311 | # takes in the moby-wsrf header and the XML message and destroys | ||
| 312 | # the underlying wsrf resource. | ||
| 313 | # return: the moby-wsrf header and XML conveying the destruction | ||
| 314 | # of the underlying wsrf resource. | ||
| 315 | #----------------------------------------------------------------- | ||
| 316 | |||
| 317 | =head2 | ||
| 318 | |||
| 319 | Name : destroy | ||
| 320 | Function : destroys an asynchronous job | ||
| 321 | Usage : $async->destroy($header, $data); | ||
| 322 | Args : $header - a string of XML representing the moby-wsrf header | ||
| 323 | $data - a string of XML representing the WSRF destroy resource call. | ||
| 324 | |||
| 325 | =cut | ||
| 326 | |||
| 327 | sub destroy { | ||
| 328 | my ( $self, $header, $data ) = @_; | ||
| 329 | my $parser = XML::LibXML->new(); | ||
| 330 | my $doc; | ||
| 331 | eval { $doc = $parser->parse_string($header); }; | ||
| 332 | |||
| 333 | #throw error if $@ | ||
| 334 | return $self->create_fault( RESOURCE_NOT_DESTROYED_FAULT, '', $@ ) if $@; | ||
| 335 | |||
| 336 | # this the TO url that we need for our header/faults | ||
| 337 | my $URL = $doc->getElementsByLocalName("To"); | ||
| 338 | $URL = $URL->get_node(1)->textContent if $URL->size > 0; | ||
| 339 | |||
| 340 | # get the service invocation id | ||
| 341 | my $ID = $doc->getElementsByLocalName("ServiceInvocationId"); | ||
| 342 | |||
| 343 | # throw error if $ID->size() <= 0 | ||
| 344 | return | ||
| 345 | $self->create_fault( RESOURCE_NOT_DESTROYED_FAULT, $URL, | ||
| 346 | 'Missing the ServiceInvocationId in the moby-wsrf header.' ) | ||
| 347 | unless $ID->size() > 0; | ||
| 348 | $ID = $ID->get_node(1)->textContent if $ID->size > 0; | ||
| 349 | $ID =~ s/ //gi; | ||
| 350 | |||
| 351 | # wrap in eval{}; because either lifetime expired or invalid ID! | ||
| 352 | my $lock; | ||
| 353 | eval {$lock = WSRF::MobyFile->new( undef, $ID );}; | ||
| 354 | |||
| 355 | return | ||
| 356 | $self->create_fault( RESOURCE_NOT_DESTROYED_FAULT, $URL, | ||
| 357 | 'Either the requested resource cannot be found or it\'s lifetime expired.' ) if $@; | ||
| 358 | my @notkilled; | ||
| 359 | foreach my $key ( keys %WSRF::WSRP::Private ) { | ||
| 360 | if ( index( $key, "pid_" ) == 0 ) { | ||
| 361 | my $pid = $WSRF::WSRP::Private{$key}; | ||
| 362 | if ($pid) { | ||
| 363 | kill( 9, $pid ) or push( @notkilled, $pid ); | ||
| 364 | } | ||
| 365 | } | ||
| 366 | } | ||
| 367 | $lock->toFile(); | ||
| 368 | |||
| 369 | # throw error if some processes were not destroyed | ||
| 370 | return | ||
| 371 | $self->create_fault( RESOURCE_NOT_DESTROYED_FAULT, $URL, | ||
| 372 | "Could not kill WS-Resource process(es): " . join( ", ", @notkilled ) ) | ||
| 373 | if ( scalar(@notkilled) ); | ||
| 374 | |||
| 375 | # wrap in eval{}; because either lifetime expired or invalid ID! | ||
| 376 | eval {$lock = WSRF::MobyFile->new( undef, $ID );}; | ||
| 377 | |||
| 378 | return | ||
| 379 | $self->create_fault( RESOURCE_NOT_DESTROYED_FAULT, $URL, | ||
| 380 | 'Either the requested resource cannot be found or it\'s lifetime expired.' ) if $@; | ||
| 381 | |||
| 382 | my $file = $WSRF::Constants::Data . $lock->ID(); | ||
| 383 | unlink $file or die "error destroying resource"; | ||
| 384 | |||
| 385 | # return the following if successful | ||
| 386 | return | ||
| 387 | $self->_resource_property_header( "ImmediateResourceTermination/DestroyResponse", | ||
| 388 | $URL ), | ||
| 389 | '<DestroyResponse xmlns="http://docs.oasis-open.org/wsrf/rl-2"/>'; | ||
| 390 | } | ||
| 391 | |||
| 392 | #----------------------------------------------------------------- | ||
| 393 | # result | ||
| 394 | # takes in the moby-wsrf header and the XML message and obtains | ||
| 395 | # the result for the underlying wsrf resource. | ||
| 396 | # return: the moby-wsrf header and XML representing the result | ||
| 397 | # for the underlying wsrf resource. | ||
| 398 | #----------------------------------------------------------------- | ||
| 399 | |||
| 400 | =head2 | ||
| 401 | |||
| 402 | Name : result | ||
| 403 | Function : obtains the result of an asynchronous job | ||
| 404 | Usage : $async->result($header, $data); | ||
| 405 | Args : $header - a string of XML representing the moby-wsrf header | ||
| 406 | $data - a string of XML representing the WSRF result call. | ||
| 407 | |||
| 408 | =cut | ||
| 409 | |||
| 410 | sub result { | ||
| 411 | my ( $self, $header, $data ) = @_; | ||
| 412 | my $parser = XML::LibXML->new(); | ||
| 413 | my $doc; | ||
| 414 | eval { $doc = $parser->parse_string($header); }; | ||
| 415 | |||
| 416 | # throw error if $@ | ||
| 417 | return | ||
| 418 | $self->create_fault( INVALID_RESOURCE_PROPERTY_QNAME_FAULT, | ||
| 419 | "", "moby-wsrf header was invalid:\n$@" ) | ||
| 420 | if $@; | ||
| 421 | |||
| 422 | # this the TO url that we need incase we throw a fault | ||
| 423 | my $URL = $doc->getElementsByLocalName("To"); | ||
| 424 | |||
| 425 | # TODO throw error if $URL->size <= 0 | ||
| 426 | $URL = $URL->get_node(1)->textContent if $URL->size > 0; | ||
| 427 | |||
| 428 | # get the service invocation id | ||
| 429 | my $ID = $doc->getElementsByLocalName("ServiceInvocationId"); | ||
| 430 | |||
| 431 | # throw error if $ID->size() <= 0 | ||
| 432 | return | ||
| 433 | $self->create_fault( RESOURCE_UNKNOWN_FAULT, $URL, | ||
| 434 | "You neglected to provide a ServiceInvocationId" ) | ||
| 435 | unless $ID->size > 0; | ||
| 436 | $ID = $ID->get_node(1)->textContent if $ID->size > 0; | ||
| 437 | $ID =~ s/ //gi; | ||
| 438 | # wrap in eval{}; because either lifetime expired or invalid ID! | ||
| 439 | my $lock; | ||
| 440 | eval {$lock = WSRF::MobyFile->new( undef, $ID );}; | ||
| 441 | return | ||
| 442 | $self->create_fault( RESOURCE_UNKNOWN_FAULT, $URL, | ||
| 443 | 'Either the requested resource cannot be found or it\'s lifetime expired.' ) if $@; | ||
| 444 | |||
| 445 | # get the query ids from the $data | ||
| 446 | my @ids = @{ $self->_get_query_ids( $data, ":result_" ) }; | ||
| 447 | |||
| 448 | # throw error if no ids found | ||
| 449 | return | ||
| 450 | $self->create_fault( RESOURCE_UNKNOWN_FAULT, $URL, | ||
| 451 | "No IDs in request for results." ) | ||
| 452 | unless scalar @ids; | ||
| 453 | |||
| 454 | my $ans = ''; | ||
| 455 | foreach my $queryID (@ids) { | ||
| 456 | my $property_status = "result_$queryID"; | ||
| 457 | # TODO - does the property exist? if not throw error so we dont pass invalid XML | ||
| 458 | $ans .= "<" | ||
| 459 | . $WSRF::WSRP::PropertyNamespaceMap->{$property_status}{prefix} | ||
| 460 | . ":$property_status"; | ||
| 461 | my $ns = | ||
| 462 | defined( $WSRF::WSRP::PropertyNamespaceMap->{$property_status}{namespace} ) | ||
| 463 | ? " xmlns:" | ||
| 464 | . $WSRF::WSRP::PropertyNamespaceMap->{$property_status}{prefix} . "=\"" | ||
| 465 | . $WSRF::WSRP::PropertyNamespaceMap->{$property_status}{namespace} . "\">" | ||
| 466 | : ">"; | ||
| 467 | $ans .= $ns; | ||
| 468 | $ans .= $WSRF::WSRP::ResourceProperties{$property_status}; | ||
| 469 | $ans .= "</" | ||
| 470 | . $WSRF::WSRP::PropertyNamespaceMap->{$property_status}{prefix} | ||
| 471 | . ":$property_status>"; | ||
| 472 | } | ||
| 473 | $lock->toFile(); | ||
| 474 | |||
| 475 | # parse the XML to see what kind of response we need to send back | ||
| 476 | eval { $doc = $parser->parse_string($data); }; | ||
| 477 | |||
| 478 | # return an error if there is a problem parsing $data | ||
| 479 | return | ||
| 480 | $self->create_fault( INVALID_RESOURCE_PROPERTY_QNAME_FAULT, | ||
| 481 | "", | ||
| 482 | "WSRF resource request was not structured correctly:\n$@" ) | ||
| 483 | if $@; | ||
| 484 | |||
| 485 | $ans = | ||
| 486 | "<GetMultipleResourcePropertiesResponse xmlns='http://docs.oasis-open.org/wsrf/rp-2'>" | ||
| 487 | . $ans | ||
| 488 | . "</GetMultipleResourcePropertiesResponse>" | ||
| 489 | if $doc->getElementsByLocalName("ResourceProperty")->size() > 0; | ||
| 490 | $ans = | ||
| 491 | "<GetResourcePropertyResponse xmlns='http://docs.oasis-open.org/wsrf/rp-2'>" | ||
| 492 | . $ans | ||
| 493 | . "</GetResourcePropertyResponse>" | ||
| 494 | unless $doc->getElementsByLocalName("ResourceProperty")->size() > 0; | ||
| 495 | |||
| 496 | # return the header and the data | ||
| 497 | return ( | ||
| 498 | $self->_resource_property_header( | ||
| 499 | "GetMultipleResourceProperties/GetMultipleResourcePropertiesResponse", | ||
| 500 | $URL | ||
| 501 | ), | ||
| 502 | $ans | ||
| 503 | ) if $doc->getElementsByLocalName("ResourceProperty")->size() > 0; | ||
| 504 | return ( | ||
| 505 | $self->_resource_property_header( | ||
| 506 | "GetResourceProperty/GetResourcePropertyResponse", $URL | ||
| 507 | ), | ||
| 508 | $ans | ||
| 509 | ) unless $doc->getElementsByLocalName("ResourceProperty")->size() > 0; | ||
| 510 | } | ||
| 511 | |||
| 512 | #----------------------------------------------------------------- | ||
| 513 | # poll | ||
| 514 | # takes in the moby-wsrf header and the XML message and checks | ||
| 515 | # the status for the underlying wsrf resource. | ||
| 516 | # return: the moby-wsrf header and XML representing the status | ||
| 517 | # of the underlying wsrf resource. | ||
| 518 | #---------------------------------------------------------------- | ||
| 519 | |||
| 520 | =head2 | ||
| 521 | |||
| 522 | Name : poll | ||
| 523 | Function : polls the status of an asynchronous job | ||
| 524 | Usage : $async->poll($header, $data); | ||
| 525 | Args : $header - a string of XML representing the moby-wsrf header | ||
| 526 | $data - a string of XML representing the WSRF poll resource status call. | ||
| 527 | |||
| 528 | =cut | ||
| 529 | |||
| 530 | sub poll { | ||
| 531 | |||
| 532 | my ( $self, $header, $data ) = @_; | ||
| 533 | my $parser = XML::LibXML->new(); | ||
| 534 | my $doc; | ||
| 535 | eval { $doc = $parser->parse_string($header); }; | ||
| 536 | |||
| 537 | # throw error if $@ | ||
| 538 | return | ||
| 539 | $self->create_fault( INVALID_RESOURCE_PROPERTY_QNAME_FAULT, | ||
| 540 | "", "moby-wsrf header was invalid:\n$@" ) | ||
| 541 | if $@; | ||
| 542 | |||
| 543 | # this the TO url that we need incase we throw a fault | ||
| 544 | my $URL = $doc->getElementsByLocalName("To"); | ||
| 545 | |||
| 546 | # TODO throw error if $URL->size <= 0 | ||
| 547 | $URL = $URL->get_node(1)->textContent if $URL->size > 0; | ||
| 548 | |||
| 549 | # get the service invocation id | ||
| 550 | my $ID = $doc->getElementsByLocalName("ServiceInvocationId"); | ||
| 551 | |||
| 552 | # throw error if $ID->size() <= 0 | ||
| 553 | return | ||
| 554 | $self->create_fault( RESOURCE_UNKNOWN_FAULT, $URL, | ||
| 555 | "You neglected to provide a ServiceInvocationId" ) | ||
| 556 | unless $ID->size > 0; | ||
| 557 | # get the service invocation id | ||
| 558 | $ID = $ID->get_node(1)->textContent if $ID->size > 0; | ||
| 559 | $ID =~ s/ //gi; | ||
| 560 | |||
| 561 | # TODO wrap in eval{}; because either lifetime expired or invalid ID! | ||
| 562 | my $lock; | ||
| 563 | eval {$lock = WSRF::MobyFile->new( undef, $ID );}; | ||
| 564 | return | ||
| 565 | $self->create_fault( RESOURCE_UNKNOWN_FAULT, $URL, | ||
| 566 | 'Either the requested resource cannot be found or it\'s lifetime expired.' ) if $@; | ||
| 567 | # get the query ids from the $data | ||
| 568 | my @ids = @{ $self->_get_query_ids( $data, ":status_" ) }; | ||
| 569 | |||
| 570 | # throw error if no ids found | ||
| 571 | return | ||
| 572 | $self->create_fault( RESOURCE_UNKNOWN_FAULT, $URL, | ||
| 573 | "No IDs in request for resource status update." ) | ||
| 574 | unless scalar @ids; | ||
| 575 | |||
| 576 | my $ans = ''; | ||
| 577 | foreach my $queryID (@ids) { | ||
| 578 | my $property_status = "status_$queryID"; | ||
| 579 | # does our property exist? | ||
| 580 | # TODO - throw error if property doesnt exist so that we dont pass invalid XML | ||
| 581 | $ans .= "<" | ||
| 582 | . $WSRF::WSRP::PropertyNamespaceMap->{$property_status}{prefix} | ||
| 583 | . ":$property_status"; | ||
| 584 | my $ns = | ||
| 585 | defined( $WSRF::WSRP::PropertyNamespaceMap->{$property_status}{namespace} ) | ||
| 586 | ? " xmlns:" | ||
| 587 | . $WSRF::WSRP::PropertyNamespaceMap->{$property_status}{prefix} . "=\"" | ||
| 588 | . $WSRF::WSRP::PropertyNamespaceMap->{$property_status}{namespace} . "\">" | ||
| 589 | : ">"; | ||
| 590 | $ans .= $ns; | ||
| 591 | $ans .= $WSRF::WSRP::ResourceProperties{$property_status}; | ||
| 592 | $ans .= "</" | ||
| 593 | . $WSRF::WSRP::PropertyNamespaceMap->{$property_status}{prefix} | ||
| 594 | . ":$property_status>"; | ||
| 595 | } | ||
| 596 | $lock->toFile(); | ||
| 597 | |||
| 598 | # parse the XML to see what kind of response we need to send back | ||
| 599 | eval { $doc = $parser->parse_string($data); }; | ||
| 600 | |||
| 601 | $ans = | ||
| 602 | "<GetMultipleResourcePropertiesResponse xmlns='http://docs.oasis-open.org/wsrf/rp-2'>" | ||
| 603 | . $ans | ||
| 604 | . "</GetMultipleResourcePropertiesResponse>" | ||
| 605 | if $doc->getElementsByLocalName("ResourceProperty")->size() > 0; | ||
| 606 | $ans = | ||
| 607 | "<GetResourcePropertyResponse xmlns='http://docs.oasis-open.org/wsrf/rp-2'>" | ||
| 608 | . $ans | ||
| 609 | . "</GetResourcePropertyResponse>" | ||
| 610 | unless $doc->getElementsByLocalName("ResourceProperty")->size() > 0; | ||
| 611 | |||
| 612 | # return the header and the data | ||
| 613 | return ( | ||
| 614 | $self->_resource_property_header( | ||
| 615 | "GetMultipleResourceProperties/GetMultipleResourcePropertiesResponse", | ||
| 616 | $URL | ||
| 617 | ), | ||
| 618 | $ans | ||
| 619 | ) if $doc->getElementsByLocalName("ResourceProperty")->size() > 0; | ||
| 620 | return ( | ||
| 621 | $self->_resource_property_header( | ||
| 622 | "GetResourceProperty/GetResourcePropertyResponse", $URL | ||
| 623 | ), | ||
| 624 | $ans | ||
| 625 | ) unless $doc->getElementsByLocalName("ResourceProperty")->size() > 0; | ||
| 626 | } | ||
| 627 | |||
| 628 | #----------------------------------------------------------------- | ||
| 629 | # create_epr: | ||
| 630 | # takes in a CGI variable | ||
| 631 | # creates a WSRF file resource and returns the EPR for it | ||
| 632 | #----------------------------------------------------------------- | ||
| 633 | |||
| 634 | =head2 | ||
| 635 | |||
| 636 | Name : create_epr | ||
| 637 | Function : creates an endpoint reference for the given CGI object | ||
| 638 | Usage : $async->create_epr($cgi); | ||
| 639 | Args : $cgi - a CGI variable | ||
| 640 | |||
| 641 | =cut | ||
| 642 | |||
| 643 | sub create_epr { | ||
| 644 | my ( $self, $query ) = @_; | ||
| 645 | |||
| 646 | # Get an identifier for the resource | ||
| 647 | my $ID = WSRF::GSutil::CalGSH_ID(); | ||
| 648 | $ENV{ID} = $ID; | ||
| 649 | |||
| 650 | # Create an EndpointReference for the resource | ||
| 651 | my $EPR = WSRF::WS_Address->new(); | ||
| 652 | |||
| 653 | my ($proto) = ( $query->https() ) ? 'https' : 'http'; | ||
| 654 | my ($host) = $query->virtual_host(); | ||
| 655 | my ($port) = $query->virtual_port(); | ||
| 656 | if ( ( $proto eq 'http' && $port eq '80' ) | ||
| 657 | || ( $proto eq 'https' && $port eq '443' ) ) | ||
| 658 | { | ||
| 659 | $port = ''; | ||
| 660 | } else { | ||
| 661 | $port = ':' . $port; | ||
| 662 | } | ||
| 663 | my ($relpath) = $query->script_name(); | ||
| 664 | my ($virtualrel) = | ||
| 665 | $ENV{'HTTP_VIA'} | ||
| 666 | || $ENV{'HTTP_FORWARDED'} | ||
| 667 | || $ENV{'HTTP_X_FORWARDED_FOR'}; | ||
| 668 | if ( defined($virtualrel) | ||
| 669 | && $virtualrel =~ /^(?:https?:\/\/[^:\/]+)?(?::[0-9]+)?(\/.*)/ ) | ||
| 670 | { | ||
| 671 | $relpath = $1; | ||
| 672 | } | ||
| 673 | |||
| 674 | $EPR->Address("$proto://$host$port$relpath?asyncId=$ID"); | ||
| 675 | $EPR->ReferenceParameters( | ||
| 676 | '<wsa:ReferenceParameters><mobyws:ServiceInvocationId xmlns:mobyws="' | ||
| 677 | . $WSRF::Constants::MOBY . '">' | ||
| 678 | . $ENV{ID} | ||
| 679 | . '</mobyws:ServiceInvocationId></wsa:ReferenceParameters>' ); | ||
| 680 | $EPR = XML::LibXML->new->parse_string( $EPR->XML )->getDocumentElement->toString; | ||
| 681 | |||
| 682 | # Write the properties to a file | ||
| 683 | WSRF::File::toFile($ID); | ||
| 684 | |||
| 685 | # strip newlines from the EPR | ||
| 686 | $EPR =~ s/[\r\n]+//g; | ||
| 687 | |||
| 688 | # Return the EndpointReference | ||
| 689 | return $EPR; | ||
| 690 | |||
| 691 | } | ||
| 692 | |||
| 693 | #----------------------------------------------------------------- | ||
| 694 | # create_fault | ||
| 695 | # creates a fault given a fault type and a service url. | ||
| 696 | # return: the moby-wsrf header and the fault as XML | ||
| 697 | #----------------------------------------------------------------- | ||
| 698 | |||
| 699 | =head2 | ||
| 700 | |||
| 701 | Name : create_fault | ||
| 702 | Function : creates a WSRF resource fault | ||
| 703 | Usage : $async->create_fault($fault_type, $url); | ||
| 704 | Args : $fault_type - one of C<RESOURCE_UNKNOWN_FAULT>, C<RESOURCE_UNAVAILABLE_FAULT>, C<RESOURCE_NOT_DESTROYED_FAULT> or C<INVALID_RESOURCE_PROPERTY_QNAME_FAULT> | ||
| 705 | $url - the service url | ||
| 706 | $desc - an optional message to include in the fault. | ||
| 707 | |||
| 708 | =cut | ||
| 709 | |||
| 710 | sub create_fault { | ||
| 711 | |||
| 712 | # fault_type is the type of fault | ||
| 713 | # service is the service URL | ||
| 714 | my ( $self, $fault_type, $service, $m ) = @_; | ||
| 715 | |||
| 716 | # create the timestamp | ||
| 717 | my $timestamp = HTTP::Date::time2isoz(); | ||
| 718 | |||
| 719 | # get the fault details | ||
| 720 | my $desc = $FAULT_DESCRIPTIONS{$fault_type} || "fault"; | ||
| 721 | my $name = $FAULT_NAMES{$fault_type} || "BaseFault"; | ||
| 722 | $desc .= "\n$m" if defined $m; | ||
| 723 | |||
| 724 | # create the fault | ||
| 725 | my $msg = <<EOF; | ||
| 726 | <$name xmlns="http://docs.oasis-open.org/wsrf/bf-2"> | ||
| 727 | <Timestamp>$timestamp</Timestamp> | ||
| 728 | <Description>$desc</Description> | ||
| 729 | </$name> | ||
| 730 | EOF | ||
| 731 | |||
| 732 | my $header = <<EOF; | ||
| 733 | <moby-wsrf> | ||
| 734 | <wsa:From xmlns:wsa"http://www.w3.org/2005/08/addressing" wsa:Id="From">$service</wsa:From> | ||
| 735 | <wsa:Action xmlns:wsa="http://www.w3.org/2005/08/addressing">http://docs.oasis-open.org/wsrf/fault</wsa:Action> | ||
| 736 | </moby-wsrf> | ||
| 737 | EOF | ||
| 738 | |||
| 739 | #remove the newlines | ||
| 740 | $header =~ s/[\r\n]+//g; | ||
| 741 | |||
| 742 | # return the $header and the data | ||
| 743 | return $header, $msg; | ||
| 744 | } | ||
| 745 | |||
| 746 | #----------------------------------------------------------------- | ||
| 747 | # _resource_property_header | ||
| 748 | # creates the moby-wsrf header given a $type | ||
| 749 | # (one our FAULT constants) and a $url (the services URL). | ||
| 750 | # return: a string of XML representing the moby-wsrf header | ||
| 751 | #----------------------------------------------------------------- | ||
| 752 | sub _resource_property_header { | ||
| 753 | my ( $self, $type, $url ) = @_; | ||
| 754 | return <<EOF; | ||
| 755 | <moby-wsrf> | ||
| 756 | <From xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' xmlns='http://www.w3.org/2005/08/addressing' wsu:Id="From">$url</From> | ||
| 757 | <Action xmlns='http://www.w3.org/2005/08/addressing'>http://docs.oasis-open.org/wsrf/rpw-2/$type</Action> | ||
| 758 | <moby-wsrf> | ||
| 759 | EOF | ||
| 760 | } | ||
| 761 | |||
| 762 | #----------------------------------------------------------------- | ||
| 763 | # _get_query_ids | ||
| 764 | # given $xml and $str, it searchs for query ids using $str as a | ||
| 765 | # the prefix to search for and then takes all the text | ||
| 766 | # afterwards | ||
| 767 | # return: an array ref of query ids | ||
| 768 | #----------------------------------------------------------------- | ||
| 769 | sub _get_query_ids { | ||
| 770 | my ( $self, $xml, $str ) = @_; | ||
| 771 | my @ids; | ||
| 772 | my $parser = XML::LibXML->new(); | ||
| 773 | my $doc; | ||
| 774 | eval { $doc = $parser->parse_string($xml); }; | ||
| 775 | |||
| 776 | # return undef if there is invalid XML | ||
| 777 | return @ids if $@; | ||
| 778 | |||
| 779 | # check for one or more ResourceProperty elements | ||
| 780 | my $nodes = $doc->getElementsByLocalName("ResourceProperty"); | ||
| 781 | if ( $nodes->size() > 0 ) { | ||
| 782 | |||
| 783 | # extract the query ids | ||
| 784 | for ( 1 ... $nodes->size() ) { | ||
| 785 | my $id = $nodes->get_node($_)->textContent; | ||
| 786 | if ( $id =~ m/\Q$str\E(.*)$/gi ) { | ||
| 787 | push @ids, $1; | ||
| 788 | } | ||
| 789 | } | ||
| 790 | } | ||
| 791 | |||
| 792 | # check for the single GetResourceProperty element | ||
| 793 | if ( $nodes->size == 0 ) { | ||
| 794 | $nodes = $doc->getElementsByLocalName("GetResourceProperty"); | ||
| 795 | if ( $nodes->size() == 1 ) { | ||
| 796 | |||
| 797 | # extract the single query id | ||
| 798 | my $id = $nodes->get_node(1)->textContent; | ||
| 799 | if ( $id =~ m/\Q$str\E(.*)$/gi ) { | ||
| 800 | push @ids, $1; | ||
| 801 | } | ||
| 802 | } | ||
| 803 | } | ||
| 804 | return \@ids; | ||
| 805 | } | ||
| 806 | |||
| 807 | 1; | ||
| 808 | __END__ |