com.intellij.util.io.HttpRequests Java Examples

The following examples show how to use com.intellij.util.io.HttpRequests. 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: QuarkusModelRegistry.java    From intellij-quarkus with Eclipse Public License 2.0 6 votes vote down vote up
public QuarkusModel load(String endPointURL, ProgressIndicator indicator) throws IOException {
    indicator.setText("Looking up Quarkus model from endpoint " + endPointURL);
    QuarkusModel model = models.get(endPointURL);
    if (model == null) {
        indicator.setText("Loading Quarkus model from endpoint " + endPointURL);
        try {
            model = ApplicationManager.getApplication().executeOnPooledThread(() -> HttpRequests.request(endPointURL + EXTENSIONS_SUFFIX).userAgent(USER_AGENT).tuner(request -> {
                request.setRequestProperty(CODE_QUARKUS_IO_CLIENT_NAME_HEADER_NAME, CODE_QUARKUS_IO_CLIENT_NAME_HEADER_VALUE);
                request.setRequestProperty(CODE_QUARKUS_IO_CLIENT_CONTACT_EMAIL_HEADER_NAME, CODE_QUARKUS_IO_CLIENT_CONTACT_EMAIL_HEADER_VALUE);
            }).connect(request -> {
                    try (Reader reader = request.getReader(indicator)) {
                        List<QuarkusExtension> extensions = mapper.readValue(reader, new TypeReference<List<QuarkusExtension>>() {
                        });
                        QuarkusModel newModel = new QuarkusModel(extensions);
                        return newModel;
                    }
            })).get();
        } catch (InterruptedException|ExecutionException e) {
            throw new IOException(e);
        }
    }
    if (model == null) {
        throw new IOException();
    }
    return model;
}
 
Example #2
Source File: ServiceAuthConfiguration.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void updateIcon() {
  myUserIcon = null;

  String email = myState.email;
  if (email == null) {
    myState.iconBytes = null;
    return;
  }

  // get node size
  int size = (int)Math.ceil(AllIcons.Actions.Find.getIconHeight() * JBUI.sysScale());
  Application.get().executeOnPooledThread(() -> {
    String emailHash = DigestUtils.md5Hex(email.toLowerCase().trim());

    try {
      byte[] bytes = HttpRequests.request("https://www.gravatar.com/avatar/" + emailHash + ".png?s=" + size + "&d=identicon").readBytes(null);

      myState.iconBytes = Base64.getEncoder().encodeToString(bytes);
    }
    catch (IOException e) {
      LOGGER.error(e);
    }
  });
}
 
Example #3
Source File: PluginDownloader.java    From consulo with Apache License 2.0 6 votes vote down vote up
private File downloadPlugin(final ProgressIndicator indicator) throws IOException {
  File pluginsTemp = new File(ContainerPathManager.get().getPluginTempPath());
  if (!pluginsTemp.exists() && !pluginsTemp.mkdirs()) {
    throw new IOException(IdeBundle.message("error.cannot.create.temp.dir", pluginsTemp));
  }
  final File file = FileUtil.createTempFile(pluginsTemp, "plugin_", "_download", true, false);

  indicator.checkCanceled();
  if (myIsPlatform) {
    indicator.setText2(IdeBundle.message("progress.downloading.platform"));
  }
  else {
    indicator.setText2(IdeBundle.message("progress.downloading.plugin", getPluginName()));
  }

  return HttpRequests.request(myPluginUrl).gzip(false).connect(request -> {
    request.saveToFile(file, indicator);

    String fileName = getFileName();
    File newFile = new File(file.getParentFile(), fileName);
    FileUtil.rename(file, newFile);
    return newFile;
  });
}
 
