dimanche 29 mai 2016

ruby on rails web service not working after upgrade to rails 4.x

some time ago, i've written a web application using RoR v.3.2.13... all was tested and working correctly for some time... now, i'm going to update all the environment to rails 4.2.5! i've found a lot of issues upgrading the framework, all was solved almost easily finding a lot of suggestions on the web!!

the last one is giving me some more troubles...

well, i've the needing to upload an .xml stream from a palm device to the server to update some database records....

this is an example of the .xml stream the mobile terminal is sending to the server:

<?xml version="1.0" encoding="utf-8"?>
<readings>
 <reading>
  <opercode>a001a</opercode>
  <barcode>000073680</barcode>
  <opid>4</opid>
  <datetime>2012-01-22T00:07:34+01:00</datetime>
 </reading>
 <reading>
  <opercode>a001a</opercode>
  <barcode>000073679</barcode>
  <opid>4</opid>
  <datetime>2012-01-22T00:07:38+01:00</datetime>
 </reading>
</readings>

as you can see, it contains a bounch of readings containing four fields each.

in case the wireless connection is not working, i've built a view that contains an html form and a file upload field....

the controller receives the stream using the same action as you can see here:

.........................
#
# receives the readings stream and passes it to the parser
def upload
  #
  # different part of code if request is from .html o .xml
  respond_to do |format|
    # the request is from .html page
    format.html do
      # checks the uploaded file 'content type'
      if params[:readings_file].instance_variable_get(:@content_type) == "text/xml"
        # the file is .xml
        # identifies the readings operator_id
        operator_id = Operator.find_by_code(params[:readings_file].instance_variable_get(:@original_filename)[0..4]).id
        # retrieves the number of readings stored into .xml file
        original_filename = params[:readings_file].instance_variable_get(:@original_filename).partition(".")[0]
        expected_readings = original_filename.partition("-P-")[2].to_i != 0 ? original_filename.partition("-P-")[2].to_i : original_filename.partition("--")[2].to_i
        # binds the temporary file for nokogiri parsing
        temp_file = File.open(params[:readings_file].instance_variable_get(:@tempfile))
        readings_to_be_loaded = Nokogiri::XML(temp_file)
        # parses the readings records
        result = parse_xml(readings_to_be_loaded, operator_id)
        # checks parsing result against expected readings count
        if result != expected_readings
          message = "WARNING - Inside .xml " + expected_readings.to_s + " records was present, only " + result.to_s + " has been accepted as valid readings!!"
        else
          message = "OK - .xml stream succesfully parsed!"
        end
      else
        # the file is not .xml
        message = "ERROR - Invalid readings file format detected --> 'not xml'!!"
      end
      logger.debug message
      redirect_to readings_path, :flash => {:message => message} and return
    end
    #
    # the request is from .xml stream
    format.xml  do
      if params[:readings].present?
        # determines the number of expected readings inside the stream and retrieves the operator code
        if params[:readings][:reading][0].nil?
          expected_readings = 1
          oper_code = params[:readings][:reading][:opercode]
        else
          expected_readings = params[:readings][:reading].count
          oper_code = params[:readings][:reading][0][:opercode]
        end
        # initializes the good readings counter
        readings_count = 0
        ........

        omissis

        ........
      else
        # the stream don't contains readings, return the http error code 406 -> 'Not Acceptable'
        logger.debug "ERROR - Readings file content is incorrectly formatted!!"
        render nothing: true, status: 406 and return
      end
    end
  end
  # not .xml request nor html upload
  # nothing has been processed, ingores the upload and returns status code 404 -> 'resource not found'
  logger.debug "ERROR - Incorrect data stream content"
  render :nothing => true, status: 404 and return
end
................

then differentiate action if the data stream was uploaded from html as file or directly as .xml stream.

now, if i upload the file using the html view, all works fine (both rails 3.2 or 4.2 are correctly working and parsing readings)....

to simulate the direct .xml upload action using console on terminal, i use this command line:

curl -v -H "Content-Type: application/xml; charset=utf-8" --data-ascii @a001a-12115-81843-P-5.xml http://localhost:3000/readings/upload.xml

if the stream is uploaded directly using .xml format... rails 3.2 runs fine, rails 4.2 returns "406 Not acceptable" (see extracted logs row!)

log from rails 3.2:

Started POST "/readings/upload.xml" for 127.0.0.1 at 2015-01-22 21:32:35 +0100
Processing by ReadingsController#upload as XML
  Parameters: {"readings"=>{"reading"=>[{"opercode"=>"a001a", "barcode"=>"000073685", "opid"=>"4", "datetime"=>"2015-01-21T20:18:20+01:00"}, {"opercode"=>"a001a", "barcode"=>"000073683", "opid"=>"4", "datetime"=>"2015-01-21T20:18:24+01:00"}, {"opercode"=>"a001a", "barcode"=>"000073682", "opid"=>"4", "datetime"=>"2015-01-21T20:18:28+01:00"}, {"opercode"=>"a001a", "barcode"=>"000073679", "opid"=>"4", "datetime"=>"2015-01-21T20:18:36+01:00"}, {"opercode"=>"a001a", "barcode"=>"000073683", "opid"=>"4", "datetime"=>"2015-01-21T20:18:41+01:00"}]}}
  Rendered text template (0.0ms)
Completed 200 OK in 49ms (Views: 48.2ms | ActiveRecord: 0.0ms)

log from rails 4.2:

Started POST "/readings/upload.xml" for 127.0.0.1 at 2016-05-27 21:11:38 +0200
Processing by ReadingsController#upload as XML
ERROR - Readings file content is incorrectly formatted!!
  Rendered text template (0.0ms)
Completed 406 Not Acceptable in 2ms (Views: 0.2ms | ActiveRecord: 0.0ms)

it seems like parameters are not reaching the controller action at all!! (i suppose this is due to some kind of restriction on 4.2 version of framework but i'm unable to find any specific info confirming or disproving my suspects)

PLS NOTE: if i try to send the stream directly using the palm device, i receive the same response!!

any suggestion is well accepted,

many thanks in advance to you all... Francesco

Aucun commentaire:

Enregistrer un commentaire