KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > security > WebCallbackHandler


1 /*
2  * JBoss, the OpenSource WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.web.tomcat.security;
9
10 import java.security.Principal JavaDoc;
11
12 import javax.security.auth.callback.Callback JavaDoc;
13 import javax.security.auth.callback.CallbackHandler JavaDoc;
14 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16
17 import org.jboss.security.auth.callback.SecurityAssociationHandler;
18
19 /**
20  * An implementation of CallbackHandler that extends the default
21  * SecurityAssociationHandler to add Callbacks that only have sense in a Web
22  * environment.
23  *
24  * In order to use it you need to override the default CallbackHandler used by
25  * the JaasSecurityManager.
26  *
27  * @see javax.security.auth.callback.CallbackHandler
28  * @see #handle(Callback[])
29  *
30  * @author Ricardo Arguello (ricardoarguello@users.sourceforge.net)
31  * @version $Revision: 1.1.2.1 $
32  */

33 public class WebCallbackHandler extends SecurityAssociationHandler implements
34       CallbackHandler JavaDoc
35 {
36    public WebCallbackHandler()
37    {
38       super();
39    }
40
41    /**
42     * Initialize the HttpServletRequestCallbackHandler with the principal and
43     * credentials to use.
44     */

45    public WebCallbackHandler(Principal JavaDoc principal, Object JavaDoc credential)
46    {
47       super(principal, credential);
48    }
49
50    /**
51     * @see org.jboss.security.auth.callback.SecurityAssociationHandler#setSecurityInfo(java.security.Principal,
52     * java.lang.Object)
53     */

54    public void setSecurityInfo(Principal JavaDoc principal, Object JavaDoc credential)
55    {
56       super.setSecurityInfo(principal, credential);
57    }
58
59    /**
60     * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
61     */

62    public void handle(Callback JavaDoc[] callbacks) throws UnsupportedCallbackException JavaDoc
63    {
64       try
65       {
66          super.handle(callbacks);
67       }
68       catch (UnsupportedCallbackException JavaDoc uce)
69       {
70          Callback JavaDoc c = uce.getCallback();
71
72          if (c instanceof HttpServletRequestCallback)
73          {
74             // Get the HttpServletRequest from the Valve
75
HttpServletRequest JavaDoc request = getHttpServletRequestFromValve();
76
77             // Set it in the Callback
78
HttpServletRequestCallback hsrc = (HttpServletRequestCallback) c;
79             hsrc.setHttpServletRequest(request);
80          }
81          else
82          {
83             throw new UnsupportedCallbackException JavaDoc(c, "Unrecognized Callback");
84          }
85       }
86    }
87
88    /**
89     * Obtains the HttpServletRequest saved inside the HttpServletRequestValve.
90     *
91     * @return an HttpServletRequest.
92     */

93    protected HttpServletRequest JavaDoc getHttpServletRequestFromValve()
94    {
95       return (HttpServletRequest JavaDoc) HttpServletRequestValve.httpRequest.get();
96    }
97 }
Popular Tags