FUS: make sure request content is consumed and resources are released

GitOrigin-RevId: 215b5730c86a04d7b54454c1f5e5bcc983ef0f7d
This commit is contained in:
Svetlana.Zemlyanskaya
2020-01-30 16:32:45 +00:00
committed by intellij-monorepo-bot
parent eed75c29b5
commit f00ff24074
2 changed files with 14 additions and 11 deletions
@@ -1,7 +1,7 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.internal.statistic;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jdom.Element;
import org.jdom.JDOMException;
@@ -19,7 +19,7 @@ public class StatisticsEventLogUtil {
@NonNls public static final String UTF8 = "UTF-8";
@NotNull
public static HttpClient create(@NotNull String userAgent) {
public static CloseableHttpClient create(@NotNull String userAgent) {
return HttpClientBuilder.create().setUserAgent(userAgent).build();
}
@@ -9,9 +9,11 @@ import com.intellij.internal.statistic.service.fus.FUSWhitelist.BuildRange;
import com.intellij.internal.statistic.service.fus.FUSWhitelist.GroupFilterCondition;
import com.intellij.internal.statistic.service.fus.FUSWhitelist.VersionRange;
import org.apache.http.*;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.utils.DateUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -71,8 +73,8 @@ public class FUStatisticsWhiteListGroupsService {
if (StatisticsEventLogUtil.isEmptyOrSpaces(serviceUrl)) return null;
String content = null;
try {
HttpResponse response = StatisticsEventLogUtil.create(userAgent).execute(new HttpGet(serviceUrl));
try (CloseableHttpClient client = StatisticsEventLogUtil.create(userAgent);
CloseableHttpResponse response = client.execute(new HttpGet(serviceUrl))) {
HttpEntity entity = response.getEntity();
if (entity != null) {
content = EntityUtils.toString(entity, StatisticsEventLogUtil.UTF8);
@@ -85,18 +87,19 @@ public class FUStatisticsWhiteListGroupsService {
}
private static long lastModifiedWhitelist(@NotNull String userAgent, @Nullable String serviceUrl) {
try {
if (!StatisticsEventLogUtil.isEmptyOrSpaces(serviceUrl)) {
final HttpResponse response = StatisticsEventLogUtil.create(userAgent).execute(new HttpHead(serviceUrl));
return Stream.of(response.getHeaders(HttpHeaders.LAST_MODIFIED)).
if (!StatisticsEventLogUtil.isEmptyOrSpaces(serviceUrl)) {
try (CloseableHttpClient client = StatisticsEventLogUtil.create(userAgent);
CloseableHttpResponse response = client.execute(new HttpHead(serviceUrl))) {
Header[] headers = response.getHeaders(HttpHeaders.LAST_MODIFIED);
return Stream.of(headers).
map(header -> header.getValue()).
filter(Objects::nonNull).
map(value -> DateUtils.parseDate(value).getTime()).
max(Long::compareTo).orElse(0L);
}
}
catch (IOException e) {
//LOG.info(e);
catch (IOException e) {
//LOG.info(e);
}
}
return 0;
}