View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.portals.bridges.struts.taglib;
18  
19  import javax.servlet.ServletRequest; // for javadoc
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.tagext.BodyContent;
22  
23  import org.apache.portals.bridges.struts.PortletServlet;
24  import org.apache.portals.bridges.struts.config.PortletURLTypes; // javadoc
25  import org.apache.struts.taglib.TagUtils;
26  
27  /***
28   * Supports the Struts html:rewrite tag to be used within a Portlet context.
29   * 
30   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
31   * @version $Id: RewriteTag.java 517068 2007-03-12 01:44:37Z ate $
32   */
33  public class RewriteTag extends org.apache.struts.taglib.html.RewriteTag 
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      /***
51       * Render an ActionURL when set to "true"
52       * @param value "true" renders an ActionURL
53       */
54      public void setActionURL(String value)
55      {
56          this.urlType = value != null && value.equalsIgnoreCase("true") ? PortletURLTypes.URLType.ACTION : null; 
57      }
58      
59      public String getRenderURL()
60      {
61          return urlType != null && urlType.equals(PortletURLTypes.URLType.RENDER) ? "true" : "false";
62      }
63          
64      /***
65       * Render a RenderURL when set to "true"
66       * @param value "true" renders a RenderURL
67       */
68      public void setRenderURL(String value)
69      {
70          this.urlType = value != null && value.equalsIgnoreCase("true") ? PortletURLTypes.URLType.RENDER : null; 
71      }
72  
73      public String getResourceURL()
74      {
75          return urlType != null && urlType.equals(PortletURLTypes.URLType.RESOURCE) ? "true" : "false";
76      }
77          
78      /***
79       * Render a ResourceURL when set to "true"
80       * @param value "true" renders a ResourceURL
81       */
82      public void setResourceURL(String value)
83      {
84          this.urlType = value != null && value.equalsIgnoreCase("true") ? PortletURLTypes.URLType.RESOURCE : null; 
85      }
86  
87      /***
88       * Generates a PortletURL or a ResourceURL for the link when in the context of a
89       * {@link PortletServlet#isPortletRequest(ServletRequest) PortletRequest}, otherwise
90       * the default behaviour is maintained.
91       * @return the link url
92       * @exception JspException if a JSP exception has occurred
93       */
94      public int doStartTag() throws JspException
95      {
96          if ( PortletServlet.isPortletRequest(pageContext.getRequest()))
97          {
98              String url = null;
99              BodyContent bodyContent = pageContext.pushBody();
100             try
101             {
102                 super.doStartTag();
103                 url = bodyContent.getString();
104                 
105                 // process embedded anchor
106                 String anchor = null;
107                 int hash = url.indexOf('#');
108                 if ( hash > -1 )
109                 {
110                     // save embedded anchor to be appended later and strip it from the url
111                     anchor = url.substring(hash);
112                     url = url.substring(0,hash);
113                 }
114                 
115                 url = TagsSupport.getURL(pageContext, url, urlType);
116 
117                 if ( anchor != null )
118                 {
119                     url = url + anchor;
120                 }
121             }
122             finally
123             {
124                 pageContext.popBody();
125             }
126             TagUtils.getInstance().write(pageContext, url);
127             return (SKIP_BODY);
128         }
129         else
130         {
131             return super.doStartTag();
132         }
133     }
134 
135     public void release() {
136 
137         super.release();
138         urlType = null;
139     }
140 }