| 1 | package SADI::Simple::AsyncService; | ||
| 2 | { | ||
| 3 | $SADI::Simple::AsyncService::VERSION = '0.15'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use SADI::Simple::ServiceDescription; | ||
| 10 | use SADI::Simple::Utils; | ||
| 11 | use SADI::Simple::OutputModel; | ||
| 12 | |||
| 13 | use Log::Log4perl; | ||
| 14 | |||
| 15 | use POSIX qw(setsid); | ||
| 16 | use Data::Dumper; | ||
| 17 | |||
| 18 | use RDF::Trine::Model 0.135; | ||
| 19 | use RDF::Trine::Serializer 0.135; | ||
| 20 | use RDF::Trine::Parser 0.135; | ||
| 21 | use RDF::Trine::Node::Resource 0.135; | ||
| 22 | use Template; | ||
| 23 | use File::Spec; | ||
| 24 | use File::Spec::Functions qw(catfile splitpath); | ||
| 25 | use File::Temp qw(tempfile); | ||
| 26 | use Storable (); | ||
| 27 | |||
| 28 | use base 'SADI::Simple::ServiceBase'; | ||
| 29 | |||
| 30 | |||
| 31 | # in seconds | ||
| 32 | use constant POLL_INTERVAL => 30; | ||
| 33 | |||
| 34 | use constant POLLING_RDF_TEMPLATE => <<TEMPLATE; | ||
| 35 | <rdf:RDF | ||
| 36 | xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" | ||
| 37 | xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"> | ||
| 38 | [% FOREACH uri IN inputURIs %] | ||
| 39 | <rdf:Description rdf:about="[% uri %]"> | ||
| 40 | <rdf:type rdf:resource="[% outClass %]"/> | ||
| 41 | <rdfs:isDefinedBy rdf:resource="[% url %]"/> | ||
| 42 | </rdf:Description> | ||
| 43 | [% END %] | ||
| 44 | </rdf:RDF> | ||
| 45 | TEMPLATE | ||
| 46 | |||
| 47 | use constant NOT_FINISHED => 0; | ||
| 48 | use constant SUCCESS => 1; | ||
| 49 | use constant ERROR => 2; | ||
| 50 | |||
| 51 | sub handle_cgi_request | ||
| 52 | { | ||
| 53 | my $self = shift; | ||
| 54 | |||
| 55 | if ($ENV{REQUEST_METHOD} eq 'GET' or $ENV{REQUEST_METHOD} eq 'HEAD') { | ||
| 56 | |||
| 57 | my $q = new CGI; | ||
| 58 | |||
| 59 | # print the interface unless we are polling | ||
| 60 | do { | ||
| 61 | print $q->header(-type=>$self->get_response_content_type()); | ||
| 62 | print $self->{Signature}->getServiceInterface($self->get_response_content_type()); | ||
| 63 | return; | ||
| 64 | } unless $q->param('poll'); | ||
| 65 | |||
| 66 | # we are polling ... | ||
| 67 | # $poll is the id for our file store | ||
| 68 | my $poll = $q->param('poll'); | ||
| 69 | my $status; | ||
| 70 | eval {$status = $self->status($poll);}; | ||
| 71 | # do something if $@ | ||
| 72 | print $q->header(-status=>"404 nothing found for the given polling parameter" ) if $@; | ||
| 73 | return if $@; | ||
| 74 | if ($status != NOT_FINISHED) { | ||
| 75 | # we are done | ||
| 76 | my $output; | ||
| 77 | eval {$output = $self->retrieve($poll);}; | ||
| 78 | my $http_status = ($status == SUCCESS) ? 200 : 500; | ||
| 79 | unless ($@) { | ||
| 80 | print $q->header(-status => $http_status, -type => $self->get_response_content_type()); | ||
| 81 | print $output; | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | } else { | ||
| 85 | # still waiting | ||
| 86 | my $signature = $self->{Signature}; | ||
| 87 | print $q->redirect(-uri=>$self->get_polling_url($poll), -status=>302, -Retry_After=>POLL_INTERVAL); | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | } else { | ||
| 91 | # call the service | ||
| 92 | |||
| 93 | # get the posted data | ||
| 94 | my $data = join "",<STDIN>; | ||
| 95 | |||
| 96 | # call the service | ||
| 97 | my ($poll_id, $input_uris, $error_msg) = $self->invoke($data); | ||
| 98 | |||
| 99 | my $q = new CGI; | ||
| 100 | |||
| 101 | if (!defined($poll_id)) { | ||
| 102 | print $q->header(-status => 500); | ||
| 103 | print $error_msg; | ||
| 104 | return; | ||
| 105 | } | ||
| 106 | |||
| 107 | print $q->header( | ||
| 108 | -type=>$self->get_response_content_type(), | ||
| 109 | -status=>202, | ||
| 110 | -Retry_After=>POLL_INTERVAL | ||
| 111 | ); | ||
| 112 | |||
| 113 | print $self->get_polling_rdf($poll_id, @$input_uris); | ||
| 114 | } | ||
| 115 | |||
| 116 | } | ||
| 117 | |||
| 118 | sub get_polling_url | ||
| 119 | { | ||
| 120 | my ($self, $poll_id) = @_; | ||
| 121 | |||
| 122 | my $base_url = $self->{Signature}->URL || CGI::url(); | ||
| 123 | return ($base_url . '?poll=' . $poll_id); | ||
| 124 | } | ||
| 125 | |||
| 126 | #----------------------------------------------------------------- | ||
| 127 | # store | ||
| 128 | # saves the state of our service invocation given a $uid. | ||
| 129 | # throws exception if there are any problems saving data to disk. | ||
| 130 | #----------------------------------------------------------------- | ||
| 131 | |||
| 132 | sub store { | ||
| 133 | |||
| 134 | my ($self, $output_model, $status, $poll_id) = @_; | ||
| 135 | |||
| 136 | my $quads; | ||
| 137 | |||
| 138 | if(($self->get_response_content_type =~ /quads/i) && ($self->{Signature}->NanoPublisher)){ | ||
| 139 | $quads = SADI::Simple::Utils->serialize_model($output_model, $self->get_response_content_type); | ||
| 140 | |||
| 141 | } | ||
| 142 | my $output = SADI::Simple::Utils->serialize_model($output_model, 'application/rdf+xml'); | ||
| 143 | |||
| 144 | my %hash; | ||
| 145 | |||
| 146 | $hash{rdfxml} = $output; | ||
| 147 | $hash{quads} = $quads; | ||
| 148 | $hash{status} = $status; | ||
| 149 | |||
| 150 | my $filename = $self->_poll_id_to_filename($poll_id); | ||
| 151 | Storable::store(\%hash, $filename) or $self->throw("unable to store state to $filename"); | ||
| 152 | |||
| 153 | } | ||
| 154 | |||
| 155 | sub _poll_id_to_filename | ||
| 156 | { | ||
| 157 | my ($self, $poll_id) = @_; | ||
| 158 | return catfile(File::Spec->tmpdir(), $poll_id); | ||
| 159 | } | ||
| 160 | |||
| 161 | #----------------------------------------------------------------- | ||
| 162 | # retrieve | ||
| 163 | # given a $uid, retrieves the current saved state for our service | ||
| 164 | # invocation | ||
| 165 | # NOTE: if a value is retrieved, then it removed from the cache | ||
| 166 | #----------------------------------------------------------------- | ||
| 167 | sub retrieve { | ||
| 168 | |||
| 169 | my ($self, $poll_id) = @_; | ||
| 170 | my $log = Log::Log4perl->get_logger(__PACKAGE__); | ||
| 171 | my $filename = $self->_poll_id_to_filename($poll_id); | ||
| 172 | |||
| 173 | my $hashref = Storable::retrieve($filename) or $self->throw("no data stored for poll_id $poll_id"); | ||
| 174 | my $rdfxml = $hashref->{rdfxml}; | ||
| 175 | my $quads = $hashref->{quads}; | ||
| 176 | |||
| 177 | unlink($filename) or $log->warn("failed to removed tempfile $filename: $!"); | ||
| 178 | |||
| 179 | if ($self->get_response_content_type eq 'text/rdf+n3') { | ||
| 180 | return SADI::Simple::Utils->rdfxml_to_n3($rdfxml); | ||
| 181 | } elsif ($self->get_response_content_type eq 'application/n-quads'){ | ||
| 182 | return $quads | ||
| 183 | } else { | ||
| 184 | return $rdfxml; | ||
| 185 | } | ||
| 186 | |||
| 187 | } | ||
| 188 | |||
| 189 | #----------------------------------------------------------------- | ||
| 190 | # completed | ||
| 191 | # given a $uid, retrieves the current state our service | ||
| 192 | # invocation. a Perl true value if completed, 0 | undef otherwise. | ||
| 193 | # Throws exception if there is nothing to retrieve for the given $uid | ||
| 194 | #----------------------------------------------------------------- | ||
| 195 | sub status { | ||
| 196 | |||
| 197 | my ($self, $poll_id) = @_; | ||
| 198 | |||
| 199 | my $filename = $self->_poll_id_to_filename($poll_id); | ||
| 200 | my $hashref = Storable::retrieve($filename) or $self->throw("no data stored for poll_id $poll_id"); | ||
| 201 | return $hashref->{status}; | ||
| 202 | |||
| 203 | } | ||
| 204 | |||
| 205 | #----------------------------------------------------------------- | ||
| 206 | # [% obj.ServiceName %] | ||
| 207 | # the main method; corresponds to the name of this SADI web service | ||
| 208 | #----------------------------------------------------------------- | ||
| 209 | |||
| 210 | sub invoke { | ||
| 211 | |||
| 212 | my ($self, $data) = @_; | ||
| 213 | |||
| 214 | my $error_msg; | ||
| 215 | my $log = Log::Log4perl->get_logger(__PACKAGE__); | ||
| 216 | |||
| 217 | Log::Log4perl::NDC->push ($$); | ||
| 218 | |||
| 219 | $log->info ('*** REQUEST START *** ' . "\n" . $self->log_request); | ||
| 220 | $log->debug ("Input raw data (first 1000 characters):\n" . substr ($data, 0, 1000)) if ($log->is_debug); | ||
| 221 | |||
| 222 | $self->default_throw_with_stack (0); | ||
| 223 | |||
| 224 | my $input_model; | ||
| 225 | my $output_model; | ||
| 226 | #$output_model = RDF::Trine::Model->temporary_model; | ||
| 227 | $output_model = SADI::Simple::OutputModel->new(); # catches quad statements necessary for NanoPubs | ||
| 228 | $output_model->_init($self); | ||
| 229 | |||
| 230 | |||
| 231 | my @inputs; | ||
| 232 | my @input_uris = (); | ||
| 233 | |||
| 234 | # save the input URIs for polling RDF | ||
| 235 | eval { | ||
| 236 | $input_model = $self->_build_model($data, $self->get_request_content_type); | ||
| 237 | @inputs = $self->_get_inputs_from_model($input_model); | ||
| 238 | |||
| 239 | push @input_uris, $_->uri foreach @inputs; | ||
| 240 | }; | ||
| 241 | |||
| 242 | # error in creating parser, or parsing input | ||
| 243 | if ($@) { | ||
| 244 | my $stack = $self->format_stack ($@); | ||
| 245 | # $self->_add_error_to_model($output_model, $@, 'Error parsing input message for sadi service!', $stack); | ||
| 246 | $log->error ($stack); | ||
| 247 | $log->info ('*** FATAL ERROR RESPONSE BACK ***'); | ||
| 248 | Log::Log4perl::NDC->pop(); | ||
| 249 | # $self->store($output_model, ERROR, $poll_id); | ||
| 250 | return (undef, undef, "Error parsing input RDF: $@"); | ||
| 251 | } | ||
| 252 | |||
| 253 | my ($fh, $filename) = tempfile( | ||
| 254 | TEMPLATE => 'sadi-XXXXXXXX', | ||
| 255 | TMPDIR => 1, | ||
| 256 | UNLINK => 0, # we do this in retrieve() | ||
| 257 | ); | ||
| 258 | |||
| 259 | close($fh) or $self->throw($!); | ||
| 260 | |||
| 261 | my $poll_id = (splitpath($filename))[2]; | ||
| 262 | |||
| 263 | unless (defined( my $pid = fork() )) { | ||
| 264 | } elsif ($pid == 0) { | ||
| 265 | |||
| 266 | # Daemonize | ||
| 267 | |||
| 268 | # note: untie does no harm if the filehandle is not tied, | ||
| 269 | # but prevents potential errors if STDIN/STDOUT/STDERR is | ||
| 270 | # tied to a class that doesn't implement OPEN | ||
| 271 | |||
| 272 | untie *STDIN; | ||
| 273 | open STDIN, File::Spec->devnull; | ||
| 274 | untie *STDOUT; | ||
| 275 | open STDOUT, File::Spec->devnull; | ||
| 276 | untie *STDERR; | ||
| 277 | open STDERR, File::Spec->devnull; | ||
| 278 | setsid unless ($^O eq 'MSWin32'); | ||
| 279 | |||
| 280 | # child process | ||
| 281 | # do something (this service main task) | ||
| 282 | eval { | ||
| 283 | # save empty output model before we begin | ||
| 284 | $self->store($output_model, NOT_FINISHED, $poll_id); | ||
| 285 | $self->process_it(\@inputs, $input_model, $output_model); | ||
| 286 | $self->_type_outputs($output_model, \@inputs); | ||
| 287 | }; | ||
| 288 | # error thrown by the implementation class | ||
| 289 | if ($@) { | ||
| 290 | my $stack = $self->format_stack ($@); | ||
| 291 | $self->_add_error_to_model($output_model, $@, 'Error running sadi service!', $stack); | ||
| 292 | $log->error ($stack); | ||
| 293 | $log->info ('*** REQUEST TERMINATED RESPONSE BACK ***'); | ||
| 294 | Log::Log4perl::NDC->pop(); | ||
| 295 | # signal that we are done | ||
| 296 | $self->store($output_model, ERROR, $poll_id); | ||
| 297 | return ; | ||
| 298 | } | ||
| 299 | |||
| 300 | # return result | ||
| 301 | $log->info ('*** RESPONSE READY *** '); | ||
| 302 | |||
| 303 | Log::Log4perl::NDC->pop(); | ||
| 304 | $self->store($output_model, SUCCESS, $poll_id); | ||
| 305 | |||
| 306 | exit 0; | ||
| 307 | } | ||
| 308 | |||
| 309 | return ($poll_id, \@input_uris); | ||
| 310 | } | ||
| 311 | |||
| 312 | sub get_polling_rdf | ||
| 313 | { | ||
| 314 | my ($self, $poll_id, @input_uris) = @_; | ||
| 315 | |||
| 316 | my $log = Log::Log4perl->get_logger(__PACKAGE__); | ||
| 317 | |||
| 318 | my $tt = Template->new( ABSOLUTE => 1, TRIM => 1 ); | ||
| 319 | my $input = POLLING_RDF_TEMPLATE; | ||
| 320 | my $signature = $self->{Signature}; | ||
| 321 | my $polling_rdf; | ||
| 322 | |||
| 323 | $tt->process( | ||
| 324 | \$input, | ||
| 325 | { | ||
| 326 | inputURIs => \@input_uris, | ||
| 327 | outClass => $signature->OutputClass, | ||
| 328 | url => $self->get_polling_url($poll_id), | ||
| 329 | }, | ||
| 330 | \$polling_rdf | ||
| 331 | ) || $log->logdie( $tt->error() ); | ||
| 332 | |||
| 333 | if ($self->get_response_content_type() eq 'text/rdf+n3') { | ||
| 334 | return SADI::Simple::Utils->rdfxml_to_n3($polling_rdf); | ||
| 335 | } | ||
| 336 | |||
| 337 | return $polling_rdf; | ||
| 338 | } | ||
| 339 | |||
| 340 | 1; | ||
| 341 | |||
| 342 | __END__ |