// Path: src/main/java/com/google/enterprise/cloudsearch/sharepoint/SamlAuthenticationHandler.java
// @VisibleForTesting
// public interface HttpPostClient {
//   public PostResponseInfo issuePostRequest(URL url,
//       Map<String, String> connectionProperties, String requestBody)
//       throws IOException;
// }
// 
// Path: src/main/java/com/google/enterprise/cloudsearch/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 = readInputStreamToString(in, CHARSET);
//       return new PostResponseInfo(result, connection.getHeaderFields());
//     } finally {
//       InputStream inputStream = connection.getResponseCode() >= 400
//           ? connection.getErrorStream() : connection.getInputStream();
//       if (inputStream != null) {
//         inputStream.close();
//       }
//     }
//   }
// 
//   private String readInputStreamToString(InputStream in, Charset charset) throws IOException {
//     return new String(ByteStreams.toByteArray(in), charset);
//   }
// }


import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.google.common.annotations.VisibleForTesting;
import com.google.enterprise.cloudsearch.sharepoint.SamlAuthenticationHandler.HttpPostClient;
import com.google.enterprise.cloudsearch.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;
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document document
          = db.parse(new InputSource(new StringReader(tokenResponse)));
      NodeList nodes
          = document.getElementsByTagNameNS(
              "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-"
                  + "wssecurity-secext-1.0.xsd", "BinarySecurityToken");
      if (nodes == null || nodes.getLength() == 0) {
        log.log(Level.WARNING, "Live Authentication token not available"
            + " in response {0}", tokenResponse);
        throw new IOException(
            "Live Authentication token not available in response");
      }
      String token = nodes.item(0).getTextContent();
      log.log(Level.FINER, "Live Authentication Token {0}", token);
      return token;
    } catch (ParserConfigurationException ex) {
      throw new IOException(ex);
    } catch (SAXException ex) {
      throw new IOException(ex);
    } catch (DOMException ex) {
      throw new IOException(ex);
    }
  }

  @Override
  public String getAuthenticationCookie(String token) throws IOException {
    URL u = new URL(login);
    Map<String, String> requestProperties = new HashMap<String, String>();
    requestProperties.put("SOAPAction", stsendpoint);
// Path: src/main/java/com/google/enterprise/cloudsearch/sharepoint/SamlAuthenticationHandler.java
// @VisibleForTesting
// public interface HttpPostClient {
//   public PostResponseInfo issuePostRequest(URL url,
//       Map<String, String> connectionProperties, String requestBody)
//       throws IOException;
// }
// 
// Path: src/main/java/com/google/enterprise/cloudsearch/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 = readInputStreamToString(in, CHARSET);
//       return new PostResponseInfo(result, connection.getHeaderFields());
//     } finally {
//       InputStream inputStream = connection.getResponseCode() >= 400
//           ? connection.getErrorStream() : connection.getInputStream();
//       if (inputStream != null) {
//         inputStream.close();
//       }
//     }
//   }
// 
//   private String readInputStreamToString(InputStream in, Charset charset) throws IOException {
//     return new String(ByteStreams.toByteArray(in), charset);
//   }
// }


// Path: src/main/java/com/google/enterprise/cloudsearch/sharepoint/LiveAuthenticationHandshakeManager.java
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.google.common.annotations.VisibleForTesting;
import com.google.enterprise.cloudsearch.sharepoint.SamlAuthenticationHandler.HttpPostClient;
import com.google.enterprise.cloudsearch.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;

      DocumentBuilder db = dbf.newDocumentBuilder();
      Document document
          = db.parse(new InputSource(new StringReader(tokenResponse)));
      NodeList nodes
          = document.getElementsByTagNameNS(
              "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-"
                  + "wssecurity-secext-1.0.xsd", "BinarySecurityToken");
      if (nodes == null || nodes.getLength() == 0) {
        log.log(Level.WARNING, "Live Authentication token not available"
            + " in response {0}", tokenResponse);
        throw new IOException(
            "Live Authentication token not available in response");
      }
      String token = nodes.item(0).getTextContent();
      log.log(Level.FINER, "Live Authentication Token {0}", token);
      return token;
    } catch (ParserConfigurationException ex) {
      throw new IOException(ex);
    } catch (SAXException ex) {
      throw new IOException(ex);
    } catch (DOMException ex) {
      throw new IOException(ex);
    }
  }

  @Override
  public String getAuthenticationCookie(String token) throws IOException {
    URL u = new URL(login);
    Map<String, String> requestProperties = new HashMap<String, String>();
    requestProperties.put("SOAPAction", stsendpoint);
    SamlAuthenticationHandler.PostResponseInfo postResponse