From 1510d98692c50532b9c0f1919d96228825f40054 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Mon, 23 Apr 2012 17:30:08 +0200 Subject: content-type negotiation implemented, url rewriting for puts --- lib/4store.rb | 175 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/opentox-server.rb | 5 +- lib/opentox.rb | 70 ++++++++++++-------- opentox-server.gemspec | 2 + 4 files changed, 225 insertions(+), 27 deletions(-) create mode 100644 lib/4store.rb diff --git a/lib/4store.rb b/lib/4store.rb new file mode 100644 index 0000000..26d3445 --- /dev/null +++ b/lib/4store.rb @@ -0,0 +1,175 @@ +module OpenTox + module Backend + class FourStore + + #TODO: catch 4store errors + + @@mime_format = { + "application/rdf+xml" => :rdfxml, + "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, + "text/html" => :html, + # TODO: compression, forms + #/sparql/ => :sparql #removed to prevent sparql injections + } + + @@format_mime = { + :rdfxml => "application/rdf+xml", + :turtle => "text/turtle", + :ntriples => "text/plain", + :uri_list => "text/uri-list", + :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] + @@rdf_formats = [:rdfxml, :turtle, :ntriples] + + def self.list mime_type + mime_type = "text/html" if mime_type.match(%r{\*/\*}) + bad_request_error "'#{mime_type}' is not a supported mime type. Please specify one of #{@@accept_formats.collect{|f| @@format_mime[f]}.join(", ")} in the Accept Header." unless @@accept_formats.include? @@mime_format[mime_type] + if mime_type =~ /json|yaml|uri-list/ + 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 + + def self.get uri, mime_type + mime_type = "text/html" if mime_type.match(%r{\*/\*}) + bad_request_error "'#{mime_type}' is not a supported mime type. Please specify one of #{@@accept_formats.collect{|f| @@format_mime[f]}.join(", ")} in the Accept Header." unless @@accept_formats.include? @@mime_format[mime_type] + not_found_error "#{uri} not found." unless list("text/uri-list").split("\n").include?(uri) + sparql = "CONSTRUCT {?s ?p ?o.} FROM <#{uri}> WHERE { ?s ?p ?o. }" + query sparql, mime_type + end + + 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' + 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 + 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 + end + + def self.delete uri + RestClientWrapper.delete data_uri uri + end + + def self.query sparql, mime_type + if sparql =~ /SELECT/i + xml = RestClient.post File.join(four_store_uri,"sparql")+"/", :query => sparql + #TODO request tab delimited format to speed up parsing + list = parse_sparql_xml_results(xml).collect{|hash| hash["s"]} + case mime_type + when /json/ + return list.to_json + when /yaml/ + return list.to_yaml + when /uri-list/ + return list.join "\n" + else + bad_request_error "#{mime_type} is not a supported mime type for SELECT statements. Please use one of text/uri-list, application/json, text/yaml, text/html." + end + elsif sparql =~ /CONSTRUCT/i + nt = RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => "text/plain").body + return nt if mime_type == 'text/plain' + case mime_type + when /turtle/ + return convert(nt,:ntriples, :turtle) + when /html/ + # TODO: fix and improve + html = "" + html += convert(nt,:ntriples, :turtle).gsub(%r{<(.*)>},'<\1>').gsub(/\n/,'
')#.gsub(/ /,' ') + html += "" + return html + when "application/rdf+xml" + return convert(nt,:ntriples, :rdfxml) + end + else + # TODO: check if this prevents SPARQL injections + bad_request_error "Only SELECT and CONSTRUCT are accepted SPARQL statements." + end + end + + private + + def self.convert rdf_string, input_format, output_format, rewrite_uri=nil + serialize(parse(rdf_string,input_format, rewrite_uri), output_format) + end + + def self.parse string, format, rewrite_uri + rdf = RDF::Graph.new + subject = nil + statements = [] # use array instead of graph for performance reasons + RDF::Reader.for(format).new(string) do |reader| + reader.each_statement do |statement| + subject = statement.subject if statement.predicate == RDF.type and statement.object == @@class + statements << statement + end + end + bad_request_error "No class specified with <#{RDF.type}> statement." unless subject + statements.each do |statement| + statement.subject = RDF::URI.new rewrite_uri if rewrite_uri and statement.subject == subject + rdf << statement + end + rdf + end + + def self.serialize rdf, format + if format == :turtle # prefixes seen to need RDF::N3 + string = RDF::N3::Writer.for(format).buffer(:prefixes => {:ot => "http://www.opentox.org/api/1.2#"}) do |writer| + rdf.each{|statement| writer << statement} + end + else + string = RDF::Writer.for(format).buffer do |writer| + rdf.each{|statement| writer << statement} + end + end + string + end + + def self.four_store_uri + $four_store[:uri].sub(%r{//},"//#{$four_store[:user]}:#{$four_store[:password]}@") + end + + def self.sparql_uri + File.join(four_store_uri, "sparql") + '/' + end + + def self.data_uri uri + File.join(four_store_uri, "data","?graph=#{uri}") + end + + def self.parse_sparql_xml_results(xml) + results = [] + doc = REXML::Document.new(REXML::Source.new(xml)) + doc.elements.each("*/results/result") do |result| + result_hash = {} + result.elements.each do |binding| + key = binding.attributes["name"] + value = binding.elements[1].text + type = binding.elements[1].name + result_hash[key] = value + end + results.push result_hash + end + results + end + + end + end +end diff --git a/lib/opentox-server.rb b/lib/opentox-server.rb index 358788b..3e5e68f 100644 --- a/lib/opentox-server.rb +++ b/lib/opentox-server.rb @@ -4,7 +4,10 @@ require 'rack/contrib' require 'sinatra' require 'sinatra/url_for' require 'roo' +require '4store-ruby' +require 'rdf/n3' require File.join(File.dirname(__FILE__),"environment.rb") +require File.join(File.dirname(__FILE__),"4store.rb") +#require File.join(File.dirname(__FILE__),"file-store.rb") require File.join(File.dirname(__FILE__),"opentox.rb") -require File.join(File.dirname(__FILE__),"file-store.rb") require File.join(File.dirname(__FILE__),"authorization-helper.rb") diff --git a/lib/opentox.rb b/lib/opentox.rb index e3c1f3b..f71eaa2 100644 --- a/lib/opentox.rb +++ b/lib/opentox.rb @@ -1,12 +1,15 @@ require 'sinatra/base' require "sinatra/reloader" +require File.join(ENV["HOME"],".opentox","config","default.rb") + +ENV["RACK_ENV"] ||= "production" module OpenTox # Base class for OpenTox services class Service < Sinatra::Base + include Backend - helpers Sinatra::UrlForHelper # use OpenTox error handling set :raise_errors, false set :show_exceptions, false @@ -16,36 +19,15 @@ module OpenTox register Sinatra::Reloader end - helpers do - def uri - params[:id] ? url_for("/#{params[:id]}", :full) : "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}" - end - end - before do - @accept = request.env['HTTP_ACCEPT'] - response['Content-Type'] = @accept - # TODO: A+A + request.content_type ? response['Content-Type'] = request.content_type : response['Content-Type'] = request.env['HTTP_ACCEPT'] end + # Attention: Error within tasks are catched by Task.create error do - # TODO: convert to OpenTox::Error and set URI error = request.env['sinatra.error'] - #error.uri = uri if error.respond_to? :report - # Errors are formated according to acccept-header - case @accept - when 'application/rdf+xml' - body = error.report.to_rdfxml - when /html/ - # TODO - # body = error.report.to_html - body = error.report.to_turtle - when "text/n3" - body = error.report.to_ntriples - else - body = error.report.to_turtle - end + body = error.report.to_turtle else response['Content-Type'] = "text/plain" body = error.message @@ -54,6 +36,42 @@ module OpenTox error.respond_to?(:http_code) ? code = error.http_code : code = 500 halt code, body end + + # Default methods, may be overwritten by derived services + # see http://jcalcote.wordpress.com/2008/10/16/put-or-post-the-rest-of-the-story/ + + # Get a list of objects at the server + get '/?' do + FourStore.list request.env['HTTP_ACCEPT'] + end + + # Create a new URI, does not accept a payload (use put for this purpose) + post '/?' do + uri = uri(SecureRandom.uuid) + FourStore.put uri, request.body.read, request.content_type + response['Content-Type'] = "text/uri-list" + uri + end + + # Get resource representation + get '/:id/?' do + FourStore.get(uri("/#{params[:id]}"), request.env['HTTP_ACCEPT']) + end + + # Modify (add rdf) a resource + post '/:id/?' do + FourStore.post uri("/#{params[:id]}"), request.body.read, request.content_type + end + + # Create or updata a resource + put '/:id/?' do + FourStore.put uri("/#{params[:id]}"), request.body.read, request.content_type + end + + # Delete a resource + delete '/:id/?' do + FourStore.delete uri("/#{params[:id]}") + end + end end - diff --git a/opentox-server.gemspec b/opentox-server.gemspec index 08438f0..9a6f1aa 100644 --- a/opentox-server.gemspec +++ b/opentox-server.gemspec @@ -27,4 +27,6 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'emk-sinatra-url-for' s.add_runtime_dependency 'roo' s.add_runtime_dependency 'unicorn' + s.add_runtime_dependency '4store-ruby' + s.add_runtime_dependency 'rdf-n3' end -- cgit v1.2.3