]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/AddedContent.pm
printing on demand instead of at the end
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / AddedContent.pm
1 package OpenILS::WWW::AddedContent;
2 use strict; use warnings;
3
4 use lib qw(/usr/lib/perl5/Bundle/);
5
6 use CGI;
7 use Apache2 ();
8 use Apache2::Log;
9 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
10 use APR::Const    -compile => qw(:error SUCCESS);
11 use Apache2::RequestRec ();
12 use Apache2::RequestIO ();
13 use Apache2::RequestUtil;
14 use Data::Dumper;
15
16 use OpenSRF::EX qw(:try);
17 use OpenSRF::Utils::Cache;
18 use OpenSRF::System;
19 use OpenSRF::Utils::Logger qw/$logger/;
20 use XML::LibXML;
21
22
23 # set the bootstrap config when this module is loaded
24 my $bs_config;
25 my $handler;
26
27 sub import {
28         my $self = shift;
29         $bs_config = shift;
30 }
31
32
33 my $net_timeout;
34 my $cache;
35 sub child_init {
36
37         OpenSRF::System->bootstrap_client( config_file => $bs_config );
38
39         my $sclient = OpenSRF::Utils::SettingsClient->new();
40         my $ac_data = $sclient->config_value("added_content");
41
42     return unless $ac_data;
43
44     $cache = OpenSRF::Utils::Cache->new;
45
46         my $ac_handler = $ac_data->{module};
47     $net_timeout = $ac_data->{timeout} || 3;
48     
49         return unless $ac_handler;
50
51         $logger->debug("Attempting to load Added Content handler: $ac_handler");
52
53         eval "use $ac_handler";
54
55         if($@) {        
56                 $logger->error("Unable to load Added Content handler [$ac_handler]: $@"); 
57                 return; 
58         }
59
60         $handler = $ac_handler->new($ac_data);
61         $logger->debug("added content loaded handler: $handler");
62 }
63
64
65 sub handler {
66
67         my $r           = shift;
68         my $cgi = CGI->new;
69         my $path = $r->path_info;
70
71         child_init() unless $handler; # why isn't apache doing this for us?
72         return Apache2::Const::NOT_FOUND unless $handler;
73
74     # if this memcache key is set, added content lookups are disabled
75         if( $cache->get_cache('ac.no_lookup') ) {
76         $logger->info("added content lookup disabled");
77             return Apache2::Const::NOT_FOUND;
78     }
79
80
81         my( undef, $data, $format, $key ) = split(/\//, $r->path_info);
82
83         my $err;
84         my $success;
85         my $method = "${data}_${format}";
86
87         try {
88                 $success = $handler->$method($key);
89         } catch Error with {
90                 my $err = shift;
91                 $logger->error("added content handler failed: $method($key) => $err");
92         };
93
94         return Apache2::Const::NOT_FOUND if $err or !$success;
95         return Apache2::Const::OK;
96 }
97
98
99
100 # generic GET call
101 sub get_url {
102         my( $self, $url ) = @_;
103         $logger->info("added content getting [timeout=$net_timeout] URL = $url");
104         my $agent = LWP::UserAgent->new(timeout => $net_timeout);
105         my $res = $agent->get($url);
106         die "added content request failed: " . $res->status_line ."\n" unless $res->is_success;
107         return $res->content;
108 }
109
110
111
112
113
114
115 1;
116