lib/oxd/oxd_connector.rb in oxd-ruby-0.1.7 vs lib/oxd/oxd_connector.rb in oxd-ruby-0.1.8

- old
+ new

@@ -1,7 +1,10 @@ require 'socket' require 'ipaddr' +require 'net/http' +require 'json' +require 'uri' # @author Inderpal Singh # @note supports oxd-version 2.4.4 module Oxd @@ -53,42 +56,72 @@ end # close connection if(socket.close) logger(:log_msg => "Client: oxd_socket_connection : disconnected.", :error => "") end + #logger(:log_msg => response) + #abort return response end + + # method to communicate with the oxD-to-http server + # @param request [JSON] representation of the JSON command string + # @param char_count [Integer] number of characters to read from response + # @return response from the oxD-to-http server + def oxd_http_request(requst, command = "") + uri = URI.parse("https://127.0.0.1/"+command) + http = Net::HTTP.new("127.0.0.1", 8443) + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + request = Net::HTTP::Post.new(uri.request_uri) + request.add_field('Content-Type', 'application/json') + request.body = requst + response = http.request(request) + response2 = response.body + return response2 + end - # method to send commands to the oxD server and to recieve the response via {#oxd_socket_request} + # @param comm [String] command string for oxd-to-http + # method to send commands to the oxD server and oxd-to-http and to recieve the response via {#oxd_socket_request} # @return [JSON] @response_object : response from the oxd server in JSON form - def request + def request(comm = "") + uri = URI.parse(@configuration.authorization_redirect_uri) logger(:log_msg => "Please enable SSL on your website or check URIs in Oxd configuration.") if (uri.scheme != 'https') validate_command - jsondata = getData.to_json - if(!is_json? (jsondata)) - logger(:log_msg => "Sending parameters must be JSON. Exiting process.") - end - length = jsondata.length - if( length <= 0 ) - logger(:log_msg => "JSON data length must be more than zero. Exiting process.") + + if(@configuration.oxd_host_port == 8099) + jsondata = getData.to_json + if(!is_json? (jsondata)) + logger(:log_msg => "Sending parameters must be JSON. Exiting process.") + end + length = jsondata.length + if( length <= 0 ) + logger(:log_msg => "JSON data length must be more than zero. Exiting process.") + else + length = length <= 999 ? sprintf('0%d', length) : length + end + @response_json = oxd_socket_request((length.to_s + jsondata).encode("UTF-8")) + @response_json.sub!(@response_json[0..3], "") else - length = length <= 999 ? sprintf('0%d', length) : length + jsondata = getData2.to_json + @response_json = oxd_http_request(jsondata, comm) end - @response_json = oxd_socket_request((length.to_s + jsondata).encode("UTF-8")) - @response_json.sub!(@response_json[0..3], "") + if (@response_json) response = JSON.parse(@response_json) if (response['status'] == 'error') logger(:log_msg => "OxD Server Error : #{response['data']['error_description']}") - elsif (response['status'] == 'ok') + elsif (response['status'] == 'ok') + @response_object = JSON.parse(@response_json) end else logger(:log_msg => "Response is empty. Exiting process.") end + return @response_object end # @return [Mixed] @response_object set by request method def getResponseObject @@ -110,10 +143,15 @@ # @return [Array] @data def getData @data = {'command' => @command, 'params' => @params} return @data end + + def getData2 + @data = @params + return @data + end # checks whether the passed string is in JSON format or not # @param string_to_validate [String] # @return [Boolean] def is_json? (string_to_validate) @@ -135,6 +173,6 @@ @logger.info(args[:log_msg]) raise (args[:error] || args[:log_msg]) if args[:error] != "" end end -end \ No newline at end of file +end