From 4bc73a2190254239742e33830747ff31fb5e431b Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 25 Apr 2012 15:24:04 +0200 Subject: new post/put policy --- lib/4store.rb | 46 ++++++++++++++++++++++++++++++---------------- lib/opentox.rb | 8 +++++--- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/lib/4store.rb b/lib/4store.rb index 26d3445..b20af00 100644 --- a/lib/4store.rb +++ b/lib/4store.rb @@ -9,10 +9,10 @@ module OpenTox "text/turtle" => :turtle, "text/plain" => :ntriples, "text/uri-list" => :uri_list, - "application/json" => :json, - "application/x-yaml" => :yaml, - "text/x-yaml" => :yaml, - "text/yaml" => :yaml, + #"application/json" => :json, + #"application/x-yaml" => :yaml, + #"text/x-yaml" => :yaml, + #"text/yaml" => :yaml, "text/html" => :html, # TODO: compression, forms #/sparql/ => :sparql #removed to prevent sparql injections @@ -23,13 +23,13 @@ module OpenTox :turtle => "text/turtle", :ntriples => "text/plain", :uri_list => "text/uri-list", - :json => "application/json", - :yaml => "text/yaml", + #:json => "application/json", + #:yaml => "text/yaml", :html => "text/html", } - @@accept_formats = [:rdfxml, :turtle, :ntriples, :uri_list, :json, :yaml, :html] - @@content_type_formats = [:rdfxml, :turtle, :ntriples, :json, :yaml] + @@accept_formats = [:rdfxml, :turtle, :ntriples, :uri_list, :html] #, :json, :yaml] + @@content_type_formats = [:rdfxml, :turtle, :ntriples]#, :json, :yaml] @@rdf_formats = [:rdfxml, :turtle, :ntriples] def self.list mime_type @@ -39,7 +39,6 @@ module OpenTox sparql = "SELECT ?s WHERE {?s <#{RDF.type}> <#{@@class}>. }" elsif mime_type =~ /turtle|html|rdf|plain/ sparql = "CONSTRUCT {?s ?p ?o.} WHERE {?s <#{RDF.type}> <#{@@class}>; ?p ?o. }" - else end query sparql, mime_type end @@ -54,15 +53,21 @@ module OpenTox def self.post uri, rdf, mime_type bad_request_error "'#{mime_type}' is not a supported content type. Please use one of #{@@content_type_formats.collect{|f| @@format_mime[f]}.join(", ")}." unless @@content_type_formats.include? @@mime_format[mime_type] - rdf = convert rdf, @@mime_format[mime_type], :ntriples, uri unless mime_type == 'text/plain' + rdf = convert rdf, @@mime_format[mime_type], :ntriples#, uri unless mime_type == 'text/plain' RestClient.post File.join(four_store_uri,"data")+"/", :data => rdf, :graph => uri, "mime-type" => "application/x-turtle" # not very consistent in 4store end - def self.put uri, rdf, mime_type + def self.put uri, rdf, mime_type, skip_rewrite=false bad_request_error "'#{mime_type}' is not a supported content type. Please use one of #{@@content_type_formats.collect{|f| @@format_mime[f]}.join(", ")}." unless @@content_type_formats.include? @@mime_format[mime_type] - rdf = convert rdf, @@mime_format[mime_type], :ntriples, uri - rdf = "<#{uri}> <#{RDF.type}> <#{@@class}>." unless rdf - RestClient.put File.join(four_store_uri,"data",uri), rdf, :content_type => "application/x-turtle" # not very consistent in 4store + uuid = uri.sub(/\/$/,'').split('/').last + bad_request_error "'#{uri}' is not a valid URI." unless uuid =~ /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/ + if !skip_rewrite + rdf = convert rdf, @@mime_format[mime_type], :ntriples, uri + elsif mime_type != "text/plain" # ntriples are not converted + rdf = convert rdf, @@mime_format[mime_type], :ntriples + end + rdf = "<#{uri}> <#{RDF.type}> <#{@@class}>." unless rdf # create empty resource + RestClient.put File.join(four_store_uri,"data",uri), rdf, :content_type => "application/x-turtle" # content-type not very consistent in 4store end def self.delete uri @@ -108,10 +113,10 @@ module OpenTox private def self.convert rdf_string, input_format, output_format, rewrite_uri=nil - serialize(parse(rdf_string,input_format, rewrite_uri), output_format) + rewrite_uri ? serialize(parse_and_rewrite_uri(rdf_string,input_format, rewrite_uri), output_format) : serialize(parse(rdf_string,input_format), output_format) end - def self.parse string, format, rewrite_uri + def self.parse_and_rewrite_uri string, format, rewrite_uri rdf = RDF::Graph.new subject = nil statements = [] # use array instead of graph for performance reasons @@ -129,8 +134,17 @@ module OpenTox rdf end + def self.parse string, format + rdf = RDF::Graph.new + RDF::Reader.for(format).new(string) do |reader| + reader.each_statement { |statement| rdf << statement } + end + rdf + end + def self.serialize rdf, format if format == :turtle # prefixes seen to need RDF::N3 + # TODO add prefixes string = RDF::N3::Writer.for(format).buffer(:prefixes => {:ot => "http://www.opentox.org/api/1.2#"}) do |writer| rdf.each{|statement| writer << statement} end diff --git a/lib/opentox.rb b/lib/opentox.rb index f71eaa2..9fac2eb 100644 --- a/lib/opentox.rb +++ b/lib/opentox.rb @@ -45,10 +45,12 @@ module OpenTox FourStore.list request.env['HTTP_ACCEPT'] end - # Create a new URI, does not accept a payload (use put for this purpose) + # Create a new resource + # TODO: handle multipart uploads post '/?' do + rdf = request.body.read uri = uri(SecureRandom.uuid) - FourStore.put uri, request.body.read, request.content_type + FourStore.put(uri, rdf, request.content_type) unless rdf == '' response['Content-Type'] = "text/uri-list" uri end @@ -58,7 +60,7 @@ module OpenTox FourStore.get(uri("/#{params[:id]}"), request.env['HTTP_ACCEPT']) end - # Modify (add rdf) a resource + # Modify (i.e. add rdf statments to) a resource post '/:id/?' do FourStore.post uri("/#{params[:id]}"), request.body.read, request.content_type end -- cgit v1.2.3