001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.portlet.mvcportlet.taglib;
022:
023: import javax.portlet.RenderRequest;
024: import javax.servlet.jsp.JspException;
025: import javax.servlet.jsp.tagext.TagSupport;
026:
027: import com.nabhinc.condition.Condition;
028: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
029: import com.nabhinc.portlet.mvcportlet.core.Constants;
030: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
031: import com.nabhinc.portlet.mvcportlet.core.RenderConfig;
032:
033: /**
034: * This tag is used to enclose other tags which access need to be controlled.
035: * The access or visibility of the enclosed controls is determined by the
036: * pre-condition rule defined by the specified request type (<code>actionType</code>
037: * or <code>renderType</code>). Note that, the request type must a valid action-type
038: * or render-type defined in configuration.
039: *
040: * @author Padmanabh Dabke
041: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
042: * @since MVCPortlet 0.8
043: */
044: public class ShowIfAllowedTag extends TagSupport {
045: /**
046: *
047: */
048: private static final long serialVersionUID = 4634581304927196853L;
049: private String siatActionType = null;
050: private String siatRenderType = null;
051:
052: public void release() {
053: super .release();
054: siatActionType = null;
055: siatRenderType = null;
056: }
057:
058: /**
059: * Returns the render type.
060: * @return
061: */
062: public String getRenderType() {
063: return siatRenderType;
064: }
065:
066: /**
067: * Sets the render type.
068: * @param name
069: */
070: public void setRenderType(String name) {
071: siatRenderType = name;
072: }
073:
074: /**
075: * Returns the action type.
076: * @return
077: */
078: public String getActionType() {
079: return siatActionType;
080: }
081:
082: /**
083: * Sets the action type.
084: * @param name
085: */
086: public void setActionType(String name) {
087: siatActionType = name;
088: }
089:
090: public int doStartTag() throws JspException {
091:
092: try {
093: RenderRequest request = (RenderRequest) pageContext
094: .getRequest().getAttribute("javax.portlet.request");
095: ControllerPortletConfig cPortlet = (ControllerPortletConfig) request
096: .getAttribute(Constants.PORTLET_CONFIG_ATTRIB);
097:
098: if (cPortlet == null) {
099: throw new JspException(
100: "MVCPortlet tag used without a Controller portlet.");
101: }
102:
103: Condition preCon = null;
104: String actionType = null;
105: if (siatActionType != null) {
106: ActionConfig aConfig = (ActionConfig) cPortlet
107: .getActionConfigMap().get(siatActionType);
108: if (aConfig == null) {
109: throw new JspException("No such action type: "
110: + siatActionType);
111: }
112: preCon = aConfig.getPrecondition();
113: actionType = siatActionType;
114: } else if (siatRenderType != null) {
115: RenderConfig rConfig = (RenderConfig) cPortlet
116: .getRenderConfigMap().get(siatRenderType);
117: if (rConfig == null) {
118: throw new JspException("No such render type: "
119: + siatRenderType);
120: }
121: preCon = rConfig.getPrecondition();
122: actionType = siatRenderType;
123:
124: } else {
125: throw new JspException(
126: "Either actiontype or rendertype must be specified.");
127: }
128:
129: if (preCon == null)
130: return EVAL_BODY_INCLUDE;
131:
132: if (preCon.isSatisfied(request, actionType)) {
133: return EVAL_BODY_INCLUDE;
134: } else {
135: return SKIP_BODY;
136: }
137:
138: } catch (Exception ex) {
139: throw new JspException(ex);
140: }
141: }
142:
143: public int doEndTag() throws JspException {
144: return EVAL_PAGE;
145: }
146:
147: }
|