From 5d178452b9fa43fda636e32a3333c9fbe82e01f3 Mon Sep 17 00:00:00 2001 From: Scott Andrews Date: Tue, 7 Oct 2008 20:04:19 +0000 Subject: [PATCH] SWF-904 Ajax Tiles partial renders fail when session is not available --- .../ajax/tiles2/AjaxTilesRequestContext.java | 131 ++++++++++++++++++ .../js/ajax/tiles2/AjaxTilesView.java | 8 +- .../tiles2/AjaxTilesRequestContextTests.java | 104 ++++++++++++++ 3 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 spring-js/src/main/java/org/springframework/js/ajax/tiles2/AjaxTilesRequestContext.java create mode 100644 spring-js/src/test/java/org/springframework/js/ajax/tiles2/AjaxTilesRequestContextTests.java diff --git a/spring-js/src/main/java/org/springframework/js/ajax/tiles2/AjaxTilesRequestContext.java b/spring-js/src/main/java/org/springframework/js/ajax/tiles2/AjaxTilesRequestContext.java new file mode 100644 index 00000000..df49968a --- /dev/null +++ b/spring-js/src/main/java/org/springframework/js/ajax/tiles2/AjaxTilesRequestContext.java @@ -0,0 +1,131 @@ +/* + * Copyright 2004-2008 the original author or authors. + * + * Licensed 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.springframework.js.ajax.tiles2; + +import java.io.IOException; +import java.util.Locale; +import java.util.Map; + +import org.apache.tiles.context.TilesRequestContext; + +/** + * Tiles request context implementation that delegates to another {@link TilesRequestContext}. + * + *

+ * This implementation will handle all dispatch requests by invoking {@link TilesRequestContext#include(String)} on the + * delegate request context. + *

+ * + * @author Scott Andrews + */ +class AjaxTilesRequestContext implements TilesRequestContext { + + private final TilesRequestContext targetReqestContext; + + /** + * Create a TilesRequestContext for use by AjaxTilesView + * + * @param targetReqestContext the target {@link TilesRequestContext} + */ + public AjaxTilesRequestContext(TilesRequestContext targetReqestContext) { + this.targetReqestContext = targetReqestContext; + } + + /** + * Delegate call to {@link TilesRequestContext#include(String)} + */ + public void dispatch(String path) throws IOException { + targetReqestContext.include(path); + } + + /** + * {@inheritDoc} + */ + public Map getHeader() { + return targetReqestContext.getHeader(); + } + + /** + * {@inheritDoc} + */ + public Map getHeaderValues() { + return targetReqestContext.getHeaderValues(); + } + + /** + * {@inheritDoc} + */ + public Map getParam() { + return targetReqestContext.getParam(); + } + + /** + * {@inheritDoc} + */ + public Map getParamValues() { + return targetReqestContext.getParamValues(); + } + + /** + * {@inheritDoc} + */ + public Object getRequest() { + return targetReqestContext.getRequest(); + } + + /** + * {@inheritDoc} + */ + public Locale getRequestLocale() { + return targetReqestContext.getRequestLocale(); + } + + /** + * {@inheritDoc} + */ + public Map getRequestScope() { + return targetReqestContext.getRequestScope(); + } + + /** + * {@inheritDoc} + */ + public Object getResponse() { + return targetReqestContext.getResponse(); + } + + /** + * {@inheritDoc} + */ + public Map getSessionScope() { + return targetReqestContext.getSessionScope(); + } + + /** + * {@inheritDoc} + */ + public void include(String path) throws IOException { + targetReqestContext.include(path); + } + + /** + * {@inheritDoc} + */ + public boolean isUserInRole(String role) { + return targetReqestContext.isUserInRole(role); + } + +} diff --git a/spring-js/src/main/java/org/springframework/js/ajax/tiles2/AjaxTilesView.java b/spring-js/src/main/java/org/springframework/js/ajax/tiles2/AjaxTilesView.java index 159421b3..b9df7c60 100644 --- a/spring-js/src/main/java/org/springframework/js/ajax/tiles2/AjaxTilesView.java +++ b/spring-js/src/main/java/org/springframework/js/ajax/tiles2/AjaxTilesView.java @@ -83,14 +83,16 @@ public class AjaxTilesView extends TilesView { exposeModelAsRequestAttributes(model, request); JstlUtils.exposeLocalizationContext(new RequestContext(request, servletContext)); - TilesRequestContext tilesRequestContext = container.getContextFactory().createRequestContext( - container.getApplicationContext(), new Object[] { request, response }); + TilesRequestContext tilesRequestContext = new AjaxTilesRequestContext(container.getContextFactory() + .createRequestContext(container.getApplicationContext(), new Object[] { request, response })); Definition compositeDefinition = container.getDefinitionsFactory().getDefinition(getUrl(), tilesRequestContext); Map flattenedAttributeMap = new HashMap(); flattenAttributeMap(container, tilesRequestContext, flattenedAttributeMap, compositeDefinition); - response.flushBuffer(); + // initialize the session before rendering any fragments. Otherwise views that require the session which has + // not otherwise been initialized will fail to render + request.getSession(); for (int i = 0; i < attrNames.length; i++) { Attribute attributeToRender = (Attribute) flattenedAttributeMap.get(attrNames[i]); diff --git a/spring-js/src/test/java/org/springframework/js/ajax/tiles2/AjaxTilesRequestContextTests.java b/spring-js/src/test/java/org/springframework/js/ajax/tiles2/AjaxTilesRequestContextTests.java new file mode 100644 index 00000000..59e8ca98 --- /dev/null +++ b/spring-js/src/test/java/org/springframework/js/ajax/tiles2/AjaxTilesRequestContextTests.java @@ -0,0 +1,104 @@ +package org.springframework.js.ajax.tiles2; + +import java.io.IOException; + +import junit.framework.TestCase; + +import org.apache.tiles.context.TilesRequestContext; +import org.easymock.EasyMock; + +public class AjaxTilesRequestContextTests extends TestCase { + + private TilesRequestContext targetContext; + private AjaxTilesRequestContext ajaxContext; + + protected void setUp() throws Exception { + targetContext = (TilesRequestContext) EasyMock.createMock(TilesRequestContext.class); + ajaxContext = new AjaxTilesRequestContext(targetContext); + } + + public void testGetHeader() { + EasyMock.expect(targetContext.getHeader()).andReturn(null); + EasyMock.replay(new Object[] { targetContext }); + ajaxContext.getHeader(); + EasyMock.verify(new Object[] { targetContext }); + } + + public void testGetHeaderValues() { + EasyMock.expect(targetContext.getHeaderValues()).andReturn(null); + EasyMock.replay(new Object[] { targetContext }); + ajaxContext.getHeaderValues(); + EasyMock.verify(new Object[] { targetContext }); + } + + public void testGetRequestScope() { + EasyMock.expect(targetContext.getRequestScope()).andReturn(null); + EasyMock.replay(new Object[] { targetContext }); + ajaxContext.getRequestScope(); + EasyMock.verify(new Object[] { targetContext }); + } + + public void testGetSessionScope() { + EasyMock.expect(targetContext.getSessionScope()).andReturn(null); + EasyMock.replay(new Object[] { targetContext }); + ajaxContext.getSessionScope(); + EasyMock.verify(new Object[] { targetContext }); + } + + public void testDispatch() throws IOException { + targetContext.include(null); + EasyMock.replay(new Object[] { targetContext }); + ajaxContext.dispatch(null); + EasyMock.verify(new Object[] { targetContext }); + } + + public void testInclude() throws IOException { + targetContext.include(null); + EasyMock.replay(new Object[] { targetContext }); + ajaxContext.include(null); + EasyMock.verify(new Object[] { targetContext }); + } + + public void testGetParam() { + EasyMock.expect(targetContext.getParam()).andReturn(null); + EasyMock.replay(new Object[] { targetContext }); + ajaxContext.getParam(); + EasyMock.verify(new Object[] { targetContext }); + } + + public void testGetParamValues() { + EasyMock.expect(targetContext.getParamValues()).andReturn(null); + EasyMock.replay(new Object[] { targetContext }); + ajaxContext.getParamValues(); + EasyMock.verify(new Object[] { targetContext }); + } + + public void testGetRequestLocale() { + EasyMock.expect(targetContext.getRequestLocale()).andReturn(null); + EasyMock.replay(new Object[] { targetContext }); + ajaxContext.getRequestLocale(); + EasyMock.verify(new Object[] { targetContext }); + } + + public void testIsUserInRole() { + EasyMock.expect(Boolean.valueOf(targetContext.isUserInRole(null))).andReturn(Boolean.FALSE); + EasyMock.replay(new Object[] { targetContext }); + ajaxContext.isUserInRole(null); + EasyMock.verify(new Object[] { targetContext }); + } + + public void testGetRequest() { + EasyMock.expect(targetContext.getRequest()).andReturn(null); + EasyMock.replay(new Object[] { targetContext }); + ajaxContext.getRequest(); + EasyMock.verify(new Object[] { targetContext }); + } + + public void testGetResponse() { + EasyMock.expect(targetContext.getResponse()).andReturn(null); + EasyMock.replay(new Object[] { targetContext }); + ajaxContext.getResponse(); + EasyMock.verify(new Object[] { targetContext }); + } + +}