1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.portals.bridges.struts.taglib;
18
19 import javax.servlet.ServletRequest;
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;
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
106 String anchor = null;
107 int hash = url.indexOf('#');
108 if ( hash > -1 )
109 {
110
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 }