WI-13349 Generating web module project from template does not work behind a proxy

This commit is contained in:
Sergey Simonchik
2012-10-19 10:46:46 +04:00
parent f61be0864f
commit d80c4b38bb
3 changed files with 67 additions and 50 deletions
@@ -9,9 +9,6 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.platform.templates.github.DownloadUtil;
import com.intellij.platform.templates.github.GeneratorException;
import com.intellij.platform.templates.github.GithubTagInfo;
@@ -53,36 +50,30 @@ public class GithubTagListProvider {
return null;
}
public Task.Backgroundable updateTagListAsynchronously(final GithubProjectGeneratorPeer peer) {
public void updateTagListAsynchronously(final GithubProjectGeneratorPeer peer) {
final String url = formatTagListDownloadUrl();
Task.Backgroundable task =
new Task.Backgroundable(null, "Updating versions of " + GithubTagListProvider.this.myRepositoryName + " repository...", true, null) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
File cacheFile = getCacheFile();
try {
DownloadUtil.downloadAtomically(indicator, url, cacheFile, myUserName, myRepositoryName);
final ImmutableSet<GithubTagInfo> infos = readTagsFromFile(cacheFile);
peer.setErrorMessage(null);
UIUtil.invokeLaterIfNeeded(new Runnable() {
public void run() {
peer.updateTagList(infos);
}
});
}
catch (IOException e) {
peer.setErrorMessage("Can not fetch tag list from '" + url + "'!");
}
catch (GeneratorException e) {
peer.setErrorMessage(getGeneratorName() + " cache update failed");
}
}
};
LOG.info(getGeneratorName() + " starting cache update from " + url + " ...");
ProgressManager.getInstance().run(task);
return task;
ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
public void run() {
File cacheFile = getCacheFile();
try {
DownloadUtil.downloadAtomically(null, url, cacheFile, myUserName, myRepositoryName);
final ImmutableSet<GithubTagInfo> infos = readTagsFromFile(cacheFile);
peer.setErrorMessage(null);
UIUtil.invokeLaterIfNeeded(new Runnable() {
public void run() {
peer.updateTagList(infos);
}
});
}
catch (IOException e) {
peer.setErrorMessage("Can not fetch tag list from '" + url + "'!");
}
catch (GeneratorException e) {
peer.setErrorMessage(getGeneratorName() + " cache update failed");
}
}
});
}
private String getGeneratorName() {
@@ -16,8 +16,6 @@ import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
@@ -121,7 +119,7 @@ public class DownloadUtil {
}, new Producer<Boolean>() {
@Override
public Boolean produce() {
return IOExceptionDialog.showErrorDialog("Download Error", "Can not download " + url + "");
return IOExceptionDialog.showErrorDialog("Download Error", "Can not download '" + url + "'");
}
}
);
@@ -204,13 +202,7 @@ public class DownloadUtil {
if (progress != null) {
progress.setText2("Downloading " + location);
}
URL url = new URL(location);
try {
HttpConfigurable.getInstance().prepareURL(location);
} catch (IOException e) {
LOG.info("Can not prepareURL '" + location + "'", e);
}
URLConnection urlConnection = url.openConnection();
HttpURLConnection urlConnection = HttpConfigurable.getInstance().openHttpConnection(location);
try {
int timeout = (int) TimeUnit.MINUTES.toMillis(2);
urlConnection.setConnectTimeout(timeout);
@@ -221,16 +213,21 @@ public class DownloadUtil {
substituteContentLength(progress, originalText, contentLength);
NetUtils.copyStreamContent(progress, in, output, contentLength);
} catch (IOException e) {
if (urlConnection instanceof HttpURLConnection) {
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
LOG.warn("Can not download '" + location
+ "', response code: " + httpURLConnection.getResponseCode()
+ ", response message: " + httpURLConnection.getResponseMessage()
+ ", headers: " + httpURLConnection.getHeaderFields()
);
}
LOG.warn("Can not download '" + location
+ "', response code: " + urlConnection.getResponseCode()
+ ", response message: " + urlConnection.getResponseMessage()
+ ", headers: " + urlConnection.getHeaderFields(),
e
);
throw e;
}
finally {
try {
urlConnection.disconnect();
} catch (Exception e) {
LOG.warn("Exception at disconnect()", e);
}
}
}
private static void substituteContentLength(@Nullable ProgressIndicator progress, @Nullable String text, int contentLengthInBytes) {
@@ -16,6 +16,7 @@
package com.intellij.util.net;
import com.btr.proxy.search.ProxySearch;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.components.*;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.util.InvalidDataException;
@@ -28,6 +29,7 @@ import com.intellij.util.xmlb.XmlSerializerUtil;
import com.intellij.util.xmlb.annotations.Transient;
import org.apache.commons.codec.binary.Base64;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.io.IOException;
@@ -104,7 +106,7 @@ public class HttpConfigurable implements PersistentStateComponent<HttpConfigurab
}
};
try {
WaitForProgressToShow.runOrInvokeAndWaitAboveProgress(runnable);
WaitForProgressToShow.runOrInvokeAndWaitAboveProgress(runnable, ModalityState.any());
}
catch (Exception e) {
// ignore
@@ -161,6 +163,33 @@ public class HttpConfigurable implements PersistentStateComponent<HttpConfigurab
}
}
/**
* Opens HTTP connection to a given location using configured http proxy settings.
* @param location url to connect to
* @return instance of {@link HttpURLConnection}
* @throws IOException in case of any I/O troubles or if created connection isn't instance of HttpURLConnection.
*/
@NotNull
public HttpURLConnection openHttpConnection(@NotNull String location) throws IOException {
setAuthenticator();
URL url = new URL(location);
final URLConnection urlConnection;
if (USE_HTTP_PROXY) {
InetSocketAddress proxyAddress = new InetSocketAddress(InetAddress.getByName(PROXY_HOST), PROXY_PORT);
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
urlConnection = url.openConnection(proxy);
}
else {
urlConnection = url.openConnection();
}
if (urlConnection instanceof HttpURLConnection) {
return (HttpURLConnection) urlConnection;
}
else {
throw new IOException("Expected " + HttpURLConnection.class + ", but got " + url.getClass());
}
}
public void setAuthenticator() {
if (USE_HTTP_PROXY) {
System.setProperty("proxySet", "true");