You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tiles.apache.org by ap...@apache.org on 2009/09/25 12:25:04 UTC

svn commit: r818809 - in /tiles/framework/trunk/tiles-portlet: ./ src/main/java/org/apache/tiles/portlet/context/ src/test/java/org/apache/tiles/portlet/context/

Author: apetrelli
Date: Fri Sep 25 10:25:04 2009
New Revision: 818809

URL: http://svn.apache.org/viewvc?rev=818809&view=rev
Log:
TILES-466
Fixed request creation in portlet environment.

Added:
    tiles/framework/trunk/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletUtil.java   (with props)
    tiles/framework/trunk/tiles-portlet/src/test/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactoryTest.java   (with props)
Modified:
    tiles/framework/trunk/tiles-portlet/pom.xml
    tiles/framework/trunk/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactory.java

Modified: tiles/framework/trunk/tiles-portlet/pom.xml
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-portlet/pom.xml?rev=818809&r1=818808&r2=818809&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-portlet/pom.xml (original)
+++ tiles/framework/trunk/tiles-portlet/pom.xml Fri Sep 25 10:25:04 2009
@@ -90,6 +90,11 @@
       <artifactId>tiles-core</artifactId>
       <version>${pom.version}</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.tiles</groupId>
+      <artifactId>tiles-servlet</artifactId>
+      <version>${pom.version}</version>
+    </dependency>
 
     <dependency>
       <groupId>org.slf4j</groupId>
@@ -121,6 +126,20 @@
     </dependency>
 
     <dependency>
+      <groupId>org.easymock</groupId>
+      <artifactId>easymockclassextension</artifactId>
+      <version>2.4</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+      <version>2.5</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
       <groupId>org.apache.shale</groupId>
       <artifactId>shale-test</artifactId>
       <version>1.0.5</version>

