diff options
author | Christoph Helma <helma@in-silico.ch> | 2012-07-06 16:44:41 +0200 |
---|---|---|
committer | Christoph Helma <helma@in-silico.ch> | 2012-07-06 16:44:41 +0200 |
commit | 6d46c0d15550498bfc84413ea3f0831f26e0e631 (patch) | |
tree | 8bd12d871d717459b9cf0799d71d2f116bb5bae4 | |
parent | c9e24988544571cce716b195d9c0f6111a3a5fb5 (diff) |
feature and task tests pass
-rw-r--r-- | lib/4store.rb | 111 | ||||
-rw-r--r-- | lib/opentox.rb | 7 |
2 files changed, 43 insertions, 75 deletions
diff --git a/lib/4store.rb b/lib/4store.rb index 83cdb81..c733f51 100644 --- a/lib/4store.rb +++ b/lib/4store.rb @@ -2,17 +2,13 @@ module OpenTox module Backend class FourStore + # TODO: simplify @@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, - #/sparql/ => :sparql #removed to prevent sparql injections 'application/sparql-results+xml' => :sparql } @@ -21,22 +17,21 @@ module OpenTox :turtle => "text/turtle", :ntriples => "text/plain", :uri_list => "text/uri-list", - #:json => "application/json", - #:yaml => "text/yaml", :html => "text/html", :sparql => 'application/sparql-results+xml' } - @@accept_formats = [:rdfxml, :turtle, :ntriples, :uri_list, :html, :sparql] #, :json, :yaml] - @@content_type_formats = [:rdfxml, :turtle, :ntriples]#, :json, :yaml] + @@accept_formats = [:rdfxml, :turtle, :ntriples, :uri_list, :html, :sparql] + @@content_type_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/ + if mime_type =~ /uri-list/ sparql = "SELECT DISTINCT ?g WHERE {GRAPH ?g {?s ?p ?o} }" elsif mime_type =~ /turtle|html|rdf|plain/ sparql = "CONSTRUCT {?s ?p ?o.} WHERE {?s <#{RDF.type}> <#{klass}>; ?p ?o. }" + else + 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] end query sparql, mime_type end @@ -44,30 +39,24 @@ module OpenTox 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 available? uri sparql = "CONSTRUCT {?s ?p ?o.} FROM <#{uri}> WHERE { ?s ?p ?o. }" - query sparql, mime_type + rdf = query sparql, mime_type + not_found_error "#{uri} not found." if rdf.empty? + rdf 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] or mime_type == "multipart/form-data" - rdf = convert rdf, @@mime_format[mime_type], :ntriples - rdf = "<#{uri}> <#{RDF.type}> <#{klass}>." unless rdf # create empty resource - rdf += "\n<#{uri}> <#{RDF::DC.date}> \"#{DateTime.now}\"." unless rdf.match(%r{#{RDF::DC.date}}) - RestClient.post File.join(four_store_uri,"data")+"/", :data => rdf, :graph => uri, "mime-type" => "application/x-turtle" # not very consistent in 4store + bad_request_error "Reqest body empty." unless rdf + mime_type = "application/x-turtle" if mime_type == "text/plain" # ntriples is turtle in 4store + RestClient.post File.join(four_store_uri,"data")+"/", :data => rdf, :graph => uri, "mime-type" => mime_type end 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] - bad_request_error "Reqest body empty." unless rdf # create empty resource - 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 - RestClient.put File.join(four_store_uri,"data",uri), rdf, :content_type => "application/x-turtle" # content-type not very consistent in 4store + bad_request_error "Reqest body empty." unless rdf + mime_type = "application/x-turtle" if mime_type == "text/plain" + RestClient.put File.join(four_store_uri,"data",uri), rdf, :content_type => mime_type # content-type not very consistent in 4store end def self.delete uri @@ -80,34 +69,33 @@ module OpenTox def self.query sparql, mime_type if sparql =~ /SELECT/i - return RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => mime_type).body.gsub(/<|>/,'').split("\n") if mime_type == 'application/sparql-results+xml' - list = RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => "text/plain").body.gsub(/<|>/,'').split("\n") - list.shift - return list unless mime_type case mime_type - when /json/ - return list.to_json - when /yaml/ - return list.to_yaml - when /uri-list/ - return list.join "\n" + when 'application/sparql-results+xml' + RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => mime_type).body + when "text/uri-list" + RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => "text/plain").body.gsub(/"|<|>/,'').split("\n").drop(1).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." + bad_request_error "#{mime_type} is not a supported mime type for SELECT statements." 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/ + when "text/plain", "application/rdf+xml" + RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => mime_type).body + when /html|turtle/ # TODO: fix and improve - html = "<html><body>" - html += convert(nt,:ntriples, :turtle).gsub(%r{<(.*)>},'<<a href="\1">\1</a>>').gsub(/\n/,'<br/>') - html += "</body></html>" - return html - when "application/rdf+xml" - return convert(nt,:ntriples, :rdfxml) + nt = RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => "text/plain").body # 4store returns ntriples for turtle + + rdf = RDF::Graph.new + RDF::Reader.for(:ntriples).new(nt) do |reader| + reader.each_statement { |statement| rdf << statement } + end + prefixes = {:rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"} + ['OT', 'DC', 'XSD'].each{|p| prefixes[p.downcase.to_sym] = eval("RDF::#{p}.to_s") } + turtle = RDF::N3::Writer.for(:turtle).buffer(:prefixes => prefixes) do |writer| + rdf.each{|statement| writer << statement} + end + turtle = "<html><body>" + turtle.gsub(%r{<(.*)>},'<<a href="\1">\1</a>>').gsub(/\n/,'<br/>') + "</body></html>" if mime_type =~ /html/ and !turtle.empty? + turtle end else # TODO: check if this prevents SPARQL injections @@ -115,12 +103,11 @@ module OpenTox end end - private - def self.klass RDF::OT[SERVICE.capitalize] end +=begin def self.available? uri sparql = "SELECT DISTINCT ?s WHERE {GRAPH <#{uri}> {?s <#{RDF.type}> <#{klass}>} }" r = query(sparql, nil) @@ -151,29 +138,7 @@ module OpenTox end 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 - prefixes = {:rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"} - ['OT', 'DC', 'XSD'].each{|p| prefixes[p.downcase.to_sym] = eval("RDF::#{p}.to_s") } - string = RDF::N3::Writer.for(format).buffer(:prefixes => prefixes) 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 +=end def self.four_store_uri # TODO remove credentials from URI 9security risk in tasks) diff --git a/lib/opentox.rb b/lib/opentox.rb index 7bc8757..6bca6bb 100644 --- a/lib/opentox.rb +++ b/lib/opentox.rb @@ -25,6 +25,7 @@ module OpenTox before do request.content_type ? response['Content-Type'] = request.content_type : response['Content-Type'] = request.env['HTTP_ACCEPT'] parse_input if request.request_method =~ /POST|PUT/ + @accept = request.env['HTTP_ACCEPT'] end helpers do @@ -59,7 +60,7 @@ module OpenTox # Get a list of objects at the server get "/#{SERVICE}/?" do - FourStore.list request.env['HTTP_ACCEPT'] + FourStore.list @accept end # Create a new resource @@ -72,7 +73,7 @@ module OpenTox # Get resource representation get "/#{SERVICE}/:id/?" do - FourStore.get(uri("/#{SERVICE}/#{params[:id]}"), request.env['HTTP_ACCEPT']) + FourStore.get(uri("/#{SERVICE}/#{params[:id]}"), @accept) end # Modify (i.e. add rdf statments to) a resource @@ -82,6 +83,8 @@ module OpenTox # Create or updata a resource put "/#{SERVICE}/:id/?" do + #puts @body + #puts @content_type FourStore.put uri("/#{SERVICE}/#{params[:id]}"), @body, @content_type end |