001: /*
002: * (C) Copyright 2000 - 2006 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portal.processor;
020:
021: import java.io.IOException;
022:
023: import javax.portlet.PortletException;
024: import javax.portlet.PortletMode;
025: import javax.portlet.PortletModeException;
026: import javax.portlet.PortletSecurityException;
027: import javax.portlet.WindowState;
028: import javax.portlet.WindowStateException;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032: import javax.servlet.http.HttpSession;
033:
034: import com.nabhinc.portal.container.ActionRequestImpl;
035: import com.nabhinc.portal.container.ActionResponseImpl;
036: import com.nabhinc.portal.core.NavigationState;
037: import com.nabhinc.portal.core.PortalConstants;
038: import com.nabhinc.portal.core.PortalUtil;
039: import com.nabhinc.portal.core.SessionCache;
040: import com.nabhinc.portal.model.PortalApplicationView;
041: import com.nabhinc.portal.model.PortalPageState;
042: import com.nabhinc.portal.model.PortletWindow;
043:
044: /**
045: *
046: *
047: * @author Padmanabh Dabke
048: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
049: */
050: public class ProcessActionProcessor extends BasePortalActionProcessor {
051: public void process(HttpServletRequest request,
052: HttpServletResponse response, SessionCache sCache,
053: PortalApplicationView portalAppView,
054: PortalPageState pageState, int startIndex,
055: String[] portalParams, String displayMode,
056: String targetWindowId, boolean isAJAXRequest)
057: throws ServletException, IOException {
058: if (portalParams.length > startIndex) {
059: targetWindowId = portalParams[startIndex];
060: startIndex++;
061: }
062: if (targetWindowId == null) {
063: throw new ServletException(
064: "Malformed portal URL. Render URL does not specify the portlet window id.");
065: }
066: PortletWindow pWindow = pageState.portalPage
067: .getPortletWindow(targetWindowId);
068: if (pWindow == null) {
069: // response.setStatus(HttpServletResponse.SC_NOT_FOUND);
070: response.sendError(HttpServletResponse.SC_NOT_FOUND);
071: return;
072: }
073: if (PortalUtil.redirectProtectedActions(request, response,
074: pWindow.getName(), isAJAXRequest))
075: return;
076: String globalWindowId = pWindow.getId();
077:
078: // Check if the render URL is changing portlet mode or window state
079: String newPortletMode = null;
080: String newWindowState = null;
081: boolean isDetached = displayMode
082: .equals(PortalConstants.DISPLAY_MODE_DETACHED);
083: HttpSession session = request.getSession();
084:
085: if (portalParams.length > startIndex) {
086: String cmd = portalParams[startIndex];
087: startIndex++;
088: if (cmd.equals(PortalConstants.PORTLET_MODE_TOKEN)) {
089: newPortletMode = portalParams[startIndex];
090: } else if (cmd.equals(PortalConstants.WINDOW_STATE_TOKEN)) {
091: newWindowState = portalParams[startIndex];
092: }
093: startIndex++;
094: }
095: if (portalParams.length > startIndex) {
096: String cmd = portalParams[startIndex];
097: startIndex++;
098: if (cmd.equals(PortalConstants.PORTLET_MODE_TOKEN)) {
099: newPortletMode = portalParams[startIndex];
100: } else if (cmd.equals(PortalConstants.WINDOW_STATE_TOKEN)) {
101: newWindowState = portalParams[startIndex];
102: }
103: startIndex++;
104: }
105:
106: WindowState oldWS = PortalUtil.isWindowMaximized(isDetached,
107: displayMode, globalWindowId, session) ? WindowState.MAXIMIZED
108: : WindowState.NORMAL;
109: WindowState newWS = oldWS;
110: if (newWindowState != null) {
111: if (newWindowState
112: .equals(PortalConstants.WINDOW_STATE_MAXIMIZED)) {
113: newWS = WindowState.MAXIMIZED;
114: } else {
115: newWS = WindowState.NORMAL;
116: }
117: }
118:
119: String errorMsg = createNewNavState(session, globalWindowId,
120: isDetached, newPortletMode, newWS, pWindow, request,
121: response, sCache, displayMode, oldWS, pageState);
122: if (errorMsg == null) {
123: String oldDisplayMode = displayMode;
124: displayMode = PortalUtil.setupWindowState(isDetached,
125: sCache.actionResponse.getWindowState().toString()
126: .toLowerCase(), session, globalWindowId,
127: displayMode);
128: if (!oldDisplayMode.equals(displayMode)) {
129: if (displayMode
130: .equals(PortalConstants.DISPLAY_MODE_NORMAL)) {
131: pageState.setNormalPageMode(session);
132: } else if (displayMode
133: .equals(PortalConstants.DISPLAY_MODE_MAXIMIZED)) {
134: pageState.setMaximizedPageMode(pWindow, session);
135: }
136: }
137:
138: // Check if the action resulted in a redirect request by the portlet
139: String rd = sCache.actionResponse.getRedirect();
140: if (rd == null) {
141: if (sCache.clientInfo.isMobileClient) {
142: ViewProcessor.dispatchRequest(request, response,
143: null, sCache.clientInfo, pWindow);
144: return;
145: }
146:
147: if (isAJAXRequest) {
148: if (oldDisplayMode.equals(displayMode)) {
149: PortalUtil.renderPortletWindow(request,
150: response, pWindow, portalAppView
151: .getPortalApplication(),
152: displayMode, true);
153: } else {
154: PortalUtil.directToURL(request, response,
155: pageState.getPageURL());
156: }
157: } else {
158: // response.sendRedirect(PortalUtil.computeBaseURL(request, pageState.portalPage, displayMode, targetWindowId));
159: if (displayMode
160: .equals(PortalConstants.DISPLAY_MODE_DETACHED)) {
161: response.sendRedirect(PortalUtil
162: .computeDetachedURL(request,
163: pageState.portalPage,
164: targetWindowId));
165: } else {
166: response.sendRedirect(pageState.getPageURL());
167: }
168: }
169: } else {
170: if (isAJAXRequest) {
171: PortalUtil.directToURL(request, response, rd);
172: } else {
173: response.sendRedirect(rd);
174: }
175: }
176: PortalUtil.uncacheContent(session, globalWindowId);
177: } else {
178: if (isAJAXRequest)
179: PortalUtil.writeAJAXErrorResponse(errorMsg, response);
180: else {
181: session.setAttribute(
182: PortalConstants.PORTAL_ERROR_MESSAGE_ATTRIB,
183: errorMsg);
184: if (displayMode
185: .equals(PortalConstants.DISPLAY_MODE_DETACHED)) {
186: response.sendRedirect(PortalUtil
187: .computeDetachedURL(request,
188: pageState.portalPage,
189: targetWindowId));
190: } else {
191: response.sendRedirect(pageState.getPageURL());
192: }
193: }
194: }
195:
196: }
197:
198: @SuppressWarnings("unchecked")
199: private String createNewNavState(HttpSession session,
200: String globalWindowId, boolean isDetached,
201: String newPortletMode, WindowState newWindowState,
202: PortletWindow pWindow, HttpServletRequest request,
203: HttpServletResponse response, SessionCache sCache,
204: String displayMode, WindowState oldWindowState,
205: PortalPageState pageState) throws IOException {
206: NavigationState ns = NavigationState.getNavigationState(
207: session, globalWindowId, isDetached);
208: PortletMode currentPortletMode = PortletMode.VIEW;
209:
210: try {
211: if (newPortletMode != null) {
212: currentPortletMode = new PortletMode(newPortletMode);
213: } else {
214: if (ns != null)
215: currentPortletMode = ns.portletMode;
216: }
217:
218: newWindowState = processAction(request, response, pWindow,
219: sCache, currentPortletMode, newWindowState);
220: if (!newWindowState.equals(oldWindowState)) {
221: if (newWindowState.equals(WindowState.MAXIMIZED))
222: pageState.setMaximizedPageMode(pWindow, session);
223: else
224: pageState.setNormalPageMode(session);
225: }
226: ns = new NavigationState();
227: ns.setPortletMode(sCache.actionRequest.getPortletMode()
228: .toString().toLowerCase());
229: ns.paramMap.putAll(sCache.actionResponse.getParamMap());
230: if (ns.paramMap.size() == 0
231: && ns.portletModeStr.equals("view")) {
232: NavigationState.removeNavigationState(session,
233: globalWindowId, isDetached);
234: } else {
235: NavigationState.setNavigationState(session,
236: globalWindowId, isDetached, ns);
237: }
238: return null;
239: } catch (PortletSecurityException e) {
240: return "You do not have enough priviledges to invoke this action.";
241: } catch (PortletModeException e) {
242: return "Unsupported portlet mode.";
243: } catch (WindowStateException e) {
244: return "Unsupported window state.";
245: } catch (PortletException e) {
246: return e.getMessage();
247: }
248: }
249:
250: protected WindowState processAction(HttpServletRequest request,
251: HttpServletResponse response, PortletWindow pWindow,
252: SessionCache sCache, PortletMode pMode, WindowState wState)
253: throws PortletException, IOException {
254: ActionRequestImpl actionRequest = sCache.actionRequest;
255: ActionResponseImpl actionResponse = sCache.actionResponse;
256: actionRequest.setPortalInterface(pWindow);
257: actionRequest.setHttpServletRequest(request);
258: actionRequest.setParameters(request.getParameterMap());
259: actionRequest.setPortletMode(pMode);
260: actionRequest.setWindowState(wState);
261: actionResponse.setHttpServletResponse(response);
262: request.setAttribute(PortalConstants.PORTLET_REQUEST_ATTRIB,
263: sCache.actionRequest);
264: request.setAttribute(PortalConstants.PORTLET_RESPONSE_ATTRIB,
265: sCache.actionResponse);
266: try {
267: pWindow.processAction(actionRequest, actionResponse,
268: request);
269: } finally {
270: request.setAttribute(
271: PortalConstants.PORTLET_REQUEST_ATTRIB,
272: sCache.renderRequest);
273: request.setAttribute(
274: PortalConstants.PORTLET_RESPONSE_ATTRIB,
275: sCache.renderResponse);
276: }
277: return actionRequest.getWindowState();
278:
279: }
280:
281: }
|