1 package OpenILS::WWW::AddedContent;
2 use strict; use warnings;
6 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
7 use APR::Const -compile => qw(:error SUCCESS);
8 use Apache2::RequestRec ();
9 use Apache2::RequestIO ();
10 use Apache2::RequestUtil;
12 use UNIVERSAL::require;
14 use OpenSRF::EX qw(:try);
15 use OpenSRF::Utils::Cache;
17 use OpenSRF::Utils::Logger qw/$logger/;
25 # set the bootstrap config when this module is loaded
34 my $handler; # added content handler class handle
35 my $cache; # memcache handle
36 my $net_timeout; # max seconds to wait for a response from the added content vendor
37 my $max_errors; # max consecutive lookup failures before added content is temporarily disabled
38 my $error_countdown; # current consecutive errors countdown
40 # number of seconds to wait before next lookup
41 # is attempted after lookups have been disabled
42 my $error_retry_timeout;
47 OpenSRF::System->bootstrap_client( config_file => $bs_config );
48 $cache = OpenSRF::Utils::Cache->new;
50 my $sclient = OpenSRF::Utils::SettingsClient->new();
51 my $ac_data = $sclient->config_value("added_content");
53 return unless $ac_data;
54 my $ac_handler = $ac_data->{module};
55 return unless $ac_handler;
57 $net_timeout = $ac_data->{timeout} || 1;
58 $error_countdown = $max_errors = $ac_data->{max_errors} || 10;
59 $error_retry_timeout = $ac_data->{retry_timeout} || 600;
61 $logger->debug("Attempting to load Added Content handler: $ac_handler");
66 $logger->error("Unable to load Added Content handler [$ac_handler]: $@");
70 $handler = $ac_handler->new($ac_data);
71 $logger->debug("added content loaded handler: $handler");
78 return Apache2::Const::DECLINED if (-e $r->filename);
81 my $path = $r->path_info;
84 my( undef, $type, $format, $key ) = split(/\//, $r->path_info);
86 child_init() unless $handler;
88 return Apache2::Const::NOT_FOUND unless $handler and $type and $format and $key;
92 my $method = "${type}_${format}";
94 return Apache2::Const::NOT_FOUND unless $handler->can($method);
95 return $res if defined($res = $AC->serve_from_cache($type, $format, $key));
96 return Apache2::Const::NOT_FOUND unless $AC->lookups_enabled;
99 $data = $handler->$method($key);
102 decr_error_countdown();
103 $logger->debug("added content handler failed: $method($key) => $err");
106 return Apache2::Const::NOT_FOUND if $err;
109 # if the AC lookup found no corresponding data, cache that information
110 $logger->debug("added content handler returned no results $method($key)") unless $data;
111 $AC->cache_result($type, $format, $key, {nocontent=>1});
112 return Apache2::Const::NOT_FOUND;
115 $AC->print_content($data);
116 $AC->cache_result($type, $format, $key, $data);
118 reset_error_countdown();
119 return Apache2::Const::OK;
123 my($class, $data, $from_cache) = @_;
124 return Apache2::Const::NOT_FOUND if $data->{nocontent};
126 my $ct = $data->{content_type};
127 my $content = $data->{content};
128 print "Content-type: $ct\n\n";
130 if($data->{binary}) {
132 # if it hasn't been cached yet, it's still in binary form
133 print( ($from_cache) ? decode_base64($content) : $content );
139 return Apache2::Const::OK;
145 # returns an HTPP::Response object
147 my( $self, $url ) = @_;
149 $logger->info("added content getting [timeout=$net_timeout, errors_remaining=$error_countdown] URL = $url");
150 my $agent = LWP::UserAgent->new(timeout => $net_timeout);
152 my $res = $agent->get($url);
153 $logger->info("added content request returned with code " . $res->code);
154 die "added content request failed: " . $res->status_line ."\n" unless $res->is_success;
159 sub lookups_enabled {
160 if( $cache->get_cache('ac.no_lookup') ) {
161 $logger->info("added content lookup disabled");
167 sub disable_lookups {
168 $cache->put_cache('ac.no_lookup', 1, $error_retry_timeout);
171 sub decr_error_countdown {
173 if($error_countdown < 1) {
174 $logger->warn("added content error count exhausted. Disabling lookups for $error_retry_timeout seconds");
175 $AC->disable_lookups;
179 sub reset_error_countdown {
180 $error_countdown = $max_errors;
184 my($class, $type, $format, $key, $data) = @_;
185 $logger->debug("caching $type/$format/$key");
186 $data->{content} = encode_base64($data->{content}) if $data->{binary};
187 return $cache->put_cache("ac.$type.$format.$key", $data);
190 sub serve_from_cache {
191 my($class, $type, $format, $key) = @_;
192 my $data = $cache->get_cache("ac.$type.$format.$key");
193 return undef unless $data;
194 $logger->debug("serving $type/$format/$key from cache");
195 return $class->print_content($data, 1);