From b86ae2cdbde042a6d65ef94228ba5d71942c266e Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 17 Dec 2014 17:01:28 +0100 Subject: [PATCH 1/6] move HttpRequests to platform-api --- .../src/com/intellij/util/io/HttpRequests.java | 6 ------ 1 file changed, 6 deletions(-) rename platform/{platform-impl => platform-api}/src/com/intellij/util/io/HttpRequests.java (97%) diff --git a/platform/platform-impl/src/com/intellij/util/io/HttpRequests.java b/platform/platform-api/src/com/intellij/util/io/HttpRequests.java similarity index 97% rename from platform/platform-impl/src/com/intellij/util/io/HttpRequests.java rename to platform/platform-api/src/com/intellij/util/io/HttpRequests.java index a131bebe9ffd..b81bca14e83a 100644 --- a/platform/platform-impl/src/com/intellij/util/io/HttpRequests.java +++ b/platform/platform-api/src/com/intellij/util/io/HttpRequests.java @@ -24,7 +24,6 @@ import com.intellij.util.SystemProperties; import com.intellij.util.net.HttpConfigurable; import com.intellij.util.net.ssl.CertificateManager; import org.jetbrains.annotations.NotNull; -import org.jetbrains.io.Responses; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; @@ -204,11 +203,6 @@ public final class HttpRequests { connection.setConnectTimeout(builder.myConnectTimeout); connection.setReadTimeout(builder.myTimeout); - String userAgent = Responses.getServerHeaderValue(); - if (userAgent != null) { - connection.setRequestProperty("User-Agent", userAgent); - } - if (connection instanceof HttpsURLConnection) { try { HttpsURLConnection httpsConnection = (HttpsURLConnection)connection; From 67017dfee17bd1e5e5d6f84d51717bf795e0bc8e Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 17 Dec 2014 17:04:58 +0100 Subject: [PATCH 2/6] set the header "User-Agent" only when needed --- .../src/com/intellij/util/io/HttpRequests.java | 12 ++++++++++++ .../vfs/impl/http/DefaultRemoteContentProvider.java | 2 ++ 2 files changed, 14 insertions(+) diff --git a/platform/platform-api/src/com/intellij/util/io/HttpRequests.java b/platform/platform-api/src/com/intellij/util/io/HttpRequests.java index b81bca14e83a..da2c77f63f74 100644 --- a/platform/platform-api/src/com/intellij/util/io/HttpRequests.java +++ b/platform/platform-api/src/com/intellij/util/io/HttpRequests.java @@ -24,6 +24,7 @@ import com.intellij.util.SystemProperties; import com.intellij.util.net.HttpConfigurable; import com.intellij.util.net.ssl.CertificateManager; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; @@ -69,6 +70,7 @@ public final class HttpRequests { private boolean myGzip = true; private boolean myForceHttps; private boolean myDisableHostVerification; + private String myUserAgent; private RequestBuilder(@NotNull String url) { myUrl = url; @@ -110,6 +112,12 @@ public final class HttpRequests { return this; } + @NotNull + public RequestBuilder userAgent(@Nullable String userAgent) { + myUserAgent = userAgent; + return this; + } + public T connect(@NotNull RequestProcessor processor) throws IOException { // todo[r.sh] drop condition in IDEA 15 if (ourWrapClassLoader) { @@ -203,6 +211,10 @@ public final class HttpRequests { connection.setConnectTimeout(builder.myConnectTimeout); connection.setReadTimeout(builder.myTimeout); + if (builder.myUserAgent != null) { + connection.setRequestProperty("User-Agent", builder.myUserAgent); + } + if (connection instanceof HttpsURLConnection) { try { HttpsURLConnection httpsConnection = (HttpsURLConnection)connection; diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/http/DefaultRemoteContentProvider.java b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/http/DefaultRemoteContentProvider.java index 48d3364c0813..72957495b1d8 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/http/DefaultRemoteContentProvider.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/http/DefaultRemoteContentProvider.java @@ -28,6 +28,7 @@ import com.intellij.util.PathUtilRt; import com.intellij.util.Url; import com.intellij.util.io.HttpRequests; import org.jetbrains.annotations.NotNull; +import org.jetbrains.io.Responses; import java.io.*; import java.net.HttpURLConnection; @@ -58,6 +59,7 @@ public class DefaultRemoteContentProvider extends RemoteContentProvider { HttpRequests.request(url.toExternalForm()) .connectTimeout(60 * 1000) .readTimeout(60 * 1000) + .userAgent(Responses.getServerHeaderValue()) .disableHostVerification() .connect(new HttpRequests.RequestProcessor() { @Override From 82dbb99f990fedeeeec5ad2f76c914d7bd4c2f1a Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 17 Dec 2014 17:18:21 +0100 Subject: [PATCH 3/6] IDEA-134370 Check connection via proxy freezes IDEA completely --- .../util/net/HttpProxySettingsUi.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/platform/platform-api/src/com/intellij/util/net/HttpProxySettingsUi.java b/platform/platform-api/src/com/intellij/util/net/HttpProxySettingsUi.java index f4c849f015f7..804e09bc1a00 100644 --- a/platform/platform-api/src/com/intellij/util/net/HttpProxySettingsUi.java +++ b/platform/platform-api/src/com/intellij/util/net/HttpProxySettingsUi.java @@ -29,6 +29,7 @@ import com.intellij.ui.PortField; import com.intellij.ui.RawCommandLineEditor; import com.intellij.ui.RelativeFont; import com.intellij.ui.components.JBRadioButton; +import com.intellij.util.io.HttpRequests; import com.intellij.util.proxy.CommonProxy; import com.intellij.util.proxy.JavaProxyProperty; import org.jetbrains.annotations.NotNull; @@ -172,27 +173,24 @@ class HttpProxySettingsUi implements ConfigurableUi { ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { @Override public void run() { - HttpURLConnection connection = null; try { //already checked for null above //noinspection ConstantConditions - connection = settings.openHttpConnection(answer); - connection.setReadTimeout(3 * 1000); - connection.setConnectTimeout(3 * 1000); - connection.connect(); - final int code = connection.getResponseCode(); - if (HttpURLConnection.HTTP_OK != code) { - exceptionReference.set(new IOException("Error code: " + code)); - } + HttpRequests.request(answer).connectTimeout(3 * 1000).readTimeout(3 * 1000).connect(new HttpRequests.RequestProcessor() { + @Override + public Object process(@NotNull HttpRequests.Request request) throws IOException { + int responseCode = ((HttpURLConnection)request.getConnection()).getResponseCode(); + if (responseCode != HttpURLConnection.HTTP_OK) { + exceptionReference.set(new IOException("Error code: " + responseCode)); + } + return null; + } + }); } catch (IOException e) { exceptionReference.set(e); } - finally { - if (connection != null) { - connection.disconnect(); - } - } + //noinspection SSBasedInspection SwingUtilities.invokeLater(new Runnable() { @Override From 7e564e790744ad5de5b949da76224419af7c6a61 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 17 Dec 2014 17:18:37 +0100 Subject: [PATCH 4/6] use HttpRequests --- .../templates/github/DownloadUtil.java | 120 +++++++++--------- 1 file changed, 59 insertions(+), 61 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/platform/templates/github/DownloadUtil.java b/platform/lang-impl/src/com/intellij/platform/templates/github/DownloadUtil.java index 0879df6ce464..b66e76de4858 100644 --- a/platform/lang-impl/src/com/intellij/platform/templates/github/DownloadUtil.java +++ b/platform/lang-impl/src/com/intellij/platform/templates/github/DownloadUtil.java @@ -7,17 +7,18 @@ import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.io.FileUtil; -import com.intellij.util.ObjectUtils; import com.intellij.util.Producer; import com.intellij.util.containers.Predicate; -import com.intellij.util.net.HttpConfigurable; +import com.intellij.util.io.HttpRequests; import com.intellij.util.net.NetUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.net.HttpURLConnection; -import java.net.URLConnection; import java.util.Locale; import java.util.concurrent.Callable; @@ -34,28 +35,27 @@ public class DownloadUtil { * {@code outputFile} isn't modified if an I/O error occurs or {@code contentChecker} is provided and returns false on the downloaded content. * More formally, the steps are: *
    - *
  1. Download {@code url} to {@code tempFile}. Stop in case of any I/O errors.
  2. - *
  3. Stop if {@code contentChecker} is provided, and it returns false on the downloaded content.
  4. - *
  5. Move {@code tempFile} to {@code outputFile}. On most OS this operation is done atomically.
  6. + *
  7. Download {@code url} to {@code tempFile}. Stop in case of any I/O errors.
  8. + *
  9. Stop if {@code contentChecker} is provided, and it returns false on the downloaded content.
  10. + *
  11. Move {@code tempFile} to {@code outputFile}. On most OS this operation is done atomically.
  12. *
- * + *

* Motivation: some web filtering products return pure HTML with HTTP 200 OK status instead of * the asked content. * - * @param indicator progress indicator - * @param url url to download - * @param outputFile output file - * @param tempFile temporary file to download to. This file is deleted on method exit. + * @param indicator progress indicator + * @param url url to download + * @param outputFile output file + * @param tempFile temporary file to download to. This file is deleted on method exit. * @param contentChecker checks whether the downloaded content is OK or not - * @returns true if no {@code contentChecker} is provided or the provided one returned true * @throws IOException if an I/O error occurs + * @returns true if no {@code contentChecker} is provided or the provided one returned true */ public static boolean downloadAtomically(@Nullable ProgressIndicator indicator, - @NotNull String url, - @NotNull File outputFile, - @NotNull File tempFile, - @Nullable Predicate contentChecker) throws IOException - { + @NotNull String url, + @NotNull File outputFile, + @NotNull File tempFile, + @Nullable Predicate contentChecker) throws IOException { try { downloadContentToFile(indicator, url, tempFile); if (contentChecker != null) { @@ -66,7 +66,8 @@ public class DownloadUtil { } FileUtil.rename(tempFile, outputFile); return true; - } finally { + } + finally { FileUtil.delete(tempFile); } } @@ -75,14 +76,13 @@ public class DownloadUtil { * Downloads content of {@code url} to {@code outputFile} atomically. * {@code outputFile} won't be modified in case of any I/O download errors. * - * @param indicator progress indicator - * @param url url to download - * @param outputFile output file + * @param indicator progress indicator + * @param url url to download + * @param outputFile output file */ public static void downloadAtomically(@Nullable ProgressIndicator indicator, @NotNull String url, - @NotNull File outputFile) throws IOException - { + @NotNull File outputFile) throws IOException { File tempFile = FileUtil.createTempFile("for-actual-downloading-", null); downloadAtomically(indicator, url, outputFile, tempFile, null); } @@ -91,16 +91,15 @@ public class DownloadUtil { * Downloads content of {@code url} to {@code outputFile} atomically. * {@code outputFile} won't be modified in case of any I/O download errors. * - * @param indicator progress indicator - * @param url url to download - * @param outputFile output file - * @param tempFile temporary file to download to. This file is deleted on method exit. + * @param indicator progress indicator + * @param url url to download + * @param outputFile output file + * @param tempFile temporary file to download to. This file is deleted on method exit. */ public static void downloadAtomically(@Nullable ProgressIndicator indicator, @NotNull String url, @NotNull File outputFile, - @NotNull File tempFile) throws IOException - { + @NotNull File tempFile) throws IOException { downloadAtomically(indicator, url, outputFile, tempFile, null); } @@ -111,8 +110,7 @@ public class DownloadUtil { @NotNull String progressTitle, @NotNull final String actionShortDescription, @NotNull final Callable supplier, - @Nullable Producer tryAgainProvider) - { + @Nullable Producer tryAgainProvider) { int attemptNumber = 1; while (true) { final Ref dataRef = Ref.create(null); @@ -160,44 +158,45 @@ public class DownloadUtil { OutputStream out = new FileOutputStream(outputFile); try { download(progress, url, out); - } finally { + } + finally { out.close(); } } - private static void download(@Nullable ProgressIndicator progress, - @NotNull String location, - @NotNull OutputStream output) throws IOException { - String originalText = progress != null ? progress.getText() : null; + private static void download(@Nullable final ProgressIndicator progress, + @NotNull final String location, + @NotNull final OutputStream output) throws IOException { + final String originalText = progress != null ? progress.getText() : null; substituteContentLength(progress, originalText, -1); if (progress != null) { progress.setText2("Downloading " + location); } - URLConnection urlConnection = HttpConfigurable.getInstance().openConnection(location); - HttpURLConnection httpURLConnection = ObjectUtils.tryCast(urlConnection, HttpURLConnection.class); + try { - urlConnection.setRequestProperty("User-Agent", ApplicationInfoEx.getInstanceEx().getFullApplicationName()); - urlConnection.connect(); - InputStream in = urlConnection.getInputStream(); - int contentLength = urlConnection.getContentLength(); - substituteContentLength(progress, originalText, contentLength); - NetUtils.copyStreamContent(progress, in, output, contentLength); - } catch (IOException e) { - String errorMessage = "Can not download '" + location + ", headers: " + urlConnection.getHeaderFields(); - if (httpURLConnection != null) { - errorMessage += "', response code: " + httpURLConnection.getResponseCode() - + ", response message: " + httpURLConnection.getResponseMessage(); - } - throw new IOException(errorMessage, e); + HttpRequests.request(location) + .userAgent(ApplicationInfoEx.getInstanceEx().getFullApplicationName()) + .connect(new HttpRequests.RequestProcessor() { + @Override + public Object process(@NotNull HttpRequests.Request request) throws IOException { + try { + int contentLength = request.getConnection().getContentLength(); + substituteContentLength(progress, originalText, contentLength); + NetUtils.copyStreamContent(progress, request.getInputStream(), output, contentLength); + } + catch (IOException e) { + HttpURLConnection connection = (HttpURLConnection)request.getConnection(); + throw new IOException("Cannot download '" + location + + ", headers: " + connection.getHeaderFields() + + "', response code: " + connection.getResponseCode() + + ", response message: " + connection.getResponseMessage(), e); + } + return null; + } + }); } - finally { - if (httpURLConnection != null) { - try { - httpURLConnection.disconnect(); - } catch (Exception e) { - LOG.warn("Exception at disconnect()", e); - } - } + catch (IOException e) { + throw new IOException("Cannot download '" + location, e); } } @@ -225,5 +224,4 @@ public class DownloadUtil { } return String.format(Locale.US, ", %.1f MB", contentLengthInBytes / (1.0 * kilo * kilo)); } - } From 87a3ac3495402ba7881ba8ff477749217b22f832 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 17 Dec 2014 17:29:17 +0100 Subject: [PATCH 5/6] use HttpRequests --- .../xpath/xslt/quickfix/DownloadManager.java | 89 ++++++++----------- 1 file changed, 37 insertions(+), 52 deletions(-) diff --git a/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/xslt/quickfix/DownloadManager.java b/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/xslt/quickfix/DownloadManager.java index fd064347c71f..e6fda73fe2fc 100644 --- a/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/xslt/quickfix/DownloadManager.java +++ b/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/xslt/quickfix/DownloadManager.java @@ -24,19 +24,22 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; -import com.intellij.util.net.HttpConfigurable; +import com.intellij.util.io.HttpRequests; import com.intellij.util.net.NetUtils; +import org.jetbrains.annotations.NotNull; -import java.io.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.lang.reflect.InvocationTargetException; -import java.net.URLConnection; import java.util.Set; -@SuppressWarnings({"StringEquality"}) public abstract class DownloadManager { private static final ExternalResourceManager resourceManager = ExternalResourceManager.getInstance(); @@ -49,57 +52,45 @@ public abstract class DownloadManager { myProgress = progress; myResourcePath = PathManager.getSystemPath() + File.separatorChar + "extResources"; - final File dir = new File(myResourcePath); - dir.mkdirs(); + //noinspection ResultOfMethodCallIgnored + new File(myResourcePath).mkdirs(); } - public void fetch(String location) throws DownloadException { - if (resourceManager.getResourceLocation(location) == location) { - myProgress.setText("Downloading " + location); - downloadAndRegister(location); + public void fetch(@NotNull final String location) throws DownloadException { + if (resourceManager.getResourceLocation(location, myProject) != location) { + return; } - } - private void downloadAndRegister(final String location) throws DownloadException { - final ExternalResourceManager resourceManager = ExternalResourceManager.getInstance(); + myProgress.setText("Downloading " + location); File file = null; try { - final URLConnection urlConnection = HttpConfigurable.getInstance().openConnection(location); - urlConnection.connect(); - final InputStream in = urlConnection.getInputStream(); - - final OutputStream out; - try { - final int total = urlConnection.getContentLength(); - - final String name = Integer.toHexString(System.identityHashCode(this)) + - "_" + - Integer.toHexString(location.hashCode()) + - "_" + - location.substring(location.lastIndexOf('/') + 1); - file = new File(myResourcePath, name.lastIndexOf('.') == -1 ? name + ".xml" : name); - out = new FileOutputStream(file); - - try { - NetUtils.copyStreamContent(myProgress, in, out, total); + file = HttpRequests.request(location).connect(new HttpRequests.RequestProcessor() { + @Override + public File process(@NotNull HttpRequests.Request request) throws IOException { + int total = request.getConnection().getContentLength(); + String name = Integer.toHexString(System.identityHashCode(this)) + "_" + + Integer.toHexString(location.hashCode()) + "_" + + location.substring(location.lastIndexOf('/') + 1); + File file = new File(myResourcePath, name.lastIndexOf('.') == -1 ? name + ".xml" : name); + OutputStream out = new FileOutputStream(file); + try { + NetUtils.copyStreamContent(myProgress, request.getInputStream(), out, total); + } + finally { + out.close(); + } + return file; } - finally { - out.close(); - } - } - finally { - in.close(); - } + }); try { final File _file = file; - //noinspection unchecked final Set[] resourceDependencies = new Set[1]; new WriteAction() { @Override - protected void run(Result result) throws Throwable { + protected void run(@NotNull Result result) throws Throwable { final VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(_file); if (vf != null) { final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(vf); @@ -130,25 +121,19 @@ public abstract class DownloadManager { } catch (Error err) { Throwable e = err.getCause(); - if (e instanceof InterruptedException) { - // OK - } - else if (e instanceof InvocationTargetException) { - final Throwable targetException = ((InvocationTargetException)e).getTargetException(); + if (e instanceof InvocationTargetException) { + Throwable targetException = ((InvocationTargetException)e).getTargetException(); if (targetException instanceof RuntimeException) { throw (RuntimeException)targetException; } else if (targetException instanceof IOException) { throw (IOException)targetException; } - else if (targetException instanceof InterruptedException) { - // OK - } - else { + else if (!(targetException instanceof InterruptedException)) { Logger.getInstance(getClass().getName()).error(e); } } - else { + else if (!(e instanceof InterruptedException)) { throw err; } } @@ -157,9 +142,9 @@ public abstract class DownloadManager { throw new DownloadException(location, e); } finally { - if (file != null && resourceManager.getResourceLocation(location) == location) { + if (file != null && resourceManager.getResourceLocation(location, myProject) == location) { // something went wrong. get rid of the file - file.delete(); + FileUtil.delete(file); } } } From 36de98fa1e3dd86a044839991d6a3639335d7d46 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 17 Dec 2014 18:08:54 +0100 Subject: [PATCH 6/6] use HttpRequests --- .../intellij/jarFinder/SourceSearcher.java | 1 - .../PluginsAdvertiser.java | 119 +++++++++--------- .../ivy/AbstractAttachSourceProvider.java | 32 ++--- 3 files changed, 73 insertions(+), 79 deletions(-) diff --git a/java/java-impl/src/com/intellij/jarFinder/SourceSearcher.java b/java/java-impl/src/com/intellij/jarFinder/SourceSearcher.java index b1d635af8525..ef945aceefbc 100644 --- a/java/java-impl/src/com/intellij/jarFinder/SourceSearcher.java +++ b/java/java-impl/src/com/intellij/jarFinder/SourceSearcher.java @@ -16,7 +16,6 @@ import java.net.HttpURLConnection; * @author Sergey Evdokimov */ public abstract class SourceSearcher { - /** * @param indicator * @param artifactId diff --git a/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/pluginsAdvertisement/PluginsAdvertiser.java b/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/pluginsAdvertisement/PluginsAdvertiser.java index 298c489b365e..62de6594ef0b 100644 --- a/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/pluginsAdvertisement/PluginsAdvertiser.java +++ b/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/pluginsAdvertisement/PluginsAdvertiser.java @@ -40,7 +40,7 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.reference.SoftReference; import com.intellij.ui.EditorNotifications; import com.intellij.util.PlatformUtils; -import com.intellij.util.net.HttpConfigurable; +import com.intellij.util.io.HttpRequests; import com.intellij.util.xmlb.XmlSerializer; import com.intellij.util.xmlb.annotations.MapAnnotation; import com.intellij.util.xmlb.annotations.Tag; @@ -54,7 +54,6 @@ import javax.swing.event.HyperlinkEvent; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; -import java.net.HttpURLConnection; import java.util.*; public class PluginsAdvertiser implements StartupActivity { @@ -80,28 +79,31 @@ public class PluginsAdvertiser implements StartupActivity { "&implementationName=" + implementationName.replaceAll("#", "%23") + "&build=" + buildNumber; try { - HttpURLConnection connection = HttpConfigurable.getInstance().openHttpConnection(pluginRepositoryUrl); - connection.connect(); - final InputStreamReader streamReader = new InputStreamReader(connection.getInputStream()); - try { - final JsonReader jsonReader = new JsonReader(streamReader); - jsonReader.setLenient(true); - final JsonElement jsonRootElement = new JsonParser().parse(jsonReader); - final List result = new ArrayList(); - for (JsonElement jsonElement : jsonRootElement.getAsJsonArray()) { - final JsonObject jsonObject = jsonElement.getAsJsonObject(); - final JsonElement pluginId = jsonObject.get("pluginId"); - final JsonElement pluginName = jsonObject.get("pluginName"); - final JsonElement bundled = jsonObject.get("bundled"); - result.add(new Plugin(PluginId.getId(StringUtil.unquoteString(pluginId.toString())), - pluginName != null ? StringUtil.unquoteString(pluginName.toString()) : null, - Boolean.parseBoolean(StringUtil.unquoteString(bundled.toString())))); + return HttpRequests.request(pluginRepositoryUrl).connect(new HttpRequests.RequestProcessor>() { + @Override + public List process(@NotNull HttpRequests.Request request) throws IOException { + final InputStreamReader streamReader = new InputStreamReader(request.getInputStream()); + try { + final JsonReader jsonReader = new JsonReader(streamReader); + jsonReader.setLenient(true); + final JsonElement jsonRootElement = new JsonParser().parse(jsonReader); + final List result = new ArrayList(); + for (JsonElement jsonElement : jsonRootElement.getAsJsonArray()) { + final JsonObject jsonObject = jsonElement.getAsJsonObject(); + final JsonElement pluginId = jsonObject.get("pluginId"); + final JsonElement pluginName = jsonObject.get("pluginName"); + final JsonElement bundled = jsonObject.get("bundled"); + result.add(new Plugin(PluginId.getId(StringUtil.unquoteString(pluginId.toString())), + pluginName != null ? StringUtil.unquoteString(pluginName.toString()) : null, + Boolean.parseBoolean(StringUtil.unquoteString(bundled.toString())))); + } + return result; + } + finally { + streamReader.close(); + } } - return result; - } - finally { - streamReader.close(); - } + }); } catch (IOException e) { LOG.info(e); @@ -116,47 +118,50 @@ public class PluginsAdvertiser implements StartupActivity { } final String pluginRepositoryUrl = FEATURE_IMPLEMENTATIONS_URL + "featureType=" + FileTypeFactory.FILE_TYPE_FACTORY_EP.getName(); try { - HttpURLConnection connection = HttpConfigurable.getInstance().openHttpConnection(pluginRepositoryUrl); - connection.connect(); - final InputStreamReader streamReader = new InputStreamReader(connection.getInputStream()); - try { - final JsonReader jsonReader = new JsonReader(streamReader); - jsonReader.setLenient(true); - final JsonElement jsonRootElement = new JsonParser().parse(jsonReader); - final Map> result = new HashMap>(); - for (JsonElement jsonElement : jsonRootElement.getAsJsonArray()) { - final JsonObject jsonObject = jsonElement.getAsJsonObject(); + return HttpRequests.request(pluginRepositoryUrl).connect(new HttpRequests.RequestProcessor>>() { + @Override + public Map> process(@NotNull HttpRequests.Request request) throws IOException { + final InputStreamReader streamReader = new InputStreamReader(request.getInputStream()); + try { + final JsonReader jsonReader = new JsonReader(streamReader); + jsonReader.setLenient(true); + final JsonElement jsonRootElement = new JsonParser().parse(jsonReader); + final Map> result = new HashMap>(); + for (JsonElement jsonElement : jsonRootElement.getAsJsonArray()) { + final JsonObject jsonObject = jsonElement.getAsJsonObject(); - final String pluginId = StringUtil.unquoteString(jsonObject.get("pluginId").toString()); - final JsonElement bundledExt = jsonObject.get("bundled"); - boolean isBundled = Boolean.parseBoolean(bundledExt.toString()); - final IdeaPluginDescriptor fromServerPluginDescription = availableIds.get(pluginId); - if (fromServerPluginDescription == null && !isBundled) continue; + final String pluginId = StringUtil.unquoteString(jsonObject.get("pluginId").toString()); + final JsonElement bundledExt = jsonObject.get("bundled"); + boolean isBundled = Boolean.parseBoolean(bundledExt.toString()); + final IdeaPluginDescriptor fromServerPluginDescription = availableIds.get(pluginId); + if (fromServerPluginDescription == null && !isBundled) continue; - final IdeaPluginDescriptor loadedPlugin = PluginManager.getPlugin(PluginId.getId(pluginId)); - if (loadedPlugin != null && loadedPlugin.isEnabled()) continue; + final IdeaPluginDescriptor loadedPlugin = PluginManager.getPlugin(PluginId.getId(pluginId)); + if (loadedPlugin != null && loadedPlugin.isEnabled()) continue; - if (loadedPlugin != null && fromServerPluginDescription != null && - StringUtil.compareVersionNumbers(loadedPlugin.getVersion(), fromServerPluginDescription.getVersion()) >= 0) continue; + if (loadedPlugin != null && fromServerPluginDescription != null && + StringUtil.compareVersionNumbers(loadedPlugin.getVersion(), fromServerPluginDescription.getVersion()) >= 0) continue; - if (fromServerPluginDescription != null && PluginManagerCore.isBrokenPlugin(fromServerPluginDescription)) continue; + if (fromServerPluginDescription != null && PluginManagerCore.isBrokenPlugin(fromServerPluginDescription)) continue; - final JsonElement ext = jsonObject.get("implementationName"); - final String extension = StringUtil.unquoteString(ext.toString()); - Set pluginIds = result.get(extension); - if (pluginIds == null) { - pluginIds = new HashSet(); - result.put(extension, pluginIds); + final JsonElement ext = jsonObject.get("implementationName"); + final String extension = StringUtil.unquoteString(ext.toString()); + Set pluginIds = result.get(extension); + if (pluginIds == null) { + pluginIds = new HashSet(); + result.put(extension, pluginIds); + } + final JsonElement pluginNameElement = jsonObject.get("pluginName"); + pluginIds.add(new Plugin(PluginId.getId(pluginId), pluginNameElement != null ? StringUtil.unquoteString(pluginNameElement.toString()) : null, isBundled)); + } + saveExtensions(result); + return result; + } + finally { + streamReader.close(); } - final JsonElement pluginNameElement = jsonObject.get("pluginName"); - pluginIds.add(new Plugin(PluginId.getId(pluginId), pluginNameElement != null ? StringUtil.unquoteString(pluginNameElement.toString()) : null, isBundled)); } - saveExtensions(result); - return result; - } - finally { - streamReader.close(); - } + }); } catch (Throwable e) { LOG.info(e); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/ivy/AbstractAttachSourceProvider.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/ivy/AbstractAttachSourceProvider.java index 3f2139c174a8..d68109862ae8 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/ivy/AbstractAttachSourceProvider.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/ivy/AbstractAttachSourceProvider.java @@ -17,15 +17,13 @@ import com.intellij.openapi.util.ActionCallback; import com.intellij.openapi.vfs.JarFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; -import com.intellij.util.net.HttpConfigurable; +import com.intellij.util.io.HttpRequests; import com.intellij.util.net.NetUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; import java.util.Arrays; import java.util.List; @@ -148,28 +146,20 @@ public abstract class AbstractAttachSourceProvider implements AttachSourcesProvi Task task = new Task.Backgroundable(myProject, "Downloading sources...", true) { @Override - public void run(@NotNull ProgressIndicator indicator) { + public void run(@NotNull final ProgressIndicator indicator) { final ByteArrayOutputStream out; - try { LOG.info("Downloading sources JAR: " + myUrl); - indicator.checkCanceled(); - - HttpURLConnection urlConnection = HttpConfigurable.getInstance().openHttpConnection(myUrl); - - int contentLength = urlConnection.getContentLength(); - - out = new ByteArrayOutputStream(contentLength > 0 ? contentLength : 100 * 1024); - - InputStream in = urlConnection.getInputStream(); - - try { - NetUtils.copyStreamContent(indicator, in, out, contentLength); - } - finally { - in.close(); - } + out = HttpRequests.request(myUrl).connect(new HttpRequests.RequestProcessor() { + @Override + public ByteArrayOutputStream process(@NotNull HttpRequests.Request request) throws IOException { + int contentLength = request.getConnection().getContentLength(); + ByteArrayOutputStream out = new ByteArrayOutputStream(contentLength > 0 ? contentLength : 100 * 1024); + NetUtils.copyStreamContent(indicator, request.getInputStream(), out, contentLength); + return out; + } + }); } catch (IOException e) { LOG.warn(e);