From 6d899a3c79e1f4fdb5305971bfde6520735ed388 Mon Sep 17 00:00:00 2001 From: NileshPanchal Date: Mon, 18 Jan 2016 18:19:53 +0530 Subject: [PATCH] parse_request based on content_type --- app/controllers/api_controller.rb | 7 +++--- app/models/route.rb | 38 ++++++++++++++++++------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 820b180..2c74b82 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -1,6 +1,5 @@ class ApiController < ApplicationController - def execute_route - + def execute_route if request.method == "GET" || request.method == "PUT" || request.method == "DELETE" input_data = request.query_parameters elsif request.method == "POST" @@ -14,7 +13,7 @@ def execute_route else route = Route.where(:uri => request.path).where.not(:kind => 'SOAP').first end - + if route.nil? log = {:route_id => nil, :status_code => '404', :response => nil} render status: 404, text: "#{request.path} not found." @@ -24,7 +23,7 @@ def execute_route log = {:route_id => route.id, :status_code => '405', :response => nil} render status: 405, text: "#{request.method} not allowed for #{params[:uri]} route." else - req_obj = route.parse_request(input_data) + req_obj = route.parse_request(input_data, request.content_type) if req_obj.instance_of?(Oga::XML::Document) or route.kind == 'PLAIN-TEXT' headers = {'Accept' => request.env['HTTP_ACCEPT'], 'X-QG-CI-URI' => request.env['HTTP_X_QG_CI_URI'], diff --git a/app/models/route.rb b/app/models/route.rb index 63284fb..7326524 100644 --- a/app/models/route.rb +++ b/app/models/route.rb @@ -9,7 +9,7 @@ class Route < ActiveRecord::Base validates_presence_of :uri, :kind, :http_method def self.options_for_kind - [['SOAP','SOAP'],['JSON','JSON'],['PLAIN-TEXT','PLAIN-TEXT']] + [['SOAP','SOAP'],['XML','XML'],['JSON','JSON'],['PLAIN-TEXT','PLAIN-TEXT']] end def self.options_for_http_method @@ -18,26 +18,32 @@ def self.options_for_http_method - def parse_request(req) + def parse_request(req, content_type) + # parsing of query_params is not yet supported if req.empty? return Oga.parse_xml('') end - if self.kind == "SOAP" - begin - document = Oga.parse_xml(req, :strict => true) - return document - rescue Exception => e - return {error: e.message} - end - elsif self.kind == "JSON" - begin - xml_str = Gyoku.xml(Oj.load(req)) - document = Oga.parse_xml(xml_str) - return document - rescue Exception => e - return {error: e.message} + parsed_application_type = content_type.split("/").first + parsed_content_type = content_type.split("/").last + + if (parsed_application_type == "application") + if (parsed_content_type.include?("soap") == true) || (parsed_content_type.include?("xml") == true) + begin + document = Oga.parse_xml(req, :strict => true) + return document + rescue Exception => e + return {error: e.message} + end + elsif (parsed_content_type.include?("json") == true) + begin + xml_str = Gyoku.xml(Oj.load(req)) + document = Oga.parse_xml(xml_str) + return document + rescue Exception => e + return {error: e.message} + end end else return req