package Alex::Dereference; # Dereference.pm - Redirect user-agents based on value of URI. Specifically, # http://infomotions.com/etexts/page/etext5813 returns an HTML page, # http://infomotions.com/etexts/data/etext5813 returns RDF, # http://infomotions.com/etexts/concordance/etext5813 redirects to a concordance, and # http://infomotions.com/etexts/id/etext5813 does content negotiation returning HTML or RDF accordingly. # Eric Lease Morgan # July 22, 2009 - Minting "cool" URLs for the purposes of Linked Data # July 23, 2009 - renamed types # August 20, 2010 - corrected content types # define use constant CONCORDANCE => 'http://infomotions.com/sandbox/concordance/?id='; use constant INSTANCE => 'alex3'; use constant URL => 73038; # require use Apache2::Const -compile => qw( OK ); use CGI; use MyLibrary::Core; use strict; # main sub handler { # initialize my $r = shift; my $cgi = CGI->new; MyLibrary::Config->instance( INSTANCE ); # parse the uri my $type = ( split '/', $r->location )[2]; my $fkey = substr( $r->uri, length $r->location ); # get the resource and sanity check my $resource = MyLibrary::Resource->new( fkey => $fkey ); if ( ! $resource ) { print $cgi->header( -status => '404 Resource not found' ) } # process each type else { # get the resource's locations my @locations = $resource->resource_locations; my $url = ''; my $rdf = ''; foreach ( @locations ) { if ( $_->resource_location_type == URL ) { $url = $_->location; $rdf = $url; $rdf =~ s/htm$/rdf/; } } # id; content negotiation if ( $type eq 'id' ) { # wants HTML if ( $cgi->Accept( 'text/html' )) { print $cgi->header( -status => '303 See Other', -Location => $url, -Vary => 'Accept' ) } # give them RDF else { print $cgi->header( -status => '303 See Other', -Location => $rdf, -Vary => 'Accept', "Content-Type" => 'application/rdf+xml' )} } # data; rdf elsif ( $type eq 'data' ) { print $cgi->header( -status => '303 See Other', -Location => $rdf, -Vary => 'Accept', "Content-Type" => 'application/rdf+xml' ) } # page; html elsif ( $type eq 'page' ) { print $cgi->header( -status => '303 See Other', -Location => $url, -Vary => 'Accept', "Content-Type" => 'text/html' ) } # concordance; for students and scholars elsif ( $type eq 'concordance' ) { print $cgi->header( -status => '303 See Other', -Location => CONCORDANCE . $fkey, -Vary => 'Accept', "Content-Type" => 'text/html' ) } } # done return Apache2::Const::OK; } # return true or die 1;