--- /dev/null
+package OpenILS::WWW::AddedContent;
+use strict; use warnings;
+
+use lib qw(/usr/lib/perl5/Bundle/);
+
+use CGI;
+use Apache2 ();
+use Apache2::Log;
+use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
+use APR::Const -compile => qw(:error SUCCESS);
+use Apache2::RequestRec ();
+use Apache2::RequestIO ();
+use Apache2::RequestUtil;
+use Data::Dumper;
+
+use OpenSRF::EX qw(:try);
+use OpenSRF::Utils::Cache;
+use OpenSRF::System;
+use OpenSRF::Utils::Logger qw/$logger/;
+use XML::LibXML;
+
+
+# set the bootstrap config when this module is loaded
+my $bs_config;
+my $handler;
+
+sub import {
+ my $self = shift;
+ $bs_config = shift;
+}
+
+
+sub child_init {
+
+ OpenSRF::System->bootstrap_client( config_file => $bs_config );
+
+ my $sclient = OpenSRF::Utils::SettingsClient->new();
+ my $ac_data = $sclient->config_value("added_content");
+ my $ac_handler = $ac_data->{module};
+ return unless $ac_handler;
+
+ $logger->debug("Attempting to load Added Content handler: $ac_handler");
+
+ eval "use $ac_handler";
+
+ if($@) {
+ $logger->error("Unable to load Added Content handler [$ac_handler]: $@");
+ return;
+ }
+
+ $handler = $ac_handler->new($ac_data);
+ $logger->debug("added content loaded handler: $handler");
+}
+
+
+sub handler {
+
+ my $r = shift;
+ my $cgi = CGI->new;
+ my $path = $r->path_info;
+
+ child_init() unless $handler; # why isn't apache doing this for us?
+
+ my( undef, $data, $format, $key ) = split(/\//, $r->path_info);
+
+ my $err;
+ my $success;
+ my $method = "${data}_${format}";
+
+ try {
+ $success = $handler->$method($key);
+ } catch Error with {
+ my $err = shift;
+ $logger->error("added content handler failed: $method($key) => $err");
+ };
+
+ return Apache2::Const::NOT_FOUND if $err or !$success;
+ return Apache2::Const::OK;
+}
+
+
+
+1;
+
--- /dev/null
+package OpenILS::WWW::AddedContent::Syndetic;
+use strict; use warnings;
+use LWP::UserAgent;
+use OpenSRF::Utils::Logger qw/$logger/;
+use OpenSRF::Utils::SettingsParser;
+use JSON;
+
+
+sub new {
+ my( $class, $args ) = @_;
+ $class = ref $class || $class;
+ return bless($args, $class);
+}
+
+sub base_url {
+ my $self = shift;
+ return $self->{base_url};
+}
+
+sub userid {
+ my $self = shift;
+ return $self->{userid};
+}
+
+
+# --------------------------------------------------------------------------
+
+sub toc_html {
+ my( $self, $key ) = @_;
+ return $self->handle_html(
+ $self->fetch_content('toc.html', $key));
+}
+
+sub toc_xml {
+ my( $self, $key ) = @_;
+ return $self->handle_xml(
+ $self->fetch_content('toc.xml', $key));
+}
+
+sub toc_json {
+ my( $self, $key ) = @_;
+ return $self->handle_json(
+ $self->fetch_content('toc.xml', $key));
+}
+
+
+# --------------------------------------------------------------------------
+
+sub excerpt_html {
+ my( $self, $key ) = @_;
+ return $self->handle_html(
+ $self->fetch_content('dbchapter.html', $key));
+}
+
+sub excerpt_xml {
+ my( $self, $key ) = @_;
+ return $self->handle_xml(
+ $self->fetch_content('dbchapter.xml', $key));
+}
+
+sub excerpt_json {
+ my( $self, $key ) = @_;
+ return $self->handle_json(
+ $self->fetch_content('dbchapter.xml', $key));
+}
+
+
+# --------------------------------------------------------------------------
+
+sub handle_json {
+ my( $self, $xml ) = @_;
+ return 0 if $xml =~ m/<title>error<\/title>/og;
+ my $doc = XML::LibXML->new->parse_string($xml);
+ return 0 unless $doc;
+ my $perl = OpenSRF::Utils::SettingsParser::XML2perl($doc->documentElement);
+ my $json = JSON->perl2JSON($perl);
+ print "Content-type: text/plain\n\n";
+ print $json;
+ return 1;
+}
+
+sub handle_xml {
+ my( $self, $xml ) = @_;
+ return 0 if $xml =~ m/<title>error<\/title>/og;
+ print "Content-Type: application/xml\n\n";
+ print $xml;
+ return 1;
+}
+
+
+sub handle_html {
+ my( $self, $content ) = @_;
+ return 0 if $content =~ m/<title>error<\/title>/og;
+
+ # Strip images because they lead to broken links
+ $content =~ s#<img.*?>.*?</img>##iog;
+ $content =~ s#<img.*?/>##iog;
+ $content =~ s#<img.*?>##iog; # - it may not be valid xml
+
+ print "Content-type: text/html\n\n";
+ print $content;
+
+ return 1;
+}
+
+sub fetch_content {
+ my( $self, $page, $key ) = @_;
+ my $uname = $self->userid;
+ my $url = $self->base_url . "?isbn=$key/$page&client=$uname&type=rw12";
+ $logger->info("added content URL = $url");
+ my $agent = LWP::UserAgent->new;
+ my $res = $agent->get($url);
+ die "added content request failed: $res->status_line\n" unless $res->is_success;
+ return $res->content;
+}
+
+
+1;