extract HttpRequests.getReader

This commit is contained in:
Vladimir Krivosheev
2014-12-23 16:55:28 +01:00
parent b20fc1c936
commit 627cf64204
13 changed files with 333 additions and 364 deletions
@@ -1,7 +1,6 @@
package com.intellij.jarFinder;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.util.ProgressStream;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.util.io.HttpRequests;
import org.jdom.Document;
@@ -10,7 +9,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.InputStream;
/**
* @author Sergey Evdokimov
@@ -25,15 +23,15 @@ public abstract class SourceSearcher {
@Nullable
protected abstract String findSourceJar(@NotNull final ProgressIndicator indicator, @NotNull String artifactId, @NotNull String version) throws SourceSearchException;
@NotNull
protected static Document readDocumentCancelable(final ProgressIndicator indicator, String url) throws IOException {
return HttpRequests.request(url)
.accept("application/xml")
.connect(new HttpRequests.RequestProcessor<Document>() {
@Override
public Document process(@NotNull HttpRequests.Request request) throws IOException {
InputStream inputStream = request.getInputStream();
try {
return JDOMUtil.loadDocument(new ProgressStream(0, request.getConnection().getContentLength(), inputStream, indicator));
return JDOMUtil.loadDocument(request.getReader(indicator));
}
catch (JDOMException e) {
throw new IOException(e);
@@ -71,8 +71,8 @@ public class ExternalJavadocPresentationTest extends LightCodeInsightTestCase {
}
@Override
public void doBuildFromStream(String url, Reader input, StringBuilder data, boolean search4Encoding) throws IOException {
super.doBuildFromStream(url, input, data, search4Encoding);
public void doBuildFromStream(String url, Reader input, StringBuilder data, boolean searchForEncoding) throws IOException {
super.doBuildFromStream(url, input, data, searchForEncoding);
}
}
JavadocExternalTestFilter filter = new JavadocExternalTestFilter(getProject());
@@ -77,7 +77,7 @@ public abstract class SettingsConnectionService {
Map<String, String> settings = ContainerUtilRt.newLinkedHashMap();
try {
Element root = JDOMUtil.load(request.getInputStream());
Element root = JDOMUtil.load(request.getReader());
for (String s : attributes) {
String attributeValue = root.getAttributeValue(s);
if (StringUtil.isNotEmpty(attributeValue)) {
@@ -29,7 +29,6 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.*;
import com.intellij.psi.PsiElement;
import com.intellij.util.SystemProperties;
import com.intellij.util.ThrowableConsumer;
import com.intellij.util.io.HttpRequests;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -38,7 +37,6 @@ import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.net.URL;
import java.util.Locale;
import java.util.concurrent.Future;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -215,56 +213,6 @@ public abstract class AbstractExternalFilter {
protected abstract RefConvertor[] getRefConverters();
private static void getReaderByUrl(@NotNull String url, final @NotNull ThrowableConsumer<Reader, IOException> consumer)
throws IOException {
if (url.startsWith(JAR_PROTOCOL)) {
VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(BrowserUtil.getDocURL(url));
if (file != null) {
consumer.consume(new StringReader(VfsUtilCore.loadText(file)));
}
return;
}
final URL parsedUrl = BrowserUtil.getURL(url);
if (parsedUrl == null) {
return;
}
HttpRequests.request(parsedUrl.toString()).connect(new HttpRequests.RequestProcessor<Void>() {
@Override
public Void process(@NotNull HttpRequests.Request request) throws IOException {
String contentEncoding = guessEncoding(parsedUrl);
InputStream inputStream = request.getInputStream();
//noinspection IOResourceOpenedButNotSafelyClosed
consumer.consume(contentEncoding != null ? new MyReader(inputStream, contentEncoding) : new MyReader(inputStream));
return null;
}
});
}
@Nullable
private static String guessEncoding(URL url) {
return HttpRequests.request(url.toExternalForm()).connect(new HttpRequests.RequestProcessor<String>() {
@Override
public String process(@NotNull HttpRequests.Request request) throws IOException {
String result = request.getConnection().getContentEncoding();
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
try {
for (String htmlLine = reader.readLine(); htmlLine != null; htmlLine = reader.readLine()) {
result = parseContentEncoding(htmlLine);
if (result != null) {
break;
}
}
}
finally {
reader.close();
}
return result;
}
}, null, null);
}
@Nullable
@SuppressWarnings({"HardCodedStringLiteral"})
public String getExternalDocInfo(final String url) throws Exception {
@@ -274,36 +222,34 @@ public abstract class AbstractExternalFilter {
return null;
}
if (url == null) {
if (url == null || !MyJavadocFetcher.ourFree) {
return null;
}
if (MyJavadocFetcher.isFree()) {
final MyJavadocFetcher fetcher = new MyJavadocFetcher(url, new MyDocBuilder() {
@Override
public void buildFromStream(String url, Reader input, StringBuilder result) throws IOException {
doBuildFromStream(url, input, result);
}
});
final Future<?> fetcherFuture = app.executeOnPooledThread(fetcher);
try {
fetcherFuture.get();
}
catch (Exception e) {
return null;
}
final Exception exception = fetcher.getException();
if (exception != null) {
fetcher.cleanup();
throw exception;
}
final String docText = correctRefs(ourAnchorSuffix.matcher(url).replaceAll(""), fetcher.getData());
if (LOG.isDebugEnabled()) {
LOG.debug("Filtered JavaDoc: " + docText + "\n");
MyJavadocFetcher fetcher = new MyJavadocFetcher(url, new MyDocBuilder() {
@Override
public void buildFromStream(String url, Reader input, StringBuilder result) throws IOException {
doBuildFromStream(url, input, result);
}
return PlatformDocumentationUtil.fixupText(docText);
});
try {
app.executeOnPooledThread(fetcher).get();
}
return null;
catch (Exception e) {
return null;
}
Exception exception = fetcher.myException;
if (exception != null) {
fetcher.myException = null;
throw exception;
}
final String docText = correctRefs(ourAnchorSuffix.matcher(url).replaceAll(""), fetcher.data.toString());
if (LOG.isDebugEnabled()) {
LOG.debug("Filtered JavaDoc: " + docText + "\n");
}
return PlatformDocumentationUtil.fixupText(docText);
}
@Nullable
@@ -315,7 +261,7 @@ public abstract class AbstractExternalFilter {
doBuildFromStream(url, input, data, true);
}
protected void doBuildFromStream(final String url, Reader input, final StringBuilder data, boolean search4Encoding) throws IOException {
protected void doBuildFromStream(final String url, Reader input, final StringBuilder data, boolean searchForEncoding) throws IOException {
Trinity<Pattern, Pattern, Boolean> settings = getParseSettings(url);
@NonNls Pattern startSection = settings.first;
@NonNls Pattern endSection = settings.second;
@@ -345,7 +291,7 @@ public abstract class AbstractExternalFilter {
BufferedReader buf = new BufferedReader(input);
do {
read = buf.readLine();
if (read != null && search4Encoding && read.contains("charset")) {
if (read != null && searchForEncoding && read.contains("charset")) {
String foundEncoding = parseContentEncoding(read);
if (foundEncoding != null) {
contentEncoding = foundEncoding;
@@ -358,14 +304,8 @@ public abstract class AbstractExternalFilter {
!contentEncoding.equals(((MyReader)input).getEncoding())) {
//restart page parsing with correct encoding
try {
final String finalContentEncoding = contentEncoding;
getReaderByUrl(url, new ThrowableConsumer<Reader, IOException>() {
@Override
public void consume(Reader reader) throws IOException {
data.delete(0, data.length());
doBuildFromStream(url, new MyReader(((MyReader)reader).getInputStream(), finalContentEncoding), data, false);
}
});
data.setLength(0);
doBuildFromStream(url, new MyReader(((MyReader)input).myInputStream, contentEncoding), data, false);
}
catch (ProcessCanceledException e) {
return;
@@ -374,7 +314,7 @@ public abstract class AbstractExternalFilter {
}
if (read == null) {
data.delete(0, data.length());
data.setLength(0);
return;
}
@@ -495,14 +435,6 @@ public abstract class AbstractExternalFilter {
ourFree = false;
}
public static boolean isFree() {
return ourFree;
}
public String getData() {
return data.toString();
}
@Override
public void run() {
try {
@@ -510,50 +442,74 @@ public abstract class AbstractExternalFilter {
return;
}
try {
getReaderByUrl(url, new ThrowableConsumer<Reader, IOException>() {
@Override
public void consume(Reader reader) throws IOException {
myBuilder.buildFromStream(url, reader, data);
}
});
if (url.startsWith(JAR_PROTOCOL)) {
VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(BrowserUtil.getDocURL(url));
if (file != null) {
myBuilder.buildFromStream(url, new StringReader(VfsUtilCore.loadText(file)), data);
}
}
catch (ProcessCanceledException ignored) {
}
catch (IOException e) {
myException = e;
else {
URL parsedUrl = BrowserUtil.getURL(url);
if (parsedUrl != null) {
HttpRequests.request(parsedUrl.toString()).connect(new HttpRequests.RequestProcessor<Void>() {
@Override
public Void process(@NotNull HttpRequests.Request request) throws IOException {
byte[] bytes = request.readBytes(null);
String contentEncoding = null;
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
try {
for (String htmlLine = reader.readLine(); htmlLine != null; htmlLine = reader.readLine()) {
contentEncoding = parseContentEncoding(htmlLine);
if (contentEncoding != null) {
break;
}
}
}
finally {
reader.close();
stream.reset();
}
if (contentEncoding == null) {
contentEncoding = request.getConnection().getContentEncoding();
}
//noinspection IOResourceOpenedButNotSafelyClosed
myBuilder.buildFromStream(url, contentEncoding != null ? new MyReader(stream, contentEncoding) : new MyReader(stream), data);
return null;
}
});
}
}
}
catch (ProcessCanceledException ignored) {
}
catch (IOException e) {
myException = e;
}
finally {
//noinspection AssignmentToStaticFieldFromInstanceMethod
ourFree = true;
}
}
public Exception getException() {
return myException;
}
public void cleanup() {
myException = null;
}
}
private static class MyReader extends InputStreamReader {
private InputStream myInputStream;
private ByteArrayInputStream myInputStream;
public MyReader(InputStream in) {
public MyReader(ByteArrayInputStream in) {
super(in);
in.reset();
myInputStream = in;
}
public MyReader(InputStream in, String charsetName) throws UnsupportedEncodingException {
public MyReader(ByteArrayInputStream in, String charsetName) throws UnsupportedEncodingException {
super(in, charsetName);
myInputStream = in;
}
public InputStream getInputStream() {
return myInputStream;
in.reset();
myInputStream = in;
}
}
}
@@ -23,6 +23,7 @@ import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.BufferExposingByteArrayOutputStream;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.util.io.StreamUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.util.ArrayUtil;
@@ -64,6 +65,12 @@ public abstract class HttpRequests {
@NotNull
InputStream getInputStream() throws IOException;
@NotNull
BufferedReader getReader() throws IOException;
@NotNull
BufferedReader getReader(@Nullable ProgressIndicator indicator) throws IOException;
boolean isSuccessful() throws IOException;
@NotNull
@@ -206,18 +213,7 @@ public abstract class HttpRequests {
int contentLength = request.getConnection().getContentLength();
BufferExposingByteArrayOutputStream out = new BufferExposingByteArrayOutputStream(contentLength > 0 ? contentLength : 16 * 1024);
NetUtils.copyStreamContent(indicator, request.getInputStream(), out, contentLength);
Charset charset = CharsetToolkit.UTF8_CHARSET;
String contentEncoding = request.getConnection().getContentEncoding();
if (contentEncoding != null) {
try {
charset = Charset.forName(contentEncoding);
}
catch (Exception ignored) {
charset = CharsetToolkit.UTF8_CHARSET;
}
}
return new String(out.getInternalBuffer(), 0, out.size(), charset);
return new String(out.getInternalBuffer(), 0, out.size(), getCharset(request));
}
});
}
@@ -250,10 +246,24 @@ public abstract class HttpRequests {
}
}
@NotNull
private static Charset getCharset(@NotNull Request request) throws IOException {
String contentEncoding = request.getConnection().getContentEncoding();
if (contentEncoding != null) {
try {
return Charset.forName(contentEncoding);
}
catch (Exception ignored) {
}
}
return CharsetToolkit.UTF8_CHARSET;
}
private static <T> T process(final RequestBuilder builder, RequestProcessor<T> processor) throws IOException {
class RequestImpl implements Request {
private URLConnection myConnection;
private InputStream myInputStream;
private BufferedReader myReader;
@NotNull
@Override
@@ -277,16 +287,38 @@ public abstract class HttpRequests {
return myInputStream;
}
@NotNull
@Override
public BufferedReader getReader() throws IOException {
return getReader(null);
}
@NotNull
@Override
public BufferedReader getReader(@Nullable ProgressIndicator indicator) throws IOException {
if (myReader == null) {
InputStream inputStream = getInputStream();
if (indicator != null) {
int contentLength = getConnection().getContentLength();
if (contentLength > 0) {
//noinspection IOResourceOpenedButNotSafelyClosed
inputStream = new ProgressMonitorInputStream(indicator, inputStream, contentLength);
}
}
myReader = new BufferedReader(new InputStreamReader(inputStream, getCharset(this)));
}
return myReader;
}
@Override
public boolean isSuccessful() throws IOException {
URLConnection connection = getConnection();
return !(connection instanceof HttpURLConnection) || ((HttpURLConnection)connection).getResponseCode() == 200;
}
private void cleanup() throws IOException {
if (myInputStream != null) {
myInputStream.close();
}
private void cleanup() {
StreamUtil.closeStream(myInputStream);
StreamUtil.closeStream(myReader);
if (myConnection instanceof HttpURLConnection) {
((HttpURLConnection)myConnection).disconnect();
}
@@ -0,0 +1,82 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.util.io;
import com.intellij.openapi.progress.ProgressIndicator;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
final class ProgressMonitorInputStream extends InputStream {
private final ProgressIndicator indicator;
private final InputStream in;
private double available;
private long count;
public ProgressMonitorInputStream(@NotNull ProgressIndicator indicator, @NotNull InputStream in, int length) {
this.indicator = indicator;
this.in = in;
available = length;
}
public int read() throws IOException {
int c = in.read();
updateProgress(c >= 0 ? 1 : 0);
return c;
}
private void updateProgress(long increment) throws InterruptedIOException {
if (indicator.isCanceled()) {
InterruptedIOException exception = new InterruptedIOException("progress");
exception.bytesTransferred = (int)count;
throw exception;
}
if (increment > 0) {
count += increment;
indicator.setFraction((double)count / available);
}
}
public int read(byte[] b) throws IOException {
int r = in.read(b);
updateProgress(r);
return r;
}
public int read(byte[] b, int off, int len) throws IOException {
int r = in.read(b, off, len);
updateProgress(r);
return r;
}
public long skip(long n) throws IOException {
long r = in.skip(n);
updateProgress(r);
return r;
}
public void close() throws IOException {
in.close();
}
@Override
public int available() throws IOException {
return in.available();
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,12 +16,11 @@
package com.intellij.diagnostic;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.vfs.CharsetToolkit;
import org.apache.http.client.fluent.Request;
import com.intellij.util.io.HttpRequests;
import org.jetbrains.annotations.NotNull;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
@@ -33,27 +32,25 @@ class DevelopersLoader {
private DevelopersLoader() {
}
public static Collection<Developer> fetchDevelopers(ProgressIndicator indicator) throws IOException {
List<Developer> developers = new LinkedList<Developer>();
developers.add(Developer.NULL);
BufferedReader reader = new BufferedReader(
new InputStreamReader(Request.Get(DEVELOPERS_LIST_URL).connectTimeout(TIMEOUT).execute().returnContent().asStream(),
CharsetToolkit.UTF8_CHARSET));
try {
while (true) {
String line = reader.readLine();
if (line == null) break;
int i = line.indexOf('\t');
if (i == -1) throw new IOException("Protocol error");
int id = Integer.parseInt(line.substring(0, i));
String name = line.substring(i + 1);
developers.add(new Developer(id, name));
indicator.checkCanceled();
public static Collection<Developer> fetchDevelopers(@NotNull final ProgressIndicator indicator) throws IOException {
return HttpRequests.request(DEVELOPERS_LIST_URL).connect(new HttpRequests.RequestProcessor<Collection<Developer>>() {
@Override
public Collection<Developer> process(@NotNull HttpRequests.Request request) throws IOException {
List<Developer> developers = new LinkedList<Developer>();
developers.add(Developer.NULL);
BufferedReader reader = request.getReader();
while (true) {
String line = reader.readLine();
if (line == null) break;
int i = line.indexOf('\t');
if (i == -1) throw new IOException("Protocol error");
int id = Integer.parseInt(line.substring(0, i));
String name = line.substring(i + 1);
developers.add(new Developer(id, name));
indicator.checkCanceled();
}
return developers;
}
return developers;
}
finally {
reader.close();
}
});
}
}
@@ -25,20 +25,19 @@ import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.updateSettings.impl.UpdateSettings;
import com.intellij.openapi.util.BuildNumber;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.util.io.HttpRequests;
import com.intellij.util.io.URLUtil;
import org.apache.http.client.utils.URIBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URLConnection;
@@ -132,7 +131,7 @@ public class RepositoryHelper {
}
}
else {
return parsePluginList(request.getInputStream());
return parsePluginList(request.getReader());
}
}
}));
@@ -148,20 +147,14 @@ public class RepositoryHelper {
}
private static List<IdeaPluginDescriptor> loadPluginList(@NotNull File file) throws IOException {
InputStream stream = new FileInputStream(file);
try {
return parsePluginList(stream);
}
finally {
stream.close();
}
return parsePluginList(new InputStreamReader(new FileInputStream(file), CharsetToolkit.UTF8_CHARSET));
}
private static List<IdeaPluginDescriptor> parsePluginList(@NotNull InputStream stream) throws IOException {
private static List<IdeaPluginDescriptor> parsePluginList(@NotNull Reader reader) throws IOException {
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
RepositoryContentHandler handler = new RepositoryContentHandler();
parser.parse(stream, handler);
parser.parse(new InputSource(reader), handler);
return handler.getPluginsList();
}
catch (ParserConfigurationException e) {
@@ -170,6 +163,9 @@ public class RepositoryHelper {
catch (SAXException e) {
throw new IOException(e);
}
finally {
reader.close();
}
}
private static List<IdeaPluginDescriptor> process(@Nullable String repositoryUrl, List<IdeaPluginDescriptor> list) {
@@ -1,61 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.progress.util;
import com.intellij.openapi.progress.ProgressIndicator;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by IntelliJ IDEA.
* User: stathik
* Date: Oct 9, 2003
* Time: 2:20:15 AM
* To change this template use Options | File Templates.
*/
public class ProgressStream extends InputStream {
private InputStream myInputStream;
private ProgressIndicator myProgressIndicator;
private long available;
private long count;
public ProgressStream(InputStream inputStream, ProgressIndicator progressIndicator) {
this (0, 0, inputStream, progressIndicator);
}
public ProgressStream(long start, long available, InputStream inputStream, ProgressIndicator progressIndicator) {
count = start;
this.available = available;
myInputStream = inputStream;
myProgressIndicator = progressIndicator;
}
public int read() throws IOException {
if (myProgressIndicator != null) {
if (myProgressIndicator.isCanceled()) {
throw new RuntimeException (new InterruptedException());
}
if (available > 0) {
myProgressIndicator.setFraction((double) count++ / (double) available);
} else {
myProgressIndicator.setFraction((double) 1 - (double) 1 / (double) count++);
}
}
return myInputStream.read();
}
}
@@ -20,7 +20,10 @@ import com.intellij.ide.IdeBundle;
import com.intellij.ide.plugins.*;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.notification.*;
import com.intellij.openapi.application.*;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.application.ex.ApplicationInfoEx;
import com.intellij.openapi.diagnostic.IdeaLoggingEvent;
import com.intellij.openapi.diagnostic.LogUtil;
@@ -219,7 +222,7 @@ public final class UpdateChecker {
@Override
public UpdatesInfo process(@NotNull HttpRequests.Request request) throws IOException {
try {
return new UpdatesInfo(JDOMUtil.load(request.getInputStream()));
return new UpdatesInfo(JDOMUtil.load(request.getReader()));
}
catch (JDOMException e) {
// corrupted content, don't bother telling user
@@ -53,7 +53,6 @@ import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class PluginsAdvertiser implements StartupActivity {
@@ -81,26 +80,20 @@ public class PluginsAdvertiser implements StartupActivity {
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();
final JsonReader jsonReader = new JsonReader(request.getReader());
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;
}
}, null, LOG);
}
@@ -114,47 +107,41 @@ public class PluginsAdvertiser implements StartupActivity {
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 JsonReader jsonReader = new JsonReader(request.getReader());
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 (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 pluginNameElement = jsonObject.get("pluginName");
pluginIds.add(new Plugin(PluginId.getId(pluginId), pluginNameElement != null ? StringUtil.unquoteString(pluginNameElement.toString()) : null, isBundled));
if (loadedPlugin != null && fromServerPluginDescription != null &&
StringUtil.compareVersionNumbers(loadedPlugin.getVersion(), fromServerPluginDescription.getVersion()) >= 0) {
continue;
}
saveExtensions(result);
return result;
}
finally {
streamReader.close();
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 pluginNameElement = jsonObject.get("pluginName");
pluginIds.add(new Plugin(PluginId.getId(pluginId), pluginNameElement != null ? StringUtil.unquoteString(pluginNameElement.toString()) : null, isBundled));
}
saveExtensions(result);
return result;
}
}, null, LOG);
}
@@ -271,7 +271,6 @@ public class JDOMUtil {
}
return c1 instanceof Element && c2 instanceof Element && areElementsEqual((Element)c1, (Element)c2);
}
private static boolean attListsEqual(@NotNull List a1, @NotNull List a2) {
@@ -303,7 +302,6 @@ public class JDOMUtil {
}
return w1.size() == w2.size() && w1.toString().equals(w2.toString());
}
@NotNull
@@ -313,14 +311,7 @@ public class JDOMUtil {
@NotNull
public static Document loadDocument(byte[] bytes) throws IOException, JDOMException {
final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
try {
return loadDocument(inputStream);
}
finally {
inputStream.close();
}
return loadDocument(new ByteArrayInputStream(bytes));
}
private static SAXBuilder getSaxBuilder() {
@@ -342,18 +333,22 @@ public class JDOMUtil {
@NotNull
public static Document loadDocument(@NotNull CharSequence seq) throws IOException, JDOMException {
return getSaxBuilder().build(new CharSequenceReader(seq));
return loadDocument(new CharSequenceReader(seq));
}
@NotNull
public static Document loadDocument(@NotNull Reader reader) throws IOException, JDOMException {
try {
return getSaxBuilder().build(reader);
}
finally {
reader.close();
}
}
@NotNull
public static Document loadDocument(File file) throws JDOMException, IOException {
final BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
try {
return loadDocument(inputStream);
}
finally {
inputStream.close();
}
return loadDocument(new BufferedInputStream(new FileInputStream(file)));
}
@NotNull
@@ -363,27 +358,17 @@ public class JDOMUtil {
@NotNull
public static Document loadDocument(@NotNull InputStream stream) throws JDOMException, IOException {
InputStreamReader reader = new InputStreamReader(stream, CharsetToolkit.UTF8_CHARSET);
try {
return getSaxBuilder().build(reader);
}
finally {
reader.close();
}
return loadDocument(new InputStreamReader(stream, CharsetToolkit.UTF8_CHARSET));
}
@Contract("null -> null; !null -> !null")
public static Element load(Reader reader) throws JDOMException, IOException {
return reader == null ? null : loadDocument(reader).detachRootElement();
}
@Contract("null -> null; !null -> !null")
public static Element load(InputStream stream) throws JDOMException, IOException {
if (stream == null) {
return null;
}
try {
return loadDocument(stream).detachRootElement();
}
finally {
stream.close();
}
return stream == null ? null : loadDocument(stream).detachRootElement();
}
@NotNull
@@ -17,7 +17,6 @@ package com.jetbrains.python.packaging;
import com.google.common.collect.Lists;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.util.io.HttpRequests;
import com.intellij.util.net.HttpConfigurable;
import com.intellij.webcore.packaging.RepoPackage;
@@ -210,33 +209,28 @@ public class PyPIPackageUtil {
@Override
public List<String> process(@NotNull HttpRequests.Request request) throws IOException {
final List<String> packages = new ArrayList<String>();
Reader reader = new InputStreamReader(request.getInputStream(), CharsetToolkit.UTF8_CHARSET);
try {
new ParserDelegator().parse(reader, new HTMLEditorKit.ParserCallback() {
boolean inTable = false;
Reader reader = request.getReader();
new ParserDelegator().parse(reader, new HTMLEditorKit.ParserCallback() {
boolean inTable = false;
@Override
public void handleStartTag(HTML.Tag tag, MutableAttributeSet set, int i) {
if ("table".equals(tag.toString())) {
inTable = !inTable;
}
if (inTable && "a".equals(tag.toString())) {
packages.add(String.valueOf(set.getAttribute(HTML.Attribute.HREF)));
}
@Override
public void handleStartTag(HTML.Tag tag, MutableAttributeSet set, int i) {
if ("table".equals(tag.toString())) {
inTable = !inTable;
}
@Override
public void handleEndTag(HTML.Tag tag, int i) {
if ("table".equals(tag.toString())) {
inTable = !inTable;
}
if (inTable && "a".equals(tag.toString())) {
packages.add(String.valueOf(set.getAttribute(HTML.Attribute.HREF)));
}
}, true);
}
finally {
reader.close();
}
}
@Override
public void handleEndTag(HTML.Tag tag, int i) {
if ("table".equals(tag.toString())) {
inTable = !inTable;
}
}
}, true);
return packages;
}
}, Collections.<String>emptyList(), LOG);
@@ -266,11 +260,11 @@ public class PyPIPackageUtil {
if (myPackageNames == null) {
final Set<String> names = new HashSet<String>();
for (String name : getPyPIPackages().keySet()) {
names.add(name.toLowerCase());
names.add(name.toLowerCase(Locale.ENGLISH));
}
myPackageNames = names;
}
return myPackageNames != null && myPackageNames.contains(packageName.toLowerCase());
return myPackageNames != null && myPackageNames.contains(packageName.toLowerCase(Locale.ENGLISH));
}
private static class PyPIXmlRpcTransport extends DefaultXmlRpcTransport {