myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [2923] trunk: refactored XML generation within RE


From: noreply
Subject: [myexperiment-hackers] [2923] trunk: refactored XML generation within REST API in anticipation of JSON support
Date: Thu, 2 Feb 2012 11:04:53 -0500 (EST)

Revision
2923
Author
dgc
Date
2012-02-02 11:04:53 -0500 (Thu, 02 Feb 2012)

Log Message

refactored XML generation within REST API in anticipation of JSON support

Modified Paths

Diff

Modified: trunk/app/controllers/api_controller.rb (2922 => 2923)


--- trunk/app/controllers/api_controller.rb	2012-02-02 15:39:11 UTC (rev 2922)
+++ trunk/app/controllers/api_controller.rb	2012-02-02 16:04:53 UTC (rev 2923)
@@ -3,7 +3,6 @@
 # Copyright (c) 2007 University of Manchester and the University of Southampton.
 # See license.txt for details.
 
-require 'rexml/document'
 require 'base64'
 require 'lib/rest'
 
@@ -14,6 +13,15 @@
     # all responses from the API are in XML
     response.content_type = "application/xml"
 
+    rest_response = process_request_aux
+
+    render(:xml => rest_response[:xml].to_s, :status => rest_response[:status])
+  end
+
+private 
+
+  def process_request_aux
+
     user = current_user
 
     auth = request.env["HTTP_AUTHORIZATION"]
@@ -70,10 +78,10 @@
     end  
 
     case rules['Type']
-      when 'index'; doc = rest_index_request(params[:uri], params[:format], rules, user, request.query_parameters)
-      when 'crud';  doc = rest_crud_request(params[:uri], params[:format], rules, user, request.query_parameters)
-      when 'call';  doc = rest_call_request(params[:uri], params[:format], rules, user, request.query_parameters)
-      else;         return rest_response(500, :reason => "Unknown REST table type")
+      when 'index'; rest_index_request(params[:uri], params[:format], rules, user, request.query_parameters)
+      when 'crud';  rest_crud_request(params[:uri], params[:format], rules, user, request.query_parameters)
+      when 'call';  rest_call_request(params[:uri], params[:format], rules, user, request.query_parameters)
+      else;         rest_response(500, :reason => "Unknown REST table type")
     end
   end
 end

Modified: trunk/lib/rest.rb (2922 => 2923)


--- trunk/lib/rest.rb	2012-02-02 15:39:11 UTC (rev 2922)
+++ trunk/lib/rest.rb	2012-02-02 16:04:53 UTC (rev 2923)
@@ -94,7 +94,7 @@
     end
   end
 
-  render(:xml => doc.to_s, :status => "#{code} #{message}")
+  { :xml => doc.to_s, :status => "#{code} #{message}" }
 end
 
 def resource_preview_url(ob, type)
@@ -320,7 +320,7 @@
     root << data unless data.nil?
   end
 
-  render(:xml => doc.to_s)
+  { :xml => doc }
 end
 
 def rest_crud_request(req_uri, format, rules, user, query)
@@ -528,7 +528,7 @@
   doc = LibXML::XML::Document.new
   doc.root = root
 
-  render(:xml => doc.to_s)
+  { :xml => doc }
 end
 
 def object_owner(ob)
@@ -779,8 +779,7 @@
   resource = eval(type).find_by_id(id)
 
   if resource.nil?
-    rest_response(404, :reason => "Couldn't find a #{type} with id #{id}")
-    return nil
+    return [nil, rest_response(404, :reason => "Couldn't find a #{type} with id #{id}")]
   end
 
   if version
@@ -790,18 +789,16 @@
   end
 
   if resource.nil?
-    rest_response(404, :reason => "#{type} #{id} does not have a version #{version}")
-    return nil
+    return [nil, rest_response(404, :reason => "#{type} #{id} does not have a version #{version}")]
   end
 
   if permission
     if !Authorization.is_authorized?(permission, nil, resource, user)
-      rest_response(401, :reason => "Not authorised for #{type} #{id}")
-      return nil
+      return [nil, rest_response(401, :reason => "Not authorised for #{type} #{id}")]
     end
   end
 
-  resource
+  [resource, nil]
 end
 
 def rest_access_redirect(opts = {})
@@ -874,17 +871,17 @@
     when 'create':
       return rest_response(401, :reason => "Not authorised to create a workflow") unless Authorization.is_authorized_for_type?('create', 'Workflow', opts[:user], nil)
       if opts[:query]['id']
