@Override
 public Object authenticate(Resource resource, Request request) {
   log.debug("authenticate");
   Auth auth = request.getAuthorization();
   Object o = securityManager.authenticate(auth.getUser(), auth.getPassword());
   log.debug("result: " + o);
   return o;
 }
 @Override
 public void appendChallenges(Resource resource, Request request, List<String> challenges) {
   String realm = securityManager.getRealm(request.getHostHeader());
   challenges.add("Basic realm=\"" + realm + "\"");
 }
  /**
   * This method creates instances of required objects which have not been set on the builder.
   *
   * <p>These are subsequently wired together immutably in HttpManager when buildHttpManager is
   * called.
   *
   * <p>You can call this before calling buildHttpManager if you would like to modify property
   * values on the created objects before HttpManager is instantiated. Otherwise, you can call
   * buildHttpManager directly and it will call init if it has not been called
   */
  public final void init() {
    if (mainResourceFactory == null) {
      rootDir = new File(System.getProperty("user.home"));
      if (!rootDir.exists() || !rootDir.isDirectory()) {
        throw new RuntimeException("Root directory is not valie: " + rootDir.getAbsolutePath());
      }
      if (securityManager == null) {
        if (mapOfNameAndPasswords == null) {
          mapOfNameAndPasswords = new HashMap<String, String>();
          mapOfNameAndPasswords.put(defaultUser, defaultPassword);
        }
        securityManager = new SimpleSecurityManager(fsRealm, mapOfNameAndPasswords);
      }
      log.info("Using securityManager: " + securityManager.getClass());
      mainResourceFactory = new FileSystemResourceFactory(rootDir, securityManager, fsContextPath);
      log.info("Using file system with root directory: " + rootDir.getAbsolutePath());
    }
    log.info("Using mainResourceFactory: " + mainResourceFactory.getClass());
    if (authenticationService == null) {
      if (authenticationHandlers == null) {
        authenticationHandlers = new ArrayList<AuthenticationHandler>();
        if (basicHandler == null) {
          if (enableBasicAuth) {
            basicHandler = new BasicAuthHandler();
          }
        }
        if (basicHandler != null) {
          authenticationHandlers.add(basicHandler);
        }
        if (digestHandler == null) {
          if (enableDigestAuth) {
            if (nonceProvider == null) {
              if (expiredNonceRemover == null) {
                expiredNonceRemover = new ExpiredNonceRemover(nonces, nonceValiditySeconds);
                showLog("expiredNonceRemover", expiredNonceRemover);
              }
              nonceProvider =
                  new SimpleMemoryNonceProvider(nonceValiditySeconds, expiredNonceRemover, nonces);
              showLog("nonceProvider", nonceProvider);
            }
            digestHandler = new DigestAuthenticationHandler(nonceProvider);
          }
        }
        if (digestHandler != null) {
          authenticationHandlers.add(digestHandler);
        }
        if (formAuthenticationHandler == null) {
          if (enableFormAuth) {
            formAuthenticationHandler = new FormAuthenticationHandler();
          }
        }
        if (formAuthenticationHandler != null) {
          authenticationHandlers.add(formAuthenticationHandler);
        }
        if (cookieAuthenticationHandler == null) {
          if (enableCookieAuth) {
            if (cookieDelegateHandlers == null) {
              // Don't add digest!
              cookieDelegateHandlers = new ArrayList<AuthenticationHandler>();
              if (basicHandler != null) {
                cookieDelegateHandlers.add(basicHandler);
                authenticationHandlers.remove(basicHandler);
              }
              if (formAuthenticationHandler != null) {
                cookieDelegateHandlers.add(formAuthenticationHandler);
                authenticationHandlers.remove(formAuthenticationHandler);
              }
            }
            cookieAuthenticationHandler =
                new CookieAuthenticationHandler(cookieDelegateHandlers, mainResourceFactory);
            authenticationHandlers.add(cookieAuthenticationHandler);
          }
        }
      }
      authenticationService = new AuthenticationService(authenticationHandlers);
      showLog("authenticationService", authenticationService);
    }

    init(authenticationService);
    shutdownHandlers.add(expiredNonceRemover);
    expiredNonceRemover.start();
  }