/**
 * Copyright 2015 LinkedIn Corp. 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.
 */
package security;

import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;

import javax.naming.AuthenticationException;
import javax.naming.NamingException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.Callback;

import com.google.common.base.Preconditions;

public class AuthenticationManager {

  public static void authenticateUser(String userName, String password) throws NamingException {
    Preconditions.checkNotNull(userName, "Username String cannot be null");
    Preconditions.checkNotNull(password, "Password String cannot be null");
    Preconditions.checkArgument(!userName.isEmpty(), "Username cannot be empty");
    Preconditions.checkArgument(!password.isEmpty(), "Password cannot be empty");
    try {
      LoginContext lc = new LoginContext("WHZ-Authentication", new WHZCallbackHandler(userName, password));
      lc.login();
    } catch (LoginException le) {
      throw new AuthenticationException(le.toString());
    }
  }

  private static class WHZCallbackHandler implements CallbackHandler {
    private String password = null;
    private String username = null;
    private WHZCallbackHandler(String username, String password) {
      this.username = username;
      this.password = password;
    }

    @Override
    public void handle(Callback[] callbacks)
        throws UnsupportedCallbackException {
      NameCallback nc = null;
      PasswordCallback pc = null;
      for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
          nc = (NameCallback) callback;
          nc.setName(this.username);
        } else if (callback instanceof PasswordCallback) {
          pc = (PasswordCallback) callback;
          pc.setPassword(this.password.toCharArray());
        } else {
          throw new UnsupportedCallbackException(callback, "The submitted Callback is unsupported");
        }
      }
    }
  }
}

@@ -523,16 +523,61 @@
 urity;%0A%0A
+import com.google.common.base.Preconditions;%0A
 import j
@@ -676,75 +676,8 @@
 ck;%0A
-import javax.security.auth.callback.UnsupportedCallbackException;%0A%0A
 impo
@@ -953,52 +953,71 @@
 ck;%0A
-%0A
 import 
-com.google.common.base.Preconditions
+org.apache.commons.lang3.StringUtils;%0Aimport play.Logger
 ;%0A%0Ap
@@ -1177,274 +1177,85 @@
 heck
-NotNull(userName, %22Username String cannot be null%22);%0A    Preconditions.checkNotNull(password, %22Password String cannot be null%22);%0A    Preconditions.checkArgument(!userName.isEmpty(), %22Username cannot be empty%22);%0A    Preconditions.checkArgument(!password.isEmpty(), %22P
+Argument(%0A        !StringUtils.isAnyEmpty(userName, password), %22Username or p
 assw
@@ -1274,16 +1274,21 @@
 e empty%22
+%0A    
 );%0A    t
@@ -1862,52 +1862,8 @@
 cks)
-%0A        throws UnsupportedCallbackException
  %7B%0A 
@@ -2283,57 +2283,20 @@
     
-throw new UnsupportedCallbackException(callback, 
+Logger.warn(
 %22The
@@ -2306,17 +2306,17 @@
 bmitted 
-C
+c
 allback 
@@ -2329,17 +2329,29 @@
 upported
-%22
+! %22, callback
 );%0A     

address more reviewer comments