// 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/SamlAuthenticationHandler.java
// @VisibleForTesting
// public static class PostResponseInfo {
//   /** Non-null contents. */
//   private final String contents;
//   /** Non-null headers. */
//   private final Map<String, List<String>> headers;
// 
//   PostResponseInfo(
//       String contents, Map<String, List<String>> headers) {
//     this.contents = contents;
//     this.headers  = (headers == null)
//         ? new HashMap<String, List<String>>() 
//         : new HashMap<String, List<String>>(headers);
//   }
// 
//   public String getPostContents() {
//     return contents;
//   }
// 
//   public Map<String, List<String>> getPostResponseHeaders() {
//     return Collections.unmodifiableMap(headers);
//   }
// 
//   public String getPostResponseHeaderField(String header) {
//     if (headers == null || !headers.containsKey(header)) {
//       return null;
//     }
//     if (headers.get(header) == null || headers.get(header).isEmpty()) {
//       return null;
//     }
//     StringBuilder sbValues = new StringBuilder();
//     for(String value : headers.get(header)) {
//       if ("".equals(value)) {
//         continue;
//       }
//       sbValues.append(value);
//       sbValues.append(";");
//     }
//     return sbValues.toString();
//   }
// }
// 
// Path: src/com/google/enterprise/adaptor/sharepoint/SamlAuthenticationHandler.java
// @VisibleForTesting
// public interface SamlHandshakeManager {
//   public String requestToken() throws IOException;
//   public String getAuthenticationCookie(String token) throws IOException;
// }


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 com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.PostResponseInfo;
import com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.SamlHandshakeManager;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLEncoder;
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 javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
    }

    public Builder setLoginUrl(String login) {      
      this.login = login;
      return this;
    }

    public Builder setTrustLocation(String trustLocation) {      
      this.trustLocation = trustLocation;
      return this;
    }

    public AdfsHandshakeManager build() {
      if (Strings.isNullOrEmpty(trustLocation) 
          || Strings.isNullOrEmpty(login)) {
        throw new NullPointerException();
      }
      return new AdfsHandshakeManager(sharePointUrl, username,
          password, stsendpoint, stsrealm, login, trustLocation, httpClient);
    }
  }

  @Override
  public String requestToken() throws IOException {
    String saml = generateSamlRequest();    
    URL u = new URL(stsendpoint);
    Map<String, String> requestHeaders = new HashMap<String, String>();
    requestHeaders.put("SOAPAction", stsendpoint);
    requestHeaders.put("Content-Type",
        "application/soap+xml; charset=utf-8");
// 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/SamlAuthenticationHandler.java
// @VisibleForTesting
// public static class PostResponseInfo {
//   /** Non-null contents. */
//   private final String contents;
//   /** Non-null headers. */
//   private final Map<String, List<String>> headers;
// 
//   PostResponseInfo(
//       String contents, Map<String, List<String>> headers) {
//     this.contents = contents;
//     this.headers  = (headers == null)
//         ? new HashMap<String, List<String>>() 
//         : new HashMap<String, List<String>>(headers);
//   }
// 
//   public String getPostContents() {
//     return contents;
//   }
// 
//   public Map<String, List<String>> getPostResponseHeaders() {
//     return Collections.unmodifiableMap(headers);
//   }
// 
//   public String getPostResponseHeaderField(String header) {
//     if (headers == null || !headers.containsKey(header)) {
//       return null;
//     }
//     if (headers.get(header) == null || headers.get(header).isEmpty()) {
//       return null;
//     }
//     StringBuilder sbValues = new StringBuilder();
//     for(String value : headers.get(header)) {
//       if ("".equals(value)) {
//         continue;
//       }
//       sbValues.append(value);
//       sbValues.append(";");
//     }
//     return sbValues.toString();
//   }
// }
// 
// Path: src/com/google/enterprise/adaptor/sharepoint/SamlAuthenticationHandler.java
// @VisibleForTesting
// public interface SamlHandshakeManager {
//   public String requestToken() throws IOException;
//   public String getAuthenticationCookie(String token) throws IOException;
// }


// Path: src/com/google/enterprise/adaptor/sharepoint/AdfsHandshakeManager.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 com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.PostResponseInfo;
import com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.SamlHandshakeManager;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLEncoder;
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 javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

    }

    public Builder setLoginUrl(String login) {      
      this.login = login;
      return this;
    }

    public Builder setTrustLocation(String trustLocation) {      
      this.trustLocation = trustLocation;
      return this;
    }

    public AdfsHandshakeManager build() {
      if (Strings.isNullOrEmpty(trustLocation) 
          || Strings.isNullOrEmpty(login)) {
        throw new NullPointerException();
      }
      return new AdfsHandshakeManager(sharePointUrl, username,
          password, stsendpoint, stsrealm, login, trustLocation, httpClient);
    }
  }

  @Override
  public String requestToken() throws IOException {
    String saml = generateSamlRequest();    
    URL u = new URL(stsendpoint);
    Map<String, String> requestHeaders = new HashMap<String, String>();
    requestHeaders.put("SOAPAction", stsendpoint);
    requestHeaders.put("Content-Type",
        "application/soap+xml; charset=utf-8");
    PostResponseInfo postResponse