com.intellij.util.net.HttpConfigurable Java Examples

The following examples show how to use com.intellij.util.net.HttpConfigurable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BackCompatibleUtils.java    From azure-devops-intellij with MIT License 6 votes vote down vote up
/**
 * Find the proxy login username
 *
 * @return
 */
public static String getProxyLogin() {
    try {
        // try to get login name using method existing in IDEA release 163.1188 and above
        final Method proxyLoginMethod = HttpConfigurable.getInstance().getClass().getDeclaredMethod(PROXY_LOGIN_METHOD);
        return (String) proxyLoginMethod.invoke(HttpConfigurable.getInstance(), null);
    } catch (Exception newImplementationException) {
        try {
            logger.warn("Failed to get proxy login using getProxyLogin() so attempting old way", newImplementationException);
            // try to get login name using global variable existing before IDEA release 163.1188
            final Field proxyLoginField = HttpConfigurable.getInstance().getClass().getDeclaredField(PROXY_LOGIN_FIELD);
            return (String) proxyLoginField.get(HttpConfigurable.getInstance());
        } catch (Exception oldImplementationException) {
            logger.warn("Failed to get proxy login using PROXY_LOGIN field", oldImplementationException);
            return StringUtils.EMPTY;
        }
    }
}
 
Example #2
Source File: PluginErrorReportSubmitter.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
private boolean tryConnectOnly(String serverUrl) {
    boolean tryAgain = false;
    do {
        try {
            HttpConfigurable httpConfigurable = HttpConfigurable.getInstance();
            httpConfigurable.prepareURL(serverUrl);
        } catch (IOException ioe) {
            LOGGER.info("Connection error", ioe);
            tryAgain = IOExceptionDialog.showErrorDialog("Error",
                    String.format("Unable to connect to \"%s\". Make sure your proxy settings are correct.", serverUrl)
            );

            // abort if cannot connect to server and user does not want to try again
            if (!tryAgain) {
                return false;
            }
        }
    } while (tryAgain);

    return true;
}
 
Example #3
Source File: PluginErrorReportSubmitter.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Nullable
private String readUrlContentWithProxy(String urlString) {
    // first, check if connection to server can be established, i.e. proxy settings are correct
    boolean tryAgain = false;
    do {
        try {
            HttpConfigurable httpConfigurable = HttpConfigurable.getInstance();
            httpConfigurable.prepareURL(urlString);
        } catch (IOException ioe) {
            LOGGER.info("Connection error", ioe);
            tryAgain = IOExceptionDialog.showErrorDialog("Error", String.format("Unable to connect to \"%s\". Make sure your proxy settings are correct.", urlString));

            // abort if cannot connect to server and user does not want to try again
            if (!tryAgain) {
                return null;
            }
        }
    } while (tryAgain);

    // second, connect to server and read content
    return readUrlContent(urlString);
}
 
Example #4
Source File: PluginErrorReportSubmitter.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Nullable
private String readUrlContent(String urlString) {
    HttpConfigurable httpConfigurable = HttpConfigurable.getInstance();

    HttpURLConnection connection = null;

    try {
        connection = httpConfigurable.openHttpConnection(urlString);

        String text = StreamUtil.readText(connection.getInputStream(), "UTF-8");
        return text.trim();
    } catch (IOException e) {
        //ignored
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }

    return null;
}
 
Example #5
Source File: HttpProxyServiceImpl.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
private void initialize() {
    useHttpProxyViaSystemProperties = StringUtils.equalsIgnoreCase(System.getProperty(PROP_PROXY_SET), "true");
    if (!useHttpProxyViaSystemProperties) {
        useHttpProxyViaIntelliJProperties = PluginServiceProvider.getInstance().isInsideIDE() &&
                HttpConfigurable.getInstance() != null &&
                HttpConfigurable.getInstance().USE_HTTP_PROXY;
    } else {
        useHttpProxyViaIntelliJProperties = false;
    }
}
 
Example #6
Source File: HttpProxyServiceImpl.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
@Override
public boolean isAuthenticationRequired() {
    initialize();
    // We don't do authentication if we are using the system properties
    final boolean result = !useHttpProxyViaSystemProperties &&
            useHttpProxyViaIntelliJProperties &&
            HttpConfigurable.getInstance().PROXY_AUTHENTICATION;
    logger.info("isAuthenticationRequired: " + result);
    return result;
}
 
