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  package org.apache.portals.bridges.struts.taglib;
17  
18  import javax.servlet.ServletRequest; // for javadoc
19  import javax.servlet.jsp.JspException;
20  import javax.servlet.jsp.tagext.BodyContent;
21  
22  import org.apache.portals.bridges.struts.PortletServlet;
23  import org.apache.portals.bridges.struts.config.PortletURLTypes; // javadoc
24  import org.apache.struts.taglib.TagUtils;
25  import org.apache.strutsel.taglib.utils.EvalHelper;
26  
27  /***
28   * Supports the Struts html-el:rewrite tag to be used within a Portlet context.
29   * 
30   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
31   * @version $Id: ELRewriteTag.java 188237 2005-01-21 17:28:13 +0100 (Fri, 21 Jan 2005) ate $
32   */
33  public class ELRewriteTag extends org.apache.strutsel.taglib.html.ELRewriteTag 
34  {
35      /***
36       * Indicates which type of a url must be generated: action, render or resource.
37       * <p>If not specified, the type will be determined by
38       * {@link PortletURLTypes#getType(String)}</p>.
39       */
40      protected PortletURLTypes.URLType urlType = null;
41          
42      /***
43       * @return "true" if an ActionURL must be rendered
44       */
45      public String getActionURL()
46      {
47          return urlType != null && urlType.equals(PortletURLTypes.URLType.ACTION) ? "true" : "false";
48      }
49  
50      private String actionURL;
51      /***
52       * Render an ActionURL when set to "true"
53       * <p>
54       * Supports jstl expression language.
55       * </p>
56       * @param actionURL when (evaluated to) "true" renders an ActionURL
57       */
58      public void setActionURL(String actionURL)
59      {
60          // delay evaluation of urlType to doStartTag
61          this.actionURL = actionURL;
62      }
63      
64      public String getRenderURL()
65      {
66          return urlType != null && urlType.equals(PortletURLTypes.URLType.RENDER) ? "true" : "false";
67      }
68          
69      private String renderURL;
70      /***
71       * Render a RenderURL when set to "true"
72       * <p>
73       * Supports jstl expression language.
74       * </p>
75       * @param renderURL when (evaluated to) "true" renders a RenderURL
76       */
77      public void setRenderURL(String renderURL)
78      {
79          // delay evaluation of urlType to doStartTag
80          this.renderURL = renderURL;
81      }
82  
83      public String getResourceURL()
84      {
85          return urlType != null && urlType.equals(PortletURLTypes.URLType.RESOURCE) ? "true" : "false";
86      }
87          
88      private String resourceURL;
89      
90      /***
91       * Render a ResourceURL when set to "true"
92       * <p>
93       * Supports jstl expression language.
94       * </p>
95       * @param resourceURL when (evaluated to) "true" renders a ResourceURL
96       */
97      public void setResourceURL(String resourceURL)
98      {
99          // delay evaluation of urlType to doStartTag
100         this.resourceURL = resourceURL;
101     }
102 
103     /***
104      * Generates a PortletURL or a ResourceURL for the link when in the context of a
105      * {@link PortletServlet#isPortletRequest(ServletRequest) PortletRequest}, otherwise
106      * the default behaviour is maintained.
107      * @return the link url
108      * @exception JspException if a JSP exception has occurred
109      */
110     public int doStartTag() throws JspException
111     {
112         evaluateExpressions();
113         
114         if ( PortletServlet.isPortletRequest(pageContext.getRequest()))
115         {
116             String url = null;
117             BodyContent bodyContent = pageContext.pushBody();
118             try
119             {
120                 super.doStartTag();
121                 url = bodyContent.getString();
122                 
123                 // process embedded anchor
124                 String anchor = null;
125                 int hash = url.indexOf('#');
126                 if ( hash > -1 )
127                 {
128                     // save embedded anchor to be appended later and strip it from the url
129                     anchor = url.substring(hash);
130                     url = url.substring(0,hash);
131                 }
132                 
133                 url = TagsSupport.getURL(pageContext, url, urlType);
134 
135                 if ( anchor != null )
136                 {
137                     url = url + anchor;
138                 }
139             }
140             finally
141             {
142                 pageContext.popBody();
143             }
144             TagUtils.getInstance().write(pageContext, url);
145             return (SKIP_BODY);
146         }
147         else
148         {
149             return super.doStartTag();
150         }
151     }
152     
153     /***
154      * Resolve the {@link #actionURL}, {@link #renderURL} and {@link #resourceURL} attributes
155      * using the Struts JSTL expression evaluation engine ({@link EvalHelper}).
156      * @exception JspException if a JSP exception has occurred
157      */
158     private void evaluateExpressions() throws JspException {
159         Boolean value;
160 
161         value = EvalHelper.evalBoolean("actionURL", actionURL,this, pageContext);
162         if ( value != null && value.booleanValue() )
163         {
164             urlType = PortletURLTypes.URLType.ACTION;
165         }
166         if ( urlType == null )
167         {
168             value = EvalHelper.evalBoolean("renderURL", renderURL,this, pageContext);
169             if ( value != null && value.booleanValue() )
170             {
171                 urlType = PortletURLTypes.URLType.RENDER;
172             }            
173         }
174         if ( urlType == null )
175         {
176             value = EvalHelper.evalBoolean("resourceURL", resourceURL,this, pageContext);
177             if ( value != null && value.booleanValue() )
178             {
179                 urlType = PortletURLTypes.URLType.RESOURCE;
180             }            
181         }
182     }
183     
184     public void release() {
185 
186         super.release();
187         urlType = null;
188         actionURL = null;
189         renderURL = null;
190         resourceURL = null;
191     }
192 }