// Path: src/com/google/enterprise/adaptor/sharepoint/SharePointAdaptor.java
// @VisibleForTesting
// static String getAdaptorUser(String username) {
//   if (!"".equals(username)) {
//     return username;
//   }
// 
//   // USERNAME and USERDOMAIN environment variables are applicable for 
//   // Windows only. On non windows adaptor machine user name will 
//   // not be empty.
//   // Using Ssytem.getenv instead on System.getProperty("user.name") because
//   // System.getProperty("user.name") returns username without domain.
//   if (System.getenv("USERDOMAIN") == null 
//       || System.getenv("USERNAME") == null) {
//     return null;
//   }
//   return  System.getenv("USERDOMAIN") + "\\" + System.getenv("USERNAME");
// }


import static com.google.enterprise.adaptor.sharepoint.SharePointAdaptor.getAdaptorUser;
import com.microsoft.schemas.sharepoint.soap.authentication.AuthenticationMode;
import com.microsoft.schemas.sharepoint.soap.authentication.AuthenticationSoap;
import com.microsoft.schemas.sharepoint.soap.authentication.LoginErrorCode;
import com.microsoft.schemas.sharepoint.soap.authentication.LoginResult;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.handler.MessageContext;
      }
      this.username = username;
      this.password = password;
      this.executor = executor;
      this.authenticationClient = authenticationClient;      
    }
    
    public SharePointFormsAuthenticationHandler build() {
      SharePointFormsAuthenticationHandler authenticationHandler
          = new SharePointFormsAuthenticationHandler(
              username, password, executor, authenticationClient);
      return authenticationHandler;
    }    
  }

  @Override
  public AuthenticationResult authenticate() throws IOException {
    if (!isFormsAuthentication()) {
      return new AuthenticationResult(null, DEFAULT_COOKIE_TIMEOUT_SECONDS,
          LoginErrorCode.NOT_IN_FORMS_AUTHENTICATION_MODE.toString());
    }
    LoginResult result;
    try {
      result = authenticationClient.login(username, password);
    } catch (WebServiceException ex) {
      log.log(Level.WARNING,
          "Forms authentication failed.", ex);
      log.log(Level.INFO, "Possible SharePoint environment configured to use "
          + "claims based windows integrated authentication. "
          + "Adaptor will fallback to use windows integrated authentication "
// Path: src/com/google/enterprise/adaptor/sharepoint/SharePointAdaptor.java
// @VisibleForTesting
// static String getAdaptorUser(String username) {
//   if (!"".equals(username)) {
//     return username;
//   }
// 
//   // USERNAME and USERDOMAIN environment variables are applicable for 
//   // Windows only. On non windows adaptor machine user name will 
//   // not be empty.
//   // Using Ssytem.getenv instead on System.getProperty("user.name") because
//   // System.getProperty("user.name") returns username without domain.
//   if (System.getenv("USERDOMAIN") == null 
//       || System.getenv("USERNAME") == null) {
//     return null;
//   }
//   return  System.getenv("USERDOMAIN") + "\\" + System.getenv("USERNAME");
// }


// Path: src/com/google/enterprise/adaptor/sharepoint/SharePointFormsAuthenticationHandler.java
import static com.google.enterprise.adaptor.sharepoint.SharePointAdaptor.getAdaptorUser;
import com.microsoft.schemas.sharepoint.soap.authentication.AuthenticationMode;
import com.microsoft.schemas.sharepoint.soap.authentication.AuthenticationSoap;
import com.microsoft.schemas.sharepoint.soap.authentication.LoginErrorCode;
import com.microsoft.schemas.sharepoint.soap.authentication.LoginResult;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.handler.MessageContext;

      }
      this.username = username;
      this.password = password;
      this.executor = executor;
      this.authenticationClient = authenticationClient;      
    }
    
    public SharePointFormsAuthenticationHandler build() {
      SharePointFormsAuthenticationHandler authenticationHandler
          = new SharePointFormsAuthenticationHandler(
              username, password, executor, authenticationClient);
      return authenticationHandler;
    }    
  }

  @Override
  public AuthenticationResult authenticate() throws IOException {
    if (!isFormsAuthentication()) {
      return new AuthenticationResult(null, DEFAULT_COOKIE_TIMEOUT_SECONDS,
          LoginErrorCode.NOT_IN_FORMS_AUTHENTICATION_MODE.toString());
    }
    LoginResult result;
    try {
      result = authenticationClient.login(username, password);
    } catch (WebServiceException ex) {
      log.log(Level.WARNING,
          "Forms authentication failed.", ex);
      log.log(Level.INFO, "Possible SharePoint environment configured to use "
          + "claims based windows integrated authentication. "
          + "Adaptor will fallback to use windows integrated authentication "
          + "using username \"{0}\"", getAdaptorUser(""));