Example #7
Source File: HttpProxyServiceImpl.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
@Override
public String getProxyHost() {
    initialize();
    // default to Fiddler proxy host
    String proxyHost = "127.0.0.1";
    if (useHttpProxyViaSystemProperties && System.getProperty(PROP_PROXY_HOST) != null) {
        proxyHost = System.getProperty(PROP_PROXY_HOST);
    } else if (useHttpProxyViaIntelliJProperties) {
        proxyHost = HttpConfigurable.getInstance().PROXY_HOST;
    }
    logger.info("getProxyHost: " + proxyHost);
    return proxyHost;
}
 
Example #8
Source File: HttpProxyServiceImpl.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
@Override
public int getProxyPort() {
    initialize();
    // default to Fiddler proxy port
    int proxyPort = 8888;
    if (useHttpProxyViaSystemProperties && System.getProperty(PROP_PROXY_PORT) != null) {
        proxyPort = SystemHelper.toInt(System.getProperty(PROP_PROXY_PORT), proxyPort);
    } else if (useHttpProxyViaIntelliJProperties) {
        proxyPort = HttpConfigurable.getInstance().PROXY_PORT;
    }
    logger.info("getProxyPort: " + proxyPort);
    return proxyPort;
}
 
Example #9
Source File: HttpProxyServiceImpl.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
@Override
public String getPassword() {
    logger.info("getPassword called");
    initialize();
    if (useHttpProxyViaIntelliJProperties) {
        return HttpConfigurable.getInstance().getPlainProxyPassword();
    } else {
        return null;
    }
}
 
Example #10
Source File: ErrorReportSender.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void sendReport(Project project, ErrorReportBean errorBean, final java.util.function.Consumer<String> callback, final Consumer<Exception> errback) {
  Task.Backgroundable.queue(project, DiagnosticBundle.message("title.submitting.error.report"), indicator -> {
    try {
      HttpConfigurable.getInstance().prepareURL(WebServiceApi.MAIN.buildUrl());

      String id = sendAndHandleResult(errorBean);

      callback.accept(id);
    }
    catch (Exception ex) {
      errback.consume(ex);
    }
  });
}
 
Example #11
Source File: HttpRequests.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static URLConnection openConnection(RequestBuilderImpl builder) throws IOException {
  String url = builder.myUrl;

  for (int i = 0; i < builder.myRedirectLimit; i++) {
    if (builder.myForceHttps && StringUtil.startsWith(url, "http:")) {
      url = "https:" + url.substring(5);
    }

    if (url.startsWith("https:") && ApplicationManager.getApplication() != null) {
      CertificateManager.getInstance();
    }

    URLConnection connection;
    if (!builder.myUseProxy) {
      connection = new URL(url).openConnection(Proxy.NO_PROXY);
    }
    else if (ApplicationManager.getApplication() == null) {
      connection = new URL(url).openConnection();
    }
    else {
      connection = HttpConfigurable.getInstance().openConnection(url);
    }

    connection.setConnectTimeout(builder.myConnectTimeout);
    connection.setReadTimeout(builder.myTimeout);

    if (builder.myUserAgent != null) {
      connection.setRequestProperty("User-Agent", builder.myUserAgent);
    }

    if (builder.myHostnameVerifier != null && connection instanceof HttpsURLConnection) {
      ((HttpsURLConnection)connection).setHostnameVerifier(builder.myHostnameVerifier);
    }

    if (builder.myGzip) {
      connection.setRequestProperty("Accept-Encoding", "gzip");
    }

    if (builder.myAccept != null) {
      connection.setRequestProperty("Accept", builder.myAccept);
    }

    connection.setUseCaches(false);

    if (builder.myTuner != null) {
      builder.myTuner.tune(connection);
    }

    if (connection instanceof HttpURLConnection) {
      int responseCode = ((HttpURLConnection)connection).getResponseCode();

      if (responseCode < 200 || responseCode >= 300 && responseCode != HttpURLConnection.HTTP_NOT_MODIFIED) {
        ((HttpURLConnection)connection).disconnect();

        if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
          url = connection.getHeaderField("Location");
          if (url != null) {
            continue;
          }
        }

        String message = IdeBundle.message("error.connection.failed.with.http.code.N", responseCode);
        throw new HttpStatusException(message, responseCode, StringUtil.notNullize(url, "Empty URL"));
      }
    }

    return connection;
  }

  throw new IOException(IdeBundle.message("error.connection.failed.redirects"));
}