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.portlet.result;
23
24 import java.io.IOException;
25 import java.util.Map;
26 import java.util.StringTokenizer;
27
28 import javax.portlet.ActionResponse;
29 import javax.portlet.PortletContext;
30 import javax.portlet.PortletException;
31 import javax.portlet.PortletMode;
32 import javax.portlet.PortletRequestDispatcher;
33 import javax.portlet.RenderRequest;
34 import javax.portlet.RenderResponse;
35 import javax.servlet.ServletContext;
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39
40 import org.apache.struts2.ServletActionContext;
41 import org.apache.struts2.dispatcher.StrutsResultSupport;
42 import org.apache.struts2.portlet.PortletActionConstants;
43 import org.apache.struts2.portlet.context.PortletActionContext;
44 import org.apache.commons.lang.xwork.StringUtils;
45
46 import com.opensymphony.xwork2.ActionInvocation;
47 import com.opensymphony.xwork2.inject.Inject;
48 import com.opensymphony.xwork2.util.logging.Logger;
49 import com.opensymphony.xwork2.util.logging.LoggerFactory;
50
51 /***
52 * Result type that includes a JSP to render.
53 *
54 */
55 public class PortletResult extends StrutsResultSupport implements PortletActionConstants {
56
57 private static final long serialVersionUID = 434251393926178567L;
58
59 private boolean useDispatcherServlet;
60
61 private String dispatcherServletName = DEFAULT_DISPATCHER_SERVLET_NAME;
62
63 /***
64 * Logger instance.
65 */
66 private static final Logger LOG = LoggerFactory.getLogger(PortletResult.class);
67
68 private String contentType = "text/html";
69
70 private String title;
71
72 protected PortletMode portletMode;
73
74 public PortletResult() {
75 super();
76 }
77
78 public PortletResult(String location) {
79 super(location);
80 }
81
82 /***
83 * Execute the result. Obtains the
84 * {@link javax.portlet.PortletRequestDispatcher}from the
85 * {@link PortletActionContext}and includes the JSP.
86 *
87 * @see com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation)
88 */
89 public void doExecute(String finalLocation, ActionInvocation actionInvocation) throws Exception {
90
91 if (PortletActionContext.isRender()) {
92 executeRenderResult(finalLocation);
93 } else if (PortletActionContext.isEvent()) {
94 executeActionResult(finalLocation, actionInvocation);
95 } else {
96 executeRegularServletResult(finalLocation, actionInvocation);
97 }
98 }
99
100 /***
101 * Executes the regular servlet result.
102 *
103 * @param finalLocation
104 * @param actionInvocation
105 */
106 private void executeRegularServletResult(String finalLocation, ActionInvocation actionInvocation)
107 throws ServletException, IOException {
108 ServletContext ctx = ServletActionContext.getServletContext();
109 HttpServletRequest req = ServletActionContext.getRequest();
110 HttpServletResponse res = ServletActionContext.getResponse();
111 try {
112 ctx.getRequestDispatcher(finalLocation).include(req, res);
113 } catch (ServletException e) {
114 LOG.error("ServletException including " + finalLocation, e);
115 throw e;
116 } catch (IOException e) {
117 LOG.error("IOException while including result '" + finalLocation + "'", e);
118 throw e;
119 }
120 }
121
122 /***
123 * Executes the action result.
124 *
125 * @param finalLocation
126 * @param invocation
127 */
128 protected void executeActionResult(String finalLocation, ActionInvocation invocation) throws Exception {
129 LOG.debug("Executing result in Event phase");
130 ActionResponse res = PortletActionContext.getActionResponse();
131 Map sessionMap = invocation.getInvocationContext().getSession();
132 LOG.debug("Setting event render parameter: " + finalLocation);
133 if (finalLocation.indexOf('?') != -1) {
134 convertQueryParamsToRenderParams(res, finalLocation.substring(finalLocation.indexOf('?') + 1));
135 finalLocation = finalLocation.substring(0, finalLocation.indexOf('?'));
136 }
137 if (finalLocation.endsWith(".action")) {
138
139 finalLocation = finalLocation.substring(0, finalLocation.lastIndexOf("."));
140 res.setRenderParameter(ACTION_PARAM, finalLocation);
141 } else {
142
143 String namespace = invocation.getProxy().getNamespace();
144 if ( namespace != null && namespace.length() > 0 && !namespace.endsWith("/")) {
145 namespace += "/";
146
147 }
148 res.setRenderParameter(ACTION_PARAM, namespace + "renderDirect");
149 sessionMap.put(RENDER_DIRECT_LOCATION, finalLocation);
150 }
151 if(portletMode != null) {
152 res.setPortletMode(portletMode);
153 res.setRenderParameter(PortletActionConstants.MODE_PARAM, portletMode.toString());
154 }
155 else {
156 res.setRenderParameter(PortletActionConstants.MODE_PARAM, PortletActionContext.getRequest().getPortletMode()
157 .toString());
158 }
159 }
160
161 /***
162 * Converts the query params to render params.
163 *
164 * @param response
165 * @param queryParams
166 */
167 protected static void convertQueryParamsToRenderParams(ActionResponse response, String queryParams) {
168 StringTokenizer tok = new StringTokenizer(queryParams, "&");
169 while (tok.hasMoreTokens()) {
170 String token = tok.nextToken();
171 String key = token.substring(0, token.indexOf('='));
172 String value = token.substring(token.indexOf('=') + 1);
173 response.setRenderParameter(key, value);
174 }
175 }
176
177 /***
178 * Executes the render result.
179 *
180 * @param finalLocation
181 * @throws PortletException
182 * @throws IOException
183 */
184 protected void executeRenderResult(final String finalLocation) throws PortletException, IOException {
185 LOG.debug("Executing result in Render phase");
186 PortletContext ctx = PortletActionContext.getPortletContext();
187 RenderRequest req = PortletActionContext.getRenderRequest();
188 RenderResponse res = PortletActionContext.getRenderResponse();
189 res.setContentType(contentType);
190 if (StringUtils.isNotEmpty(title)) {
191 res.setTitle(title);
192 }
193 LOG.debug("Location: " + finalLocation);
194 if (useDispatcherServlet) {
195 req.setAttribute(DISPATCH_TO, finalLocation);
196 PortletRequestDispatcher dispatcher = ctx.getNamedDispatcher(dispatcherServletName);
197 if(dispatcher == null) {
198 throw new PortletException("Could not locate dispatcher servlet \"" + dispatcherServletName + "\". Please configure it in your web.xml file");
199 }
200 dispatcher.include(req, res);
201 } else {
202 PortletRequestDispatcher dispatcher = ctx.getRequestDispatcher(finalLocation);
203 if (dispatcher == null) {
204 throw new PortletException("Could not locate dispatcher for '" + finalLocation + "'");
205 }
206 dispatcher.include(req, res);
207 }
208 }
209
210 /***
211 * Sets the content type.
212 *
213 * @param contentType
214 * The content type to set.
215 */
216 public void setContentType(String contentType) {
217 this.contentType = contentType;
218 }
219
220 /***
221 * Sets the title.
222 *
223 * @param title
224 * The title to set.
225 */
226 public void setTitle(String title) {
227 this.title = title;
228 }
229
230 public void setPortletMode(String portletMode) {
231 if(portletMode != null) {
232 this.portletMode = new PortletMode(portletMode);
233 }
234 }
235
236 @Inject("struts.portlet.useDispatcherServlet")
237 public void setUseDispatcherServlet(String useDispatcherServlet) {
238 this.useDispatcherServlet = "true".equalsIgnoreCase(useDispatcherServlet);
239 }
240
241 @Inject("struts.portlet.dispatcherServletName")
242 public void setDispatcherServletName(String dispatcherServletName) {
243 this.dispatcherServletName = dispatcherServletName;
244 }
245 }