Modified: tiles/framework/trunk/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactory.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactory.java?rev=818809&r1=818808&r2=818809&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactory.java (original)
+++ tiles/framework/trunk/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactory.java Fri Sep 25 10:25:04 2009
@@ -40,6 +40,14 @@
 public class PortletTilesRequestContextFactory implements
         TilesRequestContextFactory {
 
+    /**
+     * The site of the request object array in case there is a request, a
+     * response and a portlet context.
+     *
+     * @since 2.1.4
+     */
+    private static final int REQUEST_OBJECTS_LENGTH = 3;
+
     /** {@inheritDoc} */
     public void init(Map<String, String> configParameters) {
     }
@@ -47,15 +55,26 @@
     /** {@inheritDoc} */
     public TilesRequestContext createRequestContext(TilesApplicationContext context,
                                                     Object... requestItems) {
-        if (requestItems.length == 2) {
+        if (requestItems.length == 2
+                && requestItems[0] instanceof PortletRequest
+                && requestItems[1] instanceof PortletResponse) {
             PortletContext portletContext = getPortletContext(context);
             if (portletContext != null) {
                 return new PortletTilesRequestContext(context, portletContext,
                         (PortletRequest) requestItems[0],
                         (PortletResponse) requestItems[1]);
             }
+        } else if (requestItems.length == REQUEST_OBJECTS_LENGTH
+                && requestItems[0] instanceof PortletRequest
+                && requestItems[1] instanceof PortletResponse
+                && requestItems[2] instanceof PortletContext) {
+            return new PortletTilesRequestContext(context,
+                    (PortletContext) requestItems[2],
+                    (PortletRequest) requestItems[0],
+                    (PortletResponse) requestItems[1]);
         }
 
+
         return null;
     }
 

Added: tiles/framework/trunk/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletUtil.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletUtil.java?rev=818809&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletUtil.java (added)
+++ tiles/framework/trunk/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletUtil.java Fri Sep 25 10:25:04 2009
@@ -0,0 +1,220 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tiles.portlet.context;
+
+import javax.portlet.PortletContext;
+import javax.portlet.PortletRequest;
+
+import org.apache.tiles.ArrayStack;
+import org.apache.tiles.TilesContainer;
+import org.apache.tiles.access.TilesAccess;
+import org.apache.tiles.impl.NoSuchContainerException;
+import org.apache.tiles.servlet.context.ServletUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Utilities for Tiles portlet support.
+ *
+ * @version $Rev$ $Date$
+ * @since 2.0.6
+ */
+public final class PortletUtil {
+
+    /**
+     * Private constructor to avoid instantiation.
+     */
+    private PortletUtil() {
+    }
+
+    /**
+     * Returns true if forced include of the result is needed.
+     *
+     * @param request The portlet request.
+     * @return If <code>true</code> the include operation must be forced.
+     * @since 2.0.6
+     */
+    public static boolean isForceInclude(PortletRequest request) {
+        Boolean retValue = (Boolean) request
+                .getAttribute(ServletUtil.FORCE_INCLUDE_ATTRIBUTE_NAME);
+        return retValue != null && retValue.booleanValue();
+    }
+
+    /**
+     * Sets the option that enables the forced include of the response.
+     *
+     * @param request The portlet request.
+     * @param forceInclude If <code>true</code> the include operation must be
+     * forced.
+     * @since 2.0.6
+     */
+    public static void setForceInclude(PortletRequest request,
+            boolean forceInclude) {
+        Boolean retValue = Boolean.valueOf(forceInclude);
+        request.setAttribute(
+                ServletUtil.FORCE_INCLUDE_ATTRIBUTE_NAME,
+                retValue);
+    }
+
+    /**
+     * Returns the default Tiles container.
+     *
+     * @param context The portlet context to use.
+     * @return The default Tiles container.
+     * @since 2.1.2
+     */
+    public static TilesContainer getContainer(PortletContext context) {
+        return getContainer(context, TilesAccess.CONTAINER_ATTRIBUTE);
+    }
+
+    /**
+     * Returns a specific Tiles container.
+     *
+     * @param context The portlet context to use.
+     * @param key The key under which the container is stored. If null, the
+     * default container will be returned.
+     * @return The requested Tiles container.
+     * @since 2.1.2
+     */
+    public static TilesContainer getContainer(PortletContext context, String key) {
+        if (key == null) {
+            key = TilesAccess.CONTAINER_ATTRIBUTE;
+        }
+        return (TilesContainer) context.getAttribute(key);
+    }
+
+    /**
+     * Configures the default container to be used in the application.
+     *
+     * @param context The portlet context object to use.
+     * @param container The container object to set.
+     * @since 2.1.2
+     */
+    public static void setContainer(PortletContext context,
+            TilesContainer container) {
+        setContainer(context, container, TilesAccess.CONTAINER_ATTRIBUTE);
+    }
+
+    /**
+     * Configures the container to be used in the application.
+     *
+     * @param context The portlet context object to use.
+     * @param container The container object to set.
+     * @param key The key under which the container will be stored.
+     * @since 2.1.2
+     */
+    public static void setContainer(PortletContext context,
+            TilesContainer container, String key) {
+        Logger log = LoggerFactory.getLogger(PortletUtil.class);
+        if (key == null) {
+            key = TilesAccess.CONTAINER_ATTRIBUTE;
+        }
+
+        if (container == null) {
+            if (log.isInfoEnabled()) {
+                log.info("Removing TilesContext for context: " + context.getClass().getName());
+            }
+            context.removeAttribute(key);
+        }
+        if (container != null && log.isInfoEnabled()) {
+            log.info("Publishing TilesContext for context: " + context.getClass().getName());
+        }
+        context.setAttribute(key, container);
+    }
+
+    /**
+     * Sets the current container to use in web pages.
+     *
+     * @param request The request to use.
+     * @param context The portlet context to use.
+     * @param key The key under which the container is stored.
+     * @since 2.1.0
+     */
+    public static void setCurrentContainer(PortletRequest request,
+            PortletContext context, String key) {
+        TilesContainer container = getContainer(context, key);
+        if (container != null) {
+            request.setAttribute(ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
+        } else {
+            throw new NoSuchContainerException("The container with the key '"
+                    + key + "' cannot be found");
+        }
+    }
+
+    /**
+     * Sets the current container to use in web pages.
+     *
+     * @param request The request to use.
+     * @param context The portlet context to use.
+     * @param container The container to use as the current container.
+     * @since 2.1.0
+     */
+    public static void setCurrentContainer(PortletRequest request,
+            PortletContext context, TilesContainer container) {
+        if (container != null) {
+            request.setAttribute(ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
+        } else {
+            throw new NoSuchContainerException("The container cannot be null");
+        }
+    }
+
+    /**
+     * Returns the current container that has been set, or the default one.
+     *
+     * @param request The request to use.
+     * @param context The portlet context to use.
+     * @return The current Tiles container to use in web pages.
+     * @since 2.1.0
+     */
+    public static TilesContainer getCurrentContainer(PortletRequest request,
+            PortletContext context) {
+        TilesContainer container = (TilesContainer) request
+                .getAttribute(ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME);
+        if (container == null) {
+            container = getContainer(context);
+            request.setAttribute(ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME,
+                    container);
+        }
+
+        return container;
+    }
+
+    /**
+     * Returns the compose stack, that is used by the tags to compose
+     * definitions, attributes, etc.
+     *
+     * @param request The portlet request.
+     * @return The compose stack.
+     * @since 2.2.0
+     */
+    @SuppressWarnings("unchecked")
+    public static ArrayStack<Object> getComposeStack(PortletRequest request) {
+        ArrayStack<Object> composeStack = (ArrayStack<Object>) request.getAttribute(
+                ServletUtil.COMPOSE_STACK_ATTRIBUTE_NAME);
+        if (composeStack == null) {
+            composeStack = new ArrayStack<Object>();
+            request.setAttribute(ServletUtil.COMPOSE_STACK_ATTRIBUTE_NAME, composeStack);
+        }
+        return composeStack;
+    }
+}

Propchange: tiles/framework/trunk/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletUtil.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: tiles/framework/trunk/tiles-portlet/src/test/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactoryTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-portlet/src/test/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactoryTest.java?rev=818809&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-portlet/src/test/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactoryTest.java (added)
+++ tiles/framework/trunk/tiles-portlet/src/test/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactoryTest.java Fri Sep 25 10:25:04 2009
@@ -0,0 +1,124 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tiles.portlet.context;
+
+import static org.easymock.EasyMock.*;
+import static org.easymock.classextension.EasyMock.*;
+import static org.junit.Assert.*;
+
+import javax.portlet.PortletContext;
+import javax.portlet.PortletRequest;
+import javax.portlet.PortletResponse;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test for {@link PortletTilesRequestContextFactory}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class PortletTilesRequestContextFactoryTest {
+
+    /**
+     * The factory to test.
+     */
+    private PortletTilesRequestContextFactory factory;
+
+    /**
+     * Sets up the test object.
+     */
+    @Before
+    public void setUp() {
+        factory = new PortletTilesRequestContextFactory();
+    }
+
+    /**
+     * Test method for
+     * {@link PortletTilesRequestContextFactory
+     * #createRequestContext(org.apache.tiles.TilesApplicationContext, java.lang.Object[])}
+     * .
+     */
+    @Test
+    public void testCreateRequestContext2Params() {
+        PortletTilesApplicationContext applicationContext = createMock(PortletTilesApplicationContext.class);
+        PortletRequest request = createMock(PortletRequest.class);
+        PortletResponse response = createMock(PortletResponse.class);
+        PortletContext portletContext = createMock(PortletContext.class);
+
+        expect(applicationContext.getPortletContext())
+                .andReturn(portletContext);
+
+        replay(applicationContext, request, response, portletContext);
+        PortletTilesRequestContext tilesRequest = (PortletTilesRequestContext) factory
+                .createRequestContext(applicationContext, request, response);
+        Object[] objs = tilesRequest.getRequestObjects();
+        assertEquals(2, objs.length);
+        assertEquals(request, objs[0]);
+        assertEquals(response, objs[1]);
+        verify(applicationContext, request, response, portletContext);
+    }
+
+    /**
+     * Test method for
+     * {@link PortletTilesRequestContextFactory
+     * #createRequestContext(org.apache.tiles.TilesApplicationContext, java.lang.Object[])}
+     * .
+     */
+    @Test
+    public void testCreateRequestContext3Params() {
+        PortletTilesApplicationContext applicationContext = createMock(PortletTilesApplicationContext.class);
+        PortletRequest request = createMock(PortletRequest.class);
+        PortletResponse response = createMock(PortletResponse.class);
+        PortletContext portletContext = createMock(PortletContext.class);
+
+        replay(applicationContext, request, response, portletContext);
+        PortletTilesRequestContext tilesRequest = (PortletTilesRequestContext) factory
+                .createRequestContext(applicationContext, request, response,
+                        portletContext);
+        Object[] objs = tilesRequest.getRequestObjects();
+        assertEquals(2, objs.length);
+        assertEquals(request, objs[0]);
+        assertEquals(response, objs[1]);
+        verify(applicationContext, request, response, portletContext);
+    }
+
+    /**
+     * Test method for
+     * {@link org.apache.tiles.portlet.context.PortletTilesRequestContextFactory
+     * #getPortletContext(org.apache.tiles.TilesApplicationContext)}
+     * .
+     */
+    @Test
+    public void testGetPortletContext() {
+        PortletTilesApplicationContext applicationContext = createMock(PortletTilesApplicationContext.class);
+        PortletContext portletContext = createMock(PortletContext.class);
+
+        expect(applicationContext.getPortletContext())
+                .andReturn(portletContext);
+
+        replay(applicationContext, portletContext);
+        assertEquals(portletContext, factory.getPortletContext(applicationContext));
+        verify(applicationContext, portletContext);
+    }
+
+}

Propchange: tiles/framework/trunk/tiles-portlet/src/test/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-portlet/src/test/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactoryTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL