// Path: src/main/java/com/google/enterprise/cloudsearch/sharepoint/ActiveDirectoryPrincipal.java
// static enum PrincipalFormat {
//   NONE,
//   DNS,
//   NETBIOS,
//   ;
// 
//   String format(String plainName, String domain) {
//     checkArgument(
//         !Strings.isNullOrEmpty(domain) || this == PrincipalFormat.NONE,
//         "Domain can not be empty if PrincipalFormat is not NONE");
//     switch (this) {
//       case NONE:
//         return plainName;
//       case DNS:
//         return plainName + "@" + domain;
//       case NETBIOS:
//         return domain + "\\" + plainName;
//       default:
//         throw new AssertionError("Unsupported PrincipalFormat " + this);
//     }
//   }
// }
// 
// Path: src/main/java/com/google/enterprise/cloudsearch/sharepoint/SharePointConfiguration.java
// enum SharePointDeploymentType {
//   ONLINE,
//   ON_PREMISES;
// }


import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.services.cloudidentity.v1.model.EntityKey;
import com.google.api.services.cloudidentity.v1.model.Membership;
import com.google.api.services.cloudidentity.v1.model.MembershipRole;
import com.google.api.services.cloudsearch.v1.model.Principal;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.enterprise.cloudsearch.sdk.identity.IdentityGroup;
import com.google.enterprise.cloudsearch.sdk.identity.IdentitySourceConfiguration;
import com.google.enterprise.cloudsearch.sdk.identity.RepositoryContext;
import com.google.enterprise.cloudsearch.sdk.indexing.Acl;
import com.google.enterprise.cloudsearch.sharepoint.ActiveDirectoryPrincipal.PrincipalFormat;
import com.google.enterprise.cloudsearch.sharepoint.SharePointConfiguration.SharePointDeploymentType;
import com.microsoft.schemas.sharepoint.soap.GroupMembership;
import com.microsoft.schemas.sharepoint.soap.Permission;
import com.microsoft.schemas.sharepoint.soap.PolicyUser;
import com.microsoft.schemas.sharepoint.soap.Scopes.Scope;
import com.microsoft.schemas.sharepoint.soap.Site;
import com.microsoft.schemas.sharepoint.soap.TrueFalseType;
import com.microsoft.schemas.sharepoint.soap.UserDescription;
import com.microsoft.schemas.sharepoint.soap.Users;
import com.microsoft.schemas.sharepoint.soap.VirtualServer;
import com.microsoft.schemas.sharepoint.soap.Web;
import com.microsoft.schemas.sharepoint.soap.directory.GetUserCollectionFromSiteResponse;
import com.microsoft.schemas.sharepoint.soap.directory.GetUserCollectionFromSiteResponse.GetUserCollectionFromSiteResult;
import com.microsoft.schemas.sharepoint.soap.directory.User;
import com.microsoft.schemas.sharepoint.soap.directory.UserGroupSoap;
import com.microsoft.schemas.sharepoint.soap.people.ArrayOfPrincipalInfo;
import com.microsoft.schemas.sharepoint.soap.people.ArrayOfString;
import com.microsoft.schemas.sharepoint.soap.people.PeopleSoap;
import com.microsoft.schemas.sharepoint.soap.people.PrincipalInfo;
import com.microsoft.schemas.sharepoint.soap.people.SPPrincipalType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
    if (isDomainGroup
        && activeDirectoryClient.isPresent()
        && loginName.startsWith("c:0+.w|")
        && !Strings.isNullOrEmpty(sid)) {
      try {
        return activeDirectoryClient.get().getUserAccountBySid(sid);
      } catch (IOException ex) {
        log.log(
            Level.WARNING,
            String.format(
                "Error performing SID lookup for "
                    + "User %s. Returing display name %s as fallback.",
                loginName, displayName),
            ex);
        return displayName;
      }
    }
    return isSharePointOnlineDeployment()
        ? decodeSharePointOnlineClaim(loginName)
        : decodeClaim(loginName, displayName);
  }

  private Optional<Principal> getPrincipal(String id, boolean isGroup) {
    return isSharePointOnlineDeployment()
        ? getSharePointOnlinePrincipal(id, isGroup)
        : getActiveDirectoryPrincipal(id, isGroup);
  }

  private Optional<Principal> getActiveDirectoryPrincipal(String id, boolean isGroup) {
    ActiveDirectoryPrincipal adPrincipal = ActiveDirectoryPrincipal.parse(id);
// Path: src/main/java/com/google/enterprise/cloudsearch/sharepoint/ActiveDirectoryPrincipal.java
// static enum PrincipalFormat {
//   NONE,
//   DNS,
//   NETBIOS,
//   ;
// 
//   String format(String plainName, String domain) {
//     checkArgument(
//         !Strings.isNullOrEmpty(domain) || this == PrincipalFormat.NONE,
//         "Domain can not be empty if PrincipalFormat is not NONE");
//     switch (this) {
//       case NONE:
//         return plainName;
//       case DNS:
//         return plainName + "@" + domain;
//       case NETBIOS:
//         return domain + "\\" + plainName;
//       default:
//         throw new AssertionError("Unsupported PrincipalFormat " + this);
//     }
//   }
// }
// 
// Path: src/main/java/com/google/enterprise/cloudsearch/sharepoint/SharePointConfiguration.java
// enum SharePointDeploymentType {
//   ONLINE,
//   ON_PREMISES;
// }


// Path: src/main/java/com/google/enterprise/cloudsearch/sharepoint/SiteConnector.java
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.services.cloudidentity.v1.model.EntityKey;
import com.google.api.services.cloudidentity.v1.model.Membership;
import com.google.api.services.cloudidentity.v1.model.MembershipRole;
import com.google.api.services.cloudsearch.v1.model.Principal;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.enterprise.cloudsearch.sdk.identity.IdentityGroup;
import com.google.enterprise.cloudsearch.sdk.identity.IdentitySourceConfiguration;
import com.google.enterprise.cloudsearch.sdk.identity.RepositoryContext;
import com.google.enterprise.cloudsearch.sdk.indexing.Acl;
import com.google.enterprise.cloudsearch.sharepoint.ActiveDirectoryPrincipal.PrincipalFormat;
import com.google.enterprise.cloudsearch.sharepoint.SharePointConfiguration.SharePointDeploymentType;
import com.microsoft.schemas.sharepoint.soap.GroupMembership;
import com.microsoft.schemas.sharepoint.soap.Permission;
import com.microsoft.schemas.sharepoint.soap.PolicyUser;
import com.microsoft.schemas.sharepoint.soap.Scopes.Scope;
import com.microsoft.schemas.sharepoint.soap.Site;
import com.microsoft.schemas.sharepoint.soap.TrueFalseType;
import com.microsoft.schemas.sharepoint.soap.UserDescription;
import com.microsoft.schemas.sharepoint.soap.Users;
import com.microsoft.schemas.sharepoint.soap.VirtualServer;
import com.microsoft.schemas.sharepoint.soap.Web;
import com.microsoft.schemas.sharepoint.soap.directory.GetUserCollectionFromSiteResponse;
import com.microsoft.schemas.sharepoint.soap.directory.GetUserCollectionFromSiteResponse.GetUserCollectionFromSiteResult;
import com.microsoft.schemas.sharepoint.soap.directory.User;
import com.microsoft.schemas.sharepoint.soap.directory.UserGroupSoap;
import com.microsoft.schemas.sharepoint.soap.people.ArrayOfPrincipalInfo;
import com.microsoft.schemas.sharepoint.soap.people.ArrayOfString;
import com.microsoft.schemas.sharepoint.soap.people.PeopleSoap;
import com.microsoft.schemas.sharepoint.soap.people.PrincipalInfo;
import com.microsoft.schemas.sharepoint.soap.people.SPPrincipalType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

    if (isDomainGroup
        && activeDirectoryClient.isPresent()
        && loginName.startsWith("c:0+.w|")
        && !Strings.isNullOrEmpty(sid)) {
      try {
        return activeDirectoryClient.get().getUserAccountBySid(sid);
      } catch (IOException ex) {
        log.log(
            Level.WARNING,
            String.format(
                "Error performing SID lookup for "
                    + "User %s. Returing display name %s as fallback.",
                loginName, displayName),
            ex);
        return displayName;
      }
    }
    return isSharePointOnlineDeployment()
        ? decodeSharePointOnlineClaim(loginName)
        : decodeClaim(loginName, displayName);
  }

  private Optional<Principal> getPrincipal(String id, boolean isGroup) {
    return isSharePointOnlineDeployment()
        ? getSharePointOnlinePrincipal(id, isGroup)
        : getActiveDirectoryPrincipal(id, isGroup);
  }

  private Optional<Principal> getActiveDirectoryPrincipal(String id, boolean isGroup) {
    ActiveDirectoryPrincipal adPrincipal = ActiveDirectoryPrincipal.parse(id);
    if (adPrincipal.getFormat() == PrincipalFormat.NONE) {