View Javadoc

1   /*
2    * $Id: MuleWsSecurityCallbackHandler.java 10165 2007-12-28 11:34:33Z marie.rizzo $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.extras.wssecurity.callbackhandlers;
12  
13  import org.mule.MuleException;
14  import org.mule.MuleManager;
15  import org.mule.config.i18n.CoreMessages;
16  import org.mule.umo.manager.ObjectNotFoundException;
17  
18  import java.io.IOException;
19  import java.util.Properties;
20  
21  import javax.security.auth.callback.Callback;
22  import javax.security.auth.callback.CallbackHandler;
23  import javax.security.auth.callback.UnsupportedCallbackException;
24  
25  import org.apache.ws.security.WSPasswordCallback;
26  
27  public class MuleWsSecurityCallbackHandler implements CallbackHandler
28  {
29      private Properties passwords;
30  
31      /**
32       * This is the standard Mule callback handler that gets a set of passwords from
33       * the configuration file.
34       * 
35       * @throws MuleException in case of any error
36       */
37      public MuleWsSecurityCallbackHandler() throws MuleException
38      {
39          PasswordContainer pass;
40          try
41          {
42              pass = (PasswordContainer)MuleManager.getInstance().getContainerContext().getComponent(
43                  "passwords");
44              passwords = pass.getPasswords();
45          }
46          catch (ObjectNotFoundException e)
47          {
48              throw new MuleException(CoreMessages.authNoCredentials(), e);
49          }
50      }
51  
52      /**
53       * This method handles the callback, i.e. it checks whether the required password
54       * is available
55       */
56      public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
57      {
58          for (int i = 0; i < callbacks.length; i++)
59          {
60              WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];
61  
62              String pass = (String)passwords.get(pc.getIdentifer());
63              if (pass != null)
64              {
65                  pc.setPassword(pass);
66              }
67          }
68      }
69  }