Plack::Middleware::RDF::Flow - Serve RDF as Linked Data for RDF::Flow
version 0.171
use Plack::Builder; use Plack::Request; use RDF::Flow qw(rdflow_uri); my $model = RDF::Trine::Model->new( ... ); my $app = sub { my $env = shift; my $uri = rdflow_uri( $env ); [ 404, ['Content-Type'=>'text/plain'], ["URI $uri not found or not requested as RDF"] ]; }; builder { enable 'RDF::Flow', source => $model; $app; }
This Plack::Middleware provides a PSGI application to serve Linked Data. An HTTP request is mapped to an URI, that is used to retrieve RDF data from a RDF::Trine::Model or RDF::Flow::Source. Depending on the request and settings, the data is either returned in a requested serialization format or it is passed to the next PSGI application for further processing.
In detail each request is processed as following:
Determine query URI and serialization format (mime type) and set the request variables rdflow.uri, rdflow.type, and rdflow.serializer. The request URI is either taken from $env->{'rdflow.uri'} (if defined) or constructed from request's base and path. Query parameters are ignored by default.
rdflow.uri
rdflow.type
rdflow.serializer
$env->{'rdflow.uri'}
Retrieve data from a RDF::Trine::Model or a RDF::Flow::Source about the resource identified by rdflow.uri, if a serialization format was determined or if pass_through is set.
pass_through
Create and return a serialization, if a serialization format was determined. Otherwise store the retrieved RDF data in rdflow.data and pass to the next application.
rdflow.data
The following options can be set when creating a new object with new.
new
Sets a RDF::Trine::Model, a code reference, or another kind of RDF::Flow::Source to retrieve RDF data from. For testing you can use RDF::Flow::Source::Dummy which always returns a single triple.
Maps request URIs to a given URI prefix, similar to Plack::App::URLMap.
For instance if you deploy you application at http://your.domain/ and set base to http://other.domain/ then a request for http://your.domain/foo is be mapped to the URI http://other.domain/foo.
http://your.domain/
http://other.domain/
http://your.domain/foo
http://other.domain/foo
Code reference to rewrite the request URI.
Retrieve RDF data also if no serialization format was determined. In this case RDF data is stored in rdflow.data and passed to the next layer.
Defines supported serialization formats. You can either specify an array reference with serializer names or a hash reference with mappings of format names to serializer names or serializer instances. Serializer names must exist in RDF::Trine's RDF::Trine::Serializer::serializer_names and serializer instances must be subclasses of RDF::Trine::Serializer.
Plack::Middleware::RDF::Flow->new ( formats => [qw(ntriples rdfxml turtle)] ) # Plack::Middleware::RDF::Foo my $fooSerializer = Plack::Middleware::RDF->new( 'foo' ); Plack::Middleware::RDF::Flow->new ( formats => { nt => 'ntriples', rdf => 'rdfxml', xml => 'rdfxml', ttl => 'turtle', foo => $fooSerializer } );
By default the formats rdf, xml, and rdfxml (for RDF::Trine::Serializer), ttl (for RDF::Trine::Serializer::Turtle), json (for RDF::Trine::Serializer::RDFJSON), and nt (for RDF::Trine::Serializer::NTriples) are supported.
Detect serialization format via 'format' parameter. For instance foobar?format=ttl will serialize URI foobar in RDF/Turtle. This is enabled by default.
foobar?format=ttl
Detect serialization format via "file extension". For instance foobar.rdf will serialize URI foobar in RDF/XML. This is disabled by default.
foobar.rdf
Enable file extensions (not implemented yet).
http://example.org/{id} http://example.org/{id}.html http://example.org/{id}.rdf http://example.org/{id}.ttl
Given a PSGI environment, this internal (!) method queries the source(s) for a requested URI (if given) and either returns undef or a non-empty RDF::Trine::Model or RDF::Trine::Iterator. On error this method does not die but sets the environment variable rdflow.error. Note that if there are multiple source, there may be both an error, and a return value.
Given a PSGI request this function checks whether an RDF serialization format has been explicitly asked for, either by HTTP content negotiation or by format query parameter or by file extension. You can call this as method or as function and export it on request.
By now this package is experimental. Extensions are not supported yet. In contrast to other Linked Data applications, URIs must not have query parts and the distinction between information-resources and non-information resources is disregarded (some Semantic Web evangelists may be angry about this).
For a more complete package see RDF::LinkedData. You should always use Plack::Test and Test::RDF to test you application.
This package is actually a very thin layer on top of existing packages such as RDF::Trine, Plack, and Template. Theirs authors deserve all thanks.
Jakob Voß <voss@gbv.de>
This software is copyright (c) 2011 by Jakob Voß.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
1 POD Error
The following errors were encountered while parsing the POD:
Non-ASCII character seen before =encoding in 'Voß'. Assuming UTF-8
To install Plack::Middleware::RDF::Flow, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plack::Middleware::RDF::Flow
CPAN shell
perl -MCPAN -e shell install Plack::Middleware::RDF::Flow
For more information on module installation, please visit the detailed CPAN module installation guide.