KickJava   Java API By Example, From Geeks To Geeks.

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


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.web.tomcat.security;
23
24 import java.io.IOException JavaDoc;
25 import java.security.CodeSource JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.security.jacc.PolicyContext JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31
32 import org.apache.catalina.connector.Request;
33 import org.apache.catalina.connector.Response;
34 import org.apache.catalina.valves.ValveBase;
35 import org.jboss.logging.Logger;
36 import org.jboss.metadata.WebMetaData;
37 import org.jboss.security.SecurityRolesAssociation;
38
39 /**
40  * A Valve that sets the JACC context id and HttpServletRequest policy
41  * context handler value. The context id needs to be established prior to
42  * any authorization valves.
43  *
44  * @author Scott.Stark@jboss.org
45  * @version $Revision: 58442 $
46  */

47 public class JaccContextValve extends ValveBase
48 {
49    private static Logger log = Logger.getLogger(JaccContextValve.class);
50    public static ThreadLocal JavaDoc activeCS = new ThreadLocal JavaDoc();
51
52    /** The web app metadata */
53    private String JavaDoc contextID;
54    /** The web app deployment code source */
55    private CodeSource JavaDoc warCS;
56    private WebMetaData metaData;
57    private boolean trace;
58
59    public JaccContextValve(WebMetaData wmd, CodeSource JavaDoc cs)
60    {
61       this.metaData = wmd;
62       this.contextID = metaData.getJaccContextID();
63       this.warCS = cs;
64       this.trace = log.isTraceEnabled();
65    }
66
67    public void invoke(Request JavaDoc request, Response response)
68       throws IOException JavaDoc, ServletException JavaDoc
69    {
70       activeCS.set(warCS);
71       HttpServletRequest JavaDoc httpRequest = (HttpServletRequest JavaDoc) request.getRequest();
72
73       //Set the customized rolename-principalset mapping in jboss-app.xml
74
Map JavaDoc principalToRoleSetMap = metaData.getPrincipalVersusRolesMap();
75       SecurityRolesAssociation.setSecurityRoles(principalToRoleSetMap);
76       if(trace)
77          log.trace("MetaData:"+metaData+":principalToRoleSetMap"+principalToRoleSetMap);
78       
79       try
80       {
81          // Set the JACC context id
82
PolicyContext.setContextID(contextID);
83          // Set the JACC HttpServletRequest PolicyContextHandler data
84
HttpServletRequestPolicyContextHandler.setRequest(httpRequest);
85          // Perform the request
86
getNext().invoke(request, response);
87       }
88       finally
89       {
90          SecurityAssociationActions.clear();
91          activeCS.set(null);
92          SecurityRolesAssociation.setSecurityRoles(null);
93       }
94    }
95 }
96
Popular Tags