Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2008, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file 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  org.jboss.security.PicketBoxMessages;

Serializable Callback Handler

Author(s):
Anil.Saldhana@redhat.com
Since:
25 November 2008
 
 public class JBossCallbackHandler implements CallbackHandlerSerializable
 {
    private static final long serialVersionUID = 1L;
 
    private Principal principal;
    private Object credential;
 
    public JBossCallbackHandler()
    {
    }

   
Initialize the UsernamePasswordHandler with the principal and credentials to use.
 
    public JBossCallbackHandler(Principal principalObject credential)
    {
       this. = principal;
       this. = credential;
    }
 
    public void setSecurityInfo(Principal principalObject credential)
    {
       this. = principal;
       this. = credential;
    }

   
Handles SecurityAssociationCallback, ObjectCallback, NameCallback and PasswordCallback types. A NameCallback name property is set to the Prinicpal.getName() value. A PasswordCallback password property is set to the getPassword() value. The preferred SecurityAssociationCallback has its principal and credential properties set to the instance principal and credential. An ObjectCallback has its credential set to the credential value.

Throws:
UnsupportedCallbackException - thrown if any callback of type other than SecurityAssociationCallback, ObjectCallback, NameCallback or PasswordCallback are seen.
See also:
getPassword()
 
    public void handle(Callback[] callbacksthrows
    {
       for (int i = 0; i < callbacks.lengthi++)
       {
          Callback callback = callbacks[i];
          this.handleCallBackcallback ); 
       }
    }
  
   
Handle a Callback

Parameters:
c callback
Throws:
UnsupportedCallbackException If the callback is not supported by this handler
 
    protected void handleCallBackCallback c ) throws UnsupportedCallbackException
    {
       if (c instanceof SecurityAssociationCallback)
       {
         sac.setPrincipal();
         sac.setCredential();
      }
      else if (c instanceof ObjectCallback)
      {
         ObjectCallback oc = (ObjectCallbackc;
         oc.setCredential();
      }
      else if (c instanceof NameCallback)
      {
         NameCallback nc = (NameCallbackc;
         if ( != null)
            nc.setName(.getName());
      }
      else if (c instanceof PasswordCallback)
      {
         PasswordCallback pc = (PasswordCallbackc;
         char[] password = getPassword();
         if (password != null)
            pc.setPassword(password);
      }
      else
      {
         try
         {
            CallbackHandler handler = SecurityActions.getContextCallbackHandler();
            ifhandler != null )
            {
               Callback[] unknown = {c};
               handler.handle(unknown);
               return;
            }
         }
         catch (Exception e)
         {
         } 
         throw PicketBoxMessages.MESSAGES.unableToHandleCallback(cthis.getClass().getName(), c.getClass().getCanonicalName());
      }
      
   }

   
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