KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > servlets > SubjectFilter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.web.servlets;
23
24 import java.io.IOException JavaDoc;
25 import javax.servlet.Filter JavaDoc;
26 import javax.servlet.FilterConfig JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.ServletResponse JavaDoc;
30 import javax.servlet.FilterChain JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.security.auth.Subject JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35
36 import org.jboss.security.SubjectSecurityManager;
37
38 /**
39  * @author Scott.Stark@jboss.org
40  * @version $Revision: 37406 $
41  */

42 public class SubjectFilter implements Filter JavaDoc
43 {
44    public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc
45    {
46
47    }
48
49    public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
50       FilterChain JavaDoc filterChain)
51       throws IOException JavaDoc, ServletException JavaDoc
52    {
53       HttpServletResponse JavaDoc httpResponse = (HttpServletResponse JavaDoc) response;
54       try
55       {
56          Subject JavaDoc userSubject = getActiveSubject(httpResponse);
57          if (userSubject == null)
58             throw new ServletException JavaDoc("Active subject was null");
59       }
60       catch (NamingException JavaDoc e)
61       {
62          throw new ServletException JavaDoc("Failed to lookup active subject", e);
63       }
64       filterChain.doFilter(request, response);
65    }
66
67    public void destroy()
68    {
69    }
70
71    protected Subject JavaDoc getActiveSubject(HttpServletResponse JavaDoc httpResponse)
72       throws NamingException JavaDoc
73    {
74       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
75       SubjectSecurityManager mgr = (SubjectSecurityManager) ctx.lookup("java:comp/env/security/securityMgr");
76       Subject JavaDoc s0 = mgr.getActiveSubject();
77       httpResponse.addHeader("X-SubjectFilter-SubjectSecurityManager", s0.toString());
78       Subject JavaDoc s1 = (Subject JavaDoc) ctx.lookup("java:comp/env/security/subject");
79       httpResponse.addHeader("X-SubjectFilter-ENC", s1.toString());
80       return s1;
81    }
82 }
83
Popular Tags