Author: chris.laprun(a)jboss.com
Date: 2008-04-10 20:35:17 -0400 (Thu, 10 Apr 2008)
New Revision: 10532
Modified:
branches/JBoss_Portal_Branch_2_7/core/src/main/org/jboss/portal/core/model/portal/command/action/InvokePortletWindowActionCommand.java
Log:
- Started integrating new PC controller. Probably broke everything but needed to commit
now for synchronization purposes.
Modified:
branches/JBoss_Portal_Branch_2_7/core/src/main/org/jboss/portal/core/model/portal/command/action/InvokePortletWindowActionCommand.java
===================================================================
---
branches/JBoss_Portal_Branch_2_7/core/src/main/org/jboss/portal/core/model/portal/command/action/InvokePortletWindowActionCommand.java 2008-04-11
00:35:04 UTC (rev 10531)
+++
branches/JBoss_Portal_Branch_2_7/core/src/main/org/jboss/portal/core/model/portal/command/action/InvokePortletWindowActionCommand.java 2008-04-11
00:35:17 UTC (rev 10532)
@@ -22,38 +22,72 @@
******************************************************************************/
package org.jboss.portal.core.model.portal.command.action;
-import org.jboss.logging.Logger;
import org.jboss.portal.Mode;
import org.jboss.portal.WindowState;
+import org.jboss.portal.common.NotYetImplemented;
+import org.jboss.portal.common.io.IOTools;
+import org.jboss.portal.common.io.Serialization;
+import org.jboss.portal.common.io.SerializationFilter;
+import org.jboss.portal.common.util.Base64;
import org.jboss.portal.common.util.ParameterMap;
+import org.jboss.portal.core.controller.ControllerCommand;
import org.jboss.portal.core.controller.ControllerException;
import org.jboss.portal.core.controller.ControllerResponse;
import org.jboss.portal.core.controller.NoSuchResourceException;
import org.jboss.portal.core.controller.command.info.ActionCommandInfo;
import org.jboss.portal.core.controller.command.info.CommandInfo;
+import org.jboss.portal.core.controller.command.response.RedirectionResponse;
import org.jboss.portal.core.controller.portlet.ControllerResponseFactory;
-import org.jboss.portal.core.controller.portlet.PortletInvocationFactory;
import org.jboss.portal.core.model.content.Content;
import org.jboss.portal.core.model.instance.Instance;
+import org.jboss.portal.core.model.portal.PortalObject;
import org.jboss.portal.core.model.portal.PortalObjectId;
+import org.jboss.portal.core.model.portal.PortalObjectPath;
+import org.jboss.portal.core.model.portal.Window;
+import org.jboss.portal.core.model.portal.command.response.UpdateWindowResponse;
+import org.jboss.portal.core.navstate.NavigationalStateContext;
import org.jboss.portal.identity.User;
+import org.jboss.portal.portlet.Portlet;
import org.jboss.portal.portlet.PortletInvokerException;
import org.jboss.portal.portlet.StateString;
+import org.jboss.portal.portlet.URLFormat;
+import org.jboss.portal.portlet.controller.PortletController;
+import org.jboss.portal.portlet.controller.PortletControllerContext;
+import org.jboss.portal.portlet.controller.event.EventControllerContext;
+import org.jboss.portal.portlet.controller.impl.AbstractPortletControllerContext;
+import org.jboss.portal.portlet.controller.impl.PortletURLRenderer;
+import org.jboss.portal.portlet.controller.impl.URLParameterConstants;
+import org.jboss.portal.portlet.controller.impl.request.ControllerRequestFactory;
+import org.jboss.portal.portlet.controller.impl.request.ControllerRequestParameterNames;
+import org.jboss.portal.portlet.controller.request.ControllerRequest;
+import org.jboss.portal.portlet.controller.request.PortletActionRequest;
+import org.jboss.portal.portlet.controller.response.PageUpdateResponse;
+import org.jboss.portal.portlet.controller.response.PortletResponse;
+import org.jboss.portal.portlet.controller.response.ResourceResponse;
+import org.jboss.portal.portlet.controller.state.AbstractPageNavigationalState;
+import org.jboss.portal.portlet.controller.state.PageNavigationalState;
+import org.jboss.portal.portlet.controller.state.PageNavigationalStateSerialization;
+import org.jboss.portal.portlet.controller.state.StateControllerContext;
+import org.jboss.portal.portlet.controller.state.WindowNavigationalState;
+import org.jboss.portal.portlet.info.PortletInfo;
import org.jboss.portal.portlet.invocation.PortletInvocation;
import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
-import org.jboss.portal.portlet.spi.RequestContext;
+import org.jboss.portal.web.IllegalRequestException;
+import org.jboss.portal.web.WebRequest;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.Set;
+
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision$
*/
public class InvokePortletWindowActionCommand extends InvokeWindowCommand
{
-
/** . */
- private static final Logger log =
Logger.getLogger(InvokePortletWindowActionCommand.class);
-
- /** . */
private static final CommandInfo info = new ActionCommandInfo(false);
/** . */
@@ -118,13 +152,8 @@
public void acquireResources() throws NoSuchResourceException
{
super.acquireResources();
+ instance = getInstance(window);
- // We need the user id
- User user = getControllerContext().getUser();
-
- // Get instance
- instance = context.getController().getCustomizationManager().getInstance(window,
user);
-
// No instance means we can't continue
if (instance == null)
{
@@ -142,43 +171,322 @@
}
}
+ private Instance getInstance(Window window)
+ {
+ // We need the user id
+ User user = getControllerContext().getUser();
+
+ // Get instance
+ return context.getController().getCustomizationManager().getInstance(window,
user);
+ }
+
public ControllerResponse execute() throws ControllerException
{
try
{
- Mode mode = this.mode;
- if (mode == null)
+ // State controller context
+ NavigationalStateContext nsContext =
(NavigationalStateContext)context.getAttributeResolver(ControllerCommand.NAVIGATIONAL_STATE_SCOPE);
+
+ ActionStateControllerContext stateControllerContext = new
ActionStateControllerContext(nsContext);
+
+ // Portlet controller context
+ PortletControllerContext controllerContext;
+ HttpServletRequest clientRequest =
context.getServerInvocation().getServerContext().getClientRequest();
+ HttpServletResponse clientResponse =
context.getServerInvocation().getServerContext().getClientResponse();
+ try
{
- mode = Mode.VIEW;
+ controllerContext = new ActionPortletControllerContext(clientRequest,
clientResponse, stateControllerContext);
}
-
- //
- WindowState windowState = this.windowState;
- if (windowState == null)
+ catch (IOException e)
{
- windowState = WindowState.NORMAL;
+ throw new ControllerException("Couldn't create
PortletControllerContext!", e);
}
- //
- PortletInvocation invocation = PortletInvocationFactory.createAction(
- context,
- mode,
- windowState,
- navigationalState,
- interactionState,
- formParameters,
- window,
- portal);
+ // The type of invocation
+ String type = (String)clientRequest.getAttribute(URLParameterConstants.TYPE);
- //
- PortletInvocationResponse response = instance.invoke(invocation);
+ // Process only portlet type
+ // The request decoded if not null
+ PageNavigationalState pageNavigationalState = null;
+ if (URLParameterConstants.PORTLET_TYPE.equals(type))
+ {
+ WebRequest wr;
+ try
+ {
+ wr = new WebRequest(clientRequest);
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ throw new ControllerException("Couldn't create web
request!", e);
+ }
- //
- return ControllerResponseFactory.createActionResponse(targetId, response);
+ PageNavigationalStateSerialization stateSerialization = new
PageNavigationalStateSerialization(stateControllerContext);
+ ControllerRequestFactory factory = new
ControllerRequestFactory(stateSerialization);
+ ControllerRequest request = factory.createRequest(wr);
+
+ org.jboss.portal.portlet.controller.response.ControllerResponse
controllerResponse;
+ controllerResponse = new PortletController().process(controllerContext,
request);
+
+ //
+ if (controllerResponse instanceof PageUpdateResponse)
+ {
+ PageUpdateResponse pageUpdate = (PageUpdateResponse)controllerResponse;
+
+ //
+ pageNavigationalState = pageUpdate.getPageNavigationalState();
+
+ // We perform a send redirect on actions
+ if (request instanceof PortletActionRequest)
+ {
+ PortletURLRenderer renderer = new
PortletURLRenderer(pageUpdate.getPageNavigationalState(),
+ clientRequest, clientResponse, stateSerialization);
+
+ //
+ String url = renderer.renderURL(new URLFormat(null, null, true,
null));
+ return new RedirectionResponse(url);
+ }
+ }
+ else if (controllerResponse instanceof ResourceResponse)
+ {
+ // todo: deal with this later
+
+ throw new NotYetImplemented("Resources are not currently
supported!");
+
+ /* ResourceResponse resourceResponse =
(ResourceResponse)controllerResponse;
+ PortletInvocationResponse pir = resourceResponse.getResponse();
+
+ //
+ if (pir instanceof ContentResponse)
+ {
+ ContentResponse contentResponse = (ContentResponse)pir;
+
+ //
+ if (contentResponse.getType() == ContentResponse.TYPE_EMPTY)
+ {
+ clientResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
+ }
+ else
+ {
+ String contentType = contentResponse.getContentType();
+ if (contentType != null)
+ {
+ clientResponse.setContentType(contentType);
+ }
+
+ //
+ if (contentResponse.getType() == ContentResponse.TYPE_BYTES)
+ {
+ ServletOutputStream out = null;
+ try
+ {
+ out = clientResponse.getOutputStream();
+ out.write(contentResponse.getBytes());
+ }
+ catch (IOException e)
+ {
+ throw new ControllerException("Couldn't output
bytes!", e);
+ }
+ finally
+ {
+ IOTools.safeClose(out);
+ }
+ }
+ else
+ {
+ Writer writer = null;
+ try
+ {
+ writer = clientResponse.getWriter();
+ writer.write(contentResponse.getChars());
+ }
+ catch (IOException e)
+ {
+ throw new ControllerException("Couldn't output
chars!", e);
+ }
+ finally
+ {
+ IOTools.safeClose(writer);
+ }
+ }
+ }
+ }
+ else
+ {
+ // todo
+ }*/
+ }
+ else if (controllerResponse instanceof PortletResponse)
+ {
+ PortletResponse portletResponse = (PortletResponse)controllerResponse;
+
+ // Let core deal with the response
+ return ControllerResponseFactory.createActionResponse(targetId,
portletResponse.getResponse());
+
+ }
+ else
+ {
+ throw new PortletInvokerException("Unexpected response type: " +
controllerResponse.getClass().getName());
+ }
+ }
+ else
+ {
+ PageNavigationalStateSerialization serialization = new
PageNavigationalStateSerialization(controllerContext.getStateControllerContext());
+ // The nav state provided with the request
+ // Unmarshall portal navigational state if it is provided
+ pageNavigationalState = null;
+ String blah =
clientRequest.getParameter(ControllerRequestParameterNames.PAGE_NAVIGATIONAL_STATE);
+ if (blah != null)
+ {
+ byte[] bytes = Base64.decode(blah, true);
+ pageNavigationalState = IOTools.unserialize(serialization,
SerializationFilter.COMPRESSOR, bytes);
+ }
+ }
}
catch (PortletInvokerException e)
{
return ControllerResponseFactory.createResponse(e);
}
+
+ return new UpdateWindowResponse(targetId);
}
+
+ private class ActionPortletControllerContext extends AbstractPortletControllerContext
+ {
+ private final StateControllerContext stateControllerContext;
+
+ public ActionPortletControllerContext(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, ActionStateControllerContext
stateControllerContext)
+ throws IllegalRequestException, IOException
+ {
+ super(httpServletRequest, httpServletResponse);
+ stateControllerContext.setPortletControllerContext(this);
+ this.stateControllerContext = stateControllerContext;
+ }
+
+ protected Portlet getPortlet(String windowId) throws PortletInvokerException
+ {
+ PortalObjectId id = PortalObjectId.parse(windowId,
PortalObjectPath.CANONICAL_FORMAT);
+ PortalObject portalObject =
context.getController().getPortalObjectContainer().getObject(id);
+ if (portalObject instanceof Window)
+ {
+ Window window = (Window)portalObject;
+ Instance instance = getInstance(window);
+ if (instance != null)
+ {
+ return instance.getPortlet();
+ }
+ }
+
+ return null;
+ }
+
+ protected PortletInvocationResponse invoke(PortletInvocation portletInvocation)
throws PortletInvokerException
+ {
+ return instance.invoke(portletInvocation);
+ }
+
+ protected Serialization<PageNavigationalState>
getPageNavigationalStateSerialization()
+ {
+ return new PageNavigationalStateSerialization(stateControllerContext);
+ }
+
+ public EventControllerContext getEventControllerContext()
+ {
+ return null;
+ }
+
+ public StateControllerContext getStateControllerContext()
+ {
+ return stateControllerContext;
+ }
+ }
+
+ private static class ActionPageNavigationalState extends
AbstractPageNavigationalState
+ {
+ private ActionStateControllerContext controllerContext;
+
+ private ActionPageNavigationalState(ActionStateControllerContext controllerContext,
boolean modifiable)
+ {
+ super(modifiable);
+ this.controllerContext = controllerContext;
+ }
+
+ private NavigationalStateContext getNSContext()
+ {
+ return controllerContext.nsContext;
+ }
+
+ private PortletControllerContext getPCContext()
+ {
+ return controllerContext.controllerContext;
+ }
+
+ public Set<String> getWindowIds()
+ {
+ return getNSContext().getKeys();
+ }
+
+ public WindowNavigationalState getWindowNavigationalState(String windowId) throws
IllegalArgumentException
+ {
+ org.jboss.portal.core.model.portal.navstate.WindowNavigationalState coreNS =
getNSContext().getWindowNavigationalState(windowId);
+
+ if (coreNS != null)
+ {
+ return new WindowNavigationalState(coreNS.getContentState(),
coreNS.getMode(), coreNS.getWindowState());
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ public void setWindowNavigationalState(String windowId, WindowNavigationalState
windowNS) throws IllegalArgumentException, IllegalStateException
+ {
+ if (windowNS != null)
+ {
+ org.jboss.portal.core.model.portal.navstate.WindowNavigationalState coreNS =
+ new
org.jboss.portal.core.model.portal.navstate.WindowNavigationalState(windowNS.getWindowState(),
+ windowNS.getMode(), windowNS.getPortletNavigationalState());
+ getNSContext().setWindowNavigationalState(windowId, coreNS);
+ }
+ else
+ {
+ getNSContext().setWindowNavigationalState(windowId, null);
+ }
+ }
+
+ protected PortletInfo getPortletInfo(String windowId)
+ {
+ return getPCContext().getPortletInfo(windowId);
+ }
+ }
+
+ private static class ActionStateControllerContext implements StateControllerContext
+ {
+ private PortletControllerContext controllerContext;
+ private NavigationalStateContext nsContext;
+
+ public ActionStateControllerContext(NavigationalStateContext nsContext)
+ {
+ this.nsContext = nsContext;
+ }
+
+ public PageNavigationalState clonePageNavigationalState(PageNavigationalState
pageNavigationalState, boolean modifiable)
+ {
+ if (pageNavigationalState != null)
+ {
+ return new ActionPageNavigationalState(this, modifiable);
+ }
+ return null;
+ }
+
+ public PageNavigationalState createPageNavigationalState(boolean modifiable)
+ {
+ return new ActionPageNavigationalState(this, modifiable);
+ }
+
+ public void setPortletControllerContext(PortletControllerContext
portletControllerContext)
+ {
+ this.controllerContext = portletControllerContext;
+ }
+ }
}