KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > security > HasRankTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib.security;
11
12 import org.mmbase.bridge.jsp.taglib.util.Attribute;
13 import org.mmbase.bridge.jsp.taglib.*;
14 import org.mmbase.security.Rank; // sucks!
15

16 import java.util.regex.*;
17 import javax.servlet.jsp.JspTagException JavaDoc;
18
19
20 /**
21  * A very simple tag to check for the rank of the current user.
22  *
23  * @author Michiel Meeuwissen
24  * @version $Id: HasRankTag.java,v 1.3 2005/03/01 15:01:47 michiel Exp $
25  * @since MMBase-1.8
26  */

27
28 public class HasRankTag extends CloudReferrerTag implements Condition {
29
30     protected Attribute value = Attribute.NULL;
31     protected Attribute minValue = Attribute.NULL;
32     protected Attribute maxValue = Attribute.NULL;
33     protected Attribute inverse = Attribute.NULL;
34
35     public void setValue(String JavaDoc s) throws JspTagException JavaDoc {
36         value = getAttribute(s);
37     }
38     public void setMinvalue(String JavaDoc s) throws JspTagException JavaDoc {
39         minValue = getAttribute(s);
40     }
41     public void setMaxvalue(String JavaDoc s) throws JspTagException JavaDoc {
42         maxValue = getAttribute(s);
43     }
44
45     public void setInverse(String JavaDoc b) throws JspTagException JavaDoc {
46         inverse = getAttribute(b);
47     }
48     protected boolean getInverse() throws JspTagException JavaDoc {
49         return inverse.getBoolean(this, false);
50     }
51
52     public final static Pattern IS_INTEGER = Pattern.compile("\\d+");
53
54     public int doStartTag() throws JspTagException JavaDoc {
55         boolean result;
56         String JavaDoc minValueString = minValue.getString(this);
57         String JavaDoc maxValueString = maxValue.getString(this);
58         String JavaDoc valueString = value.getString(this);
59         if (valueString.length() > 0) {
60             if (minValueString.length() > 0 || maxValueString.length() > 0) {
61                 throw new TaglibException("Cannot specify 'value' attribute with one 'minvalue' or 'maxvalue' attributes.");
62             }
63             result = getCloudVar().getUser().getRank().toString().equals(valueString);
64         } else {
65             int rank = getCloudVar().getUser().getRank().getInt();
66             result = true;
67             if (minValueString.length() > 0) {
68                 int minRankInt;
69                 if (IS_INTEGER.matcher(minValueString).matches()) {
70                     minRankInt = Integer.parseInt(minValueString);
71                 } else {
72                     Rank minRank = Rank.getRank(minValueString);
73                     if (minRank == null) throw new TaglibException("Value of minrank: '" + minValueString + "' is not currently a known rank");
74                     minRankInt =minRank.getInt();
75                 }
76                 if (rank < minRankInt) {
77                     result = false;
78                 }
79             }
80             if (maxValueString.length() > 0) {
81                 int maxRankInt;
82                 if (IS_INTEGER.matcher(maxValueString).matches()) {
83                     maxRankInt = Integer.parseInt(maxValueString);
84                 } else {
85                     Rank maxRank = Rank.getRank(maxValueString);
86                     if (maxRank == null) throw new TaglibException("Value of maxrank'" + maxValueString + "' is not currently a known rank");
87                     maxRankInt = maxRank.getInt();
88                 }
89                 if (rank > maxRankInt) {
90                     result = false;
91                 }
92             }
93         }
94
95
96         if (result != getInverse()) {
97             return EVAL_BODY;
98         } else {
99             return SKIP_BODY;
100         }
101     }
102     public int doAfterBody() throws JspTagException JavaDoc {
103         if (EVAL_BODY == EVAL_BODY_BUFFERED) { // not needed if EVAL_BODY_INCLUDE
104
try{
105                 if(bodyContent != null) {
106                     bodyContent.writeOut(bodyContent.getEnclosingWriter());
107                 }
108             } catch(java.io.IOException JavaDoc e){
109                 throw new JspTagException JavaDoc("IO Error: " + e.getMessage());
110             }
111         }
112         return SKIP_BODY;
113     }
114
115 }
116
Popular Tags