Author: chris.laprun(a)jboss.com
Date: 2008-01-31 18:58:12 -0500 (Thu, 31 Jan 2008)
New Revision: 9690
Added:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletController.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletControllerContext.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletControllerContextImpl.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletRequestHandler.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletResourceRequestHandler.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/RequestHandler.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/ViewRequestHandler.java
Removed:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletController.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletControllerContext.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletControllerContextImpl.java
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortalServlet.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/SimplePortalServlet.java
Log:
- Move PortletController and PortletControllerContext* to controller package.
- PortletController.process now delegates to RequestHandlers.
- Next step is cleaning up client interface.
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortalServlet.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortalServlet.java 2008-01-31
23:10:24 UTC (rev 9689)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortalServlet.java 2008-01-31
23:58:12 UTC (rev 9690)
@@ -22,10 +22,13 @@
******************************************************************************/
package org.jboss.portal.portlet.test;
+import org.jboss.portal.portlet.test.controller.PortletController;
+import org.jboss.portal.portlet.test.controller.PortletControllerContextImpl;
+
+import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import javax.servlet.ServletException;
import java.io.IOException;
/**
Deleted:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletController.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletController.java 2008-01-31
23:10:24 UTC (rev 9689)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletController.java 2008-01-31
23:58:12 UTC (rev 9690)
@@ -1,1056 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2008, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This is free software; you can redistribute it and/or modify it *
- * under the terms of the GNU Lesser General Public License as *
- * published by the Free Software Foundation; either version 2.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
- * Lesser General Public License for more details. *
- * *
- * You should have received a copy of the GNU Lesser General Public *
- * License along with this software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
- ******************************************************************************/
-package org.jboss.portal.portlet.test;
-
-import org.apache.log4j.Logger;
-import org.jboss.portal.Mode;
-import org.jboss.portal.WindowState;
-import org.jboss.portal.common.io.IOTools;
-import org.jboss.portal.common.util.MarkupInfo;
-import org.jboss.portal.common.util.MediaType;
-import org.jboss.portal.common.util.MultiValuedPropertyMap;
-import org.jboss.portal.common.util.ParameterMap;
-import org.jboss.portal.common.xml.XMLTools;
-import org.jboss.portal.portlet.Portlet;
-import org.jboss.portal.portlet.PortletInvokerException;
-import org.jboss.portal.portlet.StateString;
-import org.jboss.portal.portlet.PortletContext;
-import org.jboss.portal.portlet.test.event.EventControllerContext;
-import org.jboss.portal.portlet.test.event.Event;
-import org.jboss.portal.portlet.cache.CacheLevel;
-import org.jboss.portal.portlet.impl.spi.AbstractClientContext;
-import org.jboss.portal.portlet.impl.spi.AbstractPortalContext;
-import org.jboss.portal.portlet.impl.spi.AbstractRequestContext;
-import org.jboss.portal.portlet.impl.spi.AbstractSecurityContext;
-import org.jboss.portal.portlet.impl.spi.AbstractServerContext;
-import org.jboss.portal.portlet.impl.spi.AbstractUserContext;
-import org.jboss.portal.portlet.impl.spi.AbstractWindowContext;
-import org.jboss.portal.portlet.info.NavigationInfo;
-import org.jboss.portal.portlet.info.ParameterInfo;
-import org.jboss.portal.portlet.invocation.ActionInvocation;
-import org.jboss.portal.portlet.invocation.EventInvocation;
-import org.jboss.portal.portlet.invocation.RenderInvocation;
-import org.jboss.portal.portlet.invocation.ResourceInvocation;
-import org.jboss.portal.portlet.invocation.response.ErrorResponse;
-import org.jboss.portal.portlet.invocation.response.FragmentResponse;
-import org.jboss.portal.portlet.invocation.response.HTTPRedirectionResponse;
-import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
-import org.jboss.portal.portlet.invocation.response.ResponseProperties;
-import org.jboss.portal.portlet.invocation.response.StateResponse;
-import org.jboss.portal.portlet.invocation.response.UpdateNavigationalStateResponse;
-import org.jboss.portal.portlet.spi.PortalContext;
-import org.jboss.portal.portlet.spi.ResourceInvocationContext;
-import org.jboss.portal.web.IllegalRequestException;
-import org.w3c.dom.Element;
-
-import javax.servlet.ServletOutputStream;
-import javax.servlet.http.Cookie;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.xml.namespace.QName;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.UnsupportedEncodingException;
-import java.io.Writer;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.LinkedList;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class PortletController
-{
-
- /** . */
- private MarkupInfo MARKUP_INFO = new MarkupInfo(MediaType.HTML, "UTF8");
-
- /** . */
- // private final boolean redirectAfterAction = true;
-
- /** . */
- private final Logger log;
-
- /** . */
- private final PortalContext portalContext;
-
- public PortletController() throws IllegalRequestException,
UnsupportedEncodingException
- {
- //
- this.log = Logger.getLogger(PortletController.class);
-
- Map<String, String> portalProperties = new HashMap<String, String>();
- portalProperties.put("javax.portlet.markup.head.element.support",
"true");
-
- this.portalContext = new AbstractPortalContext(portalProperties);
- }
-
- public void process(ControllerRequest controllerRequest) throws
PortletInvokerException, IOException
- {
- if (controllerRequest instanceof ViewRequest)
- {
- render(controllerRequest.controllerContext, new ResponseProperties(), new
PageNavigationalState());
- }
- else if (controllerRequest instanceof PortletRequest)
- {
- processPortletRequest((PortletRequest)controllerRequest);
- }
- else if (controllerRequest instanceof PortletResourceRequest)
- {
- processResourceRequest((PortletResourceRequest)controllerRequest);
- }
- else
- {
- throw new PortletInvokerException("Unknown request type: " +
controllerRequest.getClass().getName());
- }
- }
-
- private void processPortletRequest(PortletRequest portletRequest) throws IOException,
PortletInvokerException
- {
- PortletControllerContext context = portletRequest.controllerContext;
-
- //
- PortletContext portletContext =
portletRequest.controllerContext.getPortlet(portletRequest.portletId).getContext();
-
- //
- PortletInvocationResponse response = invoke(portletRequest);
-
- //
- ResponseProperties requestProperties = new ResponseProperties();
-
- //
- if (response instanceof ErrorResponse)
- {
- ErrorResponse error = (ErrorResponse)response;
-
- //
- if (error.getCause() != null)
- {
- log.error("Portlet action threw an error: " + error.getMessage(),
error.getCause());
- }
- else
- {
- log.error("Portlet action threw an error: " + error.getMessage());
- }
-
- //
- context.getClientResponse().sendError(404, error.getMessage());
- }
- else if (response instanceof StateResponse)
- {
- // Update portlet NS
- if (response instanceof UpdateNavigationalStateResponse)
- {
- UpdateNavigationalStateResponse updateResponse =
(UpdateNavigationalStateResponse)response;
- updateNavigationalState(context, portletRequest.portletId, updateResponse,
portletRequest.pageState);
-
- //
- ResponseProperties update = updateResponse.getProperties();
- if (update != null)
- {
- requestProperties.append(update);
- }
- }
-
- //
- EventControllerContext eventCC = context.getEventControllerContext();
- StateResponse stateResponse = (StateResponse)response;
-
- // Create event list and feed it with the events that may have been produced
- LinkedList<Event> eventQueue = new LinkedList<Event>();
- for (StateResponse.Event portletEvent : stateResponse.getEvents())
- {
- Event producedEvent = new Event(portletEvent.getName(),
portletEvent.getPayload(), portletContext);
- List<Event> consumedEvents = eventCC.getConsumedEvents(producedEvent);
- eventQueue.addAll(consumedEvents);
- }
-
- // Deliver events
- while (eventQueue.size() > 0)
- {
- Event event = eventQueue.removeFirst();
-
- //
- PortletInvocationResponse eventResponse = deliverEvent(context, event,
portletRequest.pageState, requestProperties.getCookies());
-
- //
- if (eventResponse instanceof UpdateNavigationalStateResponse)
- {
- UpdateNavigationalStateResponse update =
(UpdateNavigationalStateResponse)eventResponse;
-
- // Update ns
- updateNavigationalState(context, event.getPortletContext().getId(),
update, portletRequest.pageState);
-
- // Add events if any
- for (StateResponse.Event portletEvent : update.getEvents())
- {
- Event producedEvent = new Event(portletEvent.getName(),
portletEvent.getPayload(), portletContext);
- List<Event> consumedEvents =
eventCC.getConsumedEvents(producedEvent);
- eventQueue.addAll(consumedEvents);
- }
-
- //
- ResponseProperties updateProperties = update.getProperties();
- if (updateProperties != null)
- {
- requestProperties.append(updateProperties);
- }
- }
- else if (eventResponse instanceof ErrorResponse)
- {
- // Do something here !!!!
- }
- else
- {
- // Do something here !!!!
- }
- }
-
- if (response instanceof HTTPRedirectionResponse)
- {
- HTTPRedirectionResponse redirection = (HTTPRedirectionResponse)response;
- context.getClientResponse().sendRedirect(redirection.getLocation());
- return;
- }
- }
- else
- {
- System.out.println("Not yet handled " + response);
- }
-
- /*
- if (response instanceof RenderResponse && redirectAfterAction)
- {
- PortletURLRenderer renderer = new PortletURLRenderer(navState, portlet,
req, resp);
- final RenderResponse render = (RenderResponse)response;
- String location = renderer.renderURL(new RenderURL()
- {
- public StateString getNavigationalState()
- {
- return render.getNavigationalState();
- }
- public Mode getMode()
- {
- return render.getMode();
- }
- public WindowState getWindowState()
- {
- return render.getWindowState();
- }
- }, null, null, true);
- response = new HTTPRedirectionResponse(location);
- }
- */
-
- //
- render(context, requestProperties, portletRequest.pageState);
- }
-
- private void processResourceRequest(PortletResourceRequest resourceRequest) throws
IOException, PortletInvokerException
- {
- PortletInvocationResponse response = invoke(resourceRequest);
- HttpServletResponse resp = resourceRequest.controllerContext.getClientResponse();
-
- //
- if (response instanceof FragmentResponse)
- {
- FragmentResponse fragment = (FragmentResponse)response;
- ResponseProperties props = fragment.getProperties();
-
- //
- if (props != null)
- {
- renderTransportHeaders(props, resp);
- renderCookies(props, resp);
- }
-
- //
- if (fragment.getType() == FragmentResponse.TYPE_EMPTY)
- {
- resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
- }
- else
- {
- String contentType = fragment.getContentType();
- if (contentType != null)
- {
- resp.setContentType(contentType);
- }
-
- //
- if (fragment.getType() == FragmentResponse.TYPE_BYTES)
- {
- ServletOutputStream out = null;
- try
- {
- out = resp.getOutputStream();
- out.write(fragment.getBytes().toByteArray());
- }
- finally
- {
- IOTools.safeClose(out);
- }
- }
- else
- {
- Writer writer = null;
- try
- {
- writer = resp.getWriter();
- writer.write(fragment.getChars().toString());
- }
- finally
- {
- writer.close();
- }
- }
- }
- }
- else
- {
- // todo
- }
- }
-
- private PortletInvocationResponse invoke(ContainerRequest containerRequest) throws
PortletInvokerException, IOException
- {
- PortletControllerContext context = containerRequest.controllerContext;
- Portlet portlet = context.getPortlet(containerRequest.portletId);
- HttpServletRequest req = context.getClientRequest();
- HttpServletResponse resp = context.getClientResponse();
-
- //
- if (containerRequest instanceof PortletRenderRequest)
- {
- PortletRenderRequest portletRenderRequest =
(PortletRenderRequest)containerRequest;
-
- //
- UpdateNavigationalStateResponse updateNavigationalState = new
UpdateNavigationalStateResponse();
-
updateNavigationalState.setMode(portletRenderRequest.windowNavigationalState.getMode());
-
updateNavigationalState.setWindowState(portletRenderRequest.windowNavigationalState.getWindowState());
-
updateNavigationalState.setNavigationalState(portletRenderRequest.windowNavigationalState.getPortletNavigationalState());
-
updateNavigationalState.setPublicNavigationalStateUpdates(portletRenderRequest.publicNavigationalStateChanges);
-
- //
- return updateNavigationalState;
- }
- else if (containerRequest instanceof PortletActionRequest)
- {
- PortletActionRequest portletActionRequest =
(PortletActionRequest)containerRequest;
-
- //
- PageNavigationalState pageNS = portletActionRequest.pageState;
-
- //
- Mode mode = portletActionRequest.windowNavigationalState.getMode();
- if (mode == null)
- {
- mode = Mode.VIEW;
- }
-
- //
- WindowState windowState =
portletActionRequest.windowNavigationalState.getWindowState();
- if (windowState == null)
- {
- windowState = WindowState.NORMAL;
- }
-
- //
- ParameterMap publicNS = pageNS.getPublicNavigationalState(portlet);
-
- //
- PortletURLRenderer renderer = new PortletURLRenderer(pageNS,
portletActionRequest.windowNavigationalState, portlet, req, resp);
-
- //
- TestInstanceContext instanceContext = new TestInstanceContext(req,
portlet.getContext(), true);
- TestActionContext actionContext = new TestActionContext(
- renderer,
- mode,
- windowState,
- portletActionRequest.windowNavigationalState.getPortletNavigationalState(),
- publicNS,
- MARKUP_INFO,
- portletActionRequest.interactionState,
- portletActionRequest.bodyParameters != null ?
ParameterMap.clone(portletActionRequest.bodyParameters) : null);
- ActionInvocation actionInvocation = new ActionInvocation(actionContext);
-
- //
- actionInvocation.setClientContext(new AbstractClientContext(req));
- actionInvocation.setServerContext(new AbstractServerContext(req, resp));
- actionInvocation.setInstanceContext(instanceContext);
- actionInvocation.setUserContext(new AbstractUserContext(req));
- actionInvocation.setWindowContext(new
AbstractWindowContext(portlet.getContext().getId()));
- actionInvocation.setPortalContext(portalContext);
- actionInvocation.setSecurityContext(new AbstractSecurityContext(req));
- actionInvocation.setRequestContext(new AbstractRequestContext(req));
- actionInvocation.setTarget(instanceContext.getTarget());
-
- //
- try
- {
- return context.invoke(actionInvocation);
- }
- catch (PortletInvokerException e)
- {
- return null;
- }
- }
- else
- {
- PortletResourceRequest portletResourceRequest =
(PortletResourceRequest)containerRequest;
-
- //
- Mode mode = null;
- WindowState windowState = null;
- PageNavigationalState pageNS = null;
- ParameterMap publicNS = null;
- StateString portletNS = null;
- CacheLevel cacheability;
-
- //
- if (portletResourceRequest instanceof PortletScopedPortletResourceRequest)
- {
- PortletScopedPortletResourceRequest portletScopedRequest =
(PortletScopedPortletResourceRequest)portletResourceRequest;
- mode = portletScopedRequest.windowNavigationalState.getMode();
- windowState = portletScopedRequest.windowNavigationalState.getWindowState();
- portletNS =
portletScopedRequest.windowNavigationalState.getPortletNavigationalState();
-
- //
- if (portletResourceRequest instanceof PageScopedFullPortletResourceRequest)
- {
- PageScopedFullPortletResourceRequest pageScopedRequest =
(PageScopedFullPortletResourceRequest)portletResourceRequest;
- pageNS = pageScopedRequest.pageState;
- cacheability = CacheLevel.PAGE;
-
- //
- if (pageNS != null)
- {
- publicNS = pageNS.getPublicNavigationalState(portlet);
- }
- }
- else
- {
- cacheability = CacheLevel.PORTLET;
- }
- }
- else
- {
- cacheability = CacheLevel.FULL;
- }
-
- //
- if (mode == null)
- {
- mode = Mode.VIEW;
- }
- if (windowState == null)
- {
- windowState = WindowState.NORMAL;
- }
-
- //
- WindowNavigationalState windowNS = new WindowNavigationalState(portletNS, mode,
windowState);
-
- //
- PortletURLRenderer renderer = new PortletURLRenderer(pageNS, windowNS, portlet,
req, resp);
-
- //
- TestInstanceContext instanceContext = new TestInstanceContext(req,
portlet.getContext(), true);
- ResourceInvocationContext resourceInvocationContext = new ResourceTestContext(
- portletResourceRequest.resourceId,
- cacheability,
- renderer,
- mode,
- windowState,
- portletNS,
- publicNS,
- portletResourceRequest.resourceState,
- portletResourceRequest.bodyParameters != null ?
ParameterMap.clone(portletResourceRequest.bodyParameters) : null,
- MARKUP_INFO);
- ResourceInvocation resourceInvocation = new
ResourceInvocation(resourceInvocationContext);
-
- //
- resourceInvocation.setClientContext(new AbstractClientContext(req));
- resourceInvocation.setServerContext(new AbstractServerContext(req, resp));
- resourceInvocation.setInstanceContext(instanceContext);
- resourceInvocation.setUserContext(new AbstractUserContext(req));
- resourceInvocation.setWindowContext(new
AbstractWindowContext(portlet.getContext().getId()));
- resourceInvocation.setPortalContext(portalContext);
- resourceInvocation.setSecurityContext(new AbstractSecurityContext(req));
- resourceInvocation.setRequestContext(new AbstractRequestContext(req));
- resourceInvocation.setTarget(instanceContext.getTarget());
-
- //
- try
- {
- return context.invoke(resourceInvocation);
- }
- catch (PortletInvokerException e)
- {
- return null;
- }
- }
- }
-
- private PortletInvocationResponse deliverEvent(
- PortletControllerContext context,
- Event event,
- PageNavigationalState pageNS,
- List<Cookie> requestCookies) throws PortletInvokerException
- {
- Portlet portlet = context.getPortlet(event.getPortletContext().getId());
- HttpServletRequest req = context.getClientRequest();
- HttpServletResponse resp = context.getClientResponse();
- WindowNavigationalState windowNS =
pageNS.getWindowNavigationalState(portlet.getContext().getId());
- PortletURLRenderer renderer = new PortletURLRenderer(pageNS, windowNS, portlet,
req, resp);
-
- //
- if (windowNS == null)
- {
- windowNS = new WindowNavigationalState();
- }
-
- //
- ParameterMap publicNS = pageNS.getPublicNavigationalState(portlet);
-
- //
- TestInstanceContext instanceContext = new TestInstanceContext(req,
portlet.getContext(), true);
- TestEventContext actionContext = new TestEventContext(
- renderer,
- windowNS.getMode(),
- windowNS.getWindowState(),
- windowNS.getPortletNavigationalState(),
- publicNS,
- MARKUP_INFO,
- event.getName(),
- event.getPayload());
- EventInvocation eventInvocation = new EventInvocation(actionContext);
-
- //
- eventInvocation.setClientContext(new AbstractClientContext(req, requestCookies));
- eventInvocation.setServerContext(new AbstractServerContext(req, resp));
- eventInvocation.setInstanceContext(instanceContext);
- eventInvocation.setUserContext(new AbstractUserContext(req));
- eventInvocation.setWindowContext(new
AbstractWindowContext(portlet.getContext().getId()));
- eventInvocation.setPortalContext(portalContext);
- eventInvocation.setSecurityContext(new AbstractSecurityContext(req));
- eventInvocation.setTarget(instanceContext.getTarget());
-
- //
- try
- {
- return context.invoke(eventInvocation);
- }
- catch (PortletInvokerException e)
- {
- log.error("Error during event invocation", e);
-
- //
- return null;
- }
- }
-
- /**
- * @param context
- * @param pageProperties properties that needs to be rendered on this page
- * @param pageNS
- * @throws IOException
- */
- private void render(
- PortletControllerContext context,
- ResponseProperties pageProperties,
- PageNavigationalState pageNS) throws IOException
- {
- HttpServletRequest req = context.getClientRequest();
- HttpServletResponse resp = context.getClientResponse();
-
- List<FragmentResponse> fragments = Collections.emptyList();
- List<ErrorResponse> errors = Collections.emptyList();
-
- // What we collect during the different renders
- // we don't reuse the render properties argument since we want to avoid that
- // a portlet rendition affects another rendition of a portlet on the same page
- ResponseProperties renderProperties = new ResponseProperties();
-
- //
- try
- {
- Collection<Portlet> portlets = context.getPortlets();
-
- int capacity = portlets.size();
- fragments = new ArrayList<FragmentResponse>(capacity);
- errors = new ArrayList<ErrorResponse>(capacity);
-
- for (Portlet portlet : portlets)
- {
- WindowNavigationalState windowNS =
pageNS.getWindowNavigationalState(portlet.getContext().getId());
-
- //
- PortletURLRenderer renderer = new PortletURLRenderer(pageNS, windowNS,
portlet, req, resp);
-
- //
- Mode mode = Mode.VIEW;
- WindowState windowState = WindowState.NORMAL;
- StateString portletNS = null;
-
- //
- if (windowNS != null)
- {
- if (windowNS.getMode() != null)
- {
- mode = windowNS.getMode();
- }
- if (windowNS.getWindowState() != null)
- {
- windowState = windowNS.getWindowState();
- }
- if (windowNS.getPortletNavigationalState() != null)
- {
- portletNS = windowNS.getPortletNavigationalState();
- }
- }
-
- //
- ParameterMap publicNS = pageNS.getPublicNavigationalState(portlet);
-
- //
- TestInstanceContext instanceContext = new TestInstanceContext(req,
portlet.getContext(), false);
- TestRenderContext renderContext = new TestRenderContext(renderer, mode,
windowState, portletNS, publicNS, MARKUP_INFO);
-
- //
- RenderInvocation render = new RenderInvocation(renderContext);
-
- //
- render.setClientContext(new AbstractClientContext(req,
pageProperties.getCookies()));
- render.setServerContext(new AbstractServerContext(req, resp));
- render.setInstanceContext(instanceContext);
- render.setUserContext(new AbstractUserContext(req));
- render.setWindowContext(new
AbstractWindowContext(portlet.getContext().getId()));
- render.setPortalContext(portalContext);
- render.setSecurityContext(new AbstractSecurityContext(req));
- render.setTarget(instanceContext.getTarget());
-
- //
- try
- {
- PortletInvocationResponse response = context.invoke(render);
-
- if (response instanceof FragmentResponse)
- {
- FragmentResponse fragment = (FragmentResponse)response;
-
- //
- fragments.add(fragment);
-
- //
- ResponseProperties fragmentProperties = fragment.getProperties();
- if (fragmentProperties != null)
- {
- renderProperties.append(fragmentProperties);
- }
- }
- else if (response instanceof ErrorResponse)
- {
- ErrorResponse error = (ErrorResponse)response;
- errors.add(error);
- }
-
- }
- catch (PortletInvokerException e)
- {
- e.printStackTrace();
- }
- }
- }
- catch (PortletInvokerException e)
- {
- // todo
- e.printStackTrace();
- }
-
- // Now we combine the render properties with the page properties
- pageProperties.append(renderProperties);
-
- // Render the headers and cookies
- renderTransportHeaders(pageProperties, resp);
- renderCookies(pageProperties, resp);
-
- //
- resp.setContentType("text/html");
- PrintWriter writer = resp.getWriter();
- writer.print("<html>");
-
- // Render the head contributions
- writer.print("<head>");
- MultiValuedPropertyMap<Element> markupHeaders =
pageProperties.getMarkupHeaders();
- List<Element> headElement =
markupHeaders.getValues("javax.portlet.markup.head.element");
- if (headElement != null)
- {
- for (Element headerValue : headElement)
- {
- try
- {
- String serializedElement = XMLTools.toString(headerValue);
- writer.print(serializedElement);
- }
- catch (Exception e)
- {
- // todo
- e.printStackTrace();
- }
- }
- }
- writer.print("</head>");
-
-
- writer.print("<body>");
- for (FragmentResponse fragment : fragments)
- {
- if (fragment.getType() != FragmentResponse.TYPE_EMPTY)
- {
- writer.print("<div class=\"portlet\">");
- writer.print(fragment.getBuffer().getContent());
- writer.print("</div>");
- }
- else
- {
- writer.print("<div/>");
- }
- }
-
- for (ErrorResponse error : errors)
- {
- writer.print("<div class='error'>");
- writer.print(error.toHTML());
- writer.print("</div>");
- }
-
- writer.print("</body></html>");
- }
-
- private void renderCookies(ResponseProperties pageProperties, HttpServletResponse
resp)
- {
- List<Cookie> cookies = pageProperties.getCookies();
- for (Cookie cookie : cookies)
- {
- resp.addCookie(cookie);
- }
- }
-
- private void renderTransportHeaders(ResponseProperties pageProperties,
HttpServletResponse resp)
- {
- MultiValuedPropertyMap<String> transportHeaders =
pageProperties.getTransportHeaders();
- for (String headerName : transportHeaders.keySet())
- {
- for (String headerValue : transportHeaders.getValues(headerName))
- {
- resp.addHeader(headerName, headerValue);
- }
- }
- }
-
- private void updateNavigationalState(
- PortletControllerContext context,
- String portletId,
- UpdateNavigationalStateResponse update,
- PageNavigationalState pageState) throws PortletInvokerException
- {
- Portlet portlet = context.getPortlet(portletId);
-
- //
- WindowNavigationalState windowNS =
pageState.getWindowNavigationalState(portletId);
-
- //
- if (windowNS == null)
- {
- windowNS = new WindowNavigationalState();
- }
-
- //
- Mode mode = windowNS.getMode();
- if (update.getMode() != null)
- {
- mode = update.getMode();
- }
- WindowState windowState = windowNS.getWindowState();
- if (update.getWindowState() != null)
- {
- windowState = update.getWindowState();
- }
- StateString portletNS = windowNS.getPortletNavigationalState();
- if (update.getNavigationalState() != null)
- {
- portletNS = update.getNavigationalState();
- }
- windowNS = new WindowNavigationalState(portletNS, mode, windowState);
- pageState.setWindowNavigationalState(portletId, windowNS);
-
- // Now update shared state scoped at page
- Map<String, String[]> publicNS = update.getPublicNavigationalStateUpdates();
- if (publicNS != null)
- {
- NavigationInfo navigationInfo = portlet.getInfo().getNavigation();
- Map<QName, String[]> pageNS = pageState.getPageNavigationalState();
- for (Map.Entry<String, String[]> entry : publicNS.entrySet())
- {
- String id = entry.getKey();
- ParameterInfo parameterInfo = navigationInfo.getPublicParameter(id);
-
- //
- if (parameterInfo != null)
- {
- QName name = parameterInfo.getName();
- String[] value = entry.getValue();
- if (value.length > 0)
- {
- pageNS.put(name, value);
- }
- else
- {
- pageNS.remove(name);
- }
- }
- }
- }
- }
-
- /**
- * Must be used as base class for high level response provided by the controller that
- * will be translated into something at the portal level. The goal is to avoid to
manipulate
- * the HTTP response directly.
- *
- * The test bed will of course use the HTTP response but the Presentation Framework
will
- * work differently.
- *
- * So we really need to abstract everything done with the HttpServletResponse and
*never* use
- * it in the controller.
- *
- * Typical usage should be :
- * ControllerResponse response = controller.invoker(ControllerRequest request);
- *
- */
- static class ControllerResponse
- {
-
- }
-
- static class ControllerRequest
- {
- /** . */
- final PortletControllerContext controllerContext;
-
- ControllerRequest(PortletControllerContext controllerContext)
- {
- this.controllerContext = controllerContext;
- }
- }
-
- static class ViewRequest extends ControllerRequest
- {
- ViewRequest(PortletControllerContext controllerContext)
- {
- super(controllerContext);
- }
- }
-
- static class ContainerRequest extends ControllerRequest
- {
- /** . */
- final String portletId;
-
- ContainerRequest(PortletControllerContext controllerContext, String portletId)
- {
- super(controllerContext);
-
- //
- this.portletId = portletId;
- }
- }
-
- static class PortletRequest extends ContainerRequest
- {
- /** . */
- final PageNavigationalState pageState;
-
- /** . */
- final WindowNavigationalState windowNavigationalState;
-
- PortletRequest(
- PortletControllerContext controllerContext,
- String portletId,
- WindowNavigationalState windowNavigationalState,
- PageNavigationalState pageState)
- {
- super(controllerContext, portletId);
-
- //
- this.windowNavigationalState = windowNavigationalState;
- this.pageState = pageState;
- }
- }
-
- static abstract class PortletResourceRequest extends ContainerRequest
- {
- /** . */
- private static final Map<Class, CacheLevel> cacheability = new
HashMap<Class, CacheLevel>();
-
- static
- {
- cacheability.put(FullScopedCacheablePortletResourceRequest.class,
CacheLevel.FULL);
- cacheability.put(PortletScopedPortletResourceRequest.class,
CacheLevel.PORTLET);
- cacheability.put(PageScopedFullPortletResourceRequest.class, CacheLevel.PAGE);
- }
-
- /** . */
- final String resourceId;
-
- /** . */
- final StateString resourceState;
-
- /** . */
- final ParameterMap bodyParameters;
-
- PortletResourceRequest(
- PortletControllerContext controllerContext,
- String portletId,
- String resourceId,
- StateString resourceState,
- ParameterMap bodyParameters
- )
- {
- super(controllerContext, portletId);
-
- //
- this.resourceId = resourceId;
- this.resourceState = resourceState;
- this.bodyParameters = bodyParameters;
- }
-
- public CacheLevel getCacheabilityType()
- {
- return cacheability.get(getClass());
- }
- }
-
- static class FullScopedCacheablePortletResourceRequest extends PortletResourceRequest
- {
- FullScopedCacheablePortletResourceRequest(
- PortletControllerContext controllerContext,
- String portletId,
- String resourceId,
- StateString resourceState,
- ParameterMap bodyParameters)
- {
- super(controllerContext, portletId, resourceId, resourceState, bodyParameters);
- }
- }
-
- static class PortletScopedPortletResourceRequest extends
FullScopedCacheablePortletResourceRequest
- {
- /** . */
- final WindowNavigationalState windowNavigationalState;
-
- PortletScopedPortletResourceRequest(
- PortletControllerContext controllerContext,
- String portletId,
- String resourceId,
- StateString resourceState,
- ParameterMap bodyParameters,
- WindowNavigationalState windowNavigationalState)
- {
- super(controllerContext, portletId, resourceId, resourceState, bodyParameters);
-
- //
- this.windowNavigationalState = windowNavigationalState;
- }
- }
-
- static class PageScopedFullPortletResourceRequest extends
PortletScopedPortletResourceRequest
- {
- /** . */
- private final PageNavigationalState pageState;
-
- PageScopedFullPortletResourceRequest(
- PortletControllerContext controllerContext,
- String portletId,
- String resourceId,
- StateString resourceState,
- ParameterMap bodyParameters,
- PageNavigationalState pageState,
- WindowNavigationalState windowNavigationalState)
- {
- super(controllerContext, portletId, resourceId, resourceState, bodyParameters,
windowNavigationalState);
-
- //
- this.pageState = pageState;
- }
- }
-
- static class PortletActionRequest extends PortletRequest
- {
- /** . */
- final StateString interactionState;
-
- /** . */
- final ParameterMap bodyParameters;
-
- public PortletActionRequest(
- PortletControllerContext controllerContext,
- String portletId,
- StateString interactionState,
- ParameterMap bodyParameters,
- WindowNavigationalState windowNavigationalState,
- PageNavigationalState pageState)
- {
- super(controllerContext, portletId, windowNavigationalState, pageState);
-
- //
- this.interactionState = interactionState;
- this.bodyParameters = bodyParameters;
- }
- }
-
- static class PortletRenderRequest extends PortletRequest
- {
- /** . */
- final Map<String, String[]> publicNavigationalStateChanges;
-
- public PortletRenderRequest(
- PortletControllerContext controllerContext,
- String portletId,
- WindowNavigationalState windowNavigationalState,
- Map<String, String[]> publicNavigationalStateChanges,
- PageNavigationalState pageState)
- {
- super(controllerContext, portletId, windowNavigationalState, pageState);
-
- //
- this.publicNavigationalStateChanges = publicNavigationalStateChanges;
- }
- }
-}
Deleted:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletControllerContext.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletControllerContext.java 2008-01-31
23:10:24 UTC (rev 9689)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletControllerContext.java 2008-01-31
23:58:12 UTC (rev 9690)
@@ -1,57 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This is free software; you can redistribute it and/or modify it *
- * under the terms of the GNU Lesser General Public License as *
- * published by the Free Software Foundation; either version 2.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
- * Lesser General Public License for more details. *
- * *
- * You should have received a copy of the GNU Lesser General Public *
- * License along with this software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
- ******************************************************************************/
-package org.jboss.portal.portlet.test;
-
-import org.jboss.portal.portlet.Portlet;
-import org.jboss.portal.portlet.PortletInvokerException;
-import org.jboss.portal.portlet.test.event.EventControllerContext;
-import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
-import org.jboss.portal.portlet.invocation.PortletInvocation;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.ServletContext;
-import java.util.Collection;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public interface PortletControllerContext
-{
-
- ServletContext getServletContext();
-
- HttpServletRequest getClientRequest();
-
- HttpServletResponse getClientResponse();
-
- Portlet getPortlet(String portletId) throws PortletInvokerException;
-
- PortletInvocationResponse invoke(PortletInvocation invocation) throws
PortletInvokerException;
-
- Collection<Portlet> getPortlets() throws PortletInvokerException;
-
- EventControllerContext getEventControllerContext();
-
-}
Deleted:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletControllerContextImpl.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletControllerContextImpl.java 2008-01-31
23:10:24 UTC (rev 9689)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletControllerContextImpl.java 2008-01-31
23:58:12 UTC (rev 9690)
@@ -1,280 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This is free software; you can redistribute it and/or modify it *
- * under the terms of the GNU Lesser General Public License as *
- * published by the Free Software Foundation; either version 2.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
- * Lesser General Public License for more details. *
- * *
- * You should have received a copy of the GNU Lesser General Public *
- * License along with this software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
- ******************************************************************************/
-package org.jboss.portal.portlet.test;
-
-import org.jboss.portal.Mode;
-import org.jboss.portal.WindowState;
-import org.jboss.portal.common.util.ParameterMap;
-import org.jboss.portal.common.util.Tools;
-import org.jboss.portal.common.io.IOTools;
-import org.jboss.portal.portlet.Portlet;
-import org.jboss.portal.portlet.PortletContext;
-import org.jboss.portal.portlet.PortletInvoker;
-import org.jboss.portal.portlet.PortletInvokerException;
-import org.jboss.portal.portlet.ParametersStateString;
-import org.jboss.portal.portlet.StateString;
-import org.jboss.portal.portlet.cache.CacheLevel;
-import org.jboss.portal.portlet.invocation.PortletInvocation;
-import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
-import org.jboss.portal.web.Body;
-import org.jboss.portal.web.IllegalRequestException;
-import org.jboss.portal.web.WebRequest;
-import static org.jboss.portal.portlet.test.URLParameterConstants.*;
-import org.jboss.portal.portlet.test.event.EventControllerContext;
-import org.jboss.portal.portlet.test.event.EventControllerContextImpl;
-
-import javax.servlet.ServletContext;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.util.Collection;
-import java.util.Map;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class PortletControllerContextImpl implements PortletControllerContext
-{
-
- /** . */
- public static final int NAV_SCOPE = 0;
-
- /** . */
- public static final int SESSION_SCOPE = 1;
-
- /** . */
- private final HttpServletRequest req;
-
- /** . */
- private final HttpServletResponse resp;
-
- /** . */
- private final ServletContext servletContext;
-
- /** . */
- private final PortletController.ControllerRequest request;
-
- /** . */
- private final PortletInvoker invoker;
-
- /** . */
- private final EventControllerContext eventControllerContext;
-
- public PortletControllerContextImpl(
- HttpServletRequest req,
- HttpServletResponse resp,
- ServletContext servletContext)
- throws IllegalRequestException, IOException, ClassNotFoundException
- {
- PortletInvoker invoker =
(PortletInvoker)servletContext.getAttribute("ConsumerPortletInvoker");
-
- // The nav state provided with the request
- PageNavigationalState pageState = null;
-
- // The request decoded if not null
- PortletController.ControllerRequest request = null;
-
- // Process
- String pathInfo = req.getPathInfo();
- if (pathInfo != null && pathInfo.startsWith("/") &&
pathInfo.length() > 1)
- {
- // Get the target portlet
- String targetId = StringCodec.decode(pathInfo.substring(1));
-
- // Helper
- WebRequest ri = new WebRequest(req);
-
- // Unmarshall portal navigational state if it is provided
- String context = req.getParameter(PAGE_STATE);
- if (context != null)
- {
- byte[] bytes = Tools.fromHexString(context);
- pageState = PageNavigationalState.unserialize(bytes);
- }
-
- //
- Mode mode = null;
- if (req.getParameter(MODE) != null)
- {
- mode = Mode.create(req.getParameter(MODE));
- }
-
- //
- WindowState windowState = null;
- if (req.getParameter(WINDOW_STATE) != null)
- {
- windowState = WindowState.create(req.getParameter(WINDOW_STATE));
- }
-
- //
- String navigationalStateString = req.getParameter(NAVIGATIONAL_STATE);
- StateString navigationalState = null;
- if (navigationalStateString != null)
- {
- navigationalState = ParametersStateString.create(navigationalStateString);
- }
-
- //
- WindowNavigationalState windowNavigationalState = new
WindowNavigationalState(navigationalState, mode, windowState);
-
- //
- String type = req.getParameter(LIFECYCLE_TYPE);
- if (RESOURCE_LIFECYCLE.equals(type))
- {
- StateString resourceState =
ParametersStateString.create(req.getParameter(RESOURCE_STATE));
- String resourceId = req.getParameter(RESOURCE_ID);
-
-
- //
- ParameterMap formParameters = null;
- if (ri.getBody() instanceof Body.Form)
- {
- formParameters = ((Body.Form)ri.getBody()).getParameters();
- }
-
- //
- CacheLevel resourceCacheLevel =
CacheLevel.valueOf(req.getParameter(RESOURCE_CACHEABILITY));
-
- //
- switch (resourceCacheLevel)
- {
- case FULL:
- request = new
PortletController.FullScopedCacheablePortletResourceRequest(
- this,
- targetId,
- resourceId,
- resourceState,
- formParameters);
- break;
- case PORTLET:
- request = new PortletController.PortletScopedPortletResourceRequest(
- this,
- targetId,
- resourceId,
- resourceState,
- formParameters,
- windowNavigationalState);
- break;
- case PAGE:
- request = new PortletController.PageScopedFullPortletResourceRequest(
- this,
- targetId,
- resourceId,
- resourceState,
- formParameters,
- pageState,
- windowNavigationalState);
- break;
- }
- }
- else
- {
- if (ACTION_LIFECYCLE.equals(type))
- {
- ParameterMap formParameters = null;
- if (ri.getBody() instanceof Body.Form)
- {
- formParameters = ((Body.Form)ri.getBody()).getParameters();
- }
-
- //
- StateString interactionState =
ParametersStateString.create(req.getParameter(INTERACTION_STATE));
-
- //
- request = new PortletController.PortletActionRequest(
- this,
- targetId,
- interactionState,
- formParameters,
- windowNavigationalState,
- pageState);
- }
- else
- {
- Map<String, String[]> publicNavigationalStateChanges =
(Map<String,
String[]>)IOTools.unserialize(Tools.fromHexString(req.getParameter(PUBLIC_NAVIGATIONAL_STATE_CHANGES)));
-
- //
- request = new PortletController.PortletRenderRequest(
- this,
- targetId,
- windowNavigationalState,
- publicNavigationalStateChanges,
- pageState);
- }
- }
- }
- else
- {
- request = new PortletController.ViewRequest(this);
- }
-
- //
- this.request = request;
- this.req = req;
- this.resp = resp;
- this.servletContext = servletContext;
- this.invoker = invoker;
- this.eventControllerContext = new EventControllerContextImpl(invoker);
- }
-
- public PortletController.ControllerRequest getRequest()
- {
- return request;
- }
-
- public ServletContext getServletContext()
- {
- return servletContext;
- }
-
- public HttpServletRequest getClientRequest()
- {
- return req;
- }
-
- public HttpServletResponse getClientResponse()
- {
- return resp;
- }
-
- public Portlet getPortlet(String portletId) throws PortletInvokerException
- {
- return invoker.getPortlet(PortletContext.createPortletContext(portletId));
- }
-
- public PortletInvocationResponse invoke(PortletInvocation invocation) throws
PortletInvokerException
- {
- return invoker.invoke(invocation);
- }
-
- public Collection<Portlet> getPortlets() throws PortletInvokerException
- {
- return invoker.getPortlets();
- }
-
- public EventControllerContext getEventControllerContext()
- {
- return eventControllerContext;
- }
-}
Copied:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletController.java
(from rev 9688,
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletController.java)
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletController.java
(rev 0)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletController.java 2008-01-31
23:58:12 UTC (rev 9690)
@@ -0,0 +1,669 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.portlet.test.controller;
+
+import org.apache.log4j.Logger;
+import org.jboss.portal.Mode;
+import org.jboss.portal.WindowState;
+import org.jboss.portal.common.util.MarkupInfo;
+import org.jboss.portal.common.util.MediaType;
+import org.jboss.portal.common.util.MultiValuedPropertyMap;
+import org.jboss.portal.common.util.ParameterMap;
+import org.jboss.portal.common.xml.XMLTools;
+import org.jboss.portal.portlet.Portlet;
+import org.jboss.portal.portlet.PortletInvokerException;
+import org.jboss.portal.portlet.StateString;
+import org.jboss.portal.portlet.cache.CacheLevel;
+import org.jboss.portal.portlet.impl.spi.AbstractClientContext;
+import org.jboss.portal.portlet.impl.spi.AbstractPortalContext;
+import org.jboss.portal.portlet.impl.spi.AbstractSecurityContext;
+import org.jboss.portal.portlet.impl.spi.AbstractServerContext;
+import org.jboss.portal.portlet.impl.spi.AbstractUserContext;
+import org.jboss.portal.portlet.impl.spi.AbstractWindowContext;
+import org.jboss.portal.portlet.info.NavigationInfo;
+import org.jboss.portal.portlet.info.ParameterInfo;
+import org.jboss.portal.portlet.invocation.EventInvocation;
+import org.jboss.portal.portlet.invocation.RenderInvocation;
+import org.jboss.portal.portlet.invocation.response.ErrorResponse;
+import org.jboss.portal.portlet.invocation.response.FragmentResponse;
+import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
+import org.jboss.portal.portlet.invocation.response.ResponseProperties;
+import org.jboss.portal.portlet.invocation.response.UpdateNavigationalStateResponse;
+import org.jboss.portal.portlet.spi.PortalContext;
+import org.jboss.portal.portlet.test.PageNavigationalState;
+import org.jboss.portal.portlet.test.PortletURLRenderer;
+import org.jboss.portal.portlet.test.TestEventContext;
+import org.jboss.portal.portlet.test.TestInstanceContext;
+import org.jboss.portal.portlet.test.TestRenderContext;
+import org.jboss.portal.portlet.test.WindowNavigationalState;
+import org.jboss.portal.portlet.test.event.Event;
+import org.jboss.portal.web.IllegalRequestException;
+import org.w3c.dom.Element;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.namespace.QName;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class PortletController
+{
+
+ /** . */
+ static final MarkupInfo MARKUP_INFO = new MarkupInfo(MediaType.HTML,
"UTF8");
+
+ /** . */
+ // private final boolean redirectAfterAction = true;
+
+ /** . */
+ private final Logger log;
+
+ /** . */
+ private final PortalContext portalContext;
+
+ public PortletController() throws IllegalRequestException,
UnsupportedEncodingException
+ {
+ //
+ this.log = Logger.getLogger(PortletController.class);
+
+ Map<String, String> portalProperties = new HashMap<String, String>();
+ portalProperties.put("javax.portlet.markup.head.element.support",
"true");
+
+ this.portalContext = new AbstractPortalContext(portalProperties);
+ }
+
+ public void process(ControllerRequest controllerRequest) throws IOException,
PortletInvokerException
+ {
+ RequestHandler handler;
+
+ if (controllerRequest instanceof ViewRequest)
+ {
+ handler = new ViewRequestHandler(this);
+ }
+ else if (controllerRequest instanceof PortletRequest)
+ {
+ handler = new PortletRequestHandler(this);
+ }
+ else if (controllerRequest instanceof PortletResourceRequest)
+ {
+ handler = new PortletResourceRequestHandler(this);
+ }
+ else
+ {
+ throw new PortletInvokerException("Unknown request type: " +
controllerRequest.getClass().getName());
+ }
+
+ handler.handle(controllerRequest);
+ }
+
+ PortletInvocationResponse deliverEvent(
+ PortletControllerContext context,
+ Event event,
+ PageNavigationalState pageNS,
+ List<Cookie> requestCookies) throws PortletInvokerException
+ {
+ Portlet portlet = context.getPortlet(event.getPortletContext().getId());
+ HttpServletRequest req = context.getClientRequest();
+ HttpServletResponse resp = context.getClientResponse();
+ WindowNavigationalState windowNS =
pageNS.getWindowNavigationalState(portlet.getContext().getId());
+ PortletURLRenderer renderer = new PortletURLRenderer(pageNS, windowNS, portlet,
req, resp);
+
+ //
+ if (windowNS == null)
+ {
+ windowNS = new WindowNavigationalState();
+ }
+
+ //
+ ParameterMap publicNS = pageNS.getPublicNavigationalState(portlet);
+
+ //
+ TestInstanceContext instanceContext = new TestInstanceContext(req,
portlet.getContext(), true);
+ TestEventContext actionContext = new TestEventContext(
+ renderer,
+ windowNS.getMode(),
+ windowNS.getWindowState(),
+ windowNS.getPortletNavigationalState(),
+ publicNS,
+ MARKUP_INFO,
+ event.getName(),
+ event.getPayload());
+ EventInvocation eventInvocation = new EventInvocation(actionContext);
+
+ //
+ eventInvocation.setClientContext(new AbstractClientContext(req, requestCookies));
+ eventInvocation.setServerContext(new AbstractServerContext(req, resp));
+ eventInvocation.setInstanceContext(instanceContext);
+ eventInvocation.setUserContext(new AbstractUserContext(req));
+ eventInvocation.setWindowContext(new
AbstractWindowContext(portlet.getContext().getId()));
+ eventInvocation.setPortalContext(getPortalContext());
+ eventInvocation.setSecurityContext(new AbstractSecurityContext(req));
+ eventInvocation.setTarget(instanceContext.getTarget());
+
+ //
+ try
+ {
+ return context.invoke(eventInvocation);
+ }
+ catch (PortletInvokerException e)
+ {
+ log.error("Error during event invocation", e);
+
+ //
+ return null;
+ }
+ }
+
+ /**
+ * @param context
+ * @param pageProperties properties that needs to be rendered on this page
+ * @param pageNS
+ * @throws IOException
+ */
+ void render(PortletControllerContext context, ResponseProperties pageProperties,
PageNavigationalState pageNS) throws IOException
+ {
+ HttpServletRequest req = context.getClientRequest();
+ HttpServletResponse resp = context.getClientResponse();
+
+ List<FragmentResponse> fragments = Collections.emptyList();
+ List<ErrorResponse> errors = Collections.emptyList();
+
+ // What we collect during the different renders
+ // we don't reuse the render properties argument since we want to avoid that
+ // a portlet rendition affects another rendition of a portlet on the same page
+ ResponseProperties renderProperties = new ResponseProperties();
+
+ //
+ try
+ {
+ Collection<Portlet> portlets = context.getPortlets();
+
+ int capacity = portlets.size();
+ fragments = new ArrayList<FragmentResponse>(capacity);
+ errors = new ArrayList<ErrorResponse>(capacity);
+
+ for (Portlet portlet : portlets)
+ {
+ WindowNavigationalState windowNS =
pageNS.getWindowNavigationalState(portlet.getContext().getId());
+
+ //
+ PortletURLRenderer renderer = new PortletURLRenderer(pageNS, windowNS,
portlet, req, resp);
+
+ //
+ Mode mode = Mode.VIEW;
+ WindowState windowState = WindowState.NORMAL;
+ StateString portletNS = null;
+
+ //
+ if (windowNS != null)
+ {
+ if (windowNS.getMode() != null)
+ {
+ mode = windowNS.getMode();
+ }
+ if (windowNS.getWindowState() != null)
+ {
+ windowState = windowNS.getWindowState();
+ }
+ if (windowNS.getPortletNavigationalState() != null)
+ {
+ portletNS = windowNS.getPortletNavigationalState();
+ }
+ }
+
+ //
+ ParameterMap publicNS = pageNS.getPublicNavigationalState(portlet);
+
+ //
+ TestInstanceContext instanceContext = new TestInstanceContext(req,
portlet.getContext(), false);
+ TestRenderContext renderContext = new TestRenderContext(renderer, mode,
windowState, portletNS, publicNS, MARKUP_INFO);
+
+ //
+ RenderInvocation render = new RenderInvocation(renderContext);
+
+ //
+ render.setClientContext(new AbstractClientContext(req,
pageProperties.getCookies()));
+ render.setServerContext(new AbstractServerContext(req, resp));
+ render.setInstanceContext(instanceContext);
+ render.setUserContext(new AbstractUserContext(req));
+ render.setWindowContext(new
AbstractWindowContext(portlet.getContext().getId()));
+ render.setPortalContext(getPortalContext());
+ render.setSecurityContext(new AbstractSecurityContext(req));
+ render.setTarget(instanceContext.getTarget());
+
+ //
+ try
+ {
+ PortletInvocationResponse response = context.invoke(render);
+
+ if (response instanceof FragmentResponse)
+ {
+ FragmentResponse fragment = (FragmentResponse)response;
+
+ //
+ fragments.add(fragment);
+
+ //
+ ResponseProperties fragmentProperties = fragment.getProperties();
+ if (fragmentProperties != null)
+ {
+ renderProperties.append(fragmentProperties);
+ }
+ }
+ else if (response instanceof ErrorResponse)
+ {
+ ErrorResponse error = (ErrorResponse)response;
+ errors.add(error);
+ }
+
+ }
+ catch (PortletInvokerException e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+ catch (PortletInvokerException e)
+ {
+ // todo
+ e.printStackTrace();
+ }
+
+ // Now we combine the render properties with the page properties
+ pageProperties.append(renderProperties);
+
+ // Render the headers and cookies
+ renderTransportHeaders(pageProperties, resp);
+ renderCookies(pageProperties, resp);
+
+ //
+ resp.setContentType("text/html");
+ PrintWriter writer = resp.getWriter();
+ writer.print("<html>");
+
+ // Render the head contributions
+ writer.print("<head>");
+ MultiValuedPropertyMap<Element> markupHeaders =
pageProperties.getMarkupHeaders();
+ List<Element> headElement =
markupHeaders.getValues("javax.portlet.markup.head.element");
+ if (headElement != null)
+ {
+ for (Element headerValue : headElement)
+ {
+ try
+ {
+ String serializedElement = XMLTools.toString(headerValue);
+ writer.print(serializedElement);
+ }
+ catch (Exception e)
+ {
+ // todo
+ e.printStackTrace();
+ }
+ }
+ }
+ writer.print("</head>");
+
+
+ writer.print("<body>");
+ for (FragmentResponse fragment : fragments)
+ {
+ if (fragment.getType() != FragmentResponse.TYPE_EMPTY)
+ {
+ writer.print("<div class=\"portlet\">");
+ writer.print(fragment.getBuffer().getContent());
+ writer.print("</div>");
+ }
+ else
+ {
+ writer.print("<div/>");
+ }
+ }
+
+ for (ErrorResponse error : errors)
+ {
+ writer.print("<div class='error'>");
+ writer.print(error.toHTML());
+ writer.print("</div>");
+ }
+
+ writer.print("</body></html>");
+ }
+
+ void renderCookies(ResponseProperties pageProperties, HttpServletResponse resp)
+ {
+ List<Cookie> cookies = pageProperties.getCookies();
+ for (Cookie cookie : cookies)
+ {
+ resp.addCookie(cookie);
+ }
+ }
+
+ void renderTransportHeaders(ResponseProperties pageProperties, HttpServletResponse
resp)
+ {
+ MultiValuedPropertyMap<String> transportHeaders =
pageProperties.getTransportHeaders();
+ for (String headerName : transportHeaders.keySet())
+ {
+ for (String headerValue : transportHeaders.getValues(headerName))
+ {
+ resp.addHeader(headerName, headerValue);
+ }
+ }
+ }
+
+ void updateNavigationalState(
+ PortletControllerContext context,
+ String portletId,
+ UpdateNavigationalStateResponse update,
+ PageNavigationalState pageState) throws PortletInvokerException
+ {
+ Portlet portlet = context.getPortlet(portletId);
+
+ //
+ WindowNavigationalState windowNS =
pageState.getWindowNavigationalState(portletId);
+
+ //
+ if (windowNS == null)
+ {
+ windowNS = new WindowNavigationalState();
+ }
+
+ //
+ Mode mode = windowNS.getMode();
+ if (update.getMode() != null)
+ {
+ mode = update.getMode();
+ }
+ WindowState windowState = windowNS.getWindowState();
+ if (update.getWindowState() != null)
+ {
+ windowState = update.getWindowState();
+ }
+ StateString portletNS = windowNS.getPortletNavigationalState();
+ if (update.getNavigationalState() != null)
+ {
+ portletNS = update.getNavigationalState();
+ }
+ windowNS = new WindowNavigationalState(portletNS, mode, windowState);
+ pageState.setWindowNavigationalState(portletId, windowNS);
+
+ // Now update shared state scoped at page
+ Map<String, String[]> publicNS = update.getPublicNavigationalStateUpdates();
+ if (publicNS != null)
+ {
+ NavigationInfo navigationInfo = portlet.getInfo().getNavigation();
+ Map<QName, String[]> pageNS = pageState.getPageNavigationalState();
+ for (Map.Entry<String, String[]> entry : publicNS.entrySet())
+ {
+ String id = entry.getKey();
+ ParameterInfo parameterInfo = navigationInfo.getPublicParameter(id);
+
+ //
+ if (parameterInfo != null)
+ {
+ QName name = parameterInfo.getName();
+ String[] value = entry.getValue();
+ if (value.length > 0)
+ {
+ pageNS.put(name, value);
+ }
+ else
+ {
+ pageNS.remove(name);
+ }
+ }
+ }
+ }
+ }
+
+ PortalContext getPortalContext()
+ {
+ return portalContext;
+ }
+
+ /**
+ * Must be used as base class for high level response provided by the controller that
will be translated into
+ * something at the portal level. The goal is to avoid to manipulate the HTTP response
directly.
+ * <p/>
+ * The test bed will of course use the HTTP response but the Presentation Framework
will work differently.
+ * <p/>
+ * So we really need to abstract everything done with the HttpServletResponse and
*never* use it in the controller.
+ * <p/>
+ * Typical usage should be : ControllerResponse response =
controller.invoker(ControllerRequest request);
+ */
+ static class ControllerResponse
+ {
+
+ }
+
+ static class ControllerRequest
+ {
+ /** . */
+ final PortletControllerContext controllerContext;
+
+ ControllerRequest(PortletControllerContext controllerContext)
+ {
+ this.controllerContext = controllerContext;
+ }
+ }
+
+ static class ViewRequest extends ControllerRequest
+ {
+ ViewRequest(PortletControllerContext controllerContext)
+ {
+ super(controllerContext);
+ }
+ }
+
+ static class ContainerRequest extends ControllerRequest
+ {
+ /** . */
+ final String portletId;
+
+ ContainerRequest(PortletControllerContext controllerContext, String portletId)
+ {
+ super(controllerContext);
+
+ //
+ this.portletId = portletId;
+ }
+ }
+
+ static class PortletRequest extends ContainerRequest
+ {
+ /** . */
+ final PageNavigationalState pageState;
+
+ /** . */
+ final WindowNavigationalState windowNavigationalState;
+
+ PortletRequest(
+ PortletControllerContext controllerContext,
+ String portletId,
+ WindowNavigationalState windowNavigationalState,
+ PageNavigationalState pageState)
+ {
+ super(controllerContext, portletId);
+
+ //
+ this.windowNavigationalState = windowNavigationalState;
+ this.pageState = pageState;
+ }
+ }
+
+ static abstract class PortletResourceRequest extends ContainerRequest
+ {
+ /** . */
+ private static final Map<Class, CacheLevel> cacheability = new
HashMap<Class, CacheLevel>();
+
+ static
+ {
+ cacheability.put(FullScopedCacheablePortletResourceRequest.class,
CacheLevel.FULL);
+ cacheability.put(PortletScopedPortletResourceRequest.class,
CacheLevel.PORTLET);
+ cacheability.put(PageScopedFullPortletResourceRequest.class, CacheLevel.PAGE);
+ }
+
+ /** . */
+ final String resourceId;
+
+ /** . */
+ final StateString resourceState;
+
+ /** . */
+ final ParameterMap bodyParameters;
+
+ PortletResourceRequest(
+ PortletControllerContext controllerContext,
+ String portletId,
+ String resourceId,
+ StateString resourceState,
+ ParameterMap bodyParameters
+ )
+ {
+ super(controllerContext, portletId);
+
+ //
+ this.resourceId = resourceId;
+ this.resourceState = resourceState;
+ this.bodyParameters = bodyParameters;
+ }
+
+ public CacheLevel getCacheabilityType()
+ {
+ return cacheability.get(getClass());
+ }
+ }
+
+ static class FullScopedCacheablePortletResourceRequest extends PortletResourceRequest
+ {
+ FullScopedCacheablePortletResourceRequest(
+ PortletControllerContext controllerContext,
+ String portletId,
+ String resourceId,
+ StateString resourceState,
+ ParameterMap bodyParameters)
+ {
+ super(controllerContext, portletId, resourceId, resourceState, bodyParameters);
+ }
+ }
+
+ static class PortletScopedPortletResourceRequest extends
FullScopedCacheablePortletResourceRequest
+ {
+ /** . */
+ final WindowNavigationalState windowNavigationalState;
+
+ PortletScopedPortletResourceRequest(
+ PortletControllerContext controllerContext,
+ String portletId,
+ String resourceId,
+ StateString resourceState,
+ ParameterMap bodyParameters,
+ WindowNavigationalState windowNavigationalState)
+ {
+ super(controllerContext, portletId, resourceId, resourceState, bodyParameters);
+
+ //
+ this.windowNavigationalState = windowNavigationalState;
+ }
+ }
+
+ static class PageScopedFullPortletResourceRequest extends
PortletScopedPortletResourceRequest
+ {
+ /** . */
+ private final PageNavigationalState pageState;
+
+ PageScopedFullPortletResourceRequest(
+ PortletControllerContext controllerContext,
+ String portletId,
+ String resourceId,
+ StateString resourceState,
+ ParameterMap bodyParameters,
+ PageNavigationalState pageState,
+ WindowNavigationalState windowNavigationalState)
+ {
+ super(controllerContext, portletId, resourceId, resourceState, bodyParameters,
windowNavigationalState);
+
+ //
+ this.pageState = pageState;
+ }
+
+ PageNavigationalState getPageState()
+ {
+ return pageState;
+ }
+ }
+
+ static class PortletActionRequest extends PortletRequest
+ {
+ /** . */
+ final StateString interactionState;
+
+ /** . */
+ final ParameterMap bodyParameters;
+
+ public PortletActionRequest(
+ PortletControllerContext controllerContext,
+ String portletId,
+ StateString interactionState,
+ ParameterMap bodyParameters,
+ WindowNavigationalState windowNavigationalState,
+ PageNavigationalState pageState)
+ {
+ super(controllerContext, portletId, windowNavigationalState, pageState);
+
+ //
+ this.interactionState = interactionState;
+ this.bodyParameters = bodyParameters;
+ }
+ }
+
+ static class PortletRenderRequest extends PortletRequest
+ {
+ /** . */
+ final Map<String, String[]> publicNavigationalStateChanges;
+
+ public PortletRenderRequest(
+ PortletControllerContext controllerContext,
+ String portletId,
+ WindowNavigationalState windowNavigationalState,
+ Map<String, String[]> publicNavigationalStateChanges,
+ PageNavigationalState pageState)
+ {
+ super(controllerContext, portletId, windowNavigationalState, pageState);
+
+ //
+ this.publicNavigationalStateChanges = publicNavigationalStateChanges;
+ }
+ }
+}
Copied:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletControllerContext.java
(from rev 9688,
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletControllerContext.java)
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletControllerContext.java
(rev 0)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletControllerContext.java 2008-01-31
23:58:12 UTC (rev 9690)
@@ -0,0 +1,57 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.portlet.test.controller;
+
+import org.jboss.portal.portlet.Portlet;
+import org.jboss.portal.portlet.PortletInvokerException;
+import org.jboss.portal.portlet.invocation.PortletInvocation;
+import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
+import org.jboss.portal.portlet.test.event.EventControllerContext;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Collection;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public interface PortletControllerContext
+{
+
+ ServletContext getServletContext();
+
+ HttpServletRequest getClientRequest();
+
+ HttpServletResponse getClientResponse();
+
+ Portlet getPortlet(String portletId) throws PortletInvokerException;
+
+ PortletInvocationResponse invoke(PortletInvocation invocation) throws
PortletInvokerException;
+
+ Collection<Portlet> getPortlets() throws PortletInvokerException;
+
+ EventControllerContext getEventControllerContext();
+
+}
Copied:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletControllerContextImpl.java
(from rev 9688,
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletControllerContextImpl.java)
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletControllerContextImpl.java
(rev 0)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletControllerContextImpl.java 2008-01-31
23:58:12 UTC (rev 9690)
@@ -0,0 +1,282 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.portlet.test.controller;
+
+import org.jboss.portal.Mode;
+import org.jboss.portal.WindowState;
+import org.jboss.portal.common.io.IOTools;
+import org.jboss.portal.common.util.ParameterMap;
+import org.jboss.portal.common.util.Tools;
+import org.jboss.portal.portlet.ParametersStateString;
+import org.jboss.portal.portlet.Portlet;
+import org.jboss.portal.portlet.PortletContext;
+import org.jboss.portal.portlet.PortletInvoker;
+import org.jboss.portal.portlet.PortletInvokerException;
+import org.jboss.portal.portlet.StateString;
+import org.jboss.portal.portlet.cache.CacheLevel;
+import org.jboss.portal.portlet.invocation.PortletInvocation;
+import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
+import org.jboss.portal.portlet.test.PageNavigationalState;
+import org.jboss.portal.portlet.test.StringCodec;
+import static org.jboss.portal.portlet.test.URLParameterConstants.*;
+import org.jboss.portal.portlet.test.WindowNavigationalState;
+import org.jboss.portal.portlet.test.event.EventControllerContext;
+import org.jboss.portal.portlet.test.event.EventControllerContextImpl;
+import org.jboss.portal.web.Body;
+import org.jboss.portal.web.IllegalRequestException;
+import org.jboss.portal.web.WebRequest;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class PortletControllerContextImpl implements PortletControllerContext
+{
+
+ /** . */
+ public static final int NAV_SCOPE = 0;
+
+ /** . */
+ public static final int SESSION_SCOPE = 1;
+
+ /** . */
+ private final HttpServletRequest req;
+
+ /** . */
+ private final HttpServletResponse resp;
+
+ /** . */
+ private final ServletContext servletContext;
+
+ /** . */
+ private final PortletController.ControllerRequest request;
+
+ /** . */
+ private final PortletInvoker invoker;
+
+ /** . */
+ private final EventControllerContext eventControllerContext;
+
+ public PortletControllerContextImpl(
+ HttpServletRequest req,
+ HttpServletResponse resp,
+ ServletContext servletContext)
+ throws IllegalRequestException, IOException, ClassNotFoundException
+ {
+ PortletInvoker invoker =
(PortletInvoker)servletContext.getAttribute("ConsumerPortletInvoker");
+
+ // The nav state provided with the request
+ PageNavigationalState pageState = null;
+
+ // The request decoded if not null
+ PortletController.ControllerRequest request = null;
+
+ // Process
+ String pathInfo = req.getPathInfo();
+ if (pathInfo != null && pathInfo.startsWith("/") &&
pathInfo.length() > 1)
+ {
+ // Get the target portlet
+ String targetId = StringCodec.decode(pathInfo.substring(1));
+
+ // Helper
+ WebRequest ri = new WebRequest(req);
+
+ // Unmarshall portal navigational state if it is provided
+ String context = req.getParameter(PAGE_STATE);
+ if (context != null)
+ {
+ byte[] bytes = Tools.fromHexString(context);
+ pageState = PageNavigationalState.unserialize(bytes);
+ }
+
+ //
+ Mode mode = null;
+ if (req.getParameter(MODE) != null)
+ {
+ mode = Mode.create(req.getParameter(MODE));
+ }
+
+ //
+ WindowState windowState = null;
+ if (req.getParameter(WINDOW_STATE) != null)
+ {
+ windowState = WindowState.create(req.getParameter(WINDOW_STATE));
+ }
+
+ //
+ String navigationalStateString = req.getParameter(NAVIGATIONAL_STATE);
+ StateString navigationalState = null;
+ if (navigationalStateString != null)
+ {
+ navigationalState = ParametersStateString.create(navigationalStateString);
+ }
+
+ //
+ WindowNavigationalState windowNavigationalState = new
WindowNavigationalState(navigationalState, mode, windowState);
+
+ //
+ String type = req.getParameter(LIFECYCLE_TYPE);
+ if (RESOURCE_LIFECYCLE.equals(type))
+ {
+ StateString resourceState =
ParametersStateString.create(req.getParameter(RESOURCE_STATE));
+ String resourceId = req.getParameter(RESOURCE_ID);
+
+ //
+ ParameterMap formParameters = null;
+ if (ri.getBody() instanceof Body.Form)
+ {
+ formParameters = ((Body.Form)ri.getBody()).getParameters();
+ }
+
+ //
+ CacheLevel resourceCacheLevel =
CacheLevel.valueOf(req.getParameter(RESOURCE_CACHEABILITY));
+
+ //
+ switch (resourceCacheLevel)
+ {
+ case FULL:
+ request = new
PortletController.FullScopedCacheablePortletResourceRequest(
+ this,
+ targetId,
+ resourceId,
+ resourceState,
+ formParameters);
+ break;
+ case PORTLET:
+ request = new PortletController.PortletScopedPortletResourceRequest(
+ this,
+ targetId,
+ resourceId,
+ resourceState,
+ formParameters,
+ windowNavigationalState);
+ break;
+ case PAGE:
+ request = new PortletController.PageScopedFullPortletResourceRequest(
+ this,
+ targetId,
+ resourceId,
+ resourceState,
+ formParameters,
+ pageState,
+ windowNavigationalState);
+ break;
+ }
+ }
+ else
+ {
+ if (ACTION_LIFECYCLE.equals(type))
+ {
+ ParameterMap formParameters = null;
+ if (ri.getBody() instanceof Body.Form)
+ {
+ formParameters = ((Body.Form)ri.getBody()).getParameters();
+ }
+
+ //
+ StateString interactionState =
ParametersStateString.create(req.getParameter(INTERACTION_STATE));
+
+ //
+ request = new PortletController.PortletActionRequest(
+ this,
+ targetId,
+ interactionState,
+ formParameters,
+ windowNavigationalState,
+ pageState);
+ }
+ else
+ {
+ Map<String, String[]> publicNavigationalStateChanges =
(Map<String,
String[]>)IOTools.unserialize(Tools.fromHexString(req.getParameter(PUBLIC_NAVIGATIONAL_STATE_CHANGES)));
+
+ //
+ request = new PortletController.PortletRenderRequest(
+ this,
+ targetId,
+ windowNavigationalState,
+ publicNavigationalStateChanges,
+ pageState);
+ }
+ }
+ }
+ else
+ {
+ request = new PortletController.ViewRequest(this);
+ }
+
+ //
+ this.request = request;
+ this.req = req;
+ this.resp = resp;
+ this.servletContext = servletContext;
+ this.invoker = invoker;
+ this.eventControllerContext = new EventControllerContextImpl(invoker);
+ }
+
+ public PortletController.ControllerRequest getRequest()
+ {
+ return request;
+ }
+
+ public ServletContext getServletContext()
+ {
+ return servletContext;
+ }
+
+ public HttpServletRequest getClientRequest()
+ {
+ return req;
+ }
+
+ public HttpServletResponse getClientResponse()
+ {
+ return resp;
+ }
+
+ public Portlet getPortlet(String portletId) throws PortletInvokerException
+ {
+ return invoker.getPortlet(PortletContext.createPortletContext(portletId));
+ }
+
+ public PortletInvocationResponse invoke(PortletInvocation invocation) throws
PortletInvokerException
+ {
+ return invoker.invoke(invocation);
+ }
+
+ public Collection<Portlet> getPortlets() throws PortletInvokerException
+ {
+ return invoker.getPortlets();
+ }
+
+ public EventControllerContext getEventControllerContext()
+ {
+ return eventControllerContext;
+ }
+}
Added:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletRequestHandler.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletRequestHandler.java
(rev 0)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletRequestHandler.java 2008-01-31
23:58:12 UTC (rev 9690)
@@ -0,0 +1,287 @@
+/*
+ * Copyright (c) 2008, Your Corporation. All Rights Reserved.
+ */
+
+package org.jboss.portal.portlet.test.controller;
+
+import org.jboss.portal.Mode;
+import org.jboss.portal.WindowState;
+import org.jboss.portal.common.util.ParameterMap;
+import org.jboss.portal.portlet.Portlet;
+import org.jboss.portal.portlet.PortletContext;
+import org.jboss.portal.portlet.PortletInvokerException;
+import org.jboss.portal.portlet.impl.spi.AbstractClientContext;
+import org.jboss.portal.portlet.impl.spi.AbstractRequestContext;
+import org.jboss.portal.portlet.impl.spi.AbstractSecurityContext;
+import org.jboss.portal.portlet.impl.spi.AbstractServerContext;
+import org.jboss.portal.portlet.impl.spi.AbstractUserContext;
+import org.jboss.portal.portlet.impl.spi.AbstractWindowContext;
+import org.jboss.portal.portlet.invocation.ActionInvocation;
+import org.jboss.portal.portlet.invocation.response.ErrorResponse;
+import org.jboss.portal.portlet.invocation.response.HTTPRedirectionResponse;
+import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
+import org.jboss.portal.portlet.invocation.response.ResponseProperties;
+import org.jboss.portal.portlet.invocation.response.StateResponse;
+import org.jboss.portal.portlet.invocation.response.UpdateNavigationalStateResponse;
+import org.jboss.portal.portlet.test.PageNavigationalState;
+import org.jboss.portal.portlet.test.PortletURLRenderer;
+import org.jboss.portal.portlet.test.TestActionContext;
+import org.jboss.portal.portlet.test.TestInstanceContext;
+import static org.jboss.portal.portlet.test.controller.PortletController.*;
+import org.jboss.portal.portlet.test.event.Event;
+import org.jboss.portal.portlet.test.event.EventControllerContext;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class PortletRequestHandler extends RequestHandler<PortletRequest>
+{
+ public PortletRequestHandler(PortletController controller)
+ {
+ super(controller);
+ }
+
+ protected PortletRequest downcastRequest(ControllerRequest request)
+ {
+ if (!(request instanceof PortletRequest))
+ {
+ throw new IllegalArgumentException("Request " + request + "
cannot be handled by this handler");
+ }
+
+ return (PortletRequest)request;
+ }
+
+ void processResponse(PortletRequest request, PortletInvocationResponse response)
throws IOException, PortletInvokerException
+ {
+ PortletRequest portletRequest = downcastRequest(request);
+
+ PortletControllerContext context = portletRequest.controllerContext;
+
+ //
+ PortletContext portletContext =
portletRequest.controllerContext.getPortlet(portletRequest.portletId).getContext();
+ //
+ ResponseProperties requestProperties = new ResponseProperties();
+
+ //
+ if (response instanceof ErrorResponse)
+ {
+ ErrorResponse error = (ErrorResponse)response;
+
+ //
+ if (error.getCause() != null)
+ {
+ log.error("Portlet action threw an error: " + error.getMessage(),
error.getCause());
+ }
+ else
+ {
+ log.error("Portlet action threw an error: " + error.getMessage());
+ }
+
+ //
+ context.getClientResponse().sendError(404, error.getMessage());
+ }
+ else if (response instanceof StateResponse)
+ {
+ // Update portlet NS
+ if (response instanceof UpdateNavigationalStateResponse)
+ {
+ UpdateNavigationalStateResponse updateResponse =
(UpdateNavigationalStateResponse)response;
+ controller.updateNavigationalState(context, portletRequest.portletId,
updateResponse, portletRequest.pageState);
+
+ //
+ ResponseProperties update = updateResponse.getProperties();
+ if (update != null)
+ {
+ requestProperties.append(update);
+ }
+ }
+
+ //
+ EventControllerContext eventCC = context.getEventControllerContext();
+ StateResponse stateResponse = (StateResponse)response;
+
+ // Create event list and feed it with the events that may have been produced
+ LinkedList<Event> eventQueue = new LinkedList<Event>();
+ for (StateResponse.Event portletEvent : stateResponse.getEvents())
+ {
+ Event producedEvent = new Event(portletEvent.getName(),
portletEvent.getPayload(), portletContext);
+ List<Event> consumedEvents = eventCC.getConsumedEvents(producedEvent);
+ eventQueue.addAll(consumedEvents);
+ }
+
+ // Deliver events
+ while (eventQueue.size() > 0)
+ {
+ Event event = eventQueue.removeFirst();
+
+ //
+ PortletInvocationResponse eventResponse = controller.deliverEvent(context,
event, portletRequest.pageState, requestProperties.getCookies());
+
+ //
+ if (eventResponse instanceof UpdateNavigationalStateResponse)
+ {
+ UpdateNavigationalStateResponse update =
(UpdateNavigationalStateResponse)eventResponse;
+
+ // Update ns
+ controller.updateNavigationalState(context,
event.getPortletContext().getId(), update, portletRequest.pageState);
+
+ // Add events if any
+ for (StateResponse.Event portletEvent : update.getEvents())
+ {
+ Event producedEvent = new Event(portletEvent.getName(),
portletEvent.getPayload(), portletContext);
+ List<Event> consumedEvents =
eventCC.getConsumedEvents(producedEvent);
+ eventQueue.addAll(consumedEvents);
+ }
+
+ //
+ ResponseProperties updateProperties = update.getProperties();
+ if (updateProperties != null)
+ {
+ requestProperties.append(updateProperties);
+ }
+ }
+ else if (eventResponse instanceof ErrorResponse)
+ {
+ // Do something here !!!!
+ }
+ else
+ {
+ // Do something here !!!!
+ }
+ }
+
+ if (response instanceof HTTPRedirectionResponse)
+ {
+ HTTPRedirectionResponse redirection = (HTTPRedirectionResponse)response;
+ context.getClientResponse().sendRedirect(redirection.getLocation());
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("Not yet handled " + response);
+ }
+
+ /*
+ if (response instanceof RenderResponse && redirectAfterAction)
+ {
+ PortletURLRenderer renderer = new PortletURLRenderer(navState, portlet,
req, resp);
+ final RenderResponse render = (RenderResponse)response;
+ String location = renderer.renderURL(new RenderURL()
+ {
+ public StateString getNavigationalState()
+ {
+ return render.getNavigationalState();
+ }
+ public Mode getMode()
+ {
+ return render.getMode();
+ }
+ public WindowState getWindowState()
+ {
+ return render.getWindowState();
+ }
+ }, null, null, true);
+ response = new HTTPRedirectionResponse(location);
+ }
+ */
+
+ //
+ controller.render(context, requestProperties, portletRequest.pageState);
+ }
+
+ PortletInvocationResponse invoke(PortletRequest controllerRequest) throws
PortletInvokerException
+ {
+ PortletRequest portletRequest = downcastRequest(controllerRequest);
+
+ PortletControllerContext context = portletRequest.controllerContext;
+ Portlet portlet = context.getPortlet(portletRequest.portletId);
+ HttpServletRequest req = context.getClientRequest();
+ HttpServletResponse resp = context.getClientResponse();
+
+ //
+ if (portletRequest instanceof PortletRenderRequest)
+ {
+ PortletRenderRequest portletRenderRequest =
(PortletRenderRequest)portletRequest;
+
+ //
+ UpdateNavigationalStateResponse updateNavigationalState = new
UpdateNavigationalStateResponse();
+
updateNavigationalState.setMode(portletRenderRequest.windowNavigationalState.getMode());
+
updateNavigationalState.setWindowState(portletRenderRequest.windowNavigationalState.getWindowState());
+
updateNavigationalState.setNavigationalState(portletRenderRequest.windowNavigationalState.getPortletNavigationalState());
+
updateNavigationalState.setPublicNavigationalStateUpdates(portletRenderRequest.publicNavigationalStateChanges);
+
+ //
+ return updateNavigationalState;
+ }
+ else
+ {
+ PortletActionRequest portletActionRequest =
(PortletActionRequest)portletRequest;
+
+ //
+ PageNavigationalState pageNS = portletActionRequest.pageState;
+
+ //
+ Mode mode = portletActionRequest.windowNavigationalState.getMode();
+ if (mode == null)
+ {
+ mode = Mode.VIEW;
+ }
+
+ //
+ WindowState windowState =
portletActionRequest.windowNavigationalState.getWindowState();
+ if (windowState == null)
+ {
+ windowState = WindowState.NORMAL;
+ }
+
+ //
+ ParameterMap publicNS = pageNS.getPublicNavigationalState(portlet);
+
+ //
+ PortletURLRenderer renderer = new PortletURLRenderer(pageNS,
portletActionRequest.windowNavigationalState, portlet, req, resp);
+
+ //
+ TestInstanceContext instanceContext = new TestInstanceContext(req,
portlet.getContext(), true);
+ TestActionContext actionContext = new TestActionContext(
+ renderer,
+ mode,
+ windowState,
+ portletActionRequest.windowNavigationalState.getPortletNavigationalState(),
+ publicNS,
+ MARKUP_INFO,
+ portletActionRequest.interactionState,
+ portletActionRequest.bodyParameters != null ?
ParameterMap.clone(portletActionRequest.bodyParameters) : null);
+ ActionInvocation actionInvocation = new ActionInvocation(actionContext);
+
+ //
+ actionInvocation.setClientContext(new AbstractClientContext(req));
+ actionInvocation.setServerContext(new AbstractServerContext(req, resp));
+ actionInvocation.setInstanceContext(instanceContext);
+ actionInvocation.setUserContext(new AbstractUserContext(req));
+ actionInvocation.setWindowContext(new
AbstractWindowContext(portlet.getContext().getId()));
+ actionInvocation.setPortalContext(controller.getPortalContext());
+ actionInvocation.setSecurityContext(new AbstractSecurityContext(req));
+ actionInvocation.setRequestContext(new AbstractRequestContext(req));
+ actionInvocation.setTarget(instanceContext.getTarget());
+
+ //
+ try
+ {
+ return context.invoke(actionInvocation);
+ }
+ catch (PortletInvokerException e)
+ {
+ return null;
+ }
+ }
+ }
+
+}
Property changes on:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletRequestHandler.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletResourceRequestHandler.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletResourceRequestHandler.java
(rev 0)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletResourceRequestHandler.java 2008-01-31
23:58:12 UTC (rev 9690)
@@ -0,0 +1,243 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+
+package org.jboss.portal.portlet.test.controller;
+
+import org.jboss.portal.Mode;
+import org.jboss.portal.WindowState;
+import org.jboss.portal.common.io.IOTools;
+import org.jboss.portal.common.util.ParameterMap;
+import org.jboss.portal.portlet.Portlet;
+import org.jboss.portal.portlet.PortletInvokerException;
+import org.jboss.portal.portlet.StateString;
+import org.jboss.portal.portlet.cache.CacheLevel;
+import org.jboss.portal.portlet.impl.spi.AbstractClientContext;
+import org.jboss.portal.portlet.impl.spi.AbstractRequestContext;
+import org.jboss.portal.portlet.impl.spi.AbstractSecurityContext;
+import org.jboss.portal.portlet.impl.spi.AbstractServerContext;
+import org.jboss.portal.portlet.impl.spi.AbstractUserContext;
+import org.jboss.portal.portlet.impl.spi.AbstractWindowContext;
+import org.jboss.portal.portlet.invocation.ResourceInvocation;
+import org.jboss.portal.portlet.invocation.response.FragmentResponse;
+import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
+import org.jboss.portal.portlet.invocation.response.ResponseProperties;
+import org.jboss.portal.portlet.spi.ResourceInvocationContext;
+import org.jboss.portal.portlet.test.PageNavigationalState;
+import org.jboss.portal.portlet.test.PortletURLRenderer;
+import org.jboss.portal.portlet.test.ResourceTestContext;
+import org.jboss.portal.portlet.test.TestInstanceContext;
+import org.jboss.portal.portlet.test.WindowNavigationalState;
+import static org.jboss.portal.portlet.test.controller.PortletController.*;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class PortletResourceRequestHandler extends
RequestHandler<PortletResourceRequest>
+{
+ public PortletResourceRequestHandler(PortletController controller)
+ {
+ super(controller);
+ }
+
+ void processResponse(PortletResourceRequest request, PortletInvocationResponse
response) throws IOException, PortletInvokerException
+ {
+ HttpServletResponse resp = request.controllerContext.getClientResponse();
+
+ //
+ if (response instanceof FragmentResponse)
+ {
+ FragmentResponse fragment = (FragmentResponse)response;
+ ResponseProperties props = fragment.getProperties();
+
+ //
+ if (props != null)
+ {
+ controller.renderTransportHeaders(props, resp);
+ controller.renderCookies(props, resp);
+ }
+
+ //
+ if (fragment.getType() == FragmentResponse.TYPE_EMPTY)
+ {
+ resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
+ }
+ else
+ {
+ String contentType = fragment.getContentType();
+ if (contentType != null)
+ {
+ resp.setContentType(contentType);
+ }
+
+ //
+ if (fragment.getType() == FragmentResponse.TYPE_BYTES)
+ {
+ ServletOutputStream out = null;
+ try
+ {
+ out = resp.getOutputStream();
+ out.write(fragment.getBytes().toByteArray());
+ }
+ finally
+ {
+ IOTools.safeClose(out);
+ }
+ }
+ else
+ {
+ Writer writer = null;
+ try
+ {
+ writer = resp.getWriter();
+ writer.write(fragment.getChars().toString());
+ }
+ finally
+ {
+ writer.close();
+ }
+ }
+ }
+ }
+ else
+ {
+ // todo
+ }
+ }
+
+ PortletInvocationResponse invoke(PortletResourceRequest portletResourceRequest) throws
PortletInvokerException
+ {
+
+ //
+ Mode mode = null;
+ WindowState windowState = null;
+ PageNavigationalState pageNS = null;
+ ParameterMap publicNS = null;
+ StateString portletNS = null;
+ CacheLevel cacheability;
+
+ PortletControllerContext context = portletResourceRequest.controllerContext;
+ Portlet portlet = context.getPortlet(portletResourceRequest.portletId);
+ HttpServletRequest req = context.getClientRequest();
+ HttpServletResponse resp = context.getClientResponse();
+
+ //
+ if (portletResourceRequest instanceof PortletScopedPortletResourceRequest)
+ {
+ PortletScopedPortletResourceRequest portletScopedRequest =
(PortletScopedPortletResourceRequest)portletResourceRequest;
+ mode = portletScopedRequest.windowNavigationalState.getMode();
+ windowState = portletScopedRequest.windowNavigationalState.getWindowState();
+ portletNS =
portletScopedRequest.windowNavigationalState.getPortletNavigationalState();
+
+ //
+ if (portletResourceRequest instanceof PageScopedFullPortletResourceRequest)
+ {
+ PageScopedFullPortletResourceRequest pageScopedRequest =
(PageScopedFullPortletResourceRequest)portletResourceRequest;
+ pageNS = pageScopedRequest.getPageState();
+ cacheability = CacheLevel.PAGE;
+
+ //
+ if (pageNS != null)
+ {
+ publicNS = pageNS.getPublicNavigationalState(portlet);
+ }
+ }
+ else
+ {
+ cacheability = CacheLevel.PORTLET;
+ }
+ }
+ else
+ {
+ cacheability = CacheLevel.FULL;
+ }
+
+ //
+ if (mode == null)
+ {
+ mode = Mode.VIEW;
+ }
+ if (windowState == null)
+ {
+ windowState = WindowState.NORMAL;
+ }
+
+ //
+ WindowNavigationalState windowNS = new WindowNavigationalState(portletNS, mode,
windowState);
+
+ //
+ PortletURLRenderer renderer = new PortletURLRenderer(pageNS, windowNS, portlet,
req, resp);
+
+ //
+ TestInstanceContext instanceContext = new TestInstanceContext(req,
portlet.getContext(), true);
+ ResourceInvocationContext resourceInvocationContext = new ResourceTestContext(
+ portletResourceRequest.resourceId,
+ cacheability,
+ renderer,
+ mode,
+ windowState,
+ portletNS,
+ publicNS,
+ portletResourceRequest.resourceState,
+ portletResourceRequest.bodyParameters != null ?
ParameterMap.clone(portletResourceRequest.bodyParameters) : null,
+ MARKUP_INFO);
+ ResourceInvocation resourceInvocation = new
ResourceInvocation(resourceInvocationContext);
+
+ //
+ resourceInvocation.setClientContext(new AbstractClientContext(req));
+ resourceInvocation.setServerContext(new AbstractServerContext(req, resp));
+ resourceInvocation.setInstanceContext(instanceContext);
+ resourceInvocation.setUserContext(new AbstractUserContext(req));
+ resourceInvocation.setWindowContext(new
AbstractWindowContext(portlet.getContext().getId()));
+ resourceInvocation.setPortalContext(controller.getPortalContext());
+ resourceInvocation.setSecurityContext(new AbstractSecurityContext(req));
+ resourceInvocation.setRequestContext(new AbstractRequestContext(req));
+ resourceInvocation.setTarget(instanceContext.getTarget());
+
+ //
+ try
+ {
+ return context.invoke(resourceInvocation);
+ }
+ catch (PortletInvokerException e)
+ {
+ return null;
+ }
+ }
+
+ PortletResourceRequest downcastRequest(ControllerRequest request)
+ {
+ if (!(request instanceof PortletResourceRequest))
+ {
+ throw new IllegalArgumentException("Request " + request + "
cannot be handled by this handler");
+ }
+
+ return (PortletResourceRequest)request;
+ }
+}
Property changes on:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletResourceRequestHandler.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/RequestHandler.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/RequestHandler.java
(rev 0)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/RequestHandler.java 2008-01-31
23:58:12 UTC (rev 9690)
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2008, Your Corporation. All Rights Reserved.
+ */
+
+package org.jboss.portal.portlet.test.controller;
+
+import org.apache.log4j.Logger;
+import org.jboss.portal.portlet.PortletInvokerException;
+import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
+import static
org.jboss.portal.portlet.test.controller.PortletController.ControllerRequest;
+
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public abstract class RequestHandler<T extends ControllerRequest>
+{
+ protected PortletController controller;
+
+ protected static final Logger log = Logger.getLogger(RequestHandler.class);
+
+ public RequestHandler(PortletController controller)
+ {
+ this.controller = controller;
+ }
+
+ public void handle(ControllerRequest request) throws PortletInvokerException,
IOException
+ {
+ T req = downcastRequest(request);
+ PortletInvocationResponse response = invoke(req);
+
+ processResponse(req, response);
+ }
+
+ abstract void processResponse(T request, PortletInvocationResponse response) throws
IOException, PortletInvokerException;
+
+ abstract PortletInvocationResponse invoke(T controllerRequest) throws
PortletInvokerException;
+
+ abstract T downcastRequest(ControllerRequest request);
+}
Property changes on:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/RequestHandler.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/ViewRequestHandler.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/ViewRequestHandler.java
(rev 0)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/ViewRequestHandler.java 2008-01-31
23:58:12 UTC (rev 9690)
@@ -0,0 +1,65 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+
+package org.jboss.portal.portlet.test.controller;
+
+import org.jboss.portal.portlet.PortletInvokerException;
+import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
+import org.jboss.portal.portlet.invocation.response.ResponseProperties;
+import org.jboss.portal.portlet.test.PageNavigationalState;
+import static
org.jboss.portal.portlet.test.controller.PortletController.ControllerRequest;
+import static org.jboss.portal.portlet.test.controller.PortletController.ViewRequest;
+
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class ViewRequestHandler extends RequestHandler<ViewRequest>
+{
+ public ViewRequestHandler(PortletController controller)
+ {
+ super(controller);
+ }
+
+ void processResponse(ViewRequest request, PortletInvocationResponse response) throws
IOException, PortletInvokerException
+ {
+ controller.render(request.controllerContext, new ResponseProperties(), new
PageNavigationalState());
+ }
+
+ PortletInvocationResponse invoke(ViewRequest controllerRequest) throws
PortletInvokerException
+ {
+ return null;
+ }
+
+ ViewRequest downcastRequest(ControllerRequest request)
+ {
+ if (!(request instanceof ViewRequest))
+ {
+ throw new IllegalArgumentException("Request " + request + "
cannot be handled by this handler");
+ }
+
+ return (ViewRequest)request;
+ }
+}
Property changes on:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/ViewRequestHandler.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/SimplePortalServlet.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/SimplePortalServlet.java 2008-01-31
23:10:24 UTC (rev 9689)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/SimplePortalServlet.java 2008-01-31
23:58:12 UTC (rev 9690)
@@ -22,8 +22,8 @@
******************************************************************************/
package org.jboss.portal.simple;
-import org.jboss.portal.portlet.test.PortletController;
-import org.jboss.portal.portlet.test.PortletControllerContextImpl;
+import org.jboss.portal.portlet.test.controller.PortletController;
+import org.jboss.portal.portlet.test.controller.PortletControllerContextImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;