1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.components;
23
24 import java.io.IOException;
25 import java.io.Writer;
26
27 import org.apache.struts2.dispatcher.mapper.ActionMapper;
28 import org.apache.struts2.StrutsException;
29 import org.apache.struts2.portlet.context.PortletActionContext;
30 import org.apache.struts2.portlet.util.PortletUrlHelper;
31 import org.apache.commons.lang.xwork.StringUtils;
32
33 import com.opensymphony.xwork2.ActionContext;
34 import com.opensymphony.xwork2.ActionInvocation;
35 import com.opensymphony.xwork2.inject.Inject;
36
37 /***
38 * Implementation of the {@link UrlRenderer} interface that renders URLs for portlet environments.
39 *
40 * @see UrlRenderer
41 *
42 */
43 public class PortletUrlRenderer implements UrlRenderer {
44
45 /***
46 * The servlet renderer used when not executing in a portlet context.
47 */
48 private UrlRenderer servletRenderer = null;
49
50 public PortletUrlRenderer() {
51 this.servletRenderer = new ServletUrlRenderer();
52 }
53
54 @Inject
55 public void setActionMapper(ActionMapper actionMapper) {
56 servletRenderer.setActionMapper(actionMapper);
57 }
58
59 /***
60 * {@inheritDoc}
61 */
62 public void renderUrl(Writer writer, UrlProvider urlComponent) {
63 if(PortletActionContext.getPortletContext() == null || "none".equalsIgnoreCase(urlComponent.getPortletUrlType())) {
64 servletRenderer.renderUrl(writer, urlComponent);
65 }
66 else {
67 String action = null;
68 if(urlComponent.getAction() != null) {
69 action = urlComponent.findString(urlComponent.getAction());
70 }
71 String scheme = urlComponent.getHttpServletRequest().getScheme();
72
73 if (urlComponent.getScheme() != null) {
74 scheme = urlComponent.getScheme();
75 }
76
77 String result;
78 urlComponent.setNamespace(urlComponent.determineNamespace(urlComponent.getNamespace(), urlComponent.getStack(), urlComponent.getHttpServletRequest()));
79 if (onlyActionSpecified(urlComponent)) {
80 result = PortletUrlHelper.buildUrl(action, urlComponent.getNamespace(), urlComponent.getMethod(), urlComponent.getParameters(), urlComponent.getPortletUrlType(),
81 urlComponent.getPortletMode(), urlComponent.getWindowState());
82 } else if(onlyValueSpecified(urlComponent)){
83 result = PortletUrlHelper.buildResourceUrl(urlComponent.getValue(), urlComponent.getParameters());
84 }
85 else {
86 result = createDefaultUrl(urlComponent);
87 }
88 String anchor = urlComponent.getAnchor();
89 if (StringUtils.isNotEmpty(anchor)) {
90 result += '#' + urlComponent.findString(anchor);
91 }
92
93 String var = urlComponent.getVar();
94
95 if (var != null) {
96 urlComponent.putInContext(result);
97
98
99 urlComponent.getHttpServletRequest().setAttribute(var, result);
100 } else {
101 try {
102 writer.write(result);
103 } catch (IOException e) {
104 throw new StrutsException("IOError: " + e.getMessage(), e);
105 }
106 }
107 }
108 }
109
110 private String createDefaultUrl(UrlProvider urlComponent) {
111 String result;
112 ActionInvocation ai = (ActionInvocation)urlComponent.getStack().getContext().get(
113 ActionContext.ACTION_INVOCATION);
114 String action = ai.getProxy().getActionName();
115 result = PortletUrlHelper.buildUrl(action, urlComponent.getNamespace(), urlComponent.getMethod(), urlComponent.getParameters(),
116 urlComponent.getPortletUrlType(), urlComponent.getPortletMode(), urlComponent.getWindowState());
117 return result;
118 }
119
120 private boolean onlyValueSpecified(UrlProvider urlComponent) {
121 return urlComponent.getValue() != null && urlComponent.getAction() == null;
122 }
123
124 private boolean onlyActionSpecified(UrlProvider urlComponent) {
125 return urlComponent.getValue() == null && urlComponent.getAction() != null;
126 }
127
128 /***
129 * {@inheritDoc}
130 */
131 public void renderFormUrl(Form formComponent) {
132 if(PortletActionContext.getPortletContext() == null) {
133 servletRenderer.renderFormUrl(formComponent);
134 }
135 else {
136 String namespace = formComponent.determineNamespace(formComponent.namespace, formComponent.getStack(),
137 formComponent.request);
138 String action = null;
139 if (formComponent.action != null) {
140 action = formComponent.findString(formComponent.action);
141 }
142 else {
143 ActionInvocation ai = (ActionInvocation) formComponent.getStack().getContext().get(ActionContext.ACTION_INVOCATION);
144 action = ai.getProxy().getActionName();
145 }
146 String type = "action";
147 if (StringUtils.isNotEmpty(formComponent.method)) {
148 if ("GET".equalsIgnoreCase(formComponent.method.trim())) {
149 type = "render";
150 }
151 }
152 if (action != null) {
153 String result = PortletUrlHelper.buildUrl(action, namespace, null,
154 formComponent.getParameters(), type, formComponent.portletMode, formComponent.windowState);
155 formComponent.addParameter("action", result);
156
157
158
159
160 String id = formComponent.getId();
161 if (id == null) {
162 int slash = action.lastIndexOf('/');
163 int dot = action.indexOf('.', slash);
164 if (dot != -1) {
165 id = action.substring(slash + 1, dot);
166 } else {
167 id = action.substring(slash + 1);
168 }
169 formComponent.addParameter("id", formComponent.escape(id));
170 }
171 }
172 }
173
174 }
175
176 public void beforeRenderUrl(UrlProvider urlComponent) {
177 if(PortletActionContext.getPortletContext() == null) {
178 servletRenderer.beforeRenderUrl(urlComponent);
179 }
180 }
181
182 public void setServletRenderer(UrlRenderer nonPortletRenderer) {
183 this.servletRenderer = nonPortletRenderer;
184
185 }
186
187 }