blib/lib/FAIR/Accessor.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 2 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 58 CC 6 SCAR 35.9
CC
6
Cov
0%
handle_requests
42.0
BEGIN
2.0
CRAP 36.3
1 package FAIR::Accessor;
2 $FAIR::Accessor::VERSION = '0.41';
3  
4  
5  
6 # ABSTRACT: all this does is assign the HTTP call to the correct routine
7  
8  
9  
10 use base 'FAIR::AccessorBase';
11  
12 # for testing at the command-line...
13 unless ($ENV{REQUEST_METHOD}){ # if running from command line
14  
15 if ($ARGV[0]) { # if there are any user-supplied arguments
16 $ENV{REQUEST_METHOD} = $ARGV[0];
17 $ENV{'SERVER_NAME'} = $ARGV[1] ;
18 $ENV{'REQUEST_URI'} = $ARGV[2];
19 $ENV{'SCRIPT_NAME'} = $ARGV[2];
20 $ENV{'PATH_INFO'} = $ARGV[3] ;
21 } else {
22 $ENV{REQUEST_METHOD} = "GET";
23 $ENV{'SERVER_NAME'} = "example.net";
24 $ENV{'REQUEST_URI'} = "/this/thing";
25 $ENV{'SCRIPT_NAME'} = "/this/thing";
26 $ENV{'PATH_INFO'} = "/1234567";
27 }
28 }
29  
30  
31 sub handle_requests {
32  
33 my $self = shift;
34 my $base = $self->Configuration->basePATH(); # $base is a regular expression that separates the "path" from the "id" portion of the PATH_INFO environment variable
35 $base ||= "";
36 # THIS ROUTINE WILL BE SHARED BY ALL SERVERS
37 if ($ENV{REQUEST_METHOD} eq "HEAD") {
38 $self->manageHEAD();
39 exit;
40 } elsif ($ENV{REQUEST_METHOD} eq "OPTIONS"){
41 $self->manageHEAD();
42 exit;
43 } elsif ($ENV{REQUEST_METHOD} eq "GET") {
44 unless ($ENV{'REQUEST_URI'} =~ /$base/){
45 print "Status: 500\n";
46 print "Content-type: text/plain\n\nThe configured basePATH argument $base does not match the request URI $ENV{'REQUEST_URI'}\n\n";
47 exit 0;
48 }
49 my $id = $ENV{'PATH_INFO'};
50 $id =~ s/^\///; # get rid of leading /
51
52 if ($id) { #$ENV{'PATH_INFO'} this is a request like /Allele/dip21 where the user is asking for a specific individual
53 $self->printResourceHeader();
54 $self->manageResourceGET('ID' => $id);
55 } else { # this is a request like /Allele or /Allele/ where the user is asking for the container
56 $self->printContainerHeader();
57 $self->manageContainerGET();
58 }
59 } else {
60 print "Status: 405 Method Not Allowed\n";
61 print "Content-type: text/plain\n\nYou can only request HEAD, OPTIONS or GET from this LD Platform Server\n\n";
62 exit 0;
63 }
64  
65 }
66  
67  
68  
69  
70  
71  
72  
73  
74 1;
75  
76 __END__
77  
78 =pod
79  
80 =encoding UTF-8
81  
82 =head1 NAME
83  
84 FAIR::Accessor - all this does is assign the HTTP call to the correct routine
85  
86 =head1 VERSION
87  
88 version 0.41
89  
90 =head1 SYNOPSIS
91  
92 The following code is a complete implementation of a 'Hello, World!' FAIR Accessor
93  
94 C<##!/usr/local/bin/perl -w
95  
96 package Metadata;
97  
98 use strict;
99 use warnings;
100 use JSON;
101 use FAIR::Accessor;
102  
103 #-----------------------------------------------------------------
104 # Configuration and Daemon
105 #-----------------------------------------------------------------
106  
107 use base 'FAIR::Accessor';
108  
109 my $config = {
110 title => 'Semantic PHI Base Metadata Server',
111 ETAG_Base => "TopLevelMetadata_Accessor_For_SemanticPHIBase", # this is a unique identificaiton string for the service (required by the LDP specification)
112 localNamespaces => {
113 ontology => 'http://example.org/ontologies/MyOntology#',
114 }, # add a few new namespaces to the list of known namespaces....
115 localMetadataElements => [qw(hw:Greeting hw2:grusse) ], # things that we use in addition to common metadata elements
116 basePATH => 'Nameof/ThisScript', # REQUIRED regexp to match the RESTful PATH part of the URL, before the ID number
117  
118 };
119  
120 my $service = Metadata->new(%$config);
121  
122 # start daemon
123 $service->handle_requests;
124  
125  
126 #-----------------------------------------------------------------
127 # Accessor Implementation
128 #-----------------------------------------------------------------
129  
130
131 =head2 MetaContainer
132 Function: REQUIRED SUBROUTINE - returns the first-stage LD Platform list of contained URIs and the dataset metadata.
133 Args : $starting_at_record : this will be passed-in to tell you what record to start with (for paginated responses)
134 $path : the webserver's PATH_INFO environment value (used to modify the behaviour of REST services)
135 Returns : JSON encoded listref of 'meta URIs' representing individual records
136 Note : meta URIs are generally URIs that point back to this same server; calling GET on a meta URI will
137 return an RDF description of the set of DCAT distributions for that record.
138 The format of the JSON response is as follows:
139
140 {"metadata:element1" : "some value",
141 "external:metadatatype": "some other value",
142 "void:entities" : "3",
143 "ldp:contains" : ["http://myserver.org/ThisScript/record/479-467-29",
144 "http://myserver.org/ThisScript/record/479-467-32",
145 "http://myserver.org/ThisScript/record/479-467-434"
146 ]
147 }
148
149 Recommended metadata elements include dc:title, dcat:description,dcat:identifier,
150 dcat:keyword,dcat:landingPage,dcat:publisher,dcat:theme
151
152 note #1: Using dcat:theme requires you to create a SKOS concept scheme of the various ontology
153 terms that describe the data in your repository... this isn't hard, but it's not entirely trivial either...
154
155 note #2: if you return URLs in the ldp:contains, then you must also return the count of those URLs in void:entities
156 =cut
157
158
159 sub MetaContainer {
160  
161 my ($self, %ARGS) = @_;
162  
163 # this is how you would manage "RESTful" references to different subsets of your data repository
164 #if ($ENV{'REQUEST_URI'} =~ /DataSliceX/) {
165 # # some behavior for Data Slice X
166 #} elsif ($ENV{'REQUEST_URI'} =~ /DataSliceY/) {
167 # # some behavior for Data Slice Y
168 #}
169
170 my $metadata = $self->getRepositoryMetadata();
171  
172 # you may chose to return no record IDs at all, if you only want to serve repository-level metadata
173
174 my $BASE_URL = "http://" . $ENV{'SERVER_NAME'} . $ENV{'REQUEST_URI'};
175  
176 my @known_records = ('http://example.org/Nameof/ThisScript/123'); # generate a list of HTTP records
177 # note that the record URLs point to this script!
178
179 # these two pieces of metadata are required by the LDP specification
180 $metadata->{'void:entities'} = scalar(@known_records); # THE TOTAL *NUMBER* OF RECORDS THAT CAN BE SERVED
181 $metadata->{'ldp:contains'} = \@known_records; # the listref of record ids
182  
183 return encode_json($metadata);
184  
185 }
186  
187
188  
189  
190 =head2 Distributions
191  
192 Function: REQUIRED IF get_all_meta_URIs list of URIs point back to this script.
193 returns the second-stage LD Platform metadata describing the DCAT distributions, formats, and URLs
194 for a particular record
195 Args : $ID : the desired ID number, as determined by the Accessor.pm module
196 $PATH_INFO : the webserver's PATH_INFO environment value (in case the $ID best-guess is wrong... then you're on your own!)
197 Returns : JSON encoded hashref of 'meta URIs' representing individual DCAT distributions and their mime-type (mime-type is key)
198 The format for this response is (you are always allowed to use lists as values if you wish):
199
200 {"metadata":
201 {"rdf:type": ["edam:data_0006","sio:SIO_000088"]
202 "my:metadatathingy": "some value",
203 "external:metadatatype": "some other value"
204 },
205 "distributions":
206 {"application/rdf+xml" : "http://myserver.org/ThisScript/record/479-467-29X.rdf",
207 "text/html" : "http://myserver.org/ThisScript/record/479-467-29X.html"
208 }
209 }
210  
211 =cut
212  
213  
214 sub Distributions {
215 my ($self, %ARGS) = @_;
216  
217 my $ID = $ARGS{'ID'};
218 # ID is the piece of the URL that comes after http://example.org/Nameof/ThisScript/___here____
219 # you created these URLs in the MetaContainer routine above
220 my %response;
221 my %formats;
222 my %metadata;
223  
224 # using the $ID, create the links to the actual records, in whatever format...
225 my $accnumber = $ID; # maybe transform ID to get the accession
226
227 # make the links to teh text and RDF versions of this record
228 $formats{'text/html'} = "http://example.org/query.php?acc=$accnumber";
229 $formats{'application/rdf+xml'} = "http://example.org/Resource/$ID";
230  
231 # set the ontological type for the record (optional)
232 $metadata{'rdf:type'} = ['ontology:PHIBO_00022']; # note the qNAME! If you have defined 'ontology' in the $config hashref at the beginning, you can use a qname here
233  
234 # and whatever other metadata you wish (also optional)
235 my $metadata = $self->getDistributionMetadata($accnumber);
236  
237 $response{distributions} = \%formats;
238 $response{metadata} = $metadata if (keys %$metadata); # only set it if you can provided something
239  
240 my $response = encode_json(\%response);
241  
242 return $response;
243  
244 }
245  
246 sub getRepositoryMetadata {
247 my %metadata = (
248 'dc:title' => "Semantic PHI Base Accessor",
249 'dcat:description' => "FAIR Accessor server for the Semantic PHI Base. This server exposes the plant portion of the Pathogen Host Interaction database as Linked Data, following the FAIR Data Principles. This interface (the one you are reading) follows the W3C Linked Data Platform behaviors. The data provided here is an RDF transformation of data kindly provided by the researchers at PHI Base (doi:10.1093/nar/gku1165)",
250 'dcat:identifier' => "http://linkeddata.systems/SemanticPHI/Metadata",
251 'dcat:keyword' => ["pathogenesis", "plant/pathogen interactions", "PHI Base", "semantic web", "linked data", "FAIR Data", "genetic database", "phytopathology"],
252 'dcat:landingPage' => 'http://www.phi-base.org/',
253 'foaf:page' => ['http://linkeddata.systems:8890/sparql','http://www.phi-base.org/'],
254 'dcat:language' => 'http://id.loc.gov/vocabulary/iso639-1/en',
255 'dc:language' => 'http://lexvo.org/id/iso639-3/eng',
256 'dcat:publisher' => ['http://wilkinsonlab.info',"Rothamsted Research", ' http://www.rothamsted.ac.uk', 'http://www.phi-base.org'],
257 'dcat:theme' => 'http://linkeddata.systems/ConceptSchemes/semanticphi_concept_scheme.rdf', # this is the URI to a SKOS Concept Scheme
258 'daml:has-Technical-Lead' => ["Dr. Alejandro Rodriguez Gonzalez","Alejandro Rodriguez Iglesias"],
259 'daml:has-Principle-Investigator' => ["Dr. Mark Wilkinson","Dr. Kim Hammond-Kosack"],
260 'dc:creator' => 'http://www.phi-base.org/',
261 'pav:authoredBy' => ['http://orcid.org/0000-0002-9699-485X','http://orcid.org/0000-0002-6019-7306'],
262 'dcat:contactPoint' => 'http://biordf.org/DataFairPort/MiscRDF/Wilkinson.rdf',
263 'dcat:license' => 'http://purl.org/NET/rdflicense/cc-by-nd4.0',
264 'dc:license' => 'http://purl.org/NET/rdflicense/cc-by-nd4.0',
265 'dc:issued' => "2015-11-17",
266 'rdf:type' => ['prov:Collection', 'dctypes:Dataset'],
267 );
268 return \%metadata;
269
270 }
271  
272 sub getDistributionMetadata {
273 my ($self, $ID) = @_;
274 my %metadata = (
275 'dcat:description' => "RDF representation of PHI Base Interaction Record PHI:$ID",
276 'dc:title' => "PHI-Base Interaction PHI:$ID",
277 'dcat:modified' => "2015-11-17",
278 'dc:issued' => "2015-11-17",
279 'dcat:identifier' => "http://linkeddata.systems/SemanticPHI/Metadata",
280 'dcat:keyword' => ["pathogenesis", "host/pathogen interaction", "PHI Base"],
281 'dcat:landingPage' => ['http://www.phi-base.org/'],
282 'foaf:page' => 'http://linkeddata.systems:8890/sparql',
283 'foaf:page' => 'http://www.phi-base.org/',
284 'dcat:language' => 'http://id.loc.gov/vocabulary/iso639-1/en',
285 'dcat:publisher' => ['http://wilkinsonlab.info',"Rothamsted Research", 'http://www.rothamsted.ac.uk', 'http://www.phi-base.org'],
286 'daml:has-Technical-Lead' => ["Dr. Alejandro Rodriguez Gonzalez", "Alejandro Rodriguez Iglesias"],
287 'daml:has-Principle-Investigator' => ["Dr. Mark Wilkinson","Dr. Kim Hammond-Kosack"],
288 'dcat:contactPoint' => 'http://biordf.org/DataFairPort/MiscRDF/Wilkinson.rdf',
289 'void:inDataset' => 'http://linkeddata.systems/SemanticPHIBase/Metadata',
290 'dcat:license' => 'http://purl.org/NET/rdflicense/cc-by-nd4.0',
291 'dc:license' => 'http://purl.org/NET/rdflicense/cc-by-nd4.0',
292 'dc:creator' => 'http://www.phi-base.org/',
293 'pav:authoredBy' => ['http://orcid.org/0000-0002-9699-485X', 'http://orcid.org/0000-0002-6019-7306'],
294 );
295 return \%metadata;
296 }
297  
298 >
299  
300 =head1 DESCRIPTION
301  
302 FAIR Accessors are an implementation of the W3Cs Linked Data Platform.
303  
304 FAIR Accessors follow a two-stage interaction, where the first stage
305 retrieves metadata about the repository, and (optionally) a series of URLs representing
306 'meta-records' for every record in that repository (or whatever slice of the repository
307 is being served). This is accomplished by the B<MetaContainer> subroutine. These URLs
308 will generally point back at this same Accessor script (e.g. with the
309 record number appended to the URL: I<http://this.host/thisscript/12345>).
310  
311 The second stage involves retrieving metadata about individual recoreds.
312 The metadata is up to you, but optimally it would include the available
313 DCAT distributions and their file formats. The second stage can be accomplished
314 by this same Accessor script, using the Distributions subroutine.
315  
316 The two subroutine names - B<MetaContainer> and B<Distributions> - are not flexible, as they are
317 called by-name, by the Accessor libraries.
318  
319 You B<MUST> create the B<MetaContainer> subroutine, at a minimum, and it should return some metadata.
320 It does not have to return a list of known records (in which case it simply acts as a metadata
321 descriptor of the repository in general, nothing more... which is fine!... and there will be no
322 second stage interaction. In this case, you do not need to provide a B<Distributions> subroutine.)
323  
324 =head1 NAME
325  
326 FAIR::Accessor - Module for creating Linked Data Platform Accessors for the FAIR Data project
327  
328 =head1 Command-line testing
329  
330 If you wish to test your Accessor server at the command line, you can run it with the following commandline arguments (in order):
331  
332 Method (always GET, at the moment)
333 Domain
334 Request URI (i.e. the path to this script, including the script name)
335 PATH_INFO (anything that should appear in the PATH_INFO variable of the webserver)
336  
337 perl myAccessorScript GET example.net /this/myAccessorScript /1234567
338  
339 =head1 AUTHOR
340  
341 Mark Denis Wilkinson (markw [at] illuminae [dot] com)
342  
343 =head1 COPYRIGHT AND LICENSE
344  
345 This software is Copyright (c) 2016 by Mark Denis Wilkinson.
346  
347 This is free software, licensed under:
348  
349 The Apache License, Version 2.0, January 2004
350  
351 =cut