]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent.pm
LP 851915: Remove references to /openils/lib/perl5
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / AddedContent.pm
1 package OpenILS::WWW::AddedContent;
2 use strict; use warnings;
3
4 use CGI;
5 use Apache2::Log;
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;
11 use Data::Dumper;
12 use UNIVERSAL::require;
13
14 use OpenSRF::EX qw(:try);
15 use OpenSRF::Utils::Cache;
16 use OpenSRF::System;
17 use OpenSRF::Utils::Logger qw/$logger/;
18
19 use LWP::UserAgent;
20 use MIME::Base64;
21
22 my $AC = __PACKAGE__;
23
24
25 # set the bootstrap config when this module is loaded
26 my $bs_config;
27
28 sub import {
29     my $self = shift;
30     $bs_config = shift;
31 }
32
33
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
39
40 # number of seconds to wait before next lookup 
41 # is attempted after lookups have been disabled
42 my $error_retry_timeout;
43
44
45 sub child_init {
46
47     OpenSRF::System->bootstrap_client( config_file => $bs_config );
48     $cache = OpenSRF::Utils::Cache->new;
49
50     my $sclient = OpenSRF::Utils::SettingsClient->new();
51     my $ac_data = $sclient->config_value("added_content");
52
53     return unless $ac_data;
54     my $ac_handler = $ac_data->{module};
55     return unless $ac_handler;
56
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;
60
61     $logger->debug("Attempting to load Added Content handler: $ac_handler");
62
63     $ac_handler->use;
64
65     if($@) {    
66         $logger->error("Unable to load Added Content handler [$ac_handler]: $@"); 
67         return; 
68     }
69
70     $handler = $ac_handler->new($ac_data);
71     $logger->debug("added content loaded handler: $handler");
72 }
73
74
75 sub handler {
76
77     my $r   = shift;
78     return Apache2::Const::DECLINED if (-e $r->filename);
79
80     my $cgi = CGI->new;
81     my $path = $r->path_info;
82     my $res;
83
84     my( undef, $type, $format, $key ) = split(/\//, $r->path_info);
85
86     child_init() unless $handler;
87
88     return Apache2::Const::NOT_FOUND unless $handler and $type and $format and $key;
89
90     my $err;
91     my $data;
92     my $method = "${type}_${format}";
93
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;
97
98     try {
99         $data = $handler->$method($key);
100     } catch Error with { 
101         $err = shift; 
102         decr_error_countdown();
103         $logger->debug("added content handler failed: $method($key) => $err");
104     };
105
106     return Apache2::Const::NOT_FOUND if $err;
107
108     if(!$data) {
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;
113     }
114     
115     $AC->print_content($data);
116     $AC->cache_result($type, $format, $key, $data);
117
118     reset_error_countdown();
119     return Apache2::Const::OK;
120 }
121
122 sub print_content {
123     my($class, $data, $from_cache) = @_;
124     return Apache2::Const::NOT_FOUND if $data->{nocontent};
125
126     my $ct = $data->{content_type};
127     my $content = $data->{content};
128     print "Content-type: $ct\n\n";
129
130     if($data->{binary}) {
131         binmode STDOUT;
132         # if it hasn't been cached yet, it's still in binary form
133         print( ($from_cache) ? decode_base64($content) : $content );
134     } else {
135         print $content;
136     }
137
138
139     return Apache2::Const::OK;
140 }
141
142
143
144
145 # returns an HTPP::Response object
146 sub get_url {
147     my( $self, $url ) = @_;
148
149     $logger->info("added content getting [timeout=$net_timeout, errors_remaining=$error_countdown] URL = $url");
150     my $agent = LWP::UserAgent->new(timeout => $net_timeout);
151
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;
155
156     return $res;
157 }
158
159 sub lookups_enabled {
160     if( $cache->get_cache('ac.no_lookup') ) {
161         $logger->info("added content lookup disabled");
162         return undef;
163     }
164     return 1;
165 }
166
167 sub disable_lookups {
168     $cache->put_cache('ac.no_lookup', 1, $error_retry_timeout);
169 }
170
171 sub decr_error_countdown {
172     $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;
176     }
177 }
178
179 sub reset_error_countdown {
180     $error_countdown = $max_errors;
181 }
182
183 sub cache_result {
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);
188 }
189
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);
196 }
197
198
199
200 1;