// Path: src/com/google/enterprise/adaptor/sharepoint/SamlAuthenticationHandler.java
// @VisibleForTesting
// public interface HttpPostClient {
//   public PostResponseInfo issuePostRequest(URL url,
//       Map<String, String> connectionProperties, String requestBody)
//       throws IOException;
// }
// 
// Path: src/com/google/enterprise/adaptor/sharepoint/SamlAuthenticationHandler.java
// @VisibleForTesting
// public static class HttpPostClientImpl implements HttpPostClient{
//   @Override
//   public PostResponseInfo issuePostRequest(URL url,
//       Map<String, String> connectionProperties, String requestBody)
//       throws IOException {
// 
//     // Handle Unicode. Java does not properly encode the GET.
//     try {
//       url = new URL(url.toURI().toASCIIString());
//     } catch (URISyntaxException ex) {
//       throw new IOException(ex);
//     }
// 
//     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//     try {
//       connection.setDoOutput(true);
//       connection.setDoInput(true);
//       connection.setRequestMethod("POST");
//       connection.setInstanceFollowRedirects(false);
// 
//       for(String key : connectionProperties.keySet()) {
//         connection.addRequestProperty(key, connectionProperties.get(key));
//       }
// 
//       if (!connectionProperties.containsKey("Content-Length")) {
//         connection.addRequestProperty("Content-Length",
//             Integer.toString(requestBody.length()));
//       }
// 
//       OutputStream out = connection.getOutputStream();
//       Writer wout = new OutputStreamWriter(out);
//       wout.write(requestBody);
//       wout.flush();
//       wout.close();
//       InputStream in = connection.getInputStream();
//       String result = IOHelper.readInputStreamToString(in, CHARSET);
//       return new PostResponseInfo(result, connection.getHeaderFields());
//     } finally {
//       InputStream inputStream = connection.getResponseCode() >= 400
//           ? connection.getErrorStream() : connection.getInputStream();
//       if (inputStream != null) {
//         inputStream.close();
//       }
//     }
//   }
// }


import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.HttpPostClient;
import com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.HttpPostClientImpl;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
// Copyright 2014 Google Inc. All Rights Reserved.
//
// 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.adaptor.sharepoint;





/**
 * SamlHandshakeManager implementation for Live Authentication 
 * to request Live authentication token and extract authentication cookie.
 */
public class LiveAuthenticationHandshakeManager
      extends AdfsHandshakeManager {
  private static final Logger log
      = Logger.getLogger(LiveAuthenticationHandshakeManager.class.getName());
  private static final String LIVE_STS
      = "https://login.microsoftonline.com/extSTS.srf";
  private static final String LIVE_LOGIN_URL
      = "/_forms/default.aspx?wa=wsignin1.0";

  public static class Builder {
    private final String username;
    private final String password;
    private final String sharePointUrl;
    private String stsendpoint;
    private final String stsrealm;
    private final HttpPostClient httpClient;
    private String login;
    private String trustLocation;

    public Builder(String sharePointUrl, String username, String password) {
// Path: src/com/google/enterprise/adaptor/sharepoint/SamlAuthenticationHandler.java
// @VisibleForTesting
// public interface HttpPostClient {
//   public PostResponseInfo issuePostRequest(URL url,
//       Map<String, String> connectionProperties, String requestBody)
//       throws IOException;
// }
// 
// Path: src/com/google/enterprise/adaptor/sharepoint/SamlAuthenticationHandler.java
// @VisibleForTesting
// public static class HttpPostClientImpl implements HttpPostClient{
//   @Override
//   public PostResponseInfo issuePostRequest(URL url,
//       Map<String, String> connectionProperties, String requestBody)
//       throws IOException {
// 
//     // Handle Unicode. Java does not properly encode the GET.
//     try {
//       url = new URL(url.toURI().toASCIIString());
//     } catch (URISyntaxException ex) {
//       throw new IOException(ex);
//     }
// 
//     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//     try {
//       connection.setDoOutput(true);
//       connection.setDoInput(true);
//       connection.setRequestMethod("POST");
//       connection.setInstanceFollowRedirects(false);
// 
//       for(String key : connectionProperties.keySet()) {
//         connection.addRequestProperty(key, connectionProperties.get(key));
//       }
// 
//       if (!connectionProperties.containsKey("Content-Length")) {
//         connection.addRequestProperty("Content-Length",
//             Integer.toString(requestBody.length()));
//       }
// 
//       OutputStream out = connection.getOutputStream();
//       Writer wout = new OutputStreamWriter(out);
//       wout.write(requestBody);
//       wout.flush();
//       wout.close();
//       InputStream in = connection.getInputStream();
//       String result = IOHelper.readInputStreamToString(in, CHARSET);
//       return new PostResponseInfo(result, connection.getHeaderFields());
//     } finally {
//       InputStream inputStream = connection.getResponseCode() >= 400
//           ? connection.getErrorStream() : connection.getInputStream();
//       if (inputStream != null) {
//         inputStream.close();
//       }
//     }
//   }
// }


// Path: src/com/google/enterprise/adaptor/sharepoint/LiveAuthenticationHandshakeManager.java
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.HttpPostClient;
import com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.HttpPostClientImpl;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

// Copyright 2014 Google Inc. All Rights Reserved.
//
// 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.adaptor.sharepoint;





/**
 * SamlHandshakeManager implementation for Live Authentication 
 * to request Live authentication token and extract authentication cookie.
 */
public class LiveAuthenticationHandshakeManager
      extends AdfsHandshakeManager {
  private static final Logger log
      = Logger.getLogger(LiveAuthenticationHandshakeManager.class.getName());
  private static final String LIVE_STS
      = "https://login.microsoftonline.com/extSTS.srf";
  private static final String LIVE_LOGIN_URL
      = "/_forms/default.aspx?wa=wsignin1.0";

  public static class Builder {
    private final String username;
    private final String password;
    private final String sharePointUrl;
    private String stsendpoint;
    private final String stsrealm;
    private final HttpPostClient httpClient;
    private String login;
    private String trustLocation;

    public Builder(String sharePointUrl, String username, String password) {
      this(sharePointUrl, username, password, new HttpPostClientImpl());