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.StrutsException;
28 import org.apache.struts2.portlet.context.PortletActionContext;
29 import org.apache.struts2.portlet.util.PortletUrlHelper;
30
31 import com.opensymphony.xwork2.ActionContext;
32 import com.opensymphony.xwork2.ActionInvocation;
33 import com.opensymphony.xwork2.util.TextUtils;
34
35 /***
36 * Implementation of the {@link UrlRenderer} interface that renders URLs for portlet environments.
37 *
38 * @see UrlRenderer
39 *
40 */
41 public class PortletUrlRenderer implements UrlRenderer {
42
43 /***
44 * The servlet renderer used when not executing in a portlet context.
45 */
46 private UrlRenderer servletRenderer = null;
47
48 public PortletUrlRenderer() {
49 this.servletRenderer = new ServletUrlRenderer();
50 }
51
52 /***
53 * {@inheritDoc}
54 */
55 public void renderUrl(Writer writer, URL urlComponent) {
56 if(PortletActionContext.getPortletContext() == null || "none".equalsIgnoreCase(urlComponent.portletUrlType)) {
57 servletRenderer.renderUrl(writer, urlComponent);
58 }
59 else {
60 String action = null;
61 if(urlComponent.action != null) {
62 action = urlComponent.findString(urlComponent.action);
63 }
64 String scheme = urlComponent.req.getScheme();
65
66 if (urlComponent.scheme != null) {
67 scheme = urlComponent.scheme;
68 }
69
70 String result;
71 urlComponent.namespace = urlComponent.determineNamespace(urlComponent.namespace, urlComponent.stack, urlComponent.req);
72 if (onlyActionSpecified(urlComponent)) {
73 result = PortletUrlHelper.buildUrl(action, urlComponent.namespace, urlComponent.method, urlComponent.parameters, urlComponent.portletUrlType, urlComponent.portletMode, urlComponent.windowState);
74 } else if(onlyValueSpecified(urlComponent)){
75 result = PortletUrlHelper.buildResourceUrl(urlComponent.value, urlComponent.parameters);
76 }
77 else {
78 result = createDefaultUrl(urlComponent);
79 }
80 if ( urlComponent.anchor != null && urlComponent.anchor.length() > 0 ) {
81 result += '#' + urlComponent.findString(urlComponent.anchor);
82 }
83
84 String var = urlComponent.getVar();
85
86 if (var != null) {
87 urlComponent.putInContext(result);
88
89
90 urlComponent.req.setAttribute(var, result);
91 } else {
92 try {
93 writer.write(result);
94 } catch (IOException e) {
95 throw new StrutsException("IOError: " + e.getMessage(), e);
96 }
97 }
98 }
99 }
100
101 private String createDefaultUrl(URL urlComponent) {
102 String result;
103 ActionInvocation ai = (ActionInvocation)urlComponent.getStack().getContext().get(
104 ActionContext.ACTION_INVOCATION);
105 String action = ai.getProxy().getActionName();
106 result = PortletUrlHelper.buildUrl(action, urlComponent.namespace, urlComponent.method, urlComponent.parameters, urlComponent.portletUrlType, urlComponent.portletMode, urlComponent.windowState);
107 return result;
108 }
109
110 private boolean onlyValueSpecified(URL urlComponent) {
111 return urlComponent.value != null && urlComponent.action == null;
112 }
113
114 private boolean onlyActionSpecified(URL urlComponent) {
115 return urlComponent.value == null && urlComponent.action != null;
116 }
117
118 /***
119 * {@inheritDoc}
120 */
121 public void renderFormUrl(Form formComponent) {
122 if(PortletActionContext.getPortletContext() == null) {
123 servletRenderer.renderFormUrl(formComponent);
124 }
125 else {
126 String namespace = formComponent.determineNamespace(formComponent.namespace, formComponent.getStack(),
127 formComponent.request);
128 String action = null;
129 if (formComponent.action != null) {
130 action = formComponent.findString(formComponent.action);
131 }
132 else {
133 ActionInvocation ai = (ActionInvocation) formComponent.getStack().getContext().get(ActionContext.ACTION_INVOCATION);
134 action = ai.getProxy().getActionName();
135 }
136 String type = "action";
137 if (TextUtils.stringSet(formComponent.method)) {
138 if ("GET".equalsIgnoreCase(formComponent.method.trim())) {
139 type = "render";
140 }
141 }
142 if (action != null) {
143 String result = PortletUrlHelper.buildUrl(action, namespace, null,
144 formComponent.getParameters(), type, formComponent.portletMode, formComponent.windowState);
145 formComponent.addParameter("action", result);
146
147
148
149
150 String id = formComponent.getId();
151 if (id == null) {
152 int slash = action.lastIndexOf('/');
153 int dot = action.indexOf('.', slash);
154 if (dot != -1) {
155 id = action.substring(slash + 1, dot);
156 } else {
157 id = action.substring(slash + 1);
158 }
159 formComponent.addParameter("id", formComponent.escape(id));
160 }
161 }
162 }
163
164 }
165
166 public void beforeRenderUrl(URL urlComponent) {
167 if(PortletActionContext.getPortletContext() == null) {
168 servletRenderer.beforeRenderUrl(urlComponent);
169 }
170 }
171
172 public void setServletRenderer(UrlRenderer nonPortletRenderer) {
173 this.servletRenderer = nonPortletRenderer;
174
175 }
176
177 }