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/;
18 use OpenILS::Utils::CStoreEditor;
29 # set the bootstrap config when this module is loaded
38 my $handler; # added content handler class handle
39 my $cache; # memcache handle
40 my $net_timeout; # max seconds to wait for a response from the added content vendor
41 my $max_errors; # max consecutive lookup failures before added content is temporarily disabled
42 my $error_countdown; # current consecutive errors countdown
44 # number of seconds to wait before next lookup
45 # is attempted after lookups have been disabled
46 my $error_retry_timeout;
51 OpenSRF::System->bootstrap_client( config_file => $bs_config );
52 $cache = OpenSRF::Utils::Cache->new;
54 my $sclient = OpenSRF::Utils::SettingsClient->new();
55 my $ac_data = $sclient->config_value("added_content");
57 return Apache2::Const::OK unless $ac_data;
58 my $ac_handler = $ac_data->{module};
59 return Apache2::Const::OK unless $ac_handler;
61 $net_timeout = $ac_data->{timeout} || 1;
62 $error_countdown = $max_errors = $ac_data->{max_errors} || 10;
63 $error_retry_timeout = $ac_data->{retry_timeout} || 600;
65 $logger->debug("Attempting to load Added Content handler: $ac_handler");
70 $logger->error("Unable to load Added Content handler [$ac_handler]: $@");
71 return Apache2::Const::OK;
74 $handler = $ac_handler->new($ac_data);
75 $logger->debug("added content loaded handler: $handler");
76 return Apache2::Const::OK;
84 # If the URL requested matches a file on the filesystem, have Apache serve that file
85 # this allows for local content (most typically images) to be used for some requests
86 return Apache2::Const::DECLINED if (-e $r->filename);
89 my @path_parts = split( /\//, $r->unparsed_uri );
91 # Intended URL formats
92 # /opac/extras/ac/jacket/medium/ISBN_VALUE -- classic keyed-off-isbn
93 # /opac/extras/ac/-3/-2/-1
94 # /opac/extras/ac/jacket/medium/r/RECORD_ID -- provide record id (bre.id)
95 # /opac/extras/ac/-4/-3/-2/-1
96 # /opac/extras/ac/jacket/medium/m/RECORD_ID -- XXX: future use for metarecord id
98 my $keytype_in_url = $path_parts[-2]; # if not in one of m, r, this will be the $format
105 if ($keytype_in_url =~ m/^(r|m)$/) {
106 $type = $path_parts[-4];
107 $format = $path_parts[-3];
108 $keyvalue = $path_parts[-1]; # a record/metarecord id
111 $type = $path_parts[-3];
112 $format = $path_parts[-2];
113 $keyvalue = $path_parts[-1]; # an isbn
121 $cachekey = ($keytype eq "isbn") ? $keyvalue : $keytype . '_' . $keyvalue;
123 child_init() unless $handler;
125 return Apache2::Const::NOT_FOUND unless $handler and $type and $format and $cachekey;
129 my $method = "${type}_${format}";
131 return Apache2::Const::NOT_FOUND unless $handler->can($method);
132 return $res if defined($res = $AC->serve_from_cache($type, $format, $cachekey));
133 return Apache2::Const::NOT_FOUND unless $AC->lookups_enabled;
135 if ($keytype eq "isbn") { # if this request uses isbn for the key
136 # craft a hash with the single isbn, because that's all we will have
138 $keyhash->{"isbn"} = [$keyvalue];
140 my $key_data = get_rec_keys($keyvalue);
141 my @isbns = grep {$_->{tag} eq '020'} @$key_data;
142 my @issns = grep {$_->{tag} eq '022'} @$key_data;
143 my @upcs = grep {$_->{tag} eq '024'} @$key_data;
146 my $isbn_obj = Business::ISBN->new($_->{value});
148 $isbn_str = $isbn_obj->as_string([]) if defined($isbn_obj);
149 $_->{value} = $isbn_str;
150 undef $_ if !defined($_->{value});
154 my $issn_obj = Business::ISSN->new($_->{value});
156 $issn_str = $issn_obj->as_string() if defined($issn_obj && $issn_obj->is_valid);
157 $_->{value} = $issn_str;
158 undef $_ if !defined($_->{value});
162 isbn => [map {$_->{value}} @isbns],
163 issn => [map {$_->{value}} @issns],
164 upc => [map {$_->{value}} @upcs]
168 return Apache2::Const::NOT_FOUND unless @{$keyhash->{isbn}} || @{$keyhash->{issn}} || @{$keyhash->{upc}};
171 if ($handler->can('expects_keyhash') && $handler->expects_keyhash() eq 1) {
172 # Handler expects a keyhash
173 $data = $handler->$method($keyhash);
175 # Pass single ISBN as a scalar to the handler
176 $data = $handler->$method($keyhash->{isbn}[0]);
180 decr_error_countdown();
181 $logger->debug("added content handler failed: $method($keytype/$keyvalue) => $err"); # XXX: logs unhelpful hashref
184 return Apache2::Const::NOT_FOUND if $err;
187 # if the AC lookup found no corresponding data, cache that information
188 $logger->debug("added content handler returned no results $method($keytype/$keyvalue)") unless $data;
189 $AC->cache_result($type, $format, $cachekey, {nocontent=>1});
190 return Apache2::Const::NOT_FOUND;
193 $AC->print_content($data);
194 $AC->cache_result($type, $format, $cachekey, $data);
196 reset_error_countdown();
197 return Apache2::Const::OK;
200 # returns [{tag => $tag, value => $value}, {tag => $tag2, value => $value2}]
203 return OpenILS::Utils::CStoreEditor->new->json_query({
204 select => {mfr => ['tag', 'value']},
229 { class => 'mfr', field => 'id' }
235 my($class, $data, $from_cache) = @_;
236 return Apache2::Const::NOT_FOUND if $data->{nocontent};
238 my $ct = $data->{content_type};
239 my $content = $data->{content};
240 print "Content-type: $ct\n\n";
242 if($data->{binary}) {
244 # if it hasn't been cached yet, it's still in binary form
245 print( ($from_cache) ? decode_base64($content) : $content );
251 return Apache2::Const::OK;
257 # returns an HTPP::Response object
259 my( $self, $url ) = @_;
261 $logger->info("added content getting [timeout=$net_timeout, errors_remaining=$error_countdown] URL = $url");
262 my $agent = LWP::UserAgent->new(timeout => $net_timeout);
264 my $res = $agent->get($url);
265 $logger->info("added content request returned with code " . $res->code);
266 die "added content request failed: " . $res->status_line ."\n" unless $res->is_success;
271 sub lookups_enabled {
272 if( $cache->get_cache('ac.no_lookup') ) {
273 $logger->info("added content lookup disabled");
279 sub disable_lookups {
280 $cache->put_cache('ac.no_lookup', 1, $error_retry_timeout);
283 sub decr_error_countdown {
285 if($error_countdown < 1) {
286 $logger->warn("added content error count exhausted. Disabling lookups for $error_retry_timeout seconds");
287 $AC->disable_lookups;
291 sub reset_error_countdown {
292 $error_countdown = $max_errors;
296 my($class, $type, $format, $key, $data) = @_;
297 $logger->debug("caching $type/$format/$key");
298 $data->{content} = encode_base64($data->{content}) if $data->{binary};
299 return $cache->put_cache("ac.$type.$format.$key", $data);
302 sub serve_from_cache {
303 my($class, $type, $format, $key) = @_;
304 my $data = $cache->get_cache("ac.$type.$format.$key");
305 return undef unless $data;
306 $logger->debug("serving $type/$format/$key from cache");
307 return $class->print_content($data, 1);