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