class Aws::Plugins::SQSMd5s::Handler

@api private

def call(context)

def call(context)
  @handler.call(context).on_success do |response|
    case context.operation_name
    when :send_message
      validate_send_message(context, response)
    when :send_message_batch
      validate_send_message_batch(context, response)
    end
  end
end

def encode_length_and_bytes(bytes)

def encode_length_and_bytes(bytes)
  [bytes.bytesize, bytes].pack('L>a*'.freeze)
end

def encode_length_and_string(string)

def encode_length_and_string(string)
  string = String.new(string)
  string.encode!(NORMALIZED_ENCODING)
  encode_length_and_bytes(string)
end

def md5_of_message_attributes(message_attributes)

def md5_of_message_attributes(message_attributes)
  encoded = { }
  message_attributes.each do |name, attribute|
    name = name.to_s
    encoded[name] = String.new
    data_type_without_label = DATA_TYPE.match(attribute[:data_type])[1]
    encoded[name] << encode_length_and_bytes(name) <<
    encode_length_and_bytes(attribute[:data_type]) <<
    [TRANSPORT_TYPE_ENCODINGS[data_type_without_label]].pack('C'.freeze)
    if attribute[:string_value] != nil
      encoded[name] << encode_length_and_string(attribute[:string_value])
    elsif attribute[:binary_value] != nil
      encoded[name] << encode_length_and_bytes(attribute[:binary_value])
    end
  end
  buffer = encoded.keys.sort.reduce(String.new) do |string, name|
    string << encoded[name]
  end
  OpenSSL::Digest::MD5.hexdigest(buffer)
end

def md5_of_message_body(message_body)

def md5_of_message_body(message_body)
  OpenSSL::Digest::MD5.hexdigest(message_body)
end

def mismatch_error_message(section, local_md5, returned_md5, response)

def mismatch_error_message(section, local_md5, returned_md5, response)
  m = "MD5 returned by SQS does not match " <<
  "the calculation on the original request. ("
  if response.respond_to?(:id) && !response.id.nil?
    m << "Message ID: #{response.id}, "
  end
  m << "MD5 calculated by the #{section}: " <<
  "'#{local_md5}', MD5 checksum returned: '#{returned_md5}')"
end

def validate_attributes(attributes, response)

def validate_attributes(attributes, response)
  calculated_md5 = md5_of_message_attributes(attributes)
  returned_md5 = response.md5_of_message_attributes
  if returned_md5 != calculated_md5
    error_message =  mismatch_error_message(
      'message attributes',
      calculated_md5,
      returned_md5,
      response)
    raise Aws::Errors::ChecksumError, error_message
  end
end

def validate_body(body, response)

def validate_body(body, response)
  calculated_md5 = md5_of_message_body(body)
  returned_md5 = response.md5_of_message_body
  if calculated_md5 != returned_md5
    error_message =  mismatch_error_message(
      'message body',
      calculated_md5,
      returned_md5,
      response)
    raise Aws::Errors::ChecksumError, error_message
  end
end

def validate_send_message(context, response)

def validate_send_message(context, response)
  body = context.params[:message_body]
  attributes = context.params[:message_attributes]
  validate_single_message(body, attributes, response)
end

def validate_send_message_batch(context, response)

def validate_send_message_batch(context, response)
  context.params[:entries].each do |entry|
    id = entry[:id]
    body = entry[:message_body]
    attributes = entry[:message_attributes]
    message_response = response.successful.select { |r| r.id == id }[0]
    unless message_response.nil?
      validate_single_message(body, attributes, message_response)
    end
  end
end

def validate_single_message(body, attributes, response)

def validate_single_message(body, attributes, response)
  validate_body(body, response)
  validate_attributes(attributes, response) unless attributes.nil?
end