summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/authorization.rb13
-rw-r--r--lib/dataset.rb50
-rw-r--r--lib/environment.rb3
-rw-r--r--lib/feature.rb8
-rw-r--r--lib/helper.rb41
-rw-r--r--lib/model.rb12
-rw-r--r--lib/opentox.rb8
-rw-r--r--lib/parser.rb17
-rw-r--r--lib/policy.rb9
-rw-r--r--lib/task.rb12
10 files changed, 103 insertions, 70 deletions
diff --git a/lib/authorization.rb b/lib/authorization.rb
index c33f712..5bc690a 100644
--- a/lib/authorization.rb
+++ b/lib/authorization.rb
@@ -114,7 +114,9 @@ module OpenTox
begin
resource = RestClient::Resource.new("#{AA_SERVER}/pol")
out = resource.get(:subjectid => subjectid)
- return out.split("\n")
+ return out.split("\n")
+ rescue RestClient::InternalServerError => e
+ raise e.response
rescue
return nil
end
@@ -313,9 +315,14 @@ module OpenTox
false
end
end
+ true
+ end
+
+ class << self
+ alias :token_valid? :is_token_valid
end
-
- end
+
+ end
end
diff --git a/lib/dataset.rb b/lib/dataset.rb
index ae86f5f..640e3da 100644
--- a/lib/dataset.rb
+++ b/lib/dataset.rb
@@ -14,7 +14,7 @@ module OpenTox
# dataset = OpenTox::Dataset.new("http:://webservices.in-silico/ch/dataset/1")
# @param [optional, String] uri Dataset URI
# @return [OpenTox::Dataset] Dataset object
- def initialize(uri=nil)
+ def initialize(uri=nil,subjectid=nil)
super uri
@features = {}
@compounds = []
@@ -27,7 +27,7 @@ module OpenTox
# @param [optional, String] uri Dataset URI
# @return [OpenTox::Dataset] Dataset object
def self.create(uri=CONFIG[:services]["opentox-dataset"], subjectid=nil)
- dataset = Dataset.new
+ dataset = Dataset.new(nil,subjectid)
dataset.save(subjectid)
dataset
end
@@ -50,17 +50,17 @@ module OpenTox
# Find a dataset and load all data. This can be time consuming, use Dataset.new together with one of the load_* methods for a fine grained control over data loading.
# @param [String] uri Dataset URI
# @return [OpenTox::Dataset] Dataset object with all data
- def self.find(uri)
- dataset = Dataset.new(uri)
- dataset.load_all
+ def self.find(uri, subjectid=nil)
+ dataset = Dataset.new(uri, subjectid)
+ dataset.load_all(subjectid)
dataset
end
# Get all datasets from a service
# @param [optional,String] uri URI of the dataset service, defaults to service specified in configuration
# @return [Array] Array of dataset object without data (use one of the load_* methods to pull data from the server)
- def self.all(uri=CONFIG[:services]["opentox-dataset"])
- RestClientWrapper.get(uri,:accept => "text/uri-list").to_s.each_line.collect{|u| Dataset.new(u)}
+ def self.all(uri=CONFIG[:services]["opentox-dataset"], subjectid=nil)
+ RestClientWrapper.get(uri,{:accept => "text/uri-list",:subjectid => subjectid}).to_s.each_line.collect{|u| Dataset.new(u, subjectid)}
end
# Load YAML representation into the dataset
@@ -77,10 +77,10 @@ module OpenTox
# Load RDF/XML representation from a file
# @param [String] file File with RDF/XML representation of the dataset
# @return [OpenTox::Dataset] Dataset object with RDF/XML data
- def load_rdfxml_file(file)
- parser = Parser::Owl::Dataset.new @uri
+ def load_rdfxml_file(file, subjectid=nil)
+ parser = Parser::Owl::Dataset.new @uri, subjectid
parser.uri = file.path
- copy parser.load_uri
+ copy parser.load_uri(subjectid)
end
# Load CSV string (format specification: http://toxcreate.org/help)
@@ -111,26 +111,26 @@ module OpenTox
# Load and return only metadata of a Dataset object
# @return [Hash] Metadata of the dataset
- def load_metadata
- add_metadata Parser::Owl::Dataset.new(@uri).load_metadata
+ def load_metadata(subjectid=nil)
+ add_metadata Parser::Owl::Dataset.new(@uri, subjectid).load_metadata(subjectid)
self.uri = @uri if @uri # keep uri
@metadata
end
# Load all data (metadata, data_entries, compounds and features) from URI
- def load_all
+ def load_all(subjectid=nil)
if (CONFIG[:yaml_hosts].include?(URI.parse(@uri).host))
- copy YAML.load(RestClientWrapper.get(@uri, :accept => "application/x-yaml"))
+ copy YAML.load(RestClientWrapper.get(@uri, {:accept => "application/x-yaml", :subjectid => subjectid}))
else
- parser = Parser::Owl::Dataset.new(@uri)
- copy parser.load_uri
+ parser = Parser::Owl::Dataset.new(@uri, subjectid)
+ copy parser.load_uri(subjectid)
end
end
# Load and return only compound URIs from the dataset service
# @return [Array] Compound URIs in the dataset
- def load_compounds
- RestClientWrapper.get(File.join(uri,"compounds"),:accept=> "text/uri-list").to_s.each_line do |compound_uri|
+ def load_compounds(subjectid=nil)
+ RestClientWrapper.get(File.join(uri,"compounds"),{:accept=> "text/uri-list", :subjectid => subjectid}).to_s.each_line do |compound_uri|
@compounds << compound_uri.chomp
end
@compounds.uniq!
@@ -138,9 +138,9 @@ module OpenTox
# Load and return only features from the dataset service
# @return [Hash] Features of the dataset
- def load_features
- parser = Parser::Owl::Dataset.new(@uri)
- @features = parser.load_features
+ def load_features(subjectid=nil)
+ parser = Parser::Owl::Dataset.new(@uri, subjectid)
+ @features = parser.load_features(subjectid)
@features
end
@@ -290,7 +290,7 @@ module OpenTox
task_uri = RestClient.post(@uri, {:file => File.new(@path)},{:accept => "text/uri-list" , :subjectid => subjectid}).to_s.chomp
#task_uri = `curl -X POST -H "Accept:text/uri-list" -F "file=@#{@path};type=application/rdf+xml" http://apps.ideaconsult.net:8080/ambit2/dataset`
Task.find(task_uri).wait_for_completion
- self.uri = RestClientWrapper.get(task_uri,:accept => 'text/uri-list')
+ self.uri = RestClientWrapper.get(task_uri,{:accept => 'text/uri-list', :subjectid => subjectid})
end
else
# create dataset if uri is empty
@@ -325,9 +325,9 @@ module OpenTox
# Find a prediction dataset and load all data.
# @param [String] uri Prediction dataset URI
# @return [OpenTox::Dataset] Prediction dataset object with all data
- def self.find(uri)
- prediction = LazarPrediction.new(uri)
- prediction.load_all
+ def self.find(uri, subjectid=nil)
+ prediction = LazarPrediction.new(uri, subjectid)
+ prediction.load_all(subjectid)
prediction
end
diff --git a/lib/environment.rb b/lib/environment.rb
index 1761d92..203ebc6 100644
--- a/lib/environment.rb
+++ b/lib/environment.rb
@@ -84,6 +84,9 @@ class OwlNamespace
end
AA_SERVER = CONFIG[:authorization] ? (CONFIG[:authorization][:server] ? CONFIG[:authorization][:server] : nil) : nil
+CONFIG[:authorization][:authenticate_request] = [""] unless CONFIG[:authorization][:authenticate_request]
+CONFIG[:authorization][:authorize_request] = [""] unless CONFIG[:authorization][:authorize_request]
+CONFIG[:authorization][:free_request] = [""] unless CONFIG[:authorization][:free_request]
RDF = OwlNamespace.new 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
OWL = OwlNamespace.new 'http://www.w3.org/2002/07/owl#'
diff --git a/lib/feature.rb b/lib/feature.rb
index de7c757..28ac0c5 100644
--- a/lib/feature.rb
+++ b/lib/feature.rb
@@ -1,11 +1,11 @@
module OpenTox
class Feature
include OpenTox
-
- def self.find(uri)
- feature = Feature.new uri
+
+ def self.find(uri, subjectid=nil)
+ feature = Feature.new uri
if (CONFIG[:yaml_hosts].include?(URI.parse(uri).host))
- feature.add_metadata YAML.load(RestClientWrapper.get(uri,:accept => "application/x-yaml"))
+ feature.add_metadata YAML.load(RestClientWrapper.get(uri,{:accept => "application/x-yaml", :subjectid => subjectid}))
else
feature.add_metadata Parser::Owl::Dataset.new(uri).load_metadata
end
diff --git a/lib/helper.rb b/lib/helper.rb
index ff5e908..e82c8fb 100644
--- a/lib/helper.rb
+++ b/lib/helper.rb
@@ -16,25 +16,39 @@ helpers do
end
end
+
+ #Check Authorization for URI with method and subjectid.
def authorized?(subjectid)
- if CONFIG[:authorization][:authorize_request].include?(request.env['REQUEST_METHOD'])
- ret = OpenTox::Authorization.authorize("#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}", request.env['REQUEST_METHOD'], subjectid)
- LOGGER.debug "OpenTox helpers OpenTox::Authorization authorized? method: #{request.env['REQUEST_METHOD']}, URI: #{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}, subjectid: #{subjectid} with return #{ret}."
+ request_method = request.env['REQUEST_METHOD']
+ uri = clean_uri("#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}")
+ request_method = "GET" if request_method == "POST" && uri =~ /\/model\/\d+\/?$/
+ if CONFIG[:authorization][:authorize_request].include?(request_method)
+ ret = OpenTox::Authorization.authorize(uri, request_method, subjectid)
+ LOGGER.debug "OpenTox helpers OpenTox::Authorization authorized? method: #{request_method} , URI: #{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}, subjectid: #{subjectid} with return >>#{ret}<<"
return ret
end
- if CONFIG[:authorization][:authenticate_request].include?(env['REQUEST_METHOD'])
- if OpenTox::Authorization.is_token_valid(subjectid)
- return true
- end
+ if CONFIG[:authorization][:authenticate_request].include?(request_method)
+ return true if OpenTox::Authorization.is_token_valid(subjectid)
end
- LOGGER.debug "Not authorized for: #{request.env['rack.url_scheme']}://#{request['REQUEST_URI']} with Method: #{request.env['REQUEST_METHOD']} with Token #{subjectid}"
+ LOGGER.debug "Not authorized for: #{uri} with Method: #{request.env['REQUEST_METHOD']}/#{request_method} with Token #{subjectid}"
return false
end
+ #cleans URI from querystring and file-extension. Sets port 80 to emptystring
+ # @param [String] uri
+ def clean_uri(uri)
+ out = URI.parse(uri)
+ out.path = out.path[0, out.path.rindex(/[0-9]/) + 1] if out.path.rindex(/[0-9]/) #cuts after id for a&a
+ "#{out.scheme}:" + (out.port != 80 ? out.port : "") + "//#{out.host}#{out.path}"
+ end
+
+ #unprotected uris for login/logout, webapplication ...
def unprotected_requests
case env['REQUEST_URI']
when /\/login$|\/logout$|\/predict$|\/toxcreate\/models$/
return true
+ when /\/features/
+ return false
when /\/compound|\/feature|\/task|\/toxcreate/ #to fix: read from config | validation should be protected
return true
else
@@ -42,21 +56,18 @@ helpers do
end
end
- def check_subjectid(subjectid)
- return false if !subjectid
- return true if subjectid.size > 62
- false
- end
end
before do
unless !AA_SERVER or unprotected_requests or CONFIG[:authorization][:free_request].include?(env['REQUEST_METHOD'])
begin
+ subjectid = nil
subjectid = session[:subjectid] if session[:subjectid]
- subjectid = params[:subjectid] if params[:subjectid] and !check_subjectid(subjectid)
- subjectid = request.env['HTTP_SUBJECTID'] if request.env['HTTP_SUBJECTID'] and !check_subjectid(subjectid)
+ subjectid = params[:subjectid] if params[:subjectid] and !subjectid
+ subjectid = request.env['HTTP_SUBJECTID'] if request.env['HTTP_SUBJECTID'] and !subjectid
# see http://rack.rubyforge.org/doc/SPEC.html
subjectid = CGI.unescape(subjectid) if subjectid.include?("%23")
+ @subjectid = subjectid
rescue
LOGGER.debug "OpenTox ruby api wrapper: helper before filter: NO subjectid for URI: #{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}"
subjectid = ""
diff --git a/lib/model.rb b/lib/model.rb
index efa273b..85be1b5 100644
--- a/lib/model.rb
+++ b/lib/model.rb
@@ -88,15 +88,15 @@ module OpenTox
# Get URIs of all lazar models
# @return [Array] List of lazar model URIs
- def self.all
- RestClientWrapper.get(CONFIG[:services]["opentox-model"]).to_s.split("\n")
+ def self.all(subjectid=nil)
+ RestClientWrapper.get(CONFIG[:services]["opentox-model"], :subjectid => subjectid).to_s.split("\n")
end
# Find a lazar model
# @param [String] uri Model URI
# @return [OpenTox::Model::Lazar] lazar model
- def self.find(uri)
- YAML.load RestClientWrapper.get(uri,:accept => 'application/x-yaml')
+ def self.find(uri, subjectid=nil)
+ YAML.load RestClientWrapper.get(uri,{:accept => 'application/x-yaml', :subjectid => subjectid})
end
# Create a new lazar model
@@ -105,7 +105,7 @@ module OpenTox
def self.create(params)
lazar_algorithm = OpenTox::Algorithm::Generic.new File.join( CONFIG[:services]["opentox-algorithm"],"lazar")
model_uri = lazar_algorithm.run(params)
- OpenTox::Model::Lazar.find(model_uri)
+ OpenTox::Model::Lazar.find(model_uri, params[:subjectid])
end
# Get a parameter value
@@ -128,7 +128,7 @@ module OpenTox
DC.title => URI.decode(File.basename( @metadata[OT.dependentVariables] )),
OT.parameters => [{DC.title => "dataset_uri", OT.paramValue => dataset_uri}]
})
- d = Dataset.new(dataset_uri)
+ d = Dataset.new(dataset_uri,subjectid)
d.load_compounds
count = 0
d.compounds.each do |compound_uri|
diff --git a/lib/opentox.rb b/lib/opentox.rb
index 90683e5..f1af5c3 100644
--- a/lib/opentox.rb
+++ b/lib/opentox.rb
@@ -19,14 +19,14 @@ module OpenTox
# Get all objects from a service
# @return [Array] List of available URIs
- def self.all(uri)
- RestClientWrapper.get(uri,:accept => "text/uri-list").to_s.split(/\n/)
+ def self.all(uri, subjectid=nil)
+ RestClientWrapper.get(uri,:accept => "text/uri-list", :subjectid => subjectid).to_s.split(/\n/)
end
# Load (and return) metadata from object URI
# @return [Hash] Metadata
- def load_metadata
- @metadata = Parser::Owl::Generic.new(@uri).load_metadata
+ def load_metadata(subjectid=nil)
+ @metadata = Parser::Owl::Generic.new(@uri).load_metadata(subjectid)
@metadata
end
diff --git a/lib/parser.rb b/lib/parser.rb
index b727412..a913cf2 100644
--- a/lib/parser.rb
+++ b/lib/parser.rb
@@ -29,14 +29,14 @@ module OpenTox
# Read metadata from opentox service
# @return [Hash] Object metadata
- def load_metadata
+ def load_metadata(subjectid=nil)
if @dataset
uri = File.join(@uri,"metadata")
else
uri = @uri
end
-
+ uri += "?subjectid=#{CGI.escape(subjectid)}" if subjectid
statements = []
parameter_ids = []
`rapper -i rdfxml -o ntriples #{uri} 2>/dev/null`.each_line do |line|
@@ -71,9 +71,9 @@ module OpenTox
# Create a new OWL-DL dataset parser
# @param uri Dataset URI
# @return [OpenTox::Parser::Owl::Dataset] OWL-DL parser
- def initialize(uri)
+ def initialize(uri, subjectid=nil)
super uri
- @dataset = ::OpenTox::Dataset.new(@uri)
+ @dataset = ::OpenTox::Dataset.new(@uri, subjectid)
end
# Read data from dataset service. Files can be parsed by setting #uri to a filename (after initialization with a real URI)
@@ -87,12 +87,14 @@ module OpenTox
# dataset = parser.load_uri
# dataset.save
# @return [Hash] Internal dataset representation
- def load_uri
+ def load_uri(subjectid=nil)
+ uri = @uri
+ uri += "?subjectid=#{CGI.escape(subjectid)}" if subjectid
data = {}
feature_values = {}
feature = {}
other_statements = {}
- `rapper -i rdfxml -o ntriples #{@uri} 2>/dev/null`.each_line do |line|
+ `rapper -i rdfxml -o ntriples #{uri} 2>/dev/null`.each_line do |line|
triple = line.chomp.split(' ',3)
triple = triple[0..2].collect{|i| i.sub(/\s+.$/,'').gsub(/[<>"]/,'')}
case triple[1]
@@ -122,8 +124,9 @@ module OpenTox
# Read only features from a dataset service.
# @return [Hash] Internal features representation
- def load_features
+ def load_features(subjectid=nil)
uri = File.join(@uri,"features")
+ uri += "?subjectid=#{CGI.escape(subjectid)}" if subjectid
statements = []
features = Set.new
`rapper -i rdfxml -o ntriples #{uri} 2>/dev/null`.each_line do |line|
diff --git a/lib/policy.rb b/lib/policy.rb
index 08bf6ed..8591d52 100644
--- a/lib/policy.rb
+++ b/lib/policy.rb
@@ -38,6 +38,15 @@ module OpenTox
@policies.collect{ |k,v| v.uris }.flatten.uniq
end
+ #drop all policies in a policies instance
+ def names
+ out = []
+ @policies.each do |name, policy|
+ out << name
+ end
+ return out
+ end
+
#loads a default policy template in policies instance
def load_default_policy(user, uri, group="member")
template = case user
diff --git a/lib/task.rb b/lib/task.rb
index 4d1ee90..3c6aba5 100644
--- a/lib/task.rb
+++ b/lib/task.rb
@@ -49,12 +49,12 @@ module OpenTox
cpu_load = `cat /proc/loadavg`.split(/\s+/)[0..2].collect{|c| c.to_f}
nr_cpu_cores = `cat /proc/cpuinfo |grep "cpu cores"|cut -d ":" -f2|tr -d " "`.split("\n").collect{|c| c.to_i}.inject{|sum,n| sum+n}
nr_cpu_cores = 1 if !nr_cpu_cores
- if cpu_load[0] > nr_cpu_cores and cpu_load[0] > cpu_load[1] and cpu_load[1] > cpu_load[2] # average CPU load of the last minute is high and CPU load is increasing
- LOGGER.warn "Cannot start task - CPU load too high (#{cpu_load.join(", ")})"
- task.cancel
- return task
- #raise "Server too busy to start a new task"
- end
+ #if cpu_load[0] > nr_cpu_cores and cpu_load[0] > cpu_load[1] and cpu_load[1] > cpu_load[2] # average CPU load of the last minute is high and CPU load is increasing
+ # LOGGER.warn "Cannot start task - CPU load too high (#{cpu_load.join(", ")})"
+ # task.cancel
+ # return task
+ # #raise "Server too busy to start a new task"
+ #end
task_pid = Spork.spork(:logger => LOGGER) do
LOGGER.debug "Task #{task.uri} started #{Time.now}"