Example #4
Source File: QuarkusModelRegistry.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
public static void zip(String endpoint, String tool, String groupId, String artifactId, String version,
                       String className, String path, QuarkusModel model, File output) throws IOException {
    Url url = Urls.newFromEncoded(endpoint + "/api/download");
    Map<String, String> parameters = new HashMap<>();
    parameters.put(CODE_TOOL_PARAMETER_NAME, tool);
    parameters.put(CODE_GROUP_ID_PARAMETER_NAME, groupId);
    parameters.put(CODE_ARTIFACT_ID_PARAMETER_NAME, artifactId);
    parameters.put(CODE_VERSION_PARAMETER_NAME, version);
    parameters.put(CODE_CLASSNAME_PARAMETER_NAME, className);
    parameters.put(CODE_PATH_PARAMETER_NAME, path);
    parameters.put(CODE_EXTENSIONS_SHORT_PARAMETER_NAME, model.getCategories().stream().flatMap(category -> category.getExtensions().stream()).
            filter(extension -> extension.isSelected() || extension.isDefaultExtension()).
            map(extension -> extension.getShortId()).
            collect(Collectors.joining(".")));
    url = url.addParameters(parameters);
    RequestBuilder builder = HttpRequests.request(url.toString()).userAgent(QuarkusModelRegistry.USER_AGENT).tuner(connection -> {
        connection.setRequestProperty(CODE_QUARKUS_IO_CLIENT_NAME_HEADER_NAME, CODE_QUARKUS_IO_CLIENT_NAME_HEADER_VALUE);
        connection.setRequestProperty(CODE_QUARKUS_IO_CLIENT_CONTACT_EMAIL_HEADER_NAME, CODE_QUARKUS_IO_CLIENT_CONTACT_EMAIL_HEADER_VALUE);
    });
    try {
        if (ApplicationManager.getApplication().executeOnPooledThread(() -> builder.connect(request -> {
            ZipUtil.unpack(request.getInputStream(), output, name -> {
                int index = name.indexOf('/');
                return name.substring(index);
            });
            return true;
        })).get() == null) {
            throw new IOException();
        }
} catch (InterruptedException | ExecutionException e) {
        throw new IOException(e);
    }
}
 
Example #5
Source File: FlutterSurveyService.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
private static FlutterSurvey fetchSurveyContent() {
  try {
    final String contents = HttpRequests.request(CONTENT_URL).readString();
    final JsonObject json = new JsonParser().parse(contents).getAsJsonObject();
    return FlutterSurvey.fromJson(json);
  }
  catch (IOException | JsonSyntaxException e) {
    // Null content is OK in case of a transient exception.
  }
  return null;
}
 
Example #6
Source File: FlutterSurveyService.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
private static FlutterSurvey fetchSurveyContent() {
  try {
    final String contents = HttpRequests.request(CONTENT_URL).readString();
    final JsonObject json = new JsonParser().parse(contents).getAsJsonObject();
    return FlutterSurvey.fromJson(json);
  }
  catch (IOException | JsonSyntaxException e) {
    // Null content is OK in case of a transient exception.
  }
  return null;
}
 
Example #7
Source File: WebExperimentSyncer.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public String call() throws Exception {
  logger.debug("About to fetch experiments.");
  return HttpRequests.request(
          System.getProperty(EXPERIMENTS_URL_PROPERTY, DEFAULT_EXPERIMENT_URL) + pluginName)
      .readString(/* progress indicator */ null);
}
 
Example #8
Source File: HttpProxySettingsUi.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void configureCheckButton() {
  if (HttpConfigurable.getInstance() == null) {
    myCheckButton.setVisible(false);
    return;
  }

  myCheckButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(@Nonnull ActionEvent e) {
      final String title = "Check Proxy Settings";
      final String answer = Messages.showInputDialog(myMainPanel, "Warning: your settings will be saved.\n\nEnter any URL to check connection to:",
                                                     title, Messages.getQuestionIcon(), "http://", null);
      if (StringUtil.isEmptyOrSpaces(answer)) {
        return;
      }

      final HttpConfigurable settings = HttpConfigurable.getInstance();
      apply(settings);
      final AtomicReference<IOException> exceptionReference = new AtomicReference<>();
      myCheckButton.setEnabled(false);
      myCheckButton.setText("Check connection (in progress...)");
      myConnectionCheckInProgress = true;
      ApplicationManager.getApplication().executeOnPooledThread(() -> {
        try {
          //already checked for null above
          //noinspection ConstantConditions
          HttpRequests.request(answer)
                  .readTimeout(3 * 1000)
                  .tryConnect();
        }
        catch (IOException e1) {
          exceptionReference.set(e1);
        }

        //noinspection SSBasedInspection
        SwingUtilities.invokeLater(() -> {
          myConnectionCheckInProgress = false;
          reset(settings);  // since password might have been set
          Component parent;
          if (myMainPanel.isShowing()) {
            parent = myMainPanel;
            myCheckButton.setText("Check connection");
            myCheckButton.setEnabled(canEnableConnectionCheck());
          }
          else {
            IdeFrame frame = IdeFocusManager.findInstance().getLastFocusedFrame();
            if (frame == null) {
              return;
            }
            parent = frame.getComponent();
          }
          //noinspection ThrowableResultOfMethodCallIgnored
          final IOException exception = exceptionReference.get();
          if (exception == null) {
            Messages.showMessageDialog(parent, "Connection successful", title, Messages.getInformationIcon());
          }
          else {
            final String message = exception.getMessage();
            if (settings.USE_HTTP_PROXY) {
              settings.LAST_ERROR = message;
            }
            Messages.showErrorDialog(parent, errorText(message));
          }
        });
      });
    }
  });
}