tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject cvs commit: jakarta-tapestry/portlet/src/java/org/apache/tapestry/portlet RenderService.java
Date Thu, 02 Jun 2005 19:06:54 GMT
hlship      2005/06/02 12:06:54

  Modified:    portlet/src/descriptor/META-INF
                        tapestry.portlet.services.xml
               portlet/src/java/org/apache/tapestry/portlet
                        RenderService.java
  Added:       portlet/src/test/org/apache/tapestry/portlet
                        TestRenderService.java
  Log:
  Properly identify when portal mode or window state has changed.
  
  Revision  Changes    Path
  1.1                  jakarta-tapestry/portlet/src/test/org/apache/tapestry/portlet/TestRenderService.java
  
  Index: TestRenderService.java
  ===================================================================
  package org.apache.tapestry.portlet;
  
  import javax.portlet.PortletMode;
  import javax.portlet.PortletRequest;
  import javax.portlet.WindowState;
  
  import org.apache.hivemind.test.HiveMindTestCase;
  import org.apache.tapestry.IRequestCycle;
  import org.apache.tapestry.services.ServiceConstants;
  import org.easymock.MockControl;
  
  /**
   * Test for {@link org.apache.tapestry.portlet.RenderService}.
   * 
   * @author Howard M. Lewis Ship
   * @since 4.0
   */
  public class TestRenderService extends HiveMindTestCase
  {
      public void testGetLinkUnsupported()
      {
          RenderService rs = new RenderService();
  
          try
          {
              rs.getLink(null, null);
              unreachable();
          }
          catch (UnsupportedOperationException ex)
          {
              // Hm. Not actually the best message!
              assertEquals("Method getLink() is not supported for portlet requests.", ex.getMessage());
          }
      }
  
      public void testNormal() throws Exception
      {
          MockControl cyclec = newControl(IRequestCycle.class);
          IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
  
          MockControl requestc = newControl(PortletRequest.class);
          PortletRequest request = (PortletRequest) requestc.getMock();
  
          PortletRenderer renderer = (PortletRenderer) newMock(PortletRenderer.class);
  
          trainGetParameter(cyclec, cycle, PortletConstants.PORTLET_MODE, "view");
          trainGetParameter(cyclec, cycle, PortletConstants.WINDOW_STATE, "normal");
  
          trainGetPortletMode(requestc, request, PortletMode.VIEW);
          trainGetWindowState(requestc, request, WindowState.NORMAL);
  
          trainGetParameter(cyclec, cycle, ServiceConstants.PAGE, "Fred");
  
          renderer.renderPage(cycle, "Fred");
  
          replayControls();
  
          RenderService rs = new RenderService();
          rs.setPortletRenderer(renderer);
          rs.setRequest(request);
  
          rs.service(cycle);
  
          verifyControls();
      }
  
      public void testPortletModeChanged() throws Exception
      {
          MockControl cyclec = newControl(IRequestCycle.class);
          IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
  
          MockControl requestc = newControl(PortletRequest.class);
          PortletRequest request = (PortletRequest) requestc.getMock();
  
          PortletRenderer renderer = (PortletRenderer) newMock(PortletRenderer.class);
  
          PortletPageResolver resolver = newResolver(cycle, "Barney");
  
          trainGetParameter(cyclec, cycle, PortletConstants.PORTLET_MODE, "view");
          trainGetParameter(cyclec, cycle, PortletConstants.WINDOW_STATE, "normal");
  
          trainGetPortletMode(requestc, request, PortletMode.EDIT);
  
          renderer.renderPage(cycle, "Barney");
  
          replayControls();
  
          RenderService rs = new RenderService();
          rs.setPortletRenderer(renderer);
          rs.setRequest(request);
          rs.setPageResolver(resolver);
  
          rs.service(cycle);
  
          verifyControls();
      }
  
      public void testWindowStateChanged() throws Exception
      {
          MockControl cyclec = newControl(IRequestCycle.class);
          IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
  
          MockControl requestc = newControl(PortletRequest.class);
          PortletRequest request = (PortletRequest) requestc.getMock();
  
          PortletRenderer renderer = (PortletRenderer) newMock(PortletRenderer.class);
  
          PortletPageResolver resolver = newResolver(cycle, "Wilma");
  
          trainGetParameter(cyclec, cycle, PortletConstants.PORTLET_MODE, "view");
          trainGetParameter(cyclec, cycle, PortletConstants.WINDOW_STATE, "normal");
  
          trainGetPortletMode(requestc, request, PortletMode.VIEW);
          trainGetWindowState(requestc, request, WindowState.MAXIMIZED);
  
          renderer.renderPage(cycle, "Wilma");
  
          replayControls();
  
          RenderService rs = new RenderService();
          rs.setPortletRenderer(renderer);
          rs.setRequest(request);
          rs.setPageResolver(resolver);
  
          rs.service(cycle);
  
          verifyControls();
      }
  
      private PortletPageResolver newResolver(IRequestCycle cycle, String pageName)
      {
          MockControl control = newControl(PortletPageResolver.class);
          PortletPageResolver resolver = (PortletPageResolver) control.getMock();
  
          resolver.getPageNameForRequest(cycle);
          control.setReturnValue(pageName);
  
          return resolver;
      }
  
      private void trainGetWindowState(MockControl control, PortletRequest request, WindowState
state)
      {
          request.getWindowState();
          control.setReturnValue(state);
      }
  
      private void trainGetPortletMode(MockControl control, PortletRequest request, PortletMode
mode)
      {
          request.getPortletMode();
          control.setReturnValue(mode);
      }
  
      private void trainGetParameter(MockControl control, IRequestCycle cycle, String name,
              String value)
      {
          cycle.getParameter(name);
          control.setReturnValue(value);
      }
  }
  
  
  
  1.14      +2 -0      jakarta-tapestry/portlet/src/descriptor/META-INF/tapestry.portlet.services.xml
  
  Index: tapestry.portlet.services.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/portlet/src/descriptor/META-INF/tapestry.portlet.services.xml,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- tapestry.portlet.services.xml	2 Jun 2005 13:35:10 -0000	1.13
  +++ tapestry.portlet.services.xml	2 Jun 2005 19:06:54 -0000	1.14
  @@ -95,6 +95,8 @@
     <invoke-factory>
       <construct class="RenderService">
         <set-service property="portletRenderer" service-id="PortletRenderer"/>
  +      <set-service property="request" service-id="tapestry.portlet.PortletRequest"/>
  +      <set-service property="pageResolver" service-id="tapestry.portlet.resolver.PortletPageResolver"/>
       </construct>
     </invoke-factory>  
    </service-point>
  
  
  
  1.5       +53 -2     jakarta-tapestry/portlet/src/java/org/apache/tapestry/portlet/RenderService.java
  
  Index: RenderService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/portlet/src/java/org/apache/tapestry/portlet/RenderService.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- RenderService.java	18 Apr 2005 17:08:38 -0000	1.4
  +++ RenderService.java	2 Jun 2005 19:06:54 -0000	1.5
  @@ -16,6 +16,8 @@
   
   import java.io.IOException;
   
  +import javax.portlet.PortletRequest;
  +
   import org.apache.tapestry.IRequestCycle;
   import org.apache.tapestry.engine.IEngineService;
   import org.apache.tapestry.engine.ILink;
  @@ -23,7 +25,19 @@
   
   /**
    * Responsible for rendering out a page; a Portlet render URL is built during action processing
that
  - * stores the active page; this is the page that will be rendered.
  + * stores the active page; this is the page that will be rendered. The render service is
(typically)
  + * the only service that operates during a portlet RenderRequest. All other services will
be an
  + * ActionRequest that (via {@link org.apache.tapestry.portlet.PortletResponseRenderer},
writes
  + * query parameters to activate this service during the render request.
  + * <p>
  + * Problematic is is anything related to the portlet mode or window state. As per the Portlet
spec,
  + * when the user clicks the "help" or "edit" buttons (or the minimize, maximize, etc.),
this causes
  + * a new RenderRequest, but explicitly keeps the render parameters set by the most recent
  + * ActionRequest. But what Tapestry needs is to detect that the mode or state has changed
and select
  + * a different page to render the response. So we store the mode and state in effect when
the
  + * ActionRequest executed as two more query parameters, and detect changes to mode and
state that
  + * way. If there is a change, then we ignore the page query parameter and use the
  + * {@link PortletPageResolver} to figure out the correct page to display instead.
    * 
    * @author Howard M. Lewis Ship
    * @since 4.0
  @@ -31,8 +45,12 @@
    */
   public class RenderService implements IEngineService
   {
  +    private PortletRequest _request;
  +
       private PortletRenderer _portletRenderer;
   
  +    private PortletPageResolver _pageResolver;
  +
       public ILink getLink(IRequestCycle cycle, Object parameter)
       {
           throw new UnsupportedOperationException(PortletMessages.unsupportedMethod("getLink"));
  @@ -40,11 +58,34 @@
   
       public void service(IRequestCycle cycle) throws IOException
       {
  -        String pageName = cycle.getParameter(ServiceConstants.PAGE);
  +        String pageName = getPageNameToRender(cycle);
   
           _portletRenderer.renderPage(cycle, pageName);
       }
   
  +    private String getPageNameToRender(IRequestCycle cycle)
  +    {
  +        if (isStateChange(cycle))
  +            return _pageResolver.getPageNameForRequest(cycle);
  +
  +        return cycle.getParameter(ServiceConstants.PAGE);
  +    }
  +
  +    /**
  +     * Returns true if the portlet mode or the window state has changed since. The values
stored
  +     * previously (during an action request) are compared to the current values.
  +     */
  +
  +    boolean isStateChange(IRequestCycle cycle)
  +    {
  +        String expectedPortletMode = cycle.getParameter(PortletConstants.PORTLET_MODE);
  +        String expectedWindowState = cycle.getParameter(PortletConstants.WINDOW_STATE);
  +
  +        return !(_request.getPortletMode().toString().equals(expectedPortletMode) &&
_request
  +                .getWindowState().toString().equals(expectedWindowState));
  +
  +    }
  +
       public String getName()
       {
           return PortletConstants.RENDER_SERVICE;
  @@ -54,4 +95,14 @@
       {
           _portletRenderer = portletRenderer;
       }
  +
  +    public void setRequest(PortletRequest request)
  +    {
  +        _request = request;
  +    }
  +
  +    public void setPageResolver(PortletPageResolver pageResolver)
  +    {
  +        _pageResolver = pageResolver;
  +    }
   }
  \ No newline at end of file
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org


Mime
View raw message