001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.controller.portlet;
023:
024: import org.jboss.portal.Mode;
025: import org.jboss.portal.WindowState;
026: import org.jboss.portal.common.invocation.EmptyAttributeResolver;
027: import org.jboss.portal.common.invocation.resolver.MapAttributeResolver;
028: import org.jboss.portal.common.util.MarkupInfo;
029: import org.jboss.portal.core.controller.ControllerCommand;
030: import org.jboss.portal.core.controller.ControllerContext;
031: import org.jboss.portal.core.model.portal.Portal;
032: import org.jboss.portal.core.model.portal.Window;
033: import org.jboss.portal.core.model.portal.command.action.InvokePortletWindowActionCommand;
034: import org.jboss.portal.core.model.portal.command.action.InvokePortletWindowRenderCommand;
035: import org.jboss.portal.portlet.ActionURL;
036: import org.jboss.portal.portlet.PortletParameters;
037: import org.jboss.portal.portlet.PortletURL;
038: import org.jboss.portal.portlet.RenderURL;
039: import org.jboss.portal.portlet.StateString;
040: import org.jboss.portal.portlet.impl.spi.AbstractActionContext;
041: import org.jboss.portal.portlet.impl.spi.AbstractRenderContext;
042: import org.jboss.portal.portlet.invocation.ActionInvocation;
043: import org.jboss.portal.portlet.invocation.PortletInvocation;
044: import org.jboss.portal.portlet.invocation.RenderInvocation;
045: import org.jboss.portal.portlet.spi.ActionContext;
046: import org.jboss.portal.portlet.spi.RenderContext;
047: import org.jboss.portal.server.request.URLContext;
048: import org.jboss.portal.server.request.URLFormat;
049:
050: import javax.servlet.http.HttpServletRequest;
051: import javax.servlet.http.HttpServletResponse;
052:
053: /**
054: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
055: * @version $Revision: 8786 $
056: */
057: public class PortletInvocationFactory {
058:
059: public static RenderInvocation createRender(
060: ControllerContext controllerContext, Mode mode,
061: WindowState windowState, StateString navigationalState,
062: Window window, Portal portal) {
063: PortletContextFactory cf = new PortletContextFactory(
064: controllerContext, portal, window);
065: InternalInvokePortletCommandFactory cpc = new InternalInvokePortletCommandFactory(
066: window, controllerContext);
067: return createRender(controllerContext, mode, windowState,
068: navigationalState, cf, cpc);
069: }
070:
071: public static ActionInvocation createAction(
072: ControllerContext controllerContext, Mode mode,
073: WindowState windowState, StateString navigationalState,
074: StateString interactionState, PortletParameters form,
075: Window window, Portal portal) {
076: PortletContextFactory cf = new PortletContextFactory(
077: controllerContext, portal, window);
078: InternalInvokePortletCommandFactory cpc = new InternalInvokePortletCommandFactory(
079: window, controllerContext);
080: return createAction(controllerContext, mode, windowState,
081: navigationalState, interactionState, form, cf, cpc);
082: }
083:
084: public static ActionInvocation createAction(
085: ControllerContext controllerContext, Mode mode,
086: WindowState windowState, StateString navigationalState,
087: StateString interactionState, PortletParameters form,
088: PortletContextFactory cf, InvokePortletCommandFactory cpc) {
089: MarkupInfo markupInfo = (MarkupInfo) controllerContext
090: .getServerInvocation().getResponse().getContentInfo();
091:
092: //
093: ActionContext ctx = new ControllerActionContext(cpc,
094: controllerContext, mode, windowState,
095: navigationalState, markupInfo, interactionState, form);
096:
097: //
098: ActionInvocation action = new ActionInvocation(ctx);
099:
100: //
101: action.setAttribute(PortletInvocation.INVOCATION_SCOPE,
102: "controller_context", controllerContext);
103:
104: // Contextualize
105: action.setSecurityContext(cf.createSecurityContext());
106: action.setRequestContext(cf.createRequestContext());
107: action.setPortalContext(cf.createPortalContext());
108: action.setWindowContext(cf.createWindowContext());
109: action.setUserContext(cf.createUserContext());
110:
111: //
112: return action;
113: }
114:
115: public static RenderInvocation createRender(
116: ControllerContext controllerContext, Mode mode,
117: WindowState windowState, StateString navigationalState,
118: PortletContextFactory cf, InvokePortletCommandFactory cpc) {
119: MarkupInfo markupInfo = (MarkupInfo) controllerContext
120: .getServerInvocation().getResponse().getContentInfo();
121:
122: //
123: RenderContext ctx = new ControllerRenderContext(cpc,
124: controllerContext, mode, windowState,
125: navigationalState, markupInfo);
126:
127: //
128: RenderInvocation render = new RenderInvocation(ctx);
129:
130: //
131: render.setAttribute(PortletInvocation.INVOCATION_SCOPE,
132: "controller_context", controllerContext);
133:
134: // Contextualize
135: render.setSecurityContext(cf.createSecurityContext());
136: render.setRequestContext(cf.createRequestContext());
137: render.setPortalContext(cf.createPortalContext());
138: render.setWindowContext(cf.createWindowContext());
139: render.setUserContext(cf.createUserContext());
140:
141: //
142: return render;
143: }
144:
145: private static class ControllerActionContext extends
146: AbstractActionContext {
147: private InvokePortletCommandFactory cmdFactory;
148: private ControllerContext controllerContext;
149:
150: public ControllerActionContext(
151: InvokePortletCommandFactory cmdFactory,
152: ControllerContext controllerContext, Mode mode,
153: WindowState windowState, StateString navigationalState,
154: MarkupInfo markupInfo, StateString interactionState,
155: PortletParameters form) {
156: super (mode, windowState, navigationalState, markupInfo,
157: interactionState, form);
158:
159: //
160: this .cmdFactory = cmdFactory;
161: this .controllerContext = controllerContext;
162:
163: //
164: addResolver(PortletInvocation.INVOCATION_SCOPE,
165: new MapAttributeResolver());
166: addResolver(PortletInvocation.REQUEST_SCOPE,
167: controllerContext);
168: addResolver(PortletInvocation.PRINCIPAL_SCOPE,
169: controllerContext);
170: addResolver(PortletInvocation.REQUEST_PROPERTIES_SCOPE,
171: EmptyAttributeResolver.getInstance());
172: addResolver(PortletInvocation.RESPONSE_PROPERTIES_SCOPE,
173: new MapAttributeResolver());
174: }
175:
176: public HttpServletRequest getClientRequest()
177: throws IllegalStateException {
178: return controllerContext.getServerInvocation()
179: .getServerContext().getClientRequest();
180: }
181:
182: public HttpServletResponse getClientResponse()
183: throws IllegalStateException {
184: return controllerContext.getServerInvocation()
185: .getServerContext().getClientResponse();
186: }
187:
188: public String renderURL(PortletURL portletURL,
189: Boolean wantSecure, Boolean wantAuthenticated,
190: boolean relative) {
191: return PortletInvocationFactory.renderURL(
192: controllerContext, cmdFactory, portletURL,
193: wantSecure, wantAuthenticated, relative);
194: }
195: }
196:
197: private static class ControllerRenderContext extends
198: AbstractRenderContext {
199: private InvokePortletCommandFactory cmdFactory;
200: private ControllerContext controllerContext;
201:
202: public ControllerRenderContext(
203: InvokePortletCommandFactory cmdFactory,
204: ControllerContext controllerContext, Mode mode,
205: WindowState windowState, StateString navigationalState,
206: MarkupInfo markupInfo) {
207: super (mode, windowState, navigationalState, markupInfo);
208:
209: //
210: this .cmdFactory = cmdFactory;
211: this .controllerContext = controllerContext;
212:
213: //
214: addResolver(PortletInvocation.INVOCATION_SCOPE,
215: new MapAttributeResolver());
216: addResolver(PortletInvocation.REQUEST_SCOPE,
217: controllerContext);
218: addResolver(PortletInvocation.PRINCIPAL_SCOPE,
219: controllerContext);
220: addResolver(PortletInvocation.REQUEST_PROPERTIES_SCOPE,
221: new MapAttributeResolver());
222: addResolver(PortletInvocation.RESPONSE_PROPERTIES_SCOPE,
223: new MapAttributeResolver());
224: }
225:
226: public HttpServletRequest getClientRequest()
227: throws IllegalStateException {
228: return controllerContext.getServerInvocation()
229: .getServerContext().getClientRequest();
230: }
231:
232: public HttpServletResponse getClientResponse()
233: throws IllegalStateException {
234: return controllerContext.getServerInvocation()
235: .getServerContext().getClientResponse();
236: }
237:
238: public String renderURL(PortletURL portletURL,
239: Boolean wantSecure, Boolean wantAuthenticated,
240: boolean relative) {
241: return PortletInvocationFactory.renderURL(
242: controllerContext, cmdFactory, portletURL,
243: wantSecure, wantAuthenticated, relative);
244: }
245: }
246:
247: public static String renderURL(ControllerContext controllerContext,
248: InvokePortletCommandFactory factory, PortletURL portletURL,
249: Boolean wantSecure, Boolean wantAuthenticated,
250: boolean relative) {
251: ControllerCommand cmd;
252: if (portletURL instanceof ActionURL) {
253: cmd = factory
254: .createInvokeActionCommand((ActionURL) portletURL);
255: } else {
256: cmd = factory
257: .createInvokeRenderCommand((RenderURL) portletURL);
258: }
259:
260: //
261: boolean secure = controllerContext.getServerInvocation()
262: .getServerContext().getURLContext().isSecure();
263: if (wantSecure != null) {
264: secure = wantSecure.booleanValue();
265: }
266:
267: //
268: boolean authenticated = controllerContext.getServerInvocation()
269: .getServerContext().getURLContext().isAuthenticated();
270: if (wantAuthenticated != null) {
271: authenticated = wantAuthenticated.booleanValue();
272: }
273:
274: URLContext info = URLContext.newInstance(secure, authenticated);
275: return controllerContext.renderURL(cmd, info, URLFormat
276: .newInstance(relative, true));
277: }
278:
279: private static class InternalInvokePortletCommandFactory implements
280: InvokePortletCommandFactory {
281:
282: /** . */
283: Window window;
284:
285: /** . */
286: ControllerContext controllerContext;
287:
288: public InternalInvokePortletCommandFactory(Window window,
289: ControllerContext controllerContext) {
290: this .controllerContext = controllerContext;
291: this .window = window;
292: }
293:
294: public ControllerCommand createInvokeActionCommand(
295: ActionURL actionURL) {
296: return new InvokePortletWindowActionCommand(window.getId(),
297: actionURL.getMode(), actionURL.getWindowState(),
298: actionURL.getNavigationalState(), actionURL
299: .getInteractionState(), null);
300: }
301:
302: public ControllerCommand createInvokeRenderCommand(
303: RenderURL renderURL) {
304: return new InvokePortletWindowRenderCommand(window.getId(),
305: renderURL.getMode(), renderURL.getWindowState(),
306: renderURL.getNavigationalState());
307: }
308: }
309: }
|