private T executeRequestInForeground()

in src/org/jetbrains/tfsIntegration/webservice/TfsRequestManager.java [266:364]


  private <T> T executeRequestInForeground(Object projectOrComponent,
                                           final Request<T> request,
                                           @Nullable String errorMessage,
                                           final boolean reportErrorsInDialog,
                                           @Nullable Credentials overrideCredentials,
                                           boolean force)
    throws TfsException {
    LOG.assertTrue(ApplicationManager.getApplication().isDispatchThread());

    Ref<T> result = new Ref<>();
    Ref<TfsException> fatalError = new Ref<>();
    if (errorMessage != null || overrideCredentials == null && shouldShowDialog(force)) {
      Ref<Credentials> credentials =
        new Ref<>(overrideCredentials != null
                  ? overrideCredentials
                  : myServerUri != null ? TFSConfigurationManager.getInstance().getCredentials(myServerUri) : null);

      // show the dialog first, then run in modal progress over it
      Condition<TfsLoginDialog> condition = dialog -> {
        ExecuteSession<T> session = new ExecuteSession<>(dialog.getCredentials(), dialog.getContentPane(), request,
                                                         dialog.getUri());
        if (!session.execute()) {
          return false;
        }

        TfsException error = session.getError();
        if (error != null) {
          if (error instanceof UnauthorizedException || myServerUri == null || reportErrorsInDialog) {
            // continue with the dialog
            dialog.setMessage(getMessage(error, dialog.getCredentials().getType()));
            return false;
          }
          else {
            fatalError.set(error);
            if (!(error instanceof ConnectionFailedException)) {
              // we've connected successfully, so it's time to store right credentials
              TFSConfigurationManager.getInstance().storeCredentials(dialog.getUri(), session.getCredentials());
            }
          }
        }
        else {
          TFSConfigurationManager.getInstance().storeCredentials(dialog.getUri(), session.getCredentials());
          result.set(session.myResult);
        }
        return true;
      };

      TfsLoginDialog d;
      if (projectOrComponent instanceof JComponent) {
        d = new TfsLoginDialog((JComponent)projectOrComponent, myServerUri, credentials.get(), myServerUri == null, condition);
      }
      else {
        d = new TfsLoginDialog((Project)projectOrComponent, myServerUri, credentials.get(), myServerUri == null, condition);
      }

      if (errorMessage != null) {
        d.setMessage(errorMessage);
      }
      if (d.showAndGet()) {
        if (fatalError.isNull()) {
          return result.get();
        }
        else {
          throw fatalError.get();
        }
      }
      else {
        if (!force && myServerUri != null) {
          TFSConfigurationManager.getInstance().setAuthCanceled(myServerUri, projectOrComponent);
        }
        throw new AuthCancelledException(myServerUri);
      }
    }

    // run the progress, show the dialog if error occurs
    LOG.assertTrue(myServerUri != null);

    Credentials credentials =
      overrideCredentials != null ? overrideCredentials : TFSConfigurationManager.getInstance().getCredentials(myServerUri);
    ExecuteSession<T> session = new ExecuteSession<>(credentials, projectOrComponent, request, myServerUri);
    if (!session.execute()) {
      throw new UserCancelledException();
    }

    TfsException error = session.getError();
    if (error instanceof UnauthorizedException && credentials != null) {
      return executeRequestInForeground(projectOrComponent, request, getMessage(error, credentials.getType()), reportErrorsInDialog,
                                        overrideCredentials, force);
    }

    TFSConfigurationManager.getInstance().storeCredentials(myServerUri, session.getCredentials());

    if (error == null) {
      return session.myResult;
    }
    else {
      throw error;
    }
  }