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  org.jboss.security.PicketBoxMessages;

An implementation of CallbackHandler is assigned a Principal, and opaque Object credential as values passed to the constructor. This is suitable for environments that need non-interactive JAAS logins and is used by the JaasSecurityManager as its default CallbackHandler.

Author(s):
Scott.Stark@jboss.org
Version:
$Revision$
See also:
javax.security.auth.callback.CallbackHandler
handle(Callback[])
 
 public class SecurityAssociationHandler implements CallbackHandler
 {   
    private Principal principal;
    private Object credential;
 
    {
    }

   
Initialize the UsernamePasswordHandler with the principal and credentials to use.
 
    public SecurityAssociationHandler(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 c = callbacks[i];
          if (c instanceof SecurityAssociationCallback)
          {
             SecurityAssociationCallback sac = (SecurityAssociationCallbackc;
             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 the JACC context CallbackHandler 
            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