// 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;
/*
 * Copyright 2018 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.enterprise.cloudsearch.sharepoint;



class SiteConnector {
  private static final Logger log = Logger.getLogger(SiteConnector.class.getName());
  private static final String IDENTITY_CLAIMS_PREFIX = "i:0";
  private static final String OTHER_CLAIMS_PREFIX = "c:0";
  private static final String SHAREPOINT_LOCAL_GROUP_FORMAT = "[%s]%s";
  private static final Supplier<Set<Membership>> EMPTY_MEMBERSHIP = () -> ImmutableSet.of();
  private static final ImmutableList<MembershipRole> MEMBER_ROLES =
      ImmutableList.of(new MembershipRole().setName("MEMBER"));
  static final long LIST_ITEM_MASK =
      SPBasePermissions.OPEN | SPBasePermissions.VIEWPAGES | SPBasePermissions.VIEWLISTITEMS;
  /** Default identity source for external principals when no domain information is available */
  static final String DEFAULT_REFERENCE_IDENTITY_SOURCE_NAME = "defaultIdentitySource";

  private final SiteDataClient siteDataClient;
  private final UserGroupSoap userGroup;
  private final PeopleSoap people;
  private final String siteUrl;
  private final String webUrl;
  private final Optional<ActiveDirectoryClient> activeDirectoryClient;
// 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;

/*
 * Copyright 2018 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.enterprise.cloudsearch.sharepoint;



class SiteConnector {
  private static final Logger log = Logger.getLogger(SiteConnector.class.getName());
  private static final String IDENTITY_CLAIMS_PREFIX = "i:0";
  private static final String OTHER_CLAIMS_PREFIX = "c:0";
  private static final String SHAREPOINT_LOCAL_GROUP_FORMAT = "[%s]%s";
  private static final Supplier<Set<Membership>> EMPTY_MEMBERSHIP = () -> ImmutableSet.of();
  private static final ImmutableList<MembershipRole> MEMBER_ROLES =
      ImmutableList.of(new MembershipRole().setName("MEMBER"));
  static final long LIST_ITEM_MASK =
      SPBasePermissions.OPEN | SPBasePermissions.VIEWPAGES | SPBasePermissions.VIEWLISTITEMS;
  /** Default identity source for external principals when no domain information is available */
  static final String DEFAULT_REFERENCE_IDENTITY_SOURCE_NAME = "defaultIdentitySource";

  private final SiteDataClient siteDataClient;
  private final UserGroupSoap userGroup;
  private final PeopleSoap people;
  private final String siteUrl;
  private final String webUrl;
  private final Optional<ActiveDirectoryClient> activeDirectoryClient;
  private final SharePointDeploymentType sharePointDeploymentType;