Class: CPEE::ModelManagement::OpenItem

Inherits:
Riddl::Implementation
  • Object
show all
Defined in:
lib/cpee-model-management/implementation.rb

Overview

}}}

Instance Method Summary collapse

Instance Method Details

#responseObject

{{{



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/cpee-model-management/implementation.rb', line 465

def response
  where = @a[0] == :main ? '' : @r[0..-3].map{ |d| Riddl::Protocols::Utils::unescape(d) }.join('/')
  name   = File.basename(@r[-2],'.xml')
  insta  = @a[1]
  cock   = @a[2]
  views  = @a[3]
  force  = @a[4]
  models = @a[5]
  stage  = @p[0]&.value || 'draft'

  fname  = File.join(models,where,name + '.xml')

  inst = nil
  begin
    inst = if File.exist?(fname + '.active') && File.exist?(fname + '.active-uuid') && !force
      t = {
        'CPEE-INSTANCE-URL'  => File.read(fname + '.active'),
        'CPEE-INSTANCE-UUID' => File.read(fname + '.active-uuid')
      }
      status, result, headers = Riddl::Client.new(File.join(t['CPEE-INSTANCE-URL'],'properties','state')).get

      if status && status >= 200 && status < 300 && (result[0].value == 'finished' || result[0].value == 'abandoned')
        force = true
        raise
      end
      status, result, headers = Riddl::Client.new(File.join(t['CPEE-INSTANCE-URL'],'properties','attributes','uuid')).get
      if status && status >= 200 && status < 300
        t['CPEE-INSTANCE-UUID'] == result[0].value ? t : nil
      else
        nil
      end
    end || begin
      status, result, headers = Riddl::Client.new(File.join(insta,'xml')).post [
        Riddl::Parameter::Simple.new('behavior','fork_ready'),
        Riddl::Parameter::Complex.new('xml','application/xml',File.read(fname))
      ] rescue nil
      if status && status >= 200 && status < 300
        JSON::parse(result[0].value.read).tap do |t|
          File.write(File.join(fname + '.active'),t['CPEE-INSTANCE-URL'])
          File.write(File.join(fname + '.active-uuid'),t['CPEE-INSTANCE-UUID'])
        end
      else
        nil
      end
    end
  rescue => e
    retry
  end

  if inst.nil?
    @status = 400
    return Riddl::Parameter::Complex.new('nope','text/plain','No longer exists.')
  else
    insturl = inst['CPEE-INSTANCE-URL']
    @status = 302
    @headers << Riddl::Header.new('Location',cock[stage] + insturl)
  end
  nil
end