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  
26  /***
27   * Supports the Struts html:rewrite tag to be used within a Portlet context.
28   * 
29   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
30   * @version $Id: RewriteTag.java 188237 2005-01-21 17:28:13 +0100 (Fri, 21 Jan 2005) ate $
31   */
32  public class RewriteTag extends org.apache.struts.taglib.html.RewriteTag 
33  {
34      /***
35       * Indicates which type of a url must be generated: action, render or resource.
36       * <p>If not specified, the type will be determined by
37       * {@link PortletURLTypes#getType(String)}</p>.
38       */
39      protected PortletURLTypes.URLType urlType = null;
40          
41      /***
42       * @return "true" if an ActionURL must be rendered
43       */
44      public String getActionURL()
45      {
46          return urlType != null && urlType.equals(PortletURLTypes.URLType.ACTION) ? "true" : "false";
47      }
48  
49      /***
50       * Render an ActionURL when set to "true"
51       * @param value "true" renders an ActionURL
52       */
53      public void setActionURL(String value)
54      {
55          this.urlType = value != null && value.equalsIgnoreCase("true") ? PortletURLTypes.URLType.ACTION : null; 
56      }
57      
58      public String getRenderURL()
59      {
60          return urlType != null && urlType.equals(PortletURLTypes.URLType.RENDER) ? "true" : "false";
61      }
62          
63      /***
64       * Render a RenderURL when set to "true"
65       * @param value "true" renders a RenderURL
66       */
67      public void setRenderURL(String value)
68      {
69          this.urlType = value != null && value.equalsIgnoreCase("true") ? PortletURLTypes.URLType.RENDER : null; 
70      }
71  
72      public String getResourceURL()
73      {
74          return urlType != null && urlType.equals(PortletURLTypes.URLType.RESOURCE) ? "true" : "false";
75      }
76          
77      /***
78       * Render a ResourceURL when set to "true"
79       * @param value "true" renders a ResourceURL
80       */
81      public void setResourceURL(String value)
82      {
83          this.urlType = value != null && value.equalsIgnoreCase("true") ? PortletURLTypes.URLType.RESOURCE : null; 
84      }
85  
86      /***
87       * Generates a PortletURL or a ResourceURL for the link when in the context of a
88       * {@link PortletServlet#isPortletRequest(ServletRequest) PortletRequest}, otherwise
89       * the default behaviour is maintained.
90       * @return the link url
91       * @exception JspException if a JSP exception has occurred
92       */
93      public int doStartTag() throws JspException
94      {
95          if ( PortletServlet.isPortletRequest(pageContext.getRequest()))
96          {
97              String url = null;
98              BodyContent bodyContent = pageContext.pushBody();
99              try
100             {
101                 super.doStartTag();
102                 url = bodyContent.getString();
103                 
104                 // process embedded anchor
105                 String anchor = null;
106                 int hash = url.indexOf('#');
107                 if ( hash > -1 )
108                 {
109                     // save embedded anchor to be appended later and strip it from the url
110                     anchor = url.substring(hash);
111                     url = url.substring(0,hash);
112                 }
113                 
114                 url = TagsSupport.getURL(pageContext, url, urlType);
115 
116                 if ( anchor != null )
117                 {
118                     url = url + anchor;
119                 }
120             }
121             finally
122             {
123                 pageContext.popBody();
124             }
125             TagUtils.getInstance().write(pageContext, url);
126             return (SKIP_BODY);
127         }
128         else
129         {
130             return super.doStartTag();
131         }
132     }
133 
134     public void release() {
135 
136         super.release();
137         urlType = null;
138     }
139 }