class RemoteAttachmentDownloader {

    public static int retrieveRemoteAttachments(Supplier<HttpClient> httpClient, String dbAttachments, Duration downloadTimeout) throws MalformedURLException {
        AttachmentConnector attachmentConnector = new AttachmentConnector(httpClient, dbAttachments, downloadTimeout);
        AttachmentRepository attachmentRepository = new AttachmentRepository(new DatabaseConnector(httpClient, dbAttachments));

        List<AttachmentContent> remoteAttachments = attachmentRepository.getOnlyRemoteAttachments();
        log.info(format("we have %d remote attachments to retrieve", remoteAttachments.size()));

        int count = 0;

        for (AttachmentContent attachmentContent : remoteAttachments) {
            if (!attachmentContent.isOnlyRemote()) {
                log.info(format("skipping attachment (%s), which should already be available", attachmentContent.getId()));
                continue;
            }

            String attachmentContentId = attachmentContent.getId();
            log.info(format("retrieving attachment (%s) {filename=%s}", attachmentContentId, attachmentContent.getFilename()));
            log.debug("url is " + attachmentContent.getRemoteUrl());

            InputStream content = null;
            try {
                content = attachmentConnector.unsafeGetAttachmentStream(attachmentContent);
                if (content == null) {
                    log.error("null content retrieving attachment " + attachmentContentId);
                    continue;
                }
                try {
                    long length = length(content);
                    log.info(format("retrieved attachment (%s), it was %d bytes long", attachmentContentId, length));
                    count++;
                } catch (IOException e) {
                    log.error("attachment was downloaded but somehow not available in database " + attachmentContentId, e);
                }
            } catch (SW360Exception e) {
                log.error("cannot retrieve attachment " + attachmentContentId, e);
            } finally {
                closeQuietly(content, log);
            }
        }

        return count;
    }

}