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