@Override
 public void onAuthenticationFailure(
     HttpServletRequest request, HttpServletResponse response, AuthenticationException e)
     throws IOException, ServletException {
   e.printStackTrace();
   System.out.println("FAIL " + e.getMessage());
   response.getWriter().print(e.getMessage());
   response.getWriter().flush();
 }
  /** Authentication test. */
  public void doAuthTest(
      StaplerRequest req,
      StaplerResponse rsp,
      @QueryParameter String username,
      @QueryParameter String password)
      throws IOException, ServletException {
    // require the administrator permission since this is full of debug info.
    Hudson.getInstance().checkPermission(Hudson.ADMINISTER);

    StringWriter out = new StringWriter();
    PrintWriter pw = new PrintWriter(out);

    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    try {
      UserDetailsService uds = getAuthenticationProvider();
      if (uds instanceof ActiveDirectoryUnixAuthenticationProvider) {
        ActiveDirectoryUnixAuthenticationProvider p =
            (ActiveDirectoryUnixAuthenticationProvider) uds;
        DesciprotrImpl descriptor = getDescriptor();

        for (String domainName : domain.split(",")) {
          try {
            pw.println("Domain=" + domainName + " site=" + site);
            List<SocketInfo> ldapServers = descriptor.obtainLDAPServer(domainName, site, server);
            pw.println("List of domain controllers: " + ldapServers);

            for (SocketInfo ldapServer : ldapServers) {
              pw.println("Trying a domain controller at " + ldapServer);
              try {
                UserDetails d =
                    p.retrieveUser(
                        username, password, domainName, Collections.singletonList(ldapServer));
                pw.println("Authenticated as " + d);
              } catch (AuthenticationException e) {
                e.printStackTrace(pw);
              }
            }
          } catch (NamingException e) {
            pw.println("Failing to resolve domain controllers");
            e.printStackTrace(pw);
          }
        }
      } else {
        pw.println("Using Windows ADSI. No diagnostics available.");
      }
    } catch (Exception e) {
      e.printStackTrace(pw);
    } finally {
      Thread.currentThread().setContextClassLoader(ccl);
    }

    req.setAttribute("output", out.toString());
    req.getView(this, "test.jelly").forward(req, rsp);
  }