def download(destination, gav)
      raise 'Download destination must not be empty' if destination.empty?
      create_target(destination) # ensure directory path is created
      destination = File.expand_path(destination)
      if File.directory?(destination)
        dstfile = File.expand_path("#{destination}/#{gav.filename}")
      else
        dstfile = File.expand_path(destination)
      end
      # if the file already exists at the destination path than we don't need to download it again
      if sha_match?(dstfile, gav)
        # create a file that stores the sha1 for faster file comparisions later
        # This will only get created when the sha1 matches
        write_sha1("#{dstfile}", gav.sha1)
        if use_cache and not cache.exists?(gav)
          cache.add_file(gav, dstfile)
        end
        return true
      end
      # remove the previous sha1 file if it already exists
      FileUtils.rm("#{dstfile}.sha1") if File.exists?("#{dstfile}.sha1")

      if gav.sha1.nil?
        gav.sha1 = gav_data(gav)['sha1']
      end
      # use the cache if the file is in the cache
      if use_cache and cache.exists?(gav)
        cache_file_path = cache.file_path(gav)
        FileUtils.copy(cache_file_path, dstfile)
        cache.record_hit(gav)
      else
        request = Typhoeus::Request.new(
        "#{host_url}/service/local/artifact/maven/redirect",
        :params  => gav.to_hash,
        :connecttimeout => 5,
        :followlocation => true
        )
        request.on_failure do |response|
          raise("Failed to download #{gav.to_s}")
        end

        # when complete, lets write the data to the file
        # first lets compare the sha matches
        # if the gav we thought we downloaded has the same checksum, were are good
        request.on_complete do |response|
          File.open(dstfile, 'wb') { |f| f.write(response.body) } unless ! response.success?
          if not sha_match?(dstfile, gav, false)
            raise("Error sha1 mismatch gav #{gav.sha1} != #{sha(dstfile)}")
          end
          gav.attributes[:size] =  File.size(dstfile)
          gav.attributes[:total_time] =  response.options[:total_time]

          # lets cache the file if cache is on
          if use_cache
            cache.add_file(gav, dstfile)
          end
          dstfile
        end
        request.run
        dstfile
      end
      dstfile
    end
def download(destination, gav)
      raise 'Download destination must not be empty' if destination.empty?
      create_target(destination) # ensure directory path is created
      destination = File.expand_path(destination)
      if File.directory?(destination)
        dstfile = File.expand_path("#{destination}/#{gav.filename}")
      else
        dstfile = File.expand_path(destination)
      end
      # if the file already exists at the destination path than we don't need to download it again
      if sha_match?(dstfile, gav)
        # create a file that stores the sha1 for faster file comparisions later
        # This will only get created when the sha1 matches
        write_sha1("#{dstfile}", gav.sha1)
        if use_cache and not cache.exists?(gav)
          cache.add_file(gav, dstfile)
        end
        return true
      end
      # remove the previous sha1 file if it already exists
      FileUtils.rm("#{dstfile}.sha1") if File.exists?("#{dstfile}.sha1")

      if gav.sha1.nil?
        gav.sha1 = gav_data(gav)['sha1']
      end
      # use the cache if the file is in the cache
      if use_cache and cache.exists?(gav)
        cache_file_path = cache.file_path(gav)
        FileUtils.copy(cache_file_path, dstfile)
        cache.record_hit(gav)
      else
        request = Typhoeus::Request.new(
        "#{host_url}/service/local/artifact/maven/redirect",
        :params  => gav.to_hash,
        :connecttimeout => 5,
        :followlocation => true
        )
        request.on_failure do |response|
          raise("Failed to download #{gav.to_s}")
        end

        # when complete, lets write the data to the file
        # first lets compare the sha matches
        # if the gav we thought we downloaded has the same checksum, were are good
        request.on_complete do |response|
          File.open(dstfile, 'wb') { |f| f.write(response.body) } unless ! response.success?
          if not sha_match?(dstfile, gav, false)
            raise("Error sha1 mismatch gav #{gav.sha1} != #{sha(dstfile)}")
          end
          gav.attributes[:size] =  File.size(dstfile)
          gav.attributes[:total_time] =  response.options[:total_time]

          # lets cache the file if cache is on
          if use_cache
            cache.add_file(gav, dstfile)
          end
          dstfile
        end
        request.run
        dstfile
      end
      dstfile
    end
downloads the gav to the destination, returns the file if download was successful
 if cache is on then it will use the cache and if file is new will also cache the new file
 TODO need a timeout when host is unreachable