private boolean setupConnection()

in src/org/jetbrains/plugins/ipnb/configuration/IpnbConnectionManager.java [250:305]


  private boolean setupConnection(@NotNull IpnbCodePanel codePanel, @NotNull String path, @NotNull String urlString,
                                  String token, boolean isNewFormat) {
    try {
      Ref<Boolean> connectionOpened = new Ref<>(false);
      final IpnbConnectionListenerBase listener = createConnectionListener(codePanel, connectionOpened);
      final VirtualFile file = codePanel.getFileEditor().getVirtualFile();
      final String pathToFile = getRelativePathToFile(file);
      if (pathToFile == null) return false;
      final IpnbConnection connection = getConnection(urlString, listener, pathToFile, token, isNewFormat);
      int countAttempt = 0;
      while (!connectionOpened.get() && countAttempt < MAX_ATTEMPTS) {
        countAttempt += 1;
        TimeoutUtil.sleep(1000);
      }
      myKernels.put(path, connection);
    }
    catch (URISyntaxException e) {
      showMessage(codePanel.getFileEditor(), "Cannot connect to Jupyter Notebook. <a href=\"\">Run Jupyter Notebook</a>",
                  new IpnbRunAdapter(), MessageType.WARNING);
      LOG.warn("Jupyter Notebook connection refused: " + e.getMessage());
      return false;
    }
    catch (UnsupportedOperationException e) {
      showMessage(codePanel.getFileEditor(), e.getMessage(), null, MessageType.WARNING);
    }
    catch (UnknownHostException e) {
      showMessage(codePanel.getFileEditor(), "Cannot connect to Jupyter Notebook. <a href=\"\">Run Jupyter Notebook</a>",
                  new IpnbRunAdapter(), MessageType.WARNING);
    }
    catch (IOException e) {
      if (IpnbConnection.AUTHENTICATION_NEEDED.equals(e.getMessage())) {
        ApplicationManager.getApplication().invokeAndWait(() -> {
          final Pair<String, String> pair = askForUrlAndToken();
          if (pair != null) {
            myToken = pair.getFirst();
            myUrl = pair.getSecond();
          }
        });
        if (myToken != null) {
          return setupConnection(codePanel, path, urlString, token, isNewFormat);
        }
      }
      final String message = e.getMessage();
      if (message.startsWith(IpnbConnection.UNABLE_LOGIN_MESSAGE)) {
        showMessage(codePanel.getFileEditor(), "Cannot connect to Jupyter Notebook: login failed", new IpnbSettingsAdapter(),
                    MessageType.WARNING);
      }
      else if (message.startsWith(CONNECTION_REFUSED) || message.startsWith(IpnbConnection.CANNOT_START_JUPYTER)) {
        showMessage(codePanel.getFileEditor(), "Cannot connect to Jupyter Notebook. <a href=\"\">Run Jupyter Notebook</a>",
                    new IpnbRunAdapter(), MessageType.WARNING);
      }
      LOG.warn("Jupyter Notebook connection refused: " + message);
      return false;
    }
    return true;
  }