#!/usr/bin/perl

#    Copyright (C) 2009  Toby Inkster
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
use CGI qw(:standard);
use CGI::Carp 'fatalsToBrowser';
use RDF::Redland;
use Fcntl ':flock';
use strict;

# Settings
my $storage_file = 'example-storage.xml';
my $my_uri       = 'http://buzzword.org.uk/2009/posted-data/example';

# Modify this!
sub is_authorised { return 1; }

# Parse HTTP request
my $cgi = new CGI;

# Parse existing data
my $rdfxml;
open STORAGE, $storage_file;
while (<STORAGE>)
        { $rdfxml .= $_; }
close STORAGE;
my $parser = new RDF::Redland::Parser("rdfxml");
my $storage= new RDF::Redland::Storage("hashes", "test", "new='yes',hash-type='memory'");
my $model  = new RDF::Redland::Model($storage);
$parser->parse_string_into_model($rdfxml, RDF::Redland::URI->new($my_uri), $model);

# If some data has been posted by an authorised agent, then do stuff.
if (&is_authorised && $cgi->request_method eq 'POST')
{
        # Create a parser depending on the content type of posted data.
        my $parser2;
        if (lc($cgi->content_type) eq "application/turtle")
                { $parser2 = new RDF::Redland::Parser("turtle"); }
        elsif (lc($cgi->content_type) eq "text/plain")
                { $parser2 = new RDF::Redland::Parser("turtle"); }
        else
                { $parser2 = new RDF::Redland::Parser("rdfxml"); }
       
        # Parse the posted data.
        my $data = $cgi->param("POSTDATA");
        $parser2->parse_string_into_model($data, RDF::Redland::URI->new($my_uri), $model);
       
        # Store merged data.
        my $serializer = RDF::Redland::Serializer->new("rdfxml");
        open STORAGE, ">$storage_file";
        flock STORAGE, LOCK_EX;
        print STORAGE $serializer->serialize_model_to_string(RDF::Redland::URI->new($my_uri), $model);
        close STORAGE; 
}

# Default output is N-Triples as text/plain
my $format = "ntriples";   # use Accept header
my $mime   = "text/plain";

# See if a better content type has been requested
if (($cgi->Accept("application/rdf+xml") > $cgi->Accept("application/turtle"))
&&  ($cgi->Accept("application/rdf+xml") > 0))
{
        $format = "rdfxml-abbrev";
        $mime   = "application/rdf+xml";
}
elsif (($cgi->Accept("application/turtle") > $cgi->Accept("text/plain"))
   &&  ($cgi->Accept("application/turtle") > 0))
{
        $format = "turtle";
        $mime   = "application/turtle";
}

# Do output.
my $serializer = RDF::Redland::Serializer->new($format);
print $cgi->header($mime);
print $serializer->serialize_model_to_string(RDF::Redland::URI->new($my_uri), $model);

__END__