summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormguetlein <martin.guetlein@gmail.com>2011-01-28 12:20:08 +0100
committermguetlein <martin.guetlein@gmail.com>2011-01-28 12:20:08 +0100
commite1a067953dd9139b01aaebe42ff158a944240540 (patch)
treef81e2027b2fd36660f6d12427500c7b1b7fa6d9f
parent171ab814d15b9504ef9892ba5f194de8bc019f46 (diff)
extend whitelisting, get feature_type from algorithm
-rw-r--r--lib/algorithm.rb5
-rw-r--r--lib/authorization.rb18
-rw-r--r--lib/dataset.rb1
-rw-r--r--lib/feature.rb3
-rw-r--r--lib/model.rb24
-rw-r--r--lib/task.rb20
6 files changed, 52 insertions, 19 deletions
diff --git a/lib/algorithm.rb b/lib/algorithm.rb
index 58a2640..ee3109c 100644
--- a/lib/algorithm.rb
+++ b/lib/algorithm.rb
@@ -34,9 +34,10 @@ module OpenTox
# Find Generic Opentox Algorithm via URI, and loads metadata
# @param [String] uri Algorithm URI
# @return [OpenTox::Algorithm::Generic] Algorithm instance, nil if alogrithm was not found
- def self.find(uri)
+ def self.find(uri, subjectid)
+ return nil unless uri
alg = Generic.new(uri)
- alg.load_metadata
+ alg.load_metadata( subjectid )
if alg.metadata==nil or alg.metadata.size==0
nil
else
diff --git a/lib/authorization.rb b/lib/authorization.rb
index a6253b7..1573da3 100644
--- a/lib/authorization.rb
+++ b/lib/authorization.rb
@@ -328,7 +328,10 @@ module OpenTox
# @param [String] subjectid
# @return [Boolean] true if access granted, else otherwise
def self.authorized?(uri, request_method, subjectid)
- return true if OpenTox::Authorization.whitelisted?(uri, request_method)
+ if OpenTox::Authorization.whitelisted?(uri, request_method)
+ LOGGER.debug "whitelisted! "+uri.to_s
+ return true
+ end
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: #{uri}, subjectid: #{subjectid} with return >>#{ret}<<"
@@ -346,8 +349,12 @@ module OpenTox
private
def self.whitelisted?(uri, request_method)
return false unless @@whitelist[request_method]
- @@whitelist[request_method].each do |r|
- return true if r.match(uri)
+ @@whitelist[request_method].each do |regexp,invert|
+ if invert
+ return true if !regexp.match(uri)
+ else
+ return true if regexp.match(uri)
+ end
end
return false
end
@@ -356,7 +363,8 @@ module OpenTox
# adds uri/regexp-for-matching-uri to the whitelist for a request-method (i.e. access will be granted without cheking the A&A service)
# @param [String or Regexp] uri_match if string match must be ecaxt
# @param [String] request_method, must be GET, POST, PUT, DELETE
- def self.whitelist(uri_match, request_method)
+ # @param [Boolean,optional] invert, set to true if you want to whitelist everything that does not match (careful!)
+ def self.whitelist(uri_match, request_method, invert=false)
if uri_match.is_a?(Regexp)
uri_regex = uri_match
elsif uri_match.is_a?(String)
@@ -366,7 +374,7 @@ module OpenTox
end
LOGGER.info("whitelisted "+request_method.to_s+" "+uri_regex.to_s)
@@whitelist[request_method] = [] unless @@whitelist[request_method]
- @@whitelist[request_method] << uri_regex
+ @@whitelist[request_method] << [ uri_regex, invert ]
end
end
diff --git a/lib/dataset.rb b/lib/dataset.rb
index 640e3da..9c20968 100644
--- a/lib/dataset.rb
+++ b/lib/dataset.rb
@@ -51,6 +51,7 @@ module OpenTox
# @param [String] uri Dataset URI
# @return [OpenTox::Dataset] Dataset object with all data
def self.find(uri, subjectid=nil)
+ return nil unless uri
dataset = Dataset.new(uri, subjectid)
dataset.load_all(subjectid)
dataset
diff --git a/lib/feature.rb b/lib/feature.rb
index 28ac0c5..be063dd 100644
--- a/lib/feature.rb
+++ b/lib/feature.rb
@@ -3,7 +3,8 @@ module OpenTox
include OpenTox
def self.find(uri, subjectid=nil)
- feature = Feature.new uri
+ return nil unless uri
+ 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", :subjectid => subjectid}))
else
diff --git a/lib/model.rb b/lib/model.rb
index 741eea6..80d7ec4 100644
--- a/lib/model.rb
+++ b/lib/model.rb
@@ -8,13 +8,16 @@ module OpenTox
# @param [Hash] params Parameters for OpenTox model
# @param [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly
# @return [text/uri-list] Task or resource URI
- def run( params, waiting_task=nil )
- if CONFIG[:yaml_hosts].include?(URI.parse(@uri).host)
- accept = 'application/x-yaml'
- else
- accept = 'application/rdf+xml'
+ def run( params, accept_header=nil, waiting_task=nil )
+ unless accept_header
+ if CONFIG[:yaml_hosts].include?(URI.parse(@uri).host)
+ accept_header = 'application/x-yaml'
+ else
+ accept_header = 'application/rdf+xml'
+ end
end
- RestClientWrapper.post(@uri,{:accept => accept},params,waiting_task).to_s
+ LOGGER.info "running model "+@uri.to_s+", params: "+params.inspect+", accept: "+accept_header.to_s
+ RestClientWrapper.post(@uri,{:accept => accept_header},params,waiting_task).to_s
end
# Generic OpenTox model class for all API compliant services
@@ -25,6 +28,7 @@ module OpenTox
# @param [String] uri Model URI
# @return [OpenTox::Model::Generic] Model instance, nil if model was not found
def self.find(uri,subjectid=nil)
+ return nil unless uri
model = Generic.new(uri)
model.load_metadata(subjectid)
if model.metadata==nil or model.metadata.size==0
@@ -39,9 +43,12 @@ module OpenTox
def feature_type(subjectid=nil)
# dynamically perform restcalls if necessary
load_metadata(subjectid) if @metadata==nil or @metadata.size==0 or (@metadata.size==1 && @metadata.values[0]==@uri)
+
+ @algorithm = OpenTox::Algorithm::Generic.find(@metadata[OT.algorithm], subjectid) unless @algorithm
+ algorithm_title = @algorithm ? @algorithm.metadata[DC.title] : nil
@dependentVariable = OpenTox::Feature.find( @metadata[OT.dependentVariables],subjectid ) unless @dependentVariable
- [@dependentVariable.feature_type, @metadata[OT.isA], @metadata[DC.title], @uri].each do |type|
+ [@dependentVariable.feature_type, @metadata[OT.isA], @metadata[DC.title], @uri, algorithm_title].each do |type|
case type
when /(?i)classification/
return "classification"
@@ -49,7 +56,8 @@ module OpenTox
return "regression"
end
end
- raise "unknown model "+[@dependentVariable.feature_type, @metadata[OT.isA], @metadata[DC.title], @uri].inspect
+ raise "unknown model "+[@dependentVariable.feature_type, @metadata[OT.isA],
+ @metadata[DC.title], @uri, algorithm_title].inspect
end
end
diff --git a/lib/task.rb b/lib/task.rb
index 3c6aba5..74940de 100644
--- a/lib/task.rb
+++ b/lib/task.rb
@@ -78,6 +78,7 @@ module OpenTox
# @param [String] uri Task URI
# @return [OpenTox::Task] Task object
def self.find(uri)
+ return nil unless uri
task = Task.new(uri)
task.load_metadata
task
@@ -94,10 +95,23 @@ module OpenTox
@metadata = YAML.load(yaml)
end
+
def self.from_rdfxml(rdfxml)
- file = Tempfile.open("ot-rdfxml"){|f| f.write(rdfxml)}.path
+ file = Tempfile.new("ot-rdfxml")
+ file.puts rdfxml
+ file.close
+ file = "file://"+file.path
+
+ # PENDING
+ raise "Parse from file not working: what is the base-object-uri??? (omitted in triples)"
+
parser = Parser::Owl::Generic.new file
- @metadata = parser.load_metadata
+ metadata = parser.load_metadata
+ puts metadata.inspect
+
+ task = Task.new(uri)
+ task.add_metadata(metadata)
+ task
end
def to_rdfxml
@@ -232,7 +246,7 @@ module OpenTox
sleep dur
load_metadata
# if another (sub)task is waiting for self, set progress accordingly
- waiting_task.progress(@metadata[OT.percentageCompleted]) if waiting_task
+ waiting_task.progress(@metadata[OT.percentageCompleted].to_f) if waiting_task
check_state
if (Time.new > due_to_time)
raise "max wait time exceeded ("+DEFAULT_TASK_MAX_DURATION.to_s+"sec), task: '"+@uri.to_s+"'"