-        ob = obtain_rest_resource('Workflow', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+        ob, error = obtain_rest_resource('Workflow', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
       else
         ob = Workflow.new(:contributor => opts[:user])
       end
     when 'read', 'update', 'destroy':
-      ob = obtain_rest_resource('Workflow', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+      ob, error = obtain_rest_resource('Workflow', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
     else
       raise "Invalid action '#{action}'"
   end
 
-  return if ob.nil? # appropriate rest response already given
+  return error if ob.nil? # appropriate rest response already given
 
   if action == "destroy"
 
@@ -1032,12 +1029,12 @@
       return rest_response(401, :reason => "Not authorised to create a file") unless Authorization.is_authorized_for_type?('create', 'Blob', opts[:user], nil)
       ob = Blob.new(:contributor => opts[:user])
     when 'read', 'update', 'destroy':
-      ob = obtain_rest_resource('Blob', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+      ob, error = obtain_rest_resource('Blob', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
     else
       raise "Invalid action '#{action}'"
   end
 
-  return if ob.nil? # appropriate rest response already given
+  return error if ob.nil? # appropriate rest response already given
 
   if action == "destroy"
 
@@ -1143,12 +1140,12 @@
       return rest_response(401, :reason => "Not authorised to create a pack") unless Authorization.is_authorized_for_type?('create', 'Pack', opts[:user], nil)
       ob = Pack.new(:contributor => opts[:user])
     when 'read', 'update', 'destroy':
-      ob = obtain_rest_resource('Pack', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+      ob, error = obtain_rest_resource('Pack', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
     else
       raise "Invalid action '#{action}'"
   end
 
-  return if ob.nil? # appropriate rest response already given
+  return error if ob.nil? # appropriate rest response already given
 
   if action == "destroy"
 
@@ -1227,7 +1224,7 @@
 
     when 'read', 'update', 'destroy':
 
-      ob = obtain_rest_resource('PackRemoteEntry', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+      ob, error = obtain_rest_resource('PackRemoteEntry', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
 
       if ob
         return rest_response(401, :reason => "Not authorised to change the specified pack") unless Authorization.is_authorized?('edit', nil, ob.pack, opts[:user])
@@ -1237,7 +1234,7 @@
       raise "Invalid action '#{action}'"
   end
 
-  return if ob.nil? # appropriate rest response already given
+  return error if ob.nil? # appropriate rest response already given
 
   if action == "destroy"
 
@@ -1298,7 +1295,7 @@
 
     when 'read', 'update', 'destroy':
 
-      ob = obtain_rest_resource('PackContributableEntry', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+      ob, error = obtain_rest_resource('PackContributableEntry', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
 
       if ob
         return rest_response(401, :reason => "Not authorised to change the specified pack") unless Authorization.is_authorized?('edit', nil, ob.pack, opts[:user])
@@ -1308,7 +1305,7 @@
       raise "Invalid action '#{action}'"
   end
 
-  return if ob.nil? # appropriate rest response already given
+  return error if ob.nil? # appropriate rest response already given
 
   if action == "destroy"
 
@@ -1481,7 +1478,7 @@
   doc = LibXML::XML::Document.new
   doc.root = root
 
-  render(:xml => doc.to_s)
+  { :xml => doc }
 end
 
 def group_count(opts)
@@ -1492,7 +1489,7 @@
   doc = LibXML::XML::Document.new
   doc.root = root
 
-  render(:xml => doc.to_s)
+  { :xml => doc }
 end
 
 def workflow_count(opts)
@@ -1507,7 +1504,7 @@
   doc = LibXML::XML::Document.new
   doc.root = root
 
-  render(:xml => doc.to_s)
+  { :xml => doc }
 end
 
 def pack_count(opts)
@@ -1522,7 +1519,7 @@
   doc = LibXML::XML::Document.new
   doc.root = root
 
-  render(:xml => doc.to_s)
+  { :xml => doc }
 end
 
 def content_type_count(opts)
@@ -1533,7 +1530,7 @@
   doc = LibXML::XML::Document.new
   doc.root = root
 
-  render(:xml => doc.to_s)
+  { :xml => doc }
 end
 
 def get_tagged(opts)
@@ -1585,7 +1582,7 @@
     root << tag_node
   end
 
-  render(:xml => doc.to_s)
+  { :xml => doc }
 end
 
 def whoami_redirect(opts)
@@ -1672,12 +1669,12 @@
 
       ob = Comment.new(:user => opts[:user])
     when 'read', 'update', 'destroy':
-      ob = obtain_rest_resource('Comment', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+      ob, error = obtain_rest_resource('Comment', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
     else
       raise "Invalid action '#{action}'"
   end
 
-  return if ob.nil? # appropriate rest response already given
+  return error if ob.nil? # appropriate rest response already given
 
   if action == "destroy"
 
@@ -1798,12 +1795,12 @@
 
       ob = Bookmark.new(:user => opts[:user])
     when 'read', 'update', 'destroy':
-      ob = obtain_rest_resource('Bookmark', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+      ob, error = obtain_rest_resource('Bookmark', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
     else
       raise "Invalid action '#{action}'"
   end
 
-  return if ob.nil? # appropriate rest response already given
+  return error if ob.nil? # appropriate rest response already given
 
   if action == "destroy"
 
@@ -1851,12 +1848,12 @@
 
       ob = Rating.new(:user => opts[:user])
     when 'read', 'update', 'destroy':
-      ob = obtain_rest_resource('Rating', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+      ob, error = obtain_rest_resource('Rating', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
     else
       raise "Invalid action '#{action}'"
   end
 
-  return if ob.nil? # appropriate rest response already given
+  return error if ob.nil? # appropriate rest response already given
 
   if action == "destroy"
 
@@ -1907,12 +1904,12 @@
 
       ob = Tagging.new(:user => opts[:user])
     when 'read', 'update', 'destroy':
-      ob = obtain_rest_resource('Tagging', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+      ob, error = obtain_rest_resource('Tagging', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
     else
       raise "Invalid action '#{action}'"
   end
 
-  return if ob.nil? # appropriate rest response already given
+  return error if ob.nil? # appropriate rest response already given
 
   if action == "destroy"
 
@@ -1959,12 +1956,12 @@
       return rest_response(401, :reason => "Not authorised to create an ontology") unless Authorization.is_authorized_for_type?('create', 'Ontology', opts[:user], nil)
       ob = Ontology.new(:user => opts[:user])
     when 'read', 'update', 'destroy':
-      ob = obtain_rest_resource('Ontology', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+      ob, error = obtain_rest_resource('Ontology', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
     else
       raise "Invalid action '#{action}'"
   end
 
-  return if ob.nil? # appropriate rest response already given
+  return error if ob.nil? # appropriate rest response already given
 
   if action == "destroy"
 
@@ -2030,12 +2027,12 @@
       return rest_response(401, :reason => "Not authorised to create a predicate") unless Authorization.is_authorized_for_type?('create', 'Predicate', opts[:user], ontology)
       ob = Predicate.new
     when 'read', 'update', 'destroy':
-      ob = obtain_rest_resource('Predicate', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+      ob, error = obtain_rest_resource('Predicate', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
     else
       raise "Invalid action '#{action}'"
   end
 
-  return if ob.nil? # appropriate rest response already given
+  return error if ob.nil? # appropriate rest response already given
 
   if action == "destroy"
 
@@ -2093,12 +2090,12 @@
       return rest_response(401, :reason => "Not authorised to create a relationship") unless Authorization.is_authorized_for_type?('create', 'Relationship', opts[:user], context)
       ob = Relationship.new(:user => opts[:user])
     when 'read', 'update', 'destroy':
-      ob = obtain_rest_resource('Relationship', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+      ob, error = obtain_rest_resource('Relationship', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
     else
       raise "Invalid action '#{action}'"
   end
 
-  return if ob.nil? # appropriate rest response already given
+  return error if ob.nil? # appropriate rest response already given
 
   if action == "destroy"
 

Modified: trunk/test/functional/api_controller_test.rb (2922 => 2923)


--- trunk/test/functional/api_controller_test.rb	2012-02-02 15:39:11 UTC (rev 2922)
+++ trunk/test/functional/api_controller_test.rb	2012-02-02 16:04:53 UTC (rev 2923)
@@ -911,10 +911,10 @@
     # puts "Sending: #{data.inspect}"
 
     case method
-      when :get;    get(:process_request,     { :uri => uri } )
-      when :post;   post(:process_request,    { :uri => uri } )
-      when :put;    put(:process_request,     { :uri => uri } )
-      when :delete; delete(:process_request,  { :uri => uri } )
+      when :get;    get(:process_request,     { :uri => uri, :format => "xml" } )
+      when :post;   post(:process_request,    { :uri => uri, :format => "xml" } )
+      when :put;    put(:process_request,     { :uri => uri, :format => "xml" } )
+      when :delete; delete(:process_request,  { :uri => uri, :format => "xml" } )
     end
 
     # puts "Response: #{LibXML::XML::Parser.string(@response.body).parse.root.to_s}"

reply via email to

[Prev in Thread] Current Thread [Next in Thread]