use HttpRequests

This commit is contained in:
Vladimir Krivosheev
2014-12-17 18:08:54 +01:00
parent 87a3ac3495
commit 36de98fa1e
3 changed files with 73 additions and 79 deletions
@@ -16,7 +16,6 @@ import java.net.HttpURLConnection;
* @author Sergey Evdokimov
*/
public abstract class SourceSearcher {
/**
* @param indicator
* @param artifactId
@@ -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<Plugin> result = new ArrayList<Plugin>();
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<List<Plugin>>() {
@Override
public List<Plugin> 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<Plugin> result = new ArrayList<Plugin>();
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<String, Set<Plugin>> result = new HashMap<String, Set<Plugin>>();
for (JsonElement jsonElement : jsonRootElement.getAsJsonArray()) {
final JsonObject jsonObject = jsonElement.getAsJsonObject();
return HttpRequests.request(pluginRepositoryUrl).connect(new HttpRequests.RequestProcessor<Map<String, Set<Plugin>>>() {
@Override
public Map<String, Set<Plugin>> 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<String, Set<Plugin>> result = new HashMap<String, Set<Plugin>>();
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<Plugin> pluginIds = result.get(extension);
if (pluginIds == null) {
pluginIds = new HashSet<Plugin>();
result.put(extension, pluginIds);
final JsonElement ext = jsonObject.get("implementationName");
final String extension = StringUtil.unquoteString(ext.toString());
Set<Plugin> pluginIds = result.get(extension);
if (pluginIds == null) {
pluginIds = new HashSet<Plugin>();
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);
@@ -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<ByteArrayOutputStream>() {
@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);