| 1 | package FAIR::AccessorBase; | ||
| 2 | $FAIR::AccessorBase::VERSION = '1.0'; | ||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | # ABSTRACT: The core Accessor functions | ||
| 8 | |||
| 9 | |||
| 10 | use Moose; | ||
| 11 | use URI::Escape; | ||
| 12 | use JSON; | ||
| 13 | use FAIR::AccessorConfig; | ||
| 14 | use RDF::Trine::Parser 0.135; | ||
| 15 | use RDF::Trine::Model 0.135; | ||
| 16 | use RDF::Trine::Statement 0.135; | ||
| 17 | use RDF::Trine::Node::Resource; | ||
| 18 | use RDF::Trine::Node::Literal; | ||
| 19 | use Scalar::Util 'blessed'; | ||
| 20 | use Log::Log4perl; | ||
| 21 | |||
| 22 | |||
| 23 | with 'FAIR::CoreFunctions'; | ||
| 24 | |||
| 25 | has 'Configuration' => ( | ||
| 26 | isa => 'FAIR::AccessorConfig', | ||
| 27 | is => 'rw', | ||
| 28 | ); | ||
| 29 | |||
| 30 | |||
| 31 | around BUILDARGS => sub { | ||
| 32 | my %return; | ||
| 33 | $return{'Configuration'} = FAIR::AccessorConfig->new(@_); | ||
| 34 | return \%return; | ||
| 35 | }; | ||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | # ============================================ | ||
| 42 | # All Daemons must implement this method | ||
| 43 | sub Container { | ||
| 44 | my ( @args ) = @_; | ||
| 45 | |||
| 46 | # user-specific implementation will override this method | ||
| 47 | } | ||
| 48 | |||
| 49 | # ============================================ | ||
| 50 | # Some Daemons may implement this method | ||
| 51 | |||
| 52 | sub MetaRecord { | ||
| 53 | my ( @args ) = @_; | ||
| 54 | |||
| 55 | # user-specific implementation will override this method | ||
| 56 | } | ||
| 57 | |||
| 58 | # ============================================= | ||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | # =============== STAGE 1 Subroutines | ||
| 63 | |||
| 64 | sub manageContainerGET { | ||
| 65 | my ($self, %args) = @_; # %args are PATH => '/some/path' | ||
| 66 | |||
| 67 | unless ($ENV{'SCRIPT_NAME'}) {print STDERR "your servers implementation of CGI does not capture the SCRIPT_NAME; defaulting to REQUEST_URI!";} | ||
| 68 | my $SCRIPT_NAME = $ENV{'SCRIPT_NAME'}?$ENV{'SCRIPT_NAME'}:$ENV{REQUEST_URI}; # best guess! | ||
| 69 | $SCRIPT_NAME =~ s/^\///; # if it is there, get rid of the leading / | ||
| 70 | |||
| 71 | my $BASE_URL = "http://" . $ENV{'SERVER_NAME'} . "/$SCRIPT_NAME"; | ||
| 72 | $BASE_URL .= $ENV{'PATH_INFO'} if $ENV{'PATH_INFO'} ; | ||
| 73 | my $store = RDF::Trine::Store::Memory->new(); | ||
| 74 | my $model = RDF::Trine::Model->new($store); | ||
| 75 | my $ns = $self->Configuration->Namespaces; | ||
| 76 | |||
| 77 | $self->callAccessorContainer($BASE_URL, $model); | ||
| 78 | |||
| 79 | my $statement = statement($BASE_URL, $ns->rdf("type"), $ns->ldp("BasicContainer")); | ||
| 80 | $model->add_statement($statement); | ||
| 81 | |||
| 82 | unless ($model->count_statements(RDF::Trine::Node::Resource->new($BASE_URL), RDF::Trine::Node::Resource->new($ns->dc("title")))){ | ||
| 83 | $statement = statement($BASE_URL, $ns->dc("title"), $self->Configuration->{'title'}); | ||
| 84 | $model->add_statement($statement); | ||
| 85 | } | ||
| 86 | $self->serializeThis($model); | ||
| 87 | |||
| 88 | } | ||
| 89 | |||
| 90 | sub makeSensibleStatement { | ||
| 91 | my ($self, $s, $p, $o) = @_; | ||
| 92 | my ($subject, $predicate, $object); | ||
| 93 | my $NS = $self->Configuration->Namespaces(); | ||
| 94 | |||
| 95 | if (($s =~ /^http:/) || ($s =~ /^https:/)) { | ||
| 96 | $subject = $s; | ||
| 97 | } else { | ||
| 98 | my ($ns, $sub) = split /:/, $s; | ||
| 99 | $subject = $NS->$ns($sub); # add the namespace | ||
| 100 | } | ||
| 101 | |||
| 102 | if (($p =~ /^http:/) || ($p =~ /^https:/)) { | ||
| 103 | $predicate = $p; | ||
| 104 | } else { | ||
| 105 | my ($ns, $pred) = split /:/, $p; | ||
| 106 | $predicate = $NS->$ns($pred); # add the namespace | ||
| 107 | } | ||
| 108 | |||
| 109 | if (($o =~ /^http:/) || ($o =~ /^https:/)) { # if its a URL | ||
| 110 | $object = $o | ||
| 111 | } elsif ((!($o =~ /\s/)) && ($o =~ /\S+:\S+/)){ # if it looks like a qname tag | ||
| 112 | my ($ns,$obj) = split /:/, $o; | ||
| 113 | if ($NS->$ns($obj)) { | ||
| 114 | $object = $NS->$ns($obj); # add the namespace | ||
| 115 | } | ||
| 116 | } else { | ||
| 117 | $object = $o | ||
| 118 | } | ||
| 119 | |||
| 120 | my $statement = statement($subject, $predicate, $object); | ||
| 121 | |||
| 122 | return $statement; | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | sub callAccessorContainer { | ||
| 128 | my ($self, $subject, $model) = @_; | ||
| 129 | |||
| 130 | my ($Container) = $self->Container(); # this subroutine is provided by the end-user in the Accessor script on the web | ||
| 131 | die "Not a FAIR::Accessor::Container" unless $Container->isa("FAIR::Accessor::Container"); | ||
| 132 | |||
| 133 | |||
| 134 | $model->begin_bulk_ops(); | ||
| 135 | my %metadata = %{$Container->MetaData}; | ||
| 136 | |||
| 137 | my $temprdf; # doing this to make the import more efficient... I hope! | ||
| 138 | |||
| 139 | # ADD THE METADATA key/value|array pairs | ||
| 140 | foreach my $CDE(keys %metadata){ | ||
| 141 | |||
| 142 | next unless $metadata{$CDE}; # if it is blank, ignore it | ||
| 143 | |||
| 144 | my $values = $metadata{$CDE}; | ||
| 145 | $values = [$values] unless (ref($values) =~ /ARRAY/); | ||
| 146 | foreach my $value(@$values){ | ||
| 147 | my $statement = $self->makeSensibleStatement($subject, $CDE, $value); | ||
| 148 | my $str = $statement->as_string; # almost n3 format... need to fix it a bit... | ||
| 149 | $str =~ s/^\(triple\s//; | ||
| 150 | $str =~ s/\)$/./; | ||
| 151 | $temprdf .= "$str\n"; # this is RDF in n3 format | ||
| 152 | } | ||
| 153 | |||
| 154 | } | ||
| 155 | |||
| 156 | # ADD THE RECORD IDS with "contains" | ||
| 157 | my @records = @{$Container->Records}; | ||
| 158 | foreach my $rec(@records){ | ||
| 159 | my $statement = $self->makeSensibleStatement($subject, "ldp:contains", $rec); | ||
| 160 | my $str = $statement->as_string; # almost n3 format... need to fix it a bit... | ||
| 161 | $str =~ s/^\(triple\s//; | ||
| 162 | $str =~ s/\)$/./; | ||
| 163 | $temprdf .= "$str\n"; # this is RDF in n3 format | ||
| 164 | } | ||
| 165 | |||
| 166 | my $parser = RDF::Trine::Parser->new( 'ntriples' ); | ||
| 167 | $parser->parse_into_model( "http://example.org/", $temprdf, $model ); | ||
| 168 | |||
| 169 | |||
| 170 | $model->end_bulk_ops(); | ||
| 171 | |||
| 172 | |||
| 173 | if ($Container->FreeFormRDF && blessed($Container->FreeFormRDF) && $Container->FreeFormRDF->isa("RDF::Trine::Model")) { # if they are doing this, they know what they are doing! (we assume) | ||
| 174 | my $iterator = $Container->FreeFormRDF->statements; | ||
| 175 | while (my $stm = $iterator->next()) { | ||
| 176 | $model->add_statement($stm); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | } | ||
| 181 | # ====================== END OF STAGE1 SUBROUTINES | ||
| 182 | |||
| 183 | |||
| 184 | |||
| 185 | |||
| 186 | # ================== Stage 2 subroutines ============= | ||
| 187 | |||
| 188 | sub manageResourceGET { # $self->manageResourceGET('PATH' => $path, 'ID' => $id); | ||
| 189 | my ($self, %ARGS) = @_; | ||
| 190 | my $ID = $ARGS{'ID'}; | ||
| 191 | |||
| 192 | my $store = RDF::Trine::Store::Memory->new(); | ||
| 193 | my $model = RDF::Trine::Model->new($store); | ||
| 194 | |||
| 195 | $self->callDataAccessor($model, $ID); | ||
| 196 | |||
| 197 | $self->serializeThis($model); | ||
| 198 | |||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | sub callDataAccessor { | ||
| 203 | my ($self, $model, $ID) = @_; | ||
| 204 | |||
| 205 | # call out to user-provided subroutine | ||
| 206 | my ($MetaRecord) = $self->MetaRecord('ID' => $ID); # this method is provided (hopefully) by the service provider's Accessor script. | ||
| 207 | # TODO - this should be a catch, not a call... | ||
| 208 | |||
| 209 | |||
| 210 | my $subject = "http://" . $ENV{'SERVER_NAME'} . $ENV{'REQUEST_URI'}; | ||
| 211 | my $NS = $self->Configuration->Namespaces(); | ||
| 212 | |||
| 213 | #---------------------------------------------------------------------------------- | ||
| 214 | # GENERIC METADATA---------------------------------------------------------------- | ||
| 215 | #---------------------------------------------------------------------------------- | ||
| 216 | |||
| 217 | my %metadata = %{$MetaRecord->MetaData}; | ||
| 218 | foreach my $CDE(keys %metadata){ | ||
| 219 | next unless $metadata{$CDE}; # if it is blank, ignore it | ||
| 220 | |||
| 221 | my $values = $metadata{$CDE}; | ||
| 222 | $values = [$values] unless (ref($values) =~ /ARRAY/); | ||
| 223 | foreach my $value(@$values){ | ||
| 224 | my $statement = $self->makeSensibleStatement($subject, $CDE, $value); | ||
| 225 | $model->add_statement($statement); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | #---------------------------------------------------------------------------------- | ||
| 231 | # DISTRIBUTION METADATA --------------------------------------------- | ||
| 232 | #---------------------------------------------------------------------------------- | ||
| 233 | |||
| 234 | my $distributions = $MetaRecord->Distributions(); | ||
| 235 | foreach my $Dist(@$distributions){ | ||
| 236 | my $downloadURL = $Dist->downloadURL(); | ||
| 237 | my $statement = $self->makeSensibleStatement($subject, $NS->dcat('distribution'), $downloadURL); | ||
| 238 | $model->add_statement($statement); | ||
| 239 | my $projector = 0; | ||
| 240 | foreach my $type($Dist->types){ | ||
| 241 | $statement = $self->makeSensibleStatement($downloadURL, $NS->rdf('type'), $type); | ||
| 242 | $model->add_statement($statement); | ||
| 243 | $projector = 1 if ($type =~ /Projector/); # flag it as a projector for the if block below | ||
| 244 | } | ||
| 245 | |||
| 246 | foreach my $form(@{$Dist->availableformats}){ | ||
| 247 | $statement = $self->makeSensibleStatement($downloadURL, $NS->dc('format'), $form); | ||
| 248 | $model->add_statement($statement); | ||
| 249 | } | ||
| 250 | |||
| 251 | $statement = $self->makeSensibleStatement($downloadURL, $NS->dcat('downloadURL'), $downloadURL); | ||
| 252 | $model->add_statement($statement); | ||
| 253 | |||
| 254 | if ($projector) { | ||
| 255 | my $projectionmodel = $Dist->ProjectionModel(); | ||
| 256 | $model->add_iterator($projectionmodel->as_stream); | ||
| 257 | } | ||
| 258 | |||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | |||
| 264 | sub printResourceHeader { | ||
| 265 | my ($self) = @_; | ||
| 266 | my $ETAG = $self->Configuration->ETAG_Base(); | ||
| 267 | my $entity = $ENV{'PATH_INFO'}; | ||
| 268 | $entity =~ s/^\///; | ||
| 269 | # print "Content-Type: text/turtle\n"; | ||
| 270 | print "Content-Type: application/rdf+xml\n"; | ||
| 271 | print "ETag: \"$ETAG"."_"."$entity\"\n"; | ||
| 272 | print "Allow: GET,OPTIONS,HEAD\n"; | ||
| 273 | print 'Link: <http://www.w3.org/ns/ldp#Resource>; rel="type"'."\n\n"; | ||
| 274 | |||
| 275 | } | ||
| 276 | |||
| 277 | sub printContainerHeader { | ||
| 278 | my ($self) = @_; | ||
| 279 | my $ETAG = $self->Configuration->ETAG_Base(); | ||
| 280 | # print "Content-Type: text/turtle\n"; | ||
| 281 | print "Content-Type: application/rdf+xml\n"; | ||
| 282 | print "ETag: \"$ETAG\"\n"; | ||
| 283 | print "Allow: GET,OPTIONS,HEAD\n"; | ||
| 284 | print 'Link: <http://www.w3.org/ns/ldp#BasicContainer>; rel="type",'."\n"; | ||
| 285 | print ' <http://www.w3.org/ns/ldp#Resource>; rel="type"'."\n\n"; | ||
| 286 | # print "Transfer-Encoding: chunked\n\n"; | ||
| 287 | |||
| 288 | } | ||
| 289 | |||
| 290 | sub manageHEAD { | ||
| 291 | my ($self) = @_; | ||
| 292 | my $ETAG = $self->Configuration->ETAG_Base(); | ||
| 293 | |||
| 294 | print "Content-Type: text/turtle\n"; | ||
| 295 | print "ETag: \"$ETAG\"\n"; | ||
| 296 | print "Allow: GET,OPTIONS,HEAD\n\n"; | ||
| 297 | print 'Link: <http://www.w3.org/ns/ldp#BasicContainer>; rel="type",'."\n"; | ||
| 298 | print ' <http://www.w3.org/ns/ldp#Resource>; rel="type"'."\n\n"; | ||
| 299 | |||
| 300 | } | ||
| 301 | |||
| 302 | sub serializeThis{ | ||
| 303 | my ($self, $model) = @_; | ||
| 304 | # my $serializer = RDF::Trine::Serializer->new('turtle'); # the turtle serializer is simply too slow to use... | ||
| 305 | my $serializer = RDF::Trine::Serializer->new('rdfxml'); # TODO - this should work with content negotiation | ||
| 306 | print $serializer->serialize_model_to_string($model); | ||
| 307 | } | ||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | # | ||
| 312 | ## returns the request content type | ||
| 313 | ## defaults to application/rdf+xml | ||
| 314 | #sub get_request_content_type { | ||
| 315 | # my ($self) = @_; | ||
| 316 | # my $CONTENT_TYPE = 'application/rdf+xml'; | ||
| 317 | # if (defined $ENV{CONTENT_TYPE}) { | ||
| 318 | # $CONTENT_TYPE = 'text/rdf+n3' if $ENV{CONTENT_TYPE} =~ m|text/rdf\+n3|gi; | ||
| 319 | # $CONTENT_TYPE = 'text/rdf+n3' if $ENV{CONTENT_TYPE} =~ m|text/n3|gi; | ||
| 320 | # $CONTENT_TYPE = 'application/n-quads' if $ENV{CONTENT_TYPE} =~ m|application/n\-quads|gi; | ||
| 321 | # | ||
| 322 | # } | ||
| 323 | # return $CONTENT_TYPE; | ||
| 324 | #} | ||
| 325 | # | ||
| 326 | ## returns the response requested content type | ||
| 327 | ## defaults to application/rdf+xml | ||
| 328 | #sub get_response_content_type { | ||
| 329 | # my ($self) = @_; | ||
| 330 | # my $CONTENT_TYPE = 'application/rdf+xml'; | ||
| 331 | # if (defined $ENV{HTTP_ACCEPT}) { | ||
| 332 | # $CONTENT_TYPE = 'text/rdf+n3' if $ENV{HTTP_ACCEPT} =~ m|text/rdf\+n3|gi; | ||
| 333 | # $CONTENT_TYPE = 'text/rdf+n3' if $ENV{HTTP_ACCEPT} =~ m|text/n3|gi; | ||
| 334 | # $CONTENT_TYPE = 'application/n-quads' if $ENV{HTTP_ACCEPT} =~ m|application/n\-quads|gi; | ||
| 335 | # | ||
| 336 | # } | ||
| 337 | # return $CONTENT_TYPE; | ||
| 338 | #} | ||
| 339 | |||
| 340 | |||
| 341 | 1; | ||
| 342 | |||
| 343 | __END__ | ||
| 344 | |||
| 345 | =pod | ||
| 346 | |||
| 347 | =encoding UTF-8 | ||
| 348 | |||
| 349 | =head1 NAME | ||
| 350 | |||
| 351 | FAIR::AccessorBase - The core Accessor functions | ||
| 352 | |||
| 353 | =head1 VERSION | ||
| 354 | |||
| 355 | version 1.0 | ||
| 356 | |||
| 357 | =head1 AUTHOR | ||
| 358 | |||
| 359 | Mark Denis Wilkinson (markw [at] illuminae [dot] com) | ||
| 360 | |||
| 361 | =head1 COPYRIGHT AND LICENSE | ||
| 362 | |||
| 363 | This software is Copyright (c) 2016 by Mark Denis Wilkinson. | ||
| 364 | |||
| 365 | This is free software, licensed under: | ||
| 366 | |||
| 367 | The Apache License, Version 2.0, January 2004 | ||
| 368 | |||
| 369 | =cut |