Class: Aspera::Plugins::Node::NodeEmulatorServlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Includes:
ServletHelpers
Defined in:
lib/aspera/plugins/node/emulator.rb

Overview

Answers a subset of the Node API, transfers are delegated to an Emulator a new instance is created for each request

Constant Summary collapse

PATH_TRANSFERS =
'/ops/transfers'
PATH_ONE_TRANSFER =
%r{/ops/transfers/(.+)$}
PATH_BROWSE =
'/files/browse'
REALM =
'Aspera Node Emulator'
CANCEL_STATUSES =

PUT values of status that cancel the transfer (pause and resume are not supported)

%w[canceled cancelled stopped].freeze
MODIFIABLE =

PUT fields modified by transferd

%w[target_rate_kbps min_rate_kbps rate_policy].freeze
JSON_METHODS =

HTTP methods with a JSON body

%w[POST PUT].freeze

Instance Method Summary collapse

Methods included from ServletHelpers

#authorization, #basic_authorized?, #check_request, host_name, #json_error, #json_response, origin, same_origin?

Constructor Details

#initialize(server, config, simulator) ⇒ NodeEmulatorServlet

Create the servlet, with optional Basic authentication

Parameters:

  • server (WEBrick::HTTPServer)
  • config (Hash) —

    username and password (Basic authentication expected from clients, optional)

  • simulator (Emulator)


611
612
613
614
615
# File 'lib/aspera/plugins/node/emulator.rb', line 611

def initialize(server, config, simulator)
  super(server)
  @simulator = simulator
  @expected_auth = "#{config[:username]}:#{config[:password]}" unless config[:username].nil?
end

Instance Method Details

#do_CANCEL(request, response) ⇒ Object

Node API cancels a transfer with HTTP verb CANCEL

Parameters:

  • request (WEBrick::HTTPRequest)
  • response (WEBrick::HTTPResponse)


694
695
696
697
# File 'lib/aspera/plugins/node/emulator.rb', line 694

def do_CANCEL(request, response)
  known_transfer(@simulator.cancel(transfer_id(request)))
  response.status = 204
end

#do_GET(request, response) ⇒ Object

Get the node information, the list of transfers, or one transfer

Parameters:

  • request (WEBrick::HTTPRequest)
  • response (WEBrick::HTTPResponse)


654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/aspera/plugins/node/emulator.rb', line 654

def do_GET(request, response)
  json =
    case request.path
    when '/info' then @simulator.info
    when PATH_TRANSFERS
      @simulator.transfers(
        active_only: query_boolean(request, 'active_only'),
        direction:   request.query['direction']&.to_s,
        count:       query_positive(request, 'count')
      )
    when PATH_ONE_TRANSFER then known_transfer(@simulator.transfer(transfer_id(request)))
    else raise WEBrick::HTTPStatus::NotFound, "Unknown path: #{request.path}"
    end
  set_json_response(request, response, json)
end

#do_POST(request, response) ⇒ Object

Start a transfer, or list a folder

Parameters:

  • request (WEBrick::HTTPRequest)
  • response (WEBrick::HTTPResponse)


638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/aspera/plugins/node/emulator.rb', line 638

def do_POST(request, response)
  json =
    case request.path
    when PATH_TRANSFERS
      @simulator.transfer(@simulator.start(JSON.parse(request.body.to_s)))
    when PATH_BROWSE
      req = JSON.parse(request.body.to_s)
      @simulator.browse(req['path'] || '/', skip: req['skip'].to_i, count: req['count']&.to_i)
    else raise WEBrick::HTTPStatus::NotFound, "Unknown path: #{request.path}"
    end
  set_json_response(request, response, json)
end

#do_PUT(request, response) ⇒ Object

Modify a transfer, or cancel it with status

Parameters:

  • request (WEBrick::HTTPRequest)
  • response (WEBrick::HTTPResponse)

Raises:

  • (WEBrick::HTTPStatus::BadRequest)


673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/aspera/plugins/node/emulator.rb', line 673

def do_PUT(request, response)
  id = transfer_id(request)
  changes = JSON.parse(request.body.to_s)
  raise WEBrick::HTTPStatus::BadRequest, 'Body must be a JSON object' unless changes.is_a?(Hash)
  status = changes.delete('status')
  if status.nil?
    unsupported = changes.keys - MODIFIABLE
    raise WEBrick::HTTPStatus::BadRequest, "Cannot modify: #{unsupported.join(', ')}" unless unsupported.empty?
    raise WEBrick::HTTPStatus::BadRequest, 'Nothing to modify' if changes.empty?
    found = @simulator.modify(id, changes)
  else
    raise WEBrick::HTTPStatus::BadRequest, "Unsupported status: #{status}" unless CANCEL_STATUSES.include?(status)
    found = @simulator.cancel(id)
  end
  known_transfer(found)
  set_json_response(request, response, @simulator.transfer(id))
end

#service(request, response) ⇒ Object

Refuse requests of web pages, check authentication, dispatch to do_<verb>, and send errors in Node API format

Parameters:

  • request (WEBrick::HTTPRequest)
  • response (WEBrick::HTTPResponse)


620
621
622
623
624
625
626
627
628
629
630
631
632
633
# File 'lib/aspera/plugins/node/emulator.rb', line 620

def service(request, response)
  check_request(request, json: JSON_METHODS.include?(request.request_method))
  return set_error(request, response, 401, 'Invalid or missing credentials', headers: {'WWW-Authenticate' => %Q(Basic realm="#{REALM}")}) unless authorized?(request)
  super
rescue WEBrick::HTTPStatus::Error => e
  set_error(request, response, e.code, e.message)
rescue JSON::ParserError, AssertError, Aspera::Transfer::Error => e
  set_error(request, response, 400, e.message)
rescue Errno::ENOENT => e
  set_error(request, response, 404, e.message)
rescue StandardError => e
  Log.log.error { "#{request.request_method} #{request.path}: #{e.class}: #{e.message}" }
  set_error(request, response, 500, e.message)
end