KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > tags > CheckManageResourceTag


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security.tags;
21
22 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.struts.taglib.TagUtils;
27
28 import com.sslexplorer.policyframework.NoPermissionException;
29 import com.sslexplorer.policyframework.Permission;
30 import com.sslexplorer.policyframework.Resource;
31 import com.sslexplorer.policyframework.ResourceUtil;
32 import com.sslexplorer.security.LogonControllerFactory;
33 import com.sslexplorer.security.SessionInfo;
34
35 public class CheckManageResourceTag extends TagSupport JavaDoc {
36     
37     final static Log log = LogFactory.getLog(CheckManageResourceTag.class);
38     
39     boolean required = true;
40     Resource resource;
41     String JavaDoc resourceName;
42     String JavaDoc resourceProperty;
43     String JavaDoc resourceScope;
44     Permission[] permissions;
45
46     public CheckManageResourceTag() {
47     }
48
49     public int doStartTag() {
50
51         SessionInfo session = null;
52         try {
53             session = LogonControllerFactory.getInstance().getSessionInfo(pageContext.getSession());
54             if (session== null) {
55                 return required ? SKIP_BODY : EVAL_BODY_INCLUDE;
56             } else {
57                 if(resource == null) {
58                     if(resourceName == null) {
59                         throw new Exception JavaDoc("Must supply either a resource object or a bean name / property that contains the resource.");
60                     }
61                     resource = (Resource)TagUtils.getInstance().lookup(pageContext, resourceName, resourceProperty, resourceScope);
62                     if(resource == null) {
63                         throw new Exception JavaDoc("No resource under bean name " + resourceName + "/" + resourceProperty + "/" + resourceScope);
64                     }
65                 }
66                 boolean ok = false;
67                 try {
68                     ResourceUtil.checkResourceManagementRights(resource, session, permissions);
69                     ok = true;
70                 }
71                 catch(NoPermissionException npe) {
72                 }
73                 return required ? ( ok ? EVAL_BODY_INCLUDE : SKIP_BODY ) : ( ok ? SKIP_BODY : EVAL_BODY_INCLUDE );
74             }
75         } catch (Exception JavaDoc e) {
76             log.error("Failed to test manageabilitiy of resource.", e);
77         }
78         return SKIP_BODY;
79     }
80     
81     public void setResource(Resource resource) {
82         this.resource = resource;
83     }
84     
85     public void setName(String JavaDoc name) {
86         this.resourceName = name;
87     }
88     
89     public void setScope(String JavaDoc scope) {
90         this.resourceScope = scope;
91     }
92     
93     public void setProperty(String JavaDoc property) {
94         this.resourceProperty = property;
95     }
96
97     public void setRequired(boolean required) {
98         this.required = required;
99     }
100     
101     public void setPermission(Permission permission) {
102         this.permissions = new Permission[] { permission };
103     }
104     
105     public void setPermissions(Permission[] permissions) {
106         this.permissions = permissions;
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see javax.servlet.jsp.tagext.TagSupport#release()
113      */

114     public void release() {
115         required = true;
116         permissions = null;
117         resourceName = null;
118         resourceProperty = null;
119         resource = null;
120         super.release();
121     }
122 }
Popular Tags