View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.services.jsp.tags;
18  
19  // java classes
20  import java.util.Hashtable;
21  import java.util.StringTokenizer;
22  
23  // jsp api
24  import javax.servlet.jsp.PageContext;
25  import javax.servlet.jsp.JspException;
26  import javax.servlet.jsp.tagext.BodyTagSupport;
27  
28  // Turbine Classes 
29  import org.apache.turbine.util.RunData;
30  import org.apache.turbine.services.jsp.JspService;
31  
32  // jetspeed
33  import org.apache.jetspeed.modules.ParameterLoader;
34  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
35  import org.apache.jetspeed.services.logging.JetspeedLogger;
36  import org.apache.jetspeed.services.security.PortalResource;
37  import org.apache.jetspeed.om.registry.Parameter;
38  import org.apache.jetspeed.om.registry.PortletEntry;
39  import org.apache.jetspeed.services.JetspeedSecurity;
40  import org.apache.jetspeed.om.security.JetspeedUser;
41  import org.apache.jetspeed.services.Registry;
42  
43  /***
44   * Supporting class for the parameter style tag.
45   * Sends a parameter rendered using specific style to the output stream.
46   * 
47   * The following tag attributes are supported:
48   * 
49   * <UL>
50   * <LI><code>name</code>: parameter name (required).</li>
51   * <LI><code>style</code>: parameter style name (required)</LI>
52   * <LI><code>value</code>: parameter current value</LI>
53   * <LI><code>portlet</code>: portlet name to check security against</LI>
54   * </UL>
55   * <p>Note: tag body may also contain parameter style options in format: option1=value1;optionN=valueN. Check
56   * documentation for individual parameter style to see what options are supported</p>
57   * <p>Note: Use care when specifying style options in the tag body - the body is not cleansed to remove
58   * embedded carriage returns and tabs.</p>
59   * Examples:
60   * <UL>
61   * <LI><code>&lt;jetspeed:parameterStyle name="portlet-list" style="RegistryEntryListBox"/&gt;</CODE>
62   * <LI><code>&lt;jetspeed:parameterStyle name="skin-list" style="RegistryEntryListBox"&gt;registry=Skin&lt;/jetspeed:parameterStyle/&gt;</CODE>
63   * <LI><code>&lt;jetspeed:parameterStyle name="control-list" style="RegistryEntryListBox" value="TabControl"&gt;registry=PortletControl&lt;/jetspeed:parameterStyle/&gt;</CODE>
64   * </UL>
65   * 
66   * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>
67   * @version $Id: JetspeedParameterStyleTag.java,v 1.4 2004/02/23 03:59:40 jford Exp $
68   */
69  public class JetspeedParameterStyleTag extends BodyTagSupport
70  {
71      /***
72       * Static initialization of the logger for this class
73       */    
74      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedParameterStyleTag.class.getName());
75      
76      /***
77       * name parameter defines parameter name
78       */
79      private String name = null;
80  
81      /***
82       * name parameter defines parameter style
83       */
84      private String style = null;
85  
86      /***
87       * name parameter defines current parameter style value
88       */
89      private String value = null;
90  
91      /***
92       * name parameter defines portlet name to check security against
93       */
94      private String portlet = null;
95  
96      /***
97       * The setter for name parameter
98       * 
99       * @param value
100      */
101     public void setName(String value)
102     {
103         this.name = value;
104     }
105 
106     /***
107      * The setter for value parameter
108      * 
109      * @param value
110      */
111     public void setValue(String value)
112     {
113         this.value = value;
114     }
115 
116     /***
117      * The setter for syle parameter
118      * 
119      * @param value
120      */
121     public void setStyle(String value)
122     {
123         this.style = value;
124     }
125 
126     /***
127      * The setter for value parameter
128      * 
129      * @param value
130      */
131     public void setPortlet(String value)
132     {
133         this.portlet = value;
134     }
135 
136     /***
137      * 
138      * @return code
139      * @exception JspException
140      */
141     public int doStartTag() throws JspException 
142     {
143         return EVAL_BODY_TAG;
144     }
145 
146     /***
147      * 
148      * @return code
149      * @exception JspException
150      */
151     public int doEndTag() throws JspException 
152     {
153         RunData data = (RunData) pageContext.getAttribute(JspService.RUNDATA, PageContext.REQUEST_SCOPE);    
154         String result = null;
155 
156         try
157         {
158 
159             // See if body contains any parameter options
160             String body = this.getBodyContent() == null ? null : this.getBodyContent().getString();
161             Hashtable options = new Hashtable();
162 
163             if (body != null && !body.trim().equalsIgnoreCase(""))
164             {
165                 StringTokenizer st = new StringTokenizer(body, ";");
166                 String prefix = this.name + ".style.";
167                 while (st.hasMoreTokens())
168                 {
169                     StringTokenizer pair = new StringTokenizer(st.nextToken(), "=");
170                     if (pair.countTokens() == 2)
171                     {
172                         options.put(prefix + pair.nextToken().trim(), pair.nextToken().trim());
173                     }
174                 }
175             }
176 
177             boolean canAccess = true;
178 
179             // If portlet name is specified, it will be used to check security for the parameter
180             if (this.portlet != null)
181             {
182                 // Retrieve registry entry and its parameter
183                 PortletEntry entry = (PortletEntry) Registry.getEntry(Registry.PORTLET, this.portlet);
184                 Parameter param = entry.getParameter(this.name);
185 
186                 // Verify security for the parameter
187                 canAccess = JetspeedSecurity.checkPermission((JetspeedUser) data.getUser(), 
188                                                              new PortalResource(entry, param), 
189                                                              JetspeedSecurity.PERMISSION_CUSTOMIZE);
190             }
191 
192             if (canAccess)
193             {
194                 result = ParameterLoader.getInstance().eval(data, 
195                                                             this.style, 
196                                                             this.name, 
197                                                             this.value, 
198                                                             options);                
199             }
200 
201             pageContext.getOut().print(result);
202 
203 
204         }
205         catch (Exception e)
206         {
207             result = "<input type=\"text\" name=\"" + this.name + "\" value=\"" + this.value + "\"";
208 
209             String message = "Error processing portlet (PortletTag): [" + name + "]";
210             logger.error(message, e);
211             try
212             {
213                 pageContext.getOut().print(result);
214             }
215             catch (java.io.IOException ioe)
216             {
217             }
218         }
219 
220         return EVAL_PAGE;
221     }
222 
223 }