1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.portals.bridges.struts.taglib;
17
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.jsp.PageContext;
20
21 import org.apache.portals.bridges.struts.PortletServlet;
22 import org.apache.portals.bridges.struts.StrutsPortlet;
23 import org.apache.portals.bridges.struts.StrutsPortletURL;
24 import org.apache.portals.bridges.struts.config.StrutsPortletConfig;
25 import org.apache.portals.bridges.struts.config.PortletURLTypes;
26
27 /***
28 * Utility class providing common Struts Bridge Tags functionality.
29 *
30 * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
31 * @version $Id: TagsSupport.java 306958 2005-10-07 01:23:37 +0200 (Fri, 07 Oct 2005) ate $
32 */
33 class TagsSupport
34 {
35 /***
36 * Private constructor as this class isn't supposed to be instantiated.
37 *
38 */
39 private TagsSupport(){}
40
41 /***
42 * Resolves a, possibly relative, url to a context relative one for use within a Portlet context.
43 * <p>
44 * The url parameter may contain relative (../) elements.
45 * </p>
46 * @param pageContext the JSP pageContext
47 * @param url the url to resolve
48 * @return the resolved url
49 */
50 public static String getContextRelativeURL(PageContext pageContext, String url, boolean addContextPath)
51 {
52 if ( !url.startsWith("/"))
53 {
54 String newUrl = url;
55 String currentPath = (String)pageContext.getRequest().getAttribute(StrutsPortlet.PAGE_URL);
56 if ( addContextPath )
57 {
58 currentPath = ((HttpServletRequest)pageContext.getRequest()).getContextPath() + currentPath;
59 }
60 if ( addContextPath || currentPath.length() > 1
61 {
62 currentPath = currentPath.substring(0,currentPath.lastIndexOf('/'));
63 }
64 if ( currentPath.length() == 0 )
65 {
66 currentPath = "/";
67 }
68 while ( currentPath.length() > 0 )
69 {
70 if ( newUrl.startsWith("../"))
71 {
72 currentPath = currentPath.substring(0, currentPath.lastIndexOf('/'));
73 newUrl = newUrl.substring(3);
74 }
75 else
76 {
77 break;
78 }
79 }
80 if ( currentPath.length() > 1 )
81 {
82 url = currentPath + "/" + newUrl;
83 }
84 else
85 {
86 url = "/" + newUrl;
87 }
88 }
89 return url;
90 }
91
92 /***
93 * Creates an action or render PortletURL, or a ResourceURL.
94 * <p>
95 * The url parameter is first {@link #getContextRelativeURL(PageContext, String) resolved}
96 * to an context relative url.<br/>
97 * Then, a prefixed contextPath is removed from the resulting url.<br/>
98 * If the type parameter is specified (not null), the type of url created is based on its value.<br/>
99 * Otherwise, {@link PortletURLTypes#getType(String)} is used to determine which
100 * type of url must be created.
101 * </p>
102 * @param pageContext the JSP pageContext
103 * @param url the url to resolve
104 * @param type indicated which type of url must be created
105 * @return an action or render PortletURL, or a ResourceURL
106 */
107 public static String getURL(PageContext pageContext, String url, PortletURLTypes.URLType type)
108 {
109 url = getContextRelativeURL(pageContext,url,false);
110 String contextPath = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
111 if (url.startsWith(contextPath + "/"))
112 {
113 url = url.substring(contextPath.length());
114 }
115
116 if ( type == null )
117 {
118 StrutsPortletConfig strutsPortletConfig = (StrutsPortletConfig)pageContext.getAttribute(StrutsPortlet.STRUTS_PORTLET_CONFIG,PageContext.APPLICATION_SCOPE);
119 type = strutsPortletConfig.getPortletURLTypes().getType(url);
120 }
121
122 if ( type.equals(PortletURLTypes.URLType.ACTION) )
123 {
124 return StrutsPortletURL.createActionURL(pageContext.getRequest(),url).toString();
125 }
126 else if ( type.equals(PortletURLTypes.URLType.RENDER) )
127 {
128 return StrutsPortletURL.createRenderURL(pageContext.getRequest(),url).toString();
129 }
130 else
131 {
132 if ( url.startsWith("/"))
133 {
134 return contextPath + url;
135 }
136 return contextPath + "/" + url;
137 }
138 }
139
140 /***
141 * Replaces the action url as generated by the struts:form tag with an action PortletURL.
142 * @param pageContext the JSP pageContext
143 * @param formStartElement the formStartElement as generated by the struts:form tag
144 * @return the formStartElement containing an action PortletURL
145 */
146 public static String getFormTagRenderFormStartElement(PageContext pageContext, String formStartElement)
147 {
148 if ( PortletServlet.isPortletRequest(pageContext.getRequest()))
149 {
150 int actionURLStart = formStartElement.indexOf("action=") + 8;
151 int actionURLEnd = formStartElement.indexOf('"', actionURLStart);
152 String actionURL = formStartElement.substring(actionURLStart,
153 actionURLEnd);
154 formStartElement = formStartElement.substring(0, actionURLStart)
155 + StrutsPortletURL.createActionURL(pageContext.getRequest(),
156 actionURL).toString()
157 + formStartElement.substring(actionURLEnd);
158 }
159 return formStartElement;
160 }
161 }