001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.portlet.result;
006:
007: import java.io.IOException;
008: import java.util.StringTokenizer;
009:
010: import javax.portlet.ActionResponse;
011: import javax.portlet.PortletConfig;
012: import javax.portlet.PortletException;
013: import javax.portlet.PortletRequestDispatcher;
014: import javax.portlet.RenderRequest;
015: import javax.portlet.RenderResponse;
016: import javax.servlet.ServletContext;
017: import javax.servlet.ServletException;
018: import javax.servlet.http.HttpServletRequest;
019: import javax.servlet.http.HttpServletResponse;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import com.opensymphony.webwork.ServletActionContext;
026: import com.opensymphony.webwork.dispatcher.WebWorkResultSupport;
027: import com.opensymphony.webwork.portlet.PortletActionConstants;
028: import com.opensymphony.webwork.portlet.context.PortletActionContext;
029: import com.opensymphony.xwork.ActionInvocation;
030:
031: /**
032: * Result type that includes a JSP to render.
033: *
034: * @author Nils-Helge Garli
035: * @author Rainer Hermanns
036: * @version $Revision: 2338 $
037: */
038: public class PortletResult extends WebWorkResultSupport {
039:
040: /**
041: * Logger instance.
042: */
043: private static final Log LOG = LogFactory
044: .getLog(PortletResult.class);
045:
046: private String contentType = "text/html";
047:
048: private String title;
049:
050: /**
051: * Execute the result. Obtains the
052: * {@link javax.portlet.PortletRequestDispatcher}from the
053: * {@link PortletActionContext}and includes the JSP.
054: *
055: * @see com.opensymphony.xwork.Result#execute(com.opensymphony.xwork.ActionInvocation)
056: */
057: public void doExecute(String finalLocation,
058: ActionInvocation actionInvocation) throws Exception {
059:
060: if (PortletActionContext.isRender()) {
061: executeRenderResult(finalLocation);
062: } else if (PortletActionContext.isEvent()) {
063: executeActionResult(finalLocation, actionInvocation);
064: } else {
065: executeRegularServletResult(finalLocation, actionInvocation);
066: }
067: }
068:
069: /**
070: * Executes the regular servlet result.
071: *
072: * @param finalLocation
073: * @param actionInvocation
074: */
075: private void executeRegularServletResult(String finalLocation,
076: ActionInvocation actionInvocation) throws ServletException,
077: IOException {
078: ServletContext ctx = ServletActionContext.getServletContext();
079: HttpServletRequest req = ServletActionContext.getRequest();
080: HttpServletResponse res = ServletActionContext.getResponse();
081: try {
082: ctx.getRequestDispatcher(finalLocation).include(req, res);
083: } catch (ServletException e) {
084: LOG.error("ServletException including " + finalLocation, e);
085: throw e;
086: } catch (IOException e) {
087: LOG.error("IOException while including result '"
088: + finalLocation + "'", e);
089: throw e;
090: }
091: }
092:
093: /**
094: * Executes the action result.
095: *
096: * @param finalLocation
097: * @param invocation
098: */
099: protected void executeActionResult(String finalLocation,
100: ActionInvocation invocation) {
101: LOG.debug("Executing result in Event phase");
102: ActionResponse res = PortletActionContext.getActionResponse();
103: LOG.debug("Setting event render parameter: " + finalLocation);
104: if (finalLocation.indexOf('?') != -1) {
105: convertQueryParamsToRenderParams(res, finalLocation
106: .substring(finalLocation.indexOf('?') + 1));
107: finalLocation = finalLocation.substring(0, finalLocation
108: .indexOf('?'));
109: }
110: if (finalLocation.endsWith(".action")) {
111: // View is rendered with a view action...luckily...
112: finalLocation = finalLocation.substring(0, finalLocation
113: .lastIndexOf("."));
114: res.setRenderParameter(PortletActionConstants.ACTION_PARAM,
115: finalLocation);
116: } else {
117: // View is rendered outside an action...uh oh...
118: res.setRenderParameter(PortletActionConstants.ACTION_PARAM,
119: "renderDirect");
120: res.setRenderParameter("location", finalLocation);
121: }
122: res.setRenderParameter(PortletActionConstants.MODE_PARAM,
123: PortletActionContext.getRequest().getPortletMode()
124: .toString());
125: }
126:
127: /**
128: * Converts the query params to render params.
129: *
130: * @param response
131: * @param queryParams
132: */
133: protected static void convertQueryParamsToRenderParams(
134: ActionResponse response, String queryParams) {
135: StringTokenizer tok = new StringTokenizer(queryParams, "&");
136: while (tok.hasMoreTokens()) {
137: String token = tok.nextToken();
138: String key = token.substring(0, token.indexOf('='));
139: String value = token.substring(token.indexOf('=') + 1);
140: response.setRenderParameter(key, value);
141: }
142: }
143:
144: /**
145: * Executes the render result.
146: *
147: * @param finalLocation
148: * @throws PortletException
149: * @throws IOException
150: */
151: protected void executeRenderResult(final String finalLocation)
152: throws PortletException, IOException {
153: LOG.debug("Executing result in Render phase");
154: PortletConfig cfg = PortletActionContext.getPortletConfig();
155: RenderRequest req = PortletActionContext.getRenderRequest();
156: RenderResponse res = PortletActionContext.getRenderResponse();
157: LOG.debug("PortletConfig: " + cfg);
158: LOG.debug("RenderRequest: " + req);
159: LOG.debug("RenderResponse: " + res);
160: res.setContentType(contentType);
161: if (StringUtils.isNotEmpty(title)) {
162: res.setTitle(title);
163: }
164: LOG.debug("Location: " + finalLocation);
165: PortletRequestDispatcher preparator = cfg.getPortletContext()
166: .getNamedDispatcher("preparator");
167: if (preparator == null) {
168: throw new PortletException(
169: "Cannot look up 'preparator' servlet. Make sure that you"
170: + "have configured it correctly in the web.xml file.");
171: }
172: new IncludeTemplate() {
173: protected void when(PortletException e) {
174: LOG
175: .error(
176: "PortletException while dispatching to 'preparator' servlet",
177: e);
178: }
179:
180: protected void when(IOException e) {
181: LOG
182: .error(
183: "IOException while dispatching to 'preparator' servlet",
184: e);
185: }
186: }.include(preparator, req, res);
187: PortletRequestDispatcher dispatcher = cfg.getPortletContext()
188: .getRequestDispatcher(finalLocation);
189: if (dispatcher == null) {
190: throw new PortletException(
191: "Could not locate dispatcher for '" + finalLocation
192: + "'");
193: }
194: new IncludeTemplate() {
195: protected void when(PortletException e) {
196: LOG.error("PortletException while dispatching to '"
197: + finalLocation + "'");
198: }
199:
200: protected void when(IOException e) {
201: LOG.error("IOException while dispatching to '"
202: + finalLocation + "'");
203: }
204: }.include(dispatcher, req, res);
205: }
206:
207: /**
208: * Sets the content type.
209: *
210: * @param contentType The content type to set.
211: */
212: public void setContentType(String contentType) {
213: this .contentType = contentType;
214: }
215:
216: /**
217: * Sets the title.
218: *
219: * @param title The title to set.
220: */
221: public void setTitle(String title) {
222: this .title = title;
223: }
224:
225: static class IncludeTemplate {
226: protected void include(PortletRequestDispatcher dispatcher,
227: RenderRequest req, RenderResponse res)
228: throws PortletException, IOException {
229: try {
230: dispatcher.include(req, res);
231: } catch (PortletException e) {
232: when(e);
233: throw e;
234: } catch (IOException e) {
235: when(e);
236: throw e;
237: }
238: }
239:
240: protected void when(PortletException e) {
241: }
242:
243: protected void when(IOException e) {
244: }
245: }
246: }
|