-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.rb
115 lines (106 loc) · 3.46 KB
/
application.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require 'json'
require 'rest-client'
require 'rdiscount'
require_relative "./service.rb"
configure :development do
$logger = Logger.new(STDOUT)
end
before do
@version = File.read("VERSION").chomp
@accepted_select = [
"text/plain",
"text/csv",
"text/html",
"text/n3",
"text/turtle",
"text/tab-separated-values",
"application/json",
"application/rdf+xml",
"application/sparql-results+xml",
"application/sparql-results+json",
"application/vnd.ms-excel"
]
@accepted_construct = [
"text/rdf+n3",
"text/rdf+ttl",
"text/rdf+turtle",
"text/turtle",
"text/n3",
"application/turtle",
"application/x-turtle",
"text/ntriples",
"application/rdf+xml",
"text/csv",
"application/odata+json",
"application/microdata+json",
"application/rdf+json",
"application/ld+json"
]
end
get '/?' do
redirect to('/enm-viewer')
end
get '/enm-viewer/?' do
haml :query
end
# visualization route
# while xhr response gets 'null' for js function this needs an extra handling without response code
get '/search/:query?' do
sparqlstring = params["query"]
halt 400, "Not allowed SPARQL keyword.\n" if !!(sparqlstring =~ /\b(delete|insert|load|clear)\b/i)
response = RestClient::Resource.new(URI.encode("#{$service_uri}/sparql/?query=#{sparqlstring}"), :verify_ssl => 0, :headers => {:accept => "application/sparql-results+json"}).get do |response, request, result|
if response.code == 400
return "malformed query"
elsif response.code > 400
return "error processing query or fetching data"
else
return response.body
end
end
end
# sparql endpoint access
get '/sparql/:query?' do
sparqlstring = params[:query]
halt 400, "Not allowed SPARQL keyword.\n" if !!(sparqlstring =~ /\b(delete|insert|load|clear)\b/i)
type = request.env["HTTP_ACCEPT"]
if (sparqlstring =~ /^select/i ? @accepted_select.include?(type) : @accepted_construct.include?(type) )
RestClient::Resource.new(URI.encode("#{$service_uri}/sparql/?query=#{sparqlstring}"), :verify_ssl => 0, :headers => {:accept => type } ).get do |response,request,result|
if response.code == 400
halt response.code, "malformed query\n"
elsif response.code > 400
halt response.code "error processing query or fetching data\n"
else
response+"\n"
end
end
else
halt 400, "#{type} is not supported content type.\n"
end
end
# sparql result as file download in various formats
get "/download" do
type = params[:query_type]
sparqlstring = params[:queryfield]
halt 400, "Not allowed SPARQL keyword.\n" if !!(sparqlstring =~ /\b(delete|insert|load|clear)\b/i)
file = Tempfile.new("enm")
if (sparqlstring =~ /^select/i ? @accepted_select.include?(type) : @accepted_construct.include?(type) )
RestClient::Resource.new(URI.encode("#{$service_uri}/sparql/?query=#{sparqlstring}"), :verify_ssl => 0, :headers => {:accept => type}).get do |response, request, result|
if response.code == 400
halt response.code, "malformed query\n"
elsif response.code > 400
halt response.code, "error processing query or fetching data\n"
elsif response.code == 200
file.write(response)
file.rewind
send_file(file, :disposition => 'attachment', :filename => File.basename(file), :type => type)
end
file.close
file.unlink
end
else
return "#{type} is not supported content type.\n"
end
end
get '/license' do
haml :license
end