Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
 package org.jboss.security.auth.callback;
 
 import java.util.Map;
 
 
 import  org.jboss.security.PicketBoxMessages;
 
 //$Id: AppCallbackHandler.java 332 2012-07-10 15:56:58Z sguilhen@redhat.com $
 
JBAS-3109:AppCallbackHandler as the default CallbackHandler in the security module An implementation of the JAAS CallbackHandler interface that handles NameCallbacks, PasswordCallback, TextInputCallback and the JBoss ByteArrayCallback. All JBoss Callbacks must be handled. - MapCallback

Author(s):
Scott.Stark@jboss.org
Anil.Saldhana@jboss.org
Version:
$Revision: 332 $
See also:
javax.security.auth.callback.CallbackHandler
handle(Callback[])
 
 public class AppCallbackHandler implements CallbackHandler
 {
    private String username;
    private char[] password;
    private byte[] data;
    private String text
    
    private transient String prompt;
    private transient Object credential;
    
    private Map<String,ObjectkeyValuePair;  
   
   
Whether this handler gets the username/password from the console
 
    private boolean consoleHandler = false
 
    public AppCallbackHandler()
    {   
    }
    
    public AppCallbackHandler(String usernamechar[] password)
    {
       this. = username;
       this. = password;
    }
    public AppCallbackHandler(String usernamechar[] passwordbyte[] data)
    {
       this. = username;
       this. = password;
       this. = data;
    }
    public AppCallbackHandler(String usernamechar[] passwordbyte[] dataString text)
    {
       this. = username;
       this. = password;
       this. = data;
       this. = text;
    } 
   
   
Create a new AppCallbackHandler.

Parameters:
isConsoleHandler Denotes whether the input is from the console.
   public AppCallbackHandler(boolean isConsoleHandler)
   {
      this. = isConsoleHandler
   } 
   
   
Create a new AppCallbackHandler.

Parameters:
prompt Prompt meaningful to the LoginModule
   public AppCallbackHandler(String prompt)
   {
       this. = prompt;
   }
   
   
Create a new AppCallbackHandler.

Parameters:
mapOfValues Key Value Pair
   public AppCallbackHandler(Map<String,ObjectmapOfValues)
   { 
      this. = mapOfValues;
   }
   
   public void setSecurityInfo(Principal pObject cred)
   {
      this. = p.getName();
      this. = cred;
   }
   public String getPrompt()
   {
       return ;
   }
   public Object getCredential()
   {
       return ;
   }
   public void setCredential(Object credential)
   {
       this. = credential;
   }
   public void clearCredential()
   {
       this. = null;
   } 
   public void handle(Callback[] callbacksthrows
   {
      for (int i = 0; i < callbacks.lengthi++)
      {
         Callback c = callbacks[i]; 
         ifc instanceof NameCallback )
         {
            NameCallback nc = (NameCallbackc
            String prompt = nc.getPrompt();
            ifprompt == null )
               prompt = PicketBoxMessages.MESSAGES.enterUsernameMessage();
            if(this.)
               nc.setName(getUserNameFromConsole(prompt));
            else
               nc.setName();
         }
         else ifc instanceof PasswordCallback )
         {
            PasswordCallback pc = (PasswordCallbackc;
            String prompt = pc.getPrompt();
            ifprompt == null )
               prompt = PicketBoxMessages.MESSAGES.enterPasswordMessage();
            if(this.)
               pc.setPassword(getPasswordFromConsole(prompt));
            else
               if(this. != null && this. == null)
                  pc.setPassword(this.getPassword());
            else
               pc.setPassword();
         }
         else ifc instanceof TextInputCallback )
         {
            TextInputCallback tc = (TextInputCallbackc;
            tc.setText();
         }
         else ifc instanceof ByteArrayCallback )
         {
            ByteArrayCallback bac = (ByteArrayCallbackc;
            bac.setByteArray();
         }
         else if (c instanceof ObjectCallback)
         {
            ObjectCallback oc = (ObjectCallbackc;
            oc.setCredential();
         }
         else ifc instanceof MapCallback )
         {
            MapCallback mc = (MapCallbackc;
            if( != null && !.isEmpty())
            {
               for (String key : .keySet())
               {
                  mc.setInfo(key.get(key));
               }  
            }
         }
         else
         {
            throw PicketBoxMessages.MESSAGES.unableToHandleCallback(cthis.getClass().getName(), c.getClass().getCanonicalName());
         }
      }
   }
   
   private String getUserNameFromConsole(String prompt)
   {
      String uName = "";
      ..print(prompt);
      InputStreamReader isr = new InputStreamReader(.);
      BufferedReader br = new BufferedReader(isr);
      try
      {
         uName = br.readLine(); 
      }
      catch(IOException e)
      {
         throw PicketBoxMessages.MESSAGES.failedToObtainUsername(e);
      }
      return uName;
   }
   
   private char[] getPasswordFromConsole(String prompt)
   {
      String pwd = "";
      //Prompt the user for the username
      ..print(prompt);
      InputStreamReader isr = new InputStreamReader(.);
      BufferedReader br = new BufferedReader(isr);
      try
      {
         pwd = br.readLine();   
      }
      catch(IOException e)
      {
         throw PicketBoxMessages.MESSAGES.failedToObtainPassword(e);
      }
      return pwd.toCharArray();
   }
   
   
Try to convert the credential value into a char[] using the first of the following attempts which succeeds: 1. Check for instanceof char[] 2. Check for instanceof String and then use toCharArray() 3. See if credential has a toCharArray() method and use it 4. Use toString() followed by toCharArray().

Returns:
a char[] representation of the credential.
  private char[] getPassword()
  {
     char[] password = null;
     if ( instanceof char[])
     {
        password = (char[]) ;
     }
     else if ( instanceof String)
     {
        String s = (String;
        password = s.toCharArray();
     }
     else
     {
        try
        {
           Class<?>[] types = {};
           Method m = .getClass().getMethod("toCharArray"types);
           Object[] args = {};
           password = (char[]) m.invoke(args);
        }
        catch (Exception e)
        {
           if ( != null)
           {
              String s = .toString();
              password = s.toCharArray();
           }
        }
     }
     return password;
  }
New to GrepCode? Check out our FAQ X