1 package OpenILS::WWW::AddedContent;
2 use strict; use warnings;
4 use lib qw(/usr/lib/perl5/Bundle/);
8 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
9 use APR::Const -compile => qw(:error SUCCESS);
10 use Apache2::RequestRec ();
11 use Apache2::RequestIO ();
12 use Apache2::RequestUtil;
14 use UNIVERSAL::require;
16 use OpenSRF::EX qw(:try);
17 use OpenSRF::Utils::Cache;
19 use OpenSRF::Utils::Logger qw/$logger/;
27 # set the bootstrap config when this module is loaded
36 my $handler; # added content handler class handle
37 my $cache; # memcache handle
38 my $net_timeout; # max seconds to wait for a response from the added content vendor
39 my $max_errors; # max consecutive lookup failures before added content is temporarily disabled
40 my $error_countdown; # current consecutive errors countdown
42 # number of seconds to wait before next lookup
43 # is attempted after lookups have been disabled
44 my $error_retry_timeout;
49 OpenSRF::System->bootstrap_client( config_file => $bs_config );
50 $cache = OpenSRF::Utils::Cache->new;
52 my $sclient = OpenSRF::Utils::SettingsClient->new();
53 my $ac_data = $sclient->config_value("added_content");
55 return unless $ac_data;
56 my $ac_handler = $ac_data->{module};
57 return unless $ac_handler;
59 $net_timeout = $ac_data->{timeout} || 1;
60 $error_countdown = $max_errors = $ac_data->{max_errors} || 10;
61 $error_retry_timeout = $ac_data->{retry_timeout} || 600;
63 $logger->debug("Attempting to load Added Content handler: $ac_handler");
68 $logger->error("Unable to load Added Content handler [$ac_handler]: $@");
72 $handler = $ac_handler->new($ac_data);
73 $logger->debug("added content loaded handler: $handler");
80 return Apache2::Const::DECLINED if (-e $r->filename);
83 my $path = $r->path_info;
86 my( undef, $type, $format, $key ) = split(/\//, $r->path_info);
88 child_init() unless $handler;
90 return Apache2::Const::NOT_FOUND unless $handler and $type and $format and $key;
94 my $method = "${type}_${format}";
96 return Apache2::Const::NOT_FOUND unless $handler->can($method);
97 return $res if defined($res = $AC->serve_from_cache($type, $format, $key));
98 return Apache2::Const::NOT_FOUND unless $AC->lookups_enabled;
101 $data = $handler->$method($key);
104 decr_error_countdown();
105 $logger->debug("added content handler failed: $method($key) => $err");
108 return Apache2::Const::NOT_FOUND if $err;
111 # if the AC lookup found no corresponding data, cache that information
112 $logger->debug("added content handler returned no results $method($key)") unless $data;
113 $AC->cache_result($type, $format, $key, {nocontent=>1});
114 return Apache2::Const::NOT_FOUND;
117 $AC->print_content($data);
118 $AC->cache_result($type, $format, $key, $data);
120 reset_error_countdown();
121 return Apache2::Const::OK;
125 my($class, $data, $from_cache) = @_;
126 return Apache2::Const::NOT_FOUND if $data->{nocontent};
128 my $ct = $data->{content_type};
129 my $content = $data->{content};
130 print "Content-type: $ct\n\n";
132 if($data->{binary}) {
134 # if it hasn't been cached yet, it's still in binary form
135 print( ($from_cache) ? decode_base64($content) : $content );
141 return Apache2::Const::OK;
147 # returns an HTPP::Response object
149 my( $self, $url ) = @_;
151 $logger->info("added content getting [timeout=$net_timeout, errors_remaining=$error_countdown] URL = $url");
152 my $agent = LWP::UserAgent->new(timeout => $net_timeout);
154 my $res = $agent->get($url);
155 $logger->info("added content request returned with code " . $res->code);
156 die "added content request failed: " . $res->status_line ."\n" unless $res->is_success;
161 sub lookups_enabled {
162 if( $cache->get_cache('ac.no_lookup') ) {
163 $logger->info("added content lookup disabled");
169 sub disable_lookups {
170 $cache->put_cache('ac.no_lookup', 1, $error_retry_timeout);
173 sub decr_error_countdown {
175 if($error_countdown < 1) {
176 $logger->warn("added content error count exhausted. Disabling lookups for $error_retry_timeout seconds");
177 $AC->disable_lookups;
181 sub reset_error_countdown {
182 $error_countdown = $max_errors;
186 my($class, $type, $format, $key, $data) = @_;
187 $logger->debug("caching $type/$format/$key");
188 $data->{content} = encode_base64($data->{content}) if $data->{binary};
189 return $cache->put_cache("ac.$type.$format.$key", $data);
192 sub serve_from_cache {
193 my($class, $type, $format, $key) = @_;
194 my $data = $cache->get_cache("ac.$type.$format.$key");
195 return undef unless $data;
196 $logger->debug("serving $type/$format/$key from cache");
197 return $class->print_content($data, 1);