Skip to content

Commit

Permalink
#1755 write less logs when downloading files
Browse files Browse the repository at this point in the history
this code logged all headers of all http responses in separate log lines. It's not needed in 99% cases.
  • Loading branch information
asolntsev committed Mar 17, 2022
1 parent fa7ee54 commit 9279791
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Expand Up @@ -40,10 +40,12 @@
import java.net.URI;
import java.security.cert.X509Certificate;
import java.util.Optional;
import java.util.stream.Stream;

import static com.codeborne.selenide.impl.Plugins.inject;
import static java.util.Collections.emptyMap;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.stream.Collectors.joining;
import static org.apache.commons.io.FileUtils.copyInputStreamToFile;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.hc.client5.http.protocol.HttpClientContext.COOKIE_STORE;
Expand Down Expand Up @@ -209,13 +211,17 @@ protected String getFileName(String fileToDownloadLocation, HttpResponse respons
}
}

log.info("Cannot extract file name from http headers. Found headers: ");
for (Header header : response.getHeaders()) {
log.info("{}={}", header.getName(), header.getValue());
}
log.info("Cannot extract file name for {}. Found headers: {}", fileToDownloadLocation, headersToString(response));

String fileNameFromUrl = httpHelper.getFileName(fileToDownloadLocation);
return isNotBlank(fileNameFromUrl) ? fileNameFromUrl : downloader.randomFileName();
String result = isNotBlank(fileNameFromUrl) ? fileNameFromUrl : downloader.randomFileName();
log.info("Generated file name for {}: {}", fileToDownloadLocation, result);
return result;
}

@Nonnull
private String headersToString(HttpResponse response) {
return Stream.of(response.getHeaders()).map(h -> h.getName() + "=" + h.getValue()).collect(joining(", "));
}

protected void saveContentToFile(CloseableHttpResponse response, File downloadedFile) throws IOException {
Expand Down
Expand Up @@ -119,13 +119,12 @@ private String getFileName(Response response) {
return httpHelper.getFileNameFromContentDisposition(response.headers)
.map(httpHelper::normalize)
.orElseGet(() -> {
log.info("Cannot extract file name from http headers. Found headers: ");
for (Map.Entry<String, String> header : response.headers.entrySet()) {
log.info("{}={}", header.getKey(), header.getValue());
}
log.info("Cannot extract file name from http headers for {}. Found headers: {}", response.url, response.headers);

String fileNameFromUrl = httpHelper.getFileName(response.url);
return isNotBlank(fileNameFromUrl) ? fileNameFromUrl : downloader.randomFileName();
String result = isNotBlank(fileNameFromUrl) ? fileNameFromUrl : downloader.randomFileName();
log.info("Generated file name for {}: {}", response.url, result);
return result;
});
}

Expand Down

0 comments on commit 9279791

Please sign in to comment.