enabled param checking
[Evergreen.git] / OpenSRF / src / perlmods / OpenSRF / Application.pm
1 package OpenSRF::Application;
2 use vars qw/$_app $log @_METHODS $thunk $server_class/;
3
4 use base qw/OpenSRF/;
5 use OpenSRF::AppSession;
6 use OpenSRF::DomainObject::oilsMethod;
7 use OpenSRF::DomainObject::oilsResponse qw/:status/;
8 use OpenSRF::Utils::Logger qw/:level/;
9 use Data::Dumper;
10 use Time::HiRes qw/time/;
11 use OpenSRF::EX qw/:try/;
12 use Carp;
13 #use OpenSRF::UnixServer;  # to get the server class from UnixServer::App
14
15 sub DESTROY{};
16
17 use strict;
18 use warnings;
19
20 $log = 'OpenSRF::Utils::Logger';
21
22 our $in_request = 0;
23 our @pending_requests;
24
25 sub package {
26         my $self = shift;
27         return 1 unless ref($self);
28         return $self->{package};
29 }
30
31 sub signature {
32         my $self = shift;
33         return 0 unless ref($self);
34         return $self->{signature};
35 }
36
37 sub argc {
38         my $self = shift;
39         return 0 unless ref($self);
40         return $self->{argc};
41 }
42
43 sub api_name {
44         my $self = shift;
45         return 1 unless ref($self);
46         return $self->{api_name};
47 }
48
49 sub api_level {
50         my $self = shift;
51         return 1 unless ref($self);
52         return $self->{api_level};
53 }
54
55 sub server_class {
56         my $class = shift;
57         if($class) {
58                 $server_class = $class;
59         }
60         return $server_class;
61 }
62
63 sub thunk {
64         my $self = shift;
65         my $flag = shift;
66         $thunk = $flag if (defined $flag);
67         return $thunk;
68 }
69
70 sub application_implementation {
71         my $self = shift;
72         my $app = shift;
73
74         if (defined $app) {
75                 $_app = $app;
76                 eval "use $_app;";
77                 if( $@ ) {
78                         $log->error( "Error loading application_implementation: $app -> $@", ERROR);
79                 }
80
81         }
82
83         return $_app;
84 }
85
86 sub handler {
87         my ($self, $session, $app_msg) = @_;
88
89         if( ! $app_msg ) {
90                 return 1;  # error?
91         }
92
93         $log->debug( "In Application::handler()", DEBUG );
94
95         my $app = $self->application_implementation;
96
97         if( $app ) {
98                 $log->debug( "Application is $app", DEBUG);
99         }
100
101         if ($session->last_message_type eq 'REQUEST') {
102                 $log->debug( "We got a REQUEST: ". $app_msg->method);
103
104                 my $method_name = $app_msg->method;
105                 $log->debug( " * Looking up $method_name inside $app", DEBUG);
106
107                 my $method_proto = $session->last_message_api_level;
108                 $log->debug( " * Method API Level [$method_proto]", DEBUG);
109
110                 my $coderef = $app->method_lookup( $method_name, $method_proto, 1, 1 );
111
112                 unless ($coderef) {
113                         $session->status( OpenSRF::DomainObject::oilsMethodException->new( 
114                                                 statusCode => STATUS_NOTFOUND(),
115                                                 status => "Method [$method_name] not found for $app"));
116                         return 1;
117                 }
118
119                 $log->debug( " (we got coderef $coderef", DEBUG);
120
121                 unless ($session->continue_request) {
122                         $session->status(
123                                 OpenSRF::DomainObject::oilsConnectStatus->new(
124                                                 statusCode => STATUS_REDIRECTED(),
125                                                 status => 'Disconnect on max requests' ) );
126                         $session->kill_me;
127                         return 1;
128                 }
129
130                 if (ref $coderef) {
131                         my @args = $app_msg->params;
132                         my $appreq = OpenSRF::AppRequest->new( $session );
133
134                         $log->debug( "in_request = $in_request : [" . $appreq->threadTrace."]", DEBUG );
135                         if( $in_request ) {
136                                 $log->debug( "Pushing onto pending requests: " . $appreq->threadTrace, DEBUG );
137                                 push @pending_requests, [ $appreq, \@args, $coderef ]; 
138                                 return 1;
139                         }
140
141
142                         $in_request++;
143
144                         $log->debug( "Executing coderef for {$method_name}", INTERNAL );
145
146                         my $resp;
147                         try {
148                                 # un-if(0) this block to enable param checking based on signature and argc
149                                 if (1) {
150                                         if (@args < $coderef->argc) {
151                                                 die     "Not enough params passed to ".
152                                                         $coderef->api_name." : requires ". $coderef->argc
153                                         }
154                                         if (@args) {
155                                                 if (exists $coderef->signature->{params}) {
156                                                         for my $p (0 .. scalar(@{ $coderef->signature->{params} }) - 1 ) {
157                                                                 my $s = $coderef->signature->{params}->[$p];
158                                                                 my $a = $args[$p];
159                                                                 if ($s->{class} && JSON->lookup_hint(ref $a) ne $s->{class}) {
160                                                                         die "Incorrect param class at position $p : should be a '$$s{class}'";
161                                                                 } elsif ($s->{type}) {
162                                                                         if (lc($s->{type}) eq 'object' && $a !~ /HASH/o) {
163                                                                                 die "Incorrect param type at position $p : should be an 'object'";
164                                                                         } elsif (lc($s->{type}) eq 'array' && $a !~ /ARRAY/o) {
165                                                                                 die "Incorrect param type at position $p : should be an 'array'";
166                                                                         } elsif (lc($s->{type}) eq 'number' && (ref($a) || $a !~ /^-?\d+(?:\.\d+)?$/o)) {
167                                                                                 die "Incorrect param type at position $p : should be a 'number'";
168                                                                         } elsif (lc($s->{type}) eq 'string' && ref($a)) {
169                                                                                 die "Incorrect param type at position $p : should be a 'string'";
170                                                                         }
171                                                                 }
172                                                         }
173                                                 }
174                                         }
175                                 }
176
177                                 my $start = time();
178                                 warn "About to run...\n";
179                                 $resp = $coderef->run( $appreq, @args); 
180                                 warn "Done running...\n";
181                                 my $time = sprintf '%.3f', time() - $start;
182
183                                 $log->debug( "Method duration for {$method_name}:  ". $time, INFO );
184                                 if( defined( $resp ) ) {
185                                         $appreq->respond_complete( $resp );
186                                 } else {
187                                         $appreq->status( OpenSRF::DomainObject::oilsConnectStatus->new(
188                                                                 statusCode => STATUS_COMPLETE(),
189                                                                 status => 'Request Complete' ) );
190                                 }
191                         } catch Error with {
192                                 my $e = shift;
193                                 warn "Caught error from 'run' method: $e\n";
194
195                                 if(UNIVERSAL::isa($e,"Error")) {
196                                         $e = $e->stringify();
197                                 } 
198                                 my $sess_id = $session->session_id;
199                                 $session->status(
200                                         OpenSRF::DomainObject::oilsMethodException->new(
201                                                         statusCode      => STATUS_INTERNALSERVERERROR(),
202                                                         status          => " *** Call to [$method_name] failed for session ".
203                                                                            "[$sess_id], thread trace ".
204                                                                            "[".$appreq->threadTrace."]:\n$e\n"
205                                         )
206                                 );
207                         };
208
209
210
211                         # ----------------------------------------------
212
213
214                         # XXX may need this later
215                         # $_->[1] = 1 for (@OpenSRF::AppSession::_CLIENT_CACHE);
216
217                         $in_request--;
218
219                         $log->debug( "Pending Requests: " . scalar(@pending_requests), INTERNAL );
220
221                         # cycle through queued requests
222                         while( my $aref = shift @pending_requests ) {
223                                 $in_request++;
224                                 my $resp;
225                                 try {
226                                         # un-if(0) this block to enable param checking based on signature and argc
227                                         if (1) {
228                                                 if (@args < $aref->[2]->argc) {
229                                                         die     "Not enough params passed to ".
230                                                                 $aref->[2]->api_name." : requires ". $aref->[2]->argc
231                                                 }
232                                                 if (@args) {
233                                                         if (exists $aref->[2]->signature->{params}) {
234                                                                 for my $p (0 .. scalar(@{ $aref->[2]->signature->{params} }) - 1 ) {
235                                                                         my $s = $aref->[2]->signature->{params}->[$p];
236                                                                         my $a = $args[$p];
237                                                                         if ($s->{class} && JSON->lookup_hint(ref $a) ne $s->{class}) {
238                                                                                 die "Incorrect param class at position $p : should be a '$$s{class}'";
239                                                                         } elsif ($s->{type}) {
240                                                                                 if (lc($s->{type}) eq 'object' && $a !~ /HASH/o) {
241                                                                                         die "Incorrect param type at position $p : should be an 'object'";
242                                                                                 } elsif (lc($s->{type}) eq 'array' && $a !~ /ARRAY/o) {
243                                                                                         die "Incorrect param type at position $p : should be an 'array'";
244                                                                                 } elsif (lc($s->{type}) eq 'number' && (ref($a) || $a !~ /^-?\d+(?:\.\d+)?$/o)) {
245                                                                                         die "Incorrect param type at position $p : should be a 'number'";
246                                                                                 } elsif (lc($s->{type}) eq 'string' && ref($a)) {
247                                                                                         die "Incorrect param type at position $p : should be a 'string'";
248                                                                                 }
249                                                                         }
250                                                                 }
251                                                         }
252                                                 }
253                                         }
254
255                                         my $start = time;
256                                         my $response = $aref->[2]->run( $aref->[0], @{$aref->[1]} );
257                                         my $time = sprintf '%.3f', time - $start;
258                                         $log->debug( "Method duration for {[".$aref->[2]->api_name." -> ".join(', ',@{$aref->[1]}).'}:  '.$time, DEBUG );
259
260                                         $appreq = $aref->[0];   
261                                         if( ref( $response ) ) {
262                                                 $appreq->respond_complete( $response );
263                                         } else {
264                                                 $appreq->status( OpenSRF::DomainObject::oilsConnectStatus->new(
265                                                                         statusCode => STATUS_COMPLETE(),
266                                                                         status => 'Request Complete' ) );
267                                         }
268                                         $log->debug( "Executed: " . $appreq->threadTrace, DEBUG );
269                                 } catch Error with {
270                                         my $e = shift;
271                                         if(UNIVERSAL::isa($e,"Error")) {
272                                                 $e = $e->stringify();
273                                         }
274                                         $session->status(
275                                                 OpenSRF::DomainObject::oilsMethodException->new(
276                                                                 statusCode => STATUS_INTERNALSERVERERROR(),
277                                                                 status => "Call to [".$aref->[2]->api_name."] faild:  $e"
278                                                 )
279                                         );
280                                 };
281                                 $in_request--;
282                         }
283
284                         return 1;
285                 } 
286
287                 my $res = OpenSRF::DomainObject::oilsMethodException->new( 
288                                 status => "Received non-REQUEST message in Application handler");
289                 $session->send('ERROR', $res);
290                 $session->kill_me;
291                 return 1;
292
293         } else {
294                 $session->push_queue([ $app_msg, $session->last_threadTrace ]);
295         }
296
297         $session->last_message_type('');
298         $session->last_message_api_level('');
299
300         return 1;
301 }
302
303 sub is_registered {
304         my $self = shift;
305         my $api_name = shift;
306         my $api_level = shift || 1;
307         return exists($_METHODS[$api_level]{$api_name});
308 }
309
310
311 sub normalize_whitespace {
312         my $txt = shift;
313
314         $txt =~ s/^\s+//gso;
315         $txt =~ s/\s+$//gso;
316         $txt =~ s/\s+/ /gso;
317         $txt =~ s/\n//gso;
318         $txt =~ s/\. /\.  /gso;
319
320         return $txt;
321 }
322
323 sub parse_notes_signature {
324         my $string = shift;
325         my @chunks = split(/\@/so, $string);
326
327         my @params;
328         my $ret;
329         my $desc = '';
330         for (@chunks) {
331                 if (/^return (.+)$/so) {
332                         $ret = [normalize_whitespace($1)];
333                 } elsif (/^param (\w+) \b(.+)$/so) {
334                         push @params, [ $1, normalize_whitespace($2) ];
335                 } else {
336                         $desc .= '@' if $desc;
337                         $desc .= $_;
338                 }
339         }
340
341         return [normalize_whitespace($desc),\@params, $ret];
342 }
343
344 sub parse_array_signature {
345         my $array = shift;
346         my ($d,$p,$r) = @$array;
347
348         return {
349                 desc    => $d,
350                 params  => [
351                         map { 
352                                 { name  => $$_[0],
353                                   desc  => $$_[1],
354                                   type  => $$_[2],
355                                   class => $$_[3],
356                                 }
357                         } @$p
358                 ],
359                 'return'=>
360                         { desc  => $$r[0],
361                           type  => $$r[1],
362                           class => $$r[2],
363                         }
364         };
365 }
366
367 sub register_method {
368         my $self = shift;
369         my $app = ref($self) || $self;
370         my %args = @_;
371
372
373         throw OpenSRF::DomainObject::oilsMethodException unless ($args{method});
374
375         $args{api_level} = 1 unless(defined($args{api_level}));
376         $args{stream} ||= 0;
377         $args{remote} ||= 0;
378         $args{argc} ||= 0;
379         $args{package} ||= $app;                
380         $args{server_class} = server_class();
381         $args{api_name} ||= $args{server_class} . '.' . $args{method};
382
383         # un-if(0) this block to enable signature parsing
384         if (!$args{signature}) {
385                 if ($args{notes} && !ref($args{notes})) {
386                         $args{signature} =
387                                 parse_array_signature( parse_notes_signature( $args{notes} ) );
388                 }
389         } elsif( ref($args{signature}) eq 'ARRAY') {
390                 $args{signature} =
391                         parse_array_signature( $args{signature} );
392         }
393         
394         unless ($args{object_hint}) {
395                 ($args{object_hint} = $args{package}) =~ s/::/_/go;
396         }
397
398         JSON->register_class_hint( name => $args{package}, hint => $args{object_hint}, type => "hash" );
399
400         $_METHODS[$args{api_level}]{$args{api_name}} = bless \%args => $app;
401
402         __PACKAGE__->register_method(
403                 stream => 0,
404                 argc => $args{argc},
405                 api_name => $args{api_name}.'.atomic',
406                 method => 'make_stream_atomic',
407                 notes => "This is a system generated method.  Please see the definition for $args{api_name}",
408         ) if ($args{stream});
409 }
410
411 sub retrieve_remote_apis {
412         my $method = shift;
413         my $session = OpenSRF::AppSession->create('router');
414         try {
415                 $session->connect or OpenSRF::EX::WARN->throw("Connection to router timed out");
416         } catch Error with {
417                 my $e = shift;
418                 $log->debug( "Remote subrequest returned an error:\n". $e );
419                 return undef;
420         } finally {
421                 return undef unless ($session->state == $session->CONNECTED);
422         };
423
424         my $req = $session->request( 'opensrf.router.info.class.list' );
425         my $list = $req->recv;
426
427         if( UNIVERSAL::isa($list,"Error") ) {
428                 throw $list;
429         }
430
431         my $content = $list->content;
432
433         $req->finish;
434         $session->finish;
435         $session->disconnect;
436
437         my %u_list = map { ($_ => 1) } @$content;
438
439         for my $class ( keys %u_list ) {
440                 next if($class eq $server_class);
441                 populate_remote_method_cache($class, $method);
442         }
443 }
444
445 sub populate_remote_method_cache {
446         my $class = shift;
447         my $meth = shift;
448
449         my $session = OpenSRF::AppSession->create($class);
450         try {
451                 $session->connect or OpenSRF::EX::WARN->throw("Connection to $class timed out");
452
453                 my $call = 'opensrf.system.method.all' unless (defined $meth);
454                 $call = 'opensrf.system.method' if (defined $meth);
455
456                 my $req = $session->request( $call, $meth );
457
458                 while (my $method = $req->recv) {
459                         next if (UNIVERSAL::isa($method, 'Error'));
460
461                         $method = $method->content;
462                         next if ( exists($_METHODS[$$method{api_level}]) &&
463                                 exists($_METHODS[$$method{api_level}]{$$method{api_name}}) );
464                         $method->{remote} = 1;
465                         bless($method, __PACKAGE__ );
466                         $_METHODS[$$method{api_level}]{$$method{api_name}} = $method;
467                 }
468
469                 $req->finish;
470                 $session->finish;
471                 $session->disconnect;
472
473         } catch Error with {
474                 my $e = shift;
475                 $log->debug( "Remote subrequest returned an error:\n". $e );
476                 return undef;
477         };
478 }
479
480 sub method_lookup {             
481         my $self = shift;
482         my $method = shift;
483         my $proto = shift;
484         my $no_recurse = shift || 0;
485         my $no_remote = shift || 0;
486
487         # this instead of " || 1;" above to allow api_level 0
488         $proto = $self->api_level unless (defined $proto);
489
490         my $class = ref($self) || $self;
491
492         $log->debug("Lookup of [$method] by [$class] in api_level [$proto]", DEBUG);
493         $log->debug("Available methods\n\t".join("\n\t", keys %{ $_METHODS[$proto] }), INTERNAL);
494
495         my $meth;
496         if (__PACKAGE__->thunk) {
497                 for my $p ( reverse(1 .. $proto) ) {
498                         if (exists $_METHODS[$p]{$method}) {
499                                 $meth = $_METHODS[$p]{$method};
500                         }
501                 }
502         } else {
503                 if (exists $_METHODS[$proto]{$method}) {
504                         $meth = $_METHODS[$proto]{$method};
505                 }
506         }
507
508         if (defined $meth) {
509                 $log->debug("Looks like we found [$method]!", DEBUG);
510                 $log->debug("Method object is ".Dumper($meth), INTERNAL);
511                 if($no_remote and $meth->{remote}) {
512                         $log->debug("OH CRAP We're not supposed to return remote methods", WARN);
513                         return undef;
514                 }
515
516         } elsif (!$no_recurse) {
517                 $log->debug("We didn't find [$method], asking everyone else.", DEBUG);
518                 retrieve_remote_apis($method);
519                 $meth = $self->method_lookup($method,$proto,1);
520         }
521
522         return $meth;
523 }
524
525 sub run {
526         my $self = shift;
527         my $req = shift;
528
529         my $resp;
530         my @params = @_;
531
532         if ( !UNIVERSAL::isa($req, 'OpenSRF::AppRequest') ) {
533                 $log->debug("Creating a SubRequest object", DEBUG);
534                 unshift @params, $req;
535                 $req = OpenSRF::AppSubrequest->new;
536         } else {
537                 $log->debug("This is a top level request", DEBUG);
538         }
539
540         if (!$self->{remote}) {
541                 my $code ||= \&{$self->{package} . '::' . $self->{method}};
542                 $log->debug("Created coderef [$code] for $$self{package}::$$self{method}",DEBUG);
543                 my $err = undef;
544
545                 try {
546                         $resp = $code->($self, $req, @params);
547
548                 } catch Error with {
549                         my $e = shift;
550                         $err = $e;
551                         warn "Method 'run' catching error: $e\n";
552
553                         if( ref($self) eq 'HASH') {
554                                 $log->error("Sub $$self{package}::$$self{method} DIED!!!\n\t$e\n", ERROR);
555                         }
556                 };
557
558                 if($err) {
559                         if(UNIVERSAL::isa($err,"Error")) { 
560                                 warn "Throwing from method run:\n$err\n------------------\n";
561                                 throw $err;
562                         } else {
563                                 die $err->stringify; 
564                         }
565                 }
566
567
568                 $log->debug("Coderef for [$$self{package}::$$self{method}] has been run", DEBUG);
569
570                 if ( ref($req) and UNIVERSAL::isa($req, 'OpenSRF::AppSubrequest') ) {
571                         $log->debug("A SubRequest object is responding", DEBUG);
572                         $req->respond($resp) if (defined $resp);
573                         $log->debug("... Responding with : " . join(" ",$req->responses), DEBUG);
574                         return $req->responses;
575                 } else {
576                         $log->debug("A top level Request object is responding $resp", DEBUG) if (defined $resp);
577                         return $resp;
578                 }
579         } else {
580                 my $session = OpenSRF::AppSession->create($self->{server_class});
581                 try {
582                         #$session->connect or OpenSRF::EX::WARN->throw("Connection to [$$self{server_class}] timed out");
583                         my $remote_req = $session->request( $self->{api_name}, @params );
584                         while (my $remote_resp = $remote_req->recv) {
585                                 OpenSRF::Utils::Logger->debug("Remote Subrequest Received " . $remote_resp, INTERNAL );
586                                 if( UNIVERSAL::isa($remote_resp,"Error") ) {
587                                         throw $remote_resp;
588                                 }
589                                 $req->respond( $remote_resp->content );
590                         }
591                         $remote_req->finish();
592
593                 } catch Error with {
594                         my $e = shift;
595                         $log->debug( "Remote subrequest returned an error:\n". $e );
596                         return undef;
597                 };
598
599                 if ($session) {
600                         $session->disconnect();
601                         $session->finish();
602                 }
603
604                 $log->debug( "Remote Subrequest Responses " . join(" ", $req->responses), INTERNAL );
605
606                 return $req->responses;
607         }
608         # huh? how'd we get here...
609         return undef;
610 }
611
612 sub introspect {
613         my $self = shift;
614         my $client = shift;
615         my $method = shift;
616
617         $method = undef if ($self->api_name =~ /all$/o);
618
619         for my $api_level ( reverse(1 .. $#_METHODS) ) {
620                 for my $api_name ( sort keys %{$_METHODS[$api_level]} ) {
621                         if (!$_METHODS[$api_level]{$api_name}{remote}) {
622                                 if (defined($method)) {
623                                         if ($api_name =~ $method) {
624                                                 $client->respond( $_METHODS[$api_level]{$api_name} );
625                                         }
626                                 } else {
627                                         $log->debug( "Returning definition for method [$api_name]", INTERNAL );
628                                         $client->respond( $_METHODS[$api_level]{$api_name} );
629                                         $log->debug( "responed with definition for method [$api_name]", INTERNAL );
630                                 }
631                         }
632                 }
633         }
634
635         return undef;
636 }
637 __PACKAGE__->register_method(
638         stream => 1,
639         method => 'introspect',
640         api_name => 'opensrf.system.method.all',
641         argc => 0,
642         signature => {
643                 desc => q/This method is used to introspect an entire OpenSRF Application/,
644                 return => {
645                         desc => q/A stream of objects describing the methods available via this OpenSRF Application/,
646                         type => 'object'
647                 }
648         },
649 );
650 __PACKAGE__->register_method(
651         stream => 1,
652         method => 'introspect',
653         argc => 1,
654         api_name => 'opensrf.system.method',
655         argc => 1,
656         signature => {
657                 desc => q/Use this method to get the definition of a single OpenSRF Method/,
658                 params => [
659                         { desc => q/The method to introspect/,
660                           type => 'string' },
661                 ],
662                 return => { desc => q/An object describing the method requested, or an error if it can't be found/,
663                             type => 'object' }
664         },
665 );
666
667 sub echo_method {
668         my $self = shift;
669         my $client = shift;
670         my @args = @_;
671
672         $client->respond( $_ ) for (@args);
673         return undef;
674 }
675 __PACKAGE__->register_method(
676         stream => 1,
677         method => 'echo_method',
678         argc => 1,
679         api_name => 'opensrf.system.echo',
680         signature => {
681                 desc => q/A test method that will echo back it's arguments in a streaming response/,
682                 params => [
683                         { desc => q/One or more arguments to echo back/ }
684                 ],
685                 return => { desc => q/A stream of the arguments passed/ }
686         },
687 );
688
689 sub make_stream_atomic {
690         my $self = shift;
691         my $req = shift;
692         my @args = @_;
693
694         (my $m_name = $self->api_name) =~ s/\.atomic$//o;
695         my @results = $self->method_lookup($m_name)->run(@args);
696
697         return \@results;
698 }
699
700
701 1;
702
703