public static ServerDescriptor connect()

in src/org/jetbrains/tfsIntegration/config/TfsServerConnectionHelper.java [290:451]


  public static ServerDescriptor connect(URI uri, final Credentials credentials, boolean justAuthenticate, @Nullable ProgressIndicator pi)
    throws RemoteException, TfsException {
    if (pi != null) {
      pi.setText(TFSBundle.message("connecting.to.server"));
    }

    if (justAuthenticate) {
      uri = getBareUri(uri);
    }
    final ConfigurationContext context = WebServiceHelper.getStubConfigurationContext();

    Pair<URI, ConnectResponse> connectResponse;
    Pair<URI, FrameworkRegistrationEntry[]> registrationEntries = null;
    try {
      // first, try to connect to TFS 2010 Locaion service
      connectResponse = tryDifferentUris(uri, !justAuthenticate, uri12 -> {
        LocationWebServiceStub locationService =
          new LocationWebServiceStub(context, TfsUtil.appendPath(uri12, TFSConstants.LOCATION_SERVICE_ASMX));
        WebServiceHelper.setupStub(locationService, credentials, uri12);
        Connect connectParam = new Connect();
        connectParam.setConnectOptions(TFSConstants.INCLUDE_SERVICES_CONNECTION_OPTION);
        ArrayOfServiceTypeFilter serviceTypeFilters = new ArrayOfServiceTypeFilter();
        ServiceTypeFilter filter = new ServiceTypeFilter();
        filter.setServiceType("*");
        filter.setIdentifier(TFSConstants.FRAMEWORK_SERVER_DATA_PROVIDER_FILTER_GUID);
        serviceTypeFilters.addServiceTypeFilter(filter);
        connectParam.setServiceTypeFilters(serviceTypeFilters);
        connectParam.setLastChangeId(-1);
        return locationService.connect(connectParam);
      });
    }
    catch (RemoteException e) {
      LOG.info("Failed to connect to " + uri + ". Trying to use legacy Registration service.", e);

      connectResponse = null;
      // if failed, try to connect to legacy Registration service
      ThrowableConvertor<URI, FrameworkRegistrationEntry[], RemoteException> c =
        uri1 -> {
          RegistrationStub registrationStub = new RegistrationStub(context, TfsUtil.appendPath(uri1, TFSConstants.REGISTRATION_ASMX));
          WebServiceHelper.setupStub(registrationStub, credentials, uri1);
          GetRegistrationEntries getRegistrationEntriesParam = new GetRegistrationEntries();
          getRegistrationEntriesParam.setToolId(TFSConstants.TOOL_ID_TFS);
          GetRegistrationEntriesResponse registrationEntries1 = registrationStub.getRegistrationEntries(getRegistrationEntriesParam);
          return registrationEntries1.getGetRegistrationEntriesResult().getRegistrationEntry();
        };
      registrationEntries = tryDifferentUris(uri, !justAuthenticate, c);
    }

    if (connectResponse != null) {
      uri = connectResponse.first;
      // TFS 2010 -> get team project collections
      ConnectionData connectResult = connectResponse.second.getConnectResult();
      ArrayOfKeyValueOfStringString userProps = connectResult.getAuthorizedUser().getAttributes();
      String domain = getPropertyValue(userProps, TFSConstants.DOMAIN);
      String userName = getPropertyValue(userProps, TFSConstants.ACCOUNT);
      Credentials authorizedCredentials =
        new Credentials(userName, domain, credentials.getPassword(), credentials.isStorePassword(), credentials.getType());
      if (justAuthenticate) {
        return new ServerDescriptor(authorizedCredentials, uri, null);
      }

      if (pi != null) {
        pi.setText(TFSBundle.message("loading.team.project.collections"));
      }

      ServiceDefinition[] serviceDefinitions =
        connectResult.getLocationServiceData().getServiceDefinitions().getServiceDefinition();
      if (serviceDefinitions == null) {
        LOG.warn("service definitions node is null");
        throw new HostNotApplicableException(null);
      }

      String catalogServicePath = null;
      for (ServiceDefinition serviceDefinition : serviceDefinitions) {
        if (TFSConstants.CATALOG_SERVICE_CONFIG_GUID.equalsIgnoreCase(serviceDefinition.getIdentifier().getGuid())) {
          catalogServicePath = serviceDefinition.getRelativePath();
        }
      }
      if (catalogServicePath == null) {
        LOG.warn("catalog service not found by guid");
        throw new HostNotApplicableException(null);
      }

      Guid catalogResourceId = connectResult.getCatalogResourceId();
      CatalogWebServiceStub catalogService = new CatalogWebServiceStub(context, TfsUtil.appendPath(uri, catalogServicePath));
      WebServiceHelper.setupStub(catalogService, credentials, uri);
      QueryResources queryResourcesParam = new QueryResources();
      ArrayOfGuid resourceIdentitiers = new ArrayOfGuid();
      resourceIdentitiers.addGuid(catalogResourceId);
      queryResourcesParam.setResourceIdentifiers(resourceIdentitiers);
      QueryResourcesResponse queryResourcesResponse = catalogService.queryResources(queryResourcesParam);
      String referencePath = null;
      for (CatalogResource catalogResource : queryResourcesResponse.getQueryResourcesResult().getCatalogResources().getCatalogResource()) {
        if (catalogResource.getIdentifier().getGuid().equals(catalogResourceId.getGuid())) {
          referencePath = catalogResource.getNodeReferencePaths().getString()[0];
          break;
        }
      }
      if (referencePath == null) {
        throw new HostNotApplicableException(null);
      }

      QueryNodes queryNodesParam = new QueryNodes();
      ArrayOfString pathSpecs = new ArrayOfString();
      pathSpecs.addString(referencePath + TFSConstants.SINGLE_RECURSE_STAR);
      queryNodesParam.setPathSpecs(pathSpecs);
      ArrayOfGuid resourceTypeFilters = new ArrayOfGuid();
      resourceTypeFilters.addGuid(TFSConstants.PROJECT_COLLECTION_GUID);
      queryNodesParam.setResourceTypeFilters(resourceTypeFilters);
      QueryNodesResponse queryNodesResponse = catalogService.queryNodes(queryNodesParam);
      CatalogResource[] teamProjectCollections = queryNodesResponse.getQueryNodesResult().getCatalogResources().getCatalogResource();

      List<TeamProjectCollectionDescriptor> descriptors = new ArrayList<>(teamProjectCollections.length);
      for (CatalogResource collectionNode : teamProjectCollections) {
        String instanceId = getPropertyValue(collectionNode.getProperties(), TFSConstants.INSTANCE_ID_ATTRIBUTE);
        if (instanceId == null) {
          throw new HostNotApplicableException(null);
        }
        descriptors.add(new TeamProjectCollectionDescriptor(collectionNode.getDisplayName(), instanceId));
      }

      TfsBeansHolder beans = new TfsBeansHolder(uri);
      return new Tfs2010ServerDescriptor(descriptors, authorizedCredentials, uri, beans);
    }
    else {
      LOG.assertTrue(registrationEntries != null);
      uri = registrationEntries.first;
      // TFS 200x
      if (justAuthenticate) {
        String authorizedUsername = getAuthorizedCredentialsFor200x(context, uri, credentials);
        Credentials authorizedCredentials =
          new Credentials(authorizedUsername, credentials.getPassword(), credentials.isStorePassword(), credentials.getType());
        return new ServerDescriptor(authorizedCredentials, uri, null);
      }

      String instanceId = null;
      if (registrationEntries.second != null) {
        outer_loop:
        for (FrameworkRegistrationEntry entry : registrationEntries.second) {
          if (TFSConstants.TOOL_ID_TFS.equals(entry.getType())) {
            for (RegistrationExtendedAttribute2 attribute : entry.getRegistrationExtendedAttributes().getRegistrationExtendedAttribute()) {
              if (TFSConstants.INSTANCE_ID_ATTRIBUTE.equals(attribute.getName())) {
                instanceId = attribute.getValue();
                break outer_loop;
              }
            }
          }
        }
      }
      if (instanceId == null) {
        throw new HostNotApplicableException(null);
      }

      String qName = getAuthorizedCredentialsFor200x(context, uri, credentials);
      Credentials authorizedCredentials =
        new Credentials(qName, credentials.getPassword(), credentials.isStorePassword(), credentials.getType());

      TfsBeansHolder beans = new TfsBeansHolder(uri);
      Workspace[] workspaces = queryWorkspaces(authorizedCredentials, pi, beans);
      return new Tfs200xServerDescriptor(instanceId, authorizedCredentials, uri, workspaces, beans);
    }
  }