modules used for the opac
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 25 Mar 2005 15:55:47 +0000 (15:55 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 25 Mar 2005 15:55:47 +0000 (15:55 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@400 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/WWW/Method.pm [new file with mode: 0644]
Open-ILS/src/perlmods/OpenILS/WWW/Web.pm [new file with mode: 0644]

diff --git a/Open-ILS/src/perlmods/OpenILS/WWW/Method.pm b/Open-ILS/src/perlmods/OpenILS/WWW/Method.pm
new file mode 100644 (file)
index 0000000..43be50b
--- /dev/null
@@ -0,0 +1,128 @@
+package OpenILS::WWW::Method;
+use strict; use warnings;
+
+use Apache2 ();
+use Apache::Log;
+use Apache::Const -compile => qw(OK REDIRECT :log);
+use APR::Const    -compile => qw(:error SUCCESS);
+use Apache::RequestRec ();
+use Apache::RequestIO ();
+use Apache::RequestUtil;
+
+use JSON;
+
+use CGI ();
+
+use OpenSRF::EX qw(:try);
+use OpenSRF::System;
+
+my %session_hash;
+
+use constant MAX_SESSION_REQUESTS => 20;
+
+sub handler {
+
+       my $apache = shift;
+       my $cgi = CGI->new( $apache );
+
+       my $method = $cgi->param("method");
+       my $service = $cgi->param("service");
+
+       my @a = $cgi->param();
+
+       my @param_array;
+       my %param_hash;
+
+       if(defined($cgi->param("__param"))) {
+               for my $param ( $cgi->param("__param")) {
+                       push( @param_array, JSON->JSON2perl( $param ));
+               }
+       } else {
+               for my $param ($cgi->param()) {
+                       $param_hash{$param} = $cgi->param($param)
+                               unless( $param eq "method" or $param eq "service" );
+               }
+       }
+
+       print "Content-type: text/plain; charset=utf-8\n\n";
+
+       if( @param_array ) {
+               perform_method($service, $method, @param_array);
+       } else {
+               perform_method($service, $method, %param_hash);
+       }
+
+       use Data::Dumper;
+       warn JSON->perl2JSON( \@param_array );
+       warn "===============\n";
+       warn Dumper \@param_array;
+
+       return Apache::OK;
+}
+
+sub child_init_handler {
+       OpenSRF::System->bootstrap_client( 
+                       config_file => "/pines/conf/bootstrap.conf" );
+}
+
+
+sub perform_method {
+
+       my ($service, $method, @params) = @_;
+
+       warn "performing method $method for service $service with params @params\n";
+
+       my $session;
+
+       if($session_hash{$service} ) {
+
+               $session = $session_hash{$service};
+               $session->{web_count} += 1;
+
+               if( $session->{web_count} > MAX_SESSION_REQUESTS) {
+                       $session->disconnect();
+                       $session->{web_count} = 1;
+               }
+
+       } else { 
+
+               $session = OpenSRF::AppSession->create($service); 
+               $session_hash{$service} = $session;
+               $session->{web_count} = 1;
+
+       }
+
+       my $request = $session->request( $method, @params );
+
+       my @results;
+       while( my $response = $request->recv(20) ) {
+               
+               if( UNIVERSAL::isa( $response, "Error" )) {
+                       warn "Received exception: " . $response->stringify . "\n";
+                       print  JSON->perl2JSON($response->stringify);
+                       $request->finish();
+                       return 0;
+               }
+
+               my $content = $response->content;
+               push @results, $content;
+       }
+
+
+       if(!$request->complete) { 
+               warn "<b>ERROR Completing Request</b>"; 
+               print JSON->perl2JSON("<b>ERROR Completing Request</b>"); 
+               $request->finish();
+               return 0;
+       }
+
+       $request->finish();
+       $session->finish();
+
+       print JSON->perl2JSON( \@results );
+
+       return 1;
+}
+
+
+1;
diff --git a/Open-ILS/src/perlmods/OpenILS/WWW/Web.pm b/Open-ILS/src/perlmods/OpenILS/WWW/Web.pm
new file mode 100644 (file)
index 0000000..afd6ce8
--- /dev/null
@@ -0,0 +1,90 @@
+package OpenILS::WWW::Web;
+use strict; use warnings;
+
+use Apache2 ();
+use Apache::Log;
+use Apache::Const -compile => qw(OK REDIRECT :log);
+use APR::Const    -compile => qw(:error SUCCESS);
+use Apache::RequestRec ();
+use Apache::RequestIO ();
+use Apache::RequestUtil;
+
+use CGI ();
+use Template qw(:template);
+
+use OpenSRF::EX qw(:try);
+use OpenSRF::System;
+
+my $main_ttk = "opac/page_router.ttk";
+my $error_ttk = "opac/error.ttk";
+my $init_ttk = "opac/page_init.ttk";
+my $bootstrap = "/pines/conf/bootstrap.conf";
+my $child_init_ttk = "opac/child_init.ttk";
+
+my $includes = [  
+                               '/pines/cvs/ILS/Open-ILS/src/templates'
+                       ];
+
+my $plugin_base = 'OpenILS::Template::Plugin';
+
+sub handler {
+
+       my $apache = shift;
+       print "Content-type: text/html; charset=utf-8\n\n";
+
+       _process_template(
+                       apache          => $apache,
+                       template                => $main_ttk,
+                       pre_process     => $init_ttk );
+
+       return Apache::OK;
+}
+
+sub child_init_handler {
+       _process_template(  template => $child_init_ttk );
+}
+
+sub _process_template {
+
+       my %params = @_;
+       my $ttk                         = $params{template}             || return undef;
+       my $apache                      = $params{apache}                       || undef;
+       my $pre_process = $params{pre_process}  || undef;
+       my $param_hash          = $params{params}                       || {};
+
+       my $template;
+
+       $template = Template->new( { 
+               OUTPUT                  => $apache, 
+               ABSOLUTE                        => 1, 
+               RELATIVE                        => 1,
+               PLUGIN_BASE             => $plugin_base,
+               PRE_PROCESS             => $pre_process,
+               INCLUDE_PATH    => $includes, 
+               PRE_CHOMP               => 1,
+               POST_CHOMP              => 1,
+               } 
+       );
+
+       try {
+
+               if( ! $template->process( $ttk, $param_hash ) ) { 
+                       warn  "Error Occured: " . $template->error();
+                       my $err = $template->error();
+                       $err =~ s/\n/\<br\/\>/g;
+                       warn "Error processing template $ttk\n";        
+                       my $string =  "<br><b>Unable to process template:<br/><br/> " . $err . "!!!</b>";
+                       $template->process( $error_ttk , { error => $string } );
+               }
+
+       } catch Error with {
+               my $e = shift;
+               warn "Error processing template $ttk:  $e - $@ \n";     
+               print "<center><br/><br/><b>Error<br/><br/> $e <br/><br/> $@ </b><br/></center>";
+               return;
+       };
+
+}
+
+
+1;