You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by cr...@apache.org on 2007/03/07 18:43:16 UTC

svn commit: r515658 - in /beehive/trunk/netui: src/scoping/org/apache/beehive/netui/pageflow/scoping/ScopedServletUtils.java test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedServletUtilsTest.java

Author: crogers
Date: Wed Mar  7 09:43:11 2007
New Revision: 515658

URL: http://svn.apache.org/viewvc?view=rev&rev=515658
Log:
Implemented ScopedServletUtils.updateScopedResponse() to allow callers to get a new ScopedResponse instance. (BEEHIVE-1181)

Tests: NetUI BVT (WinXP passed)


Added:
    beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedServletUtilsTest.java   (with props)
Modified:
    beehive/trunk/netui/src/scoping/org/apache/beehive/netui/pageflow/scoping/ScopedServletUtils.java

Modified: beehive/trunk/netui/src/scoping/org/apache/beehive/netui/pageflow/scoping/ScopedServletUtils.java
URL: http://svn.apache.org/viewvc/beehive/trunk/netui/src/scoping/org/apache/beehive/netui/pageflow/scoping/ScopedServletUtils.java?view=diff&rev=515658&r1=515657&r2=515658
==============================================================================
--- beehive/trunk/netui/src/scoping/org/apache/beehive/netui/pageflow/scoping/ScopedServletUtils.java (original)
+++ beehive/trunk/netui/src/scoping/org/apache/beehive/netui/pageflow/scoping/ScopedServletUtils.java Wed Mar  7 09:43:11 2007
@@ -108,7 +108,8 @@
     }
 
     /**
-     * Get the cached wrapper servlet response.  If none exists, creates one and caches it.
+     * Get the cached wrapper servlet response.  If none exists, creates
+     * one and caches it.
      *
      * @param realResponse the "real" (outer) ServletResponse, which will be wrapped.
      * @param scopedRequest the ScopedRequest returned from {@link #getScopedRequest}.
@@ -118,15 +119,13 @@
                                                     ScopedRequest scopedRequest )
     {
         assert ! ( realResponse instanceof ScopedResponse );
-        
+
         String responseAttr = getScopedName( OVERRIDE_RESPONSE_ATTR,
-                                                                      scopedRequest.getScopeKey() );
+                                             scopedRequest.getScopeKey() );
         HttpServletRequest outerRequest = scopedRequest.getOuterRequest();
         ScopedResponse scopedResponse = ( ScopedResponse ) outerRequest.getAttribute( responseAttr );
         
-        //
-        // If it doesn't exist, create it and cache it.
-        //
+        // If it doesn't exist, create one and cache it.
         if ( scopedResponse == null )
         {
             scopedResponse = new ScopedResponseImpl( realResponse );
@@ -137,9 +136,37 @@
     }
 
     /**
+     * Updates the cached wrapper servlet response.  If none exists or the
+     * cached ScopedResponse wraps a different HttpServletResponse than the
+     * one passed in, then creates a new one, caches and returns it.
+     *
+     * @param realResponse the "real" (outer) ServletResponse, which will be wrapped.
+     * @param scopedRequest the ScopedRequest returned from {@link #getScopedRequest}.
+     * @return the cached (or newly-created) ScopedResponse.
+     */
+    public static ScopedResponse updateScopedResponse(HttpServletResponse realResponse,
+                                                      ScopedRequest scopedRequest)
+    {
+        assert !(realResponse instanceof ScopedResponse);
+
+        String responseAttr = getScopedName(OVERRIDE_RESPONSE_ATTR,
+                scopedRequest.getScopeKey());
+        HttpServletRequest outerRequest = scopedRequest.getOuterRequest();
+        ScopedResponse scopedResponse = (ScopedResponse) outerRequest.getAttribute(responseAttr);
+
+        // If it doesn't exist or the real response is different, create and cache it.
+        if (scopedResponse == null || realResponse != scopedResponse.getOuterResponse()) {
+            scopedResponse = new ScopedResponseImpl(realResponse);
+            outerRequest.setAttribute(responseAttr, scopedResponse);
+        }
+
+        return scopedResponse;
+    }
+
+    /**
      * Find all scoped objects ({@link ScopedRequest}, {@link ScopedResponse})
-     * which have a certain scope-key, replaces this scope-key with the new one, and re-caches the objects
-     * the new scope-key.
+     * which have a certain scope-key, replaces this scope-key with the new one,
+     * and re-caches the objects the new scope-key.
      * @param oldScopeKey
      * @param newScopeKey
      * @param request the real (outer) request, where the scoped objects are cached.
@@ -215,8 +242,8 @@
      * Get the outer (unwrapped) request.
      * 
      * @param request the request to unwrap.
-     * @return the outer request, if the given request is a ScopedRequest (or wraps a ScopedRequest);
-     *         otherwise, the given request itself.
+     * @return the outer request, if the given request is a ScopedRequest (or wraps
+     *         a ScopedRequest); otherwise, the given request itself.
      */ 
     public static ServletRequest getOuterServletRequest( ServletRequest request )
     {
@@ -225,8 +252,8 @@
     }
     
     /**
-     * Unwraps the contained ScopedRequest from the given ServletRequest, which may be a
-     * ServletRequestWrapper.
+     * Unwraps the contained ScopedRequest from the given ServletRequest, which may
+     * be a ServletRequestWrapper.
      * 
      * @param request the ScopedRequest, or a wrapper (ServletRequestWrapper) around it.
      * @return the unwrapped ScopedRequest.
@@ -257,8 +284,8 @@
     }  
     
     /**
-     * Unwraps the contained ScopedResponseImpl from the given ServletResponse, which may be a
-     * ServletResponseWrapper.
+     * Unwraps the contained ScopedResponseImpl from the given ServletResponse, which
+     * may be a ServletResponseWrapper.
      * 
      * @param response the ScopedResponse, or a wrapper (ServletResponseWrapper) around it.
      * @return the unwrapped ScopedResponseImpl.
@@ -302,8 +329,9 @@
     }
     
     /**
-     * If the request is a ScopedRequest, this returns an attribute whose name is scoped to that request's scope-ID;
-     * otherwise, it is a straight passthrough to {@link HttpSession#getAttribute}.
+     * If the request is a ScopedRequest, this returns an attribute whose name is
+     * scoped to that request's scope-ID; otherwise, it is a straight passthrough
+     * to {@link HttpSession#getAttribute}.
      * 
      * @exclude
      */ 
@@ -322,8 +350,9 @@
     }
 
     /**
-     * If the request is a ScopedRequest, this sets an attribute whose name is scoped to that request's scope-ID;
-     * otherwise, it is a straight passthrough to {@link HttpSession#setAttribute}.
+     * If the request is a ScopedRequest, this sets an attribute whose name is
+     * scoped to that request's scope-ID; otherwise, it is a straight passthrough to
+     * {@link HttpSession#setAttribute}.
      * 
      * @exclude
      */ 
@@ -333,8 +362,9 @@
     }
 
     /**
-     * If the request is a ScopedRequest, this removes an attribute whose name is scoped to that request's scope-ID;
-     * otherwise, it is a straight passthrough to {@link HttpSession#removeAttribute}.
+     * If the request is a ScopedRequest, this removes an attribute whose name is
+     * scoped to that request's scope-ID; otherwise, it is a straight passthrough to
+     * {@link HttpSession#removeAttribute}.
      * 
      * @exclude
      */ 
@@ -349,9 +379,9 @@
     }
     
     /**
-     * Get an attribute from the given request, and if it is a {@link ScopedRequest}, ensure that the attribute
-     * is <strong>not</strong> "showing through" from the outer request, even if the ScopedRequest allows that by
-     * default.
+     * Get an attribute from the given request, and if it is a {@link ScopedRequest},
+     * ensure that the attribute is <strong>not</strong> "showing through" from the
+     * outer request, even if the ScopedRequest allows that by default.
      * 
      * @exclude
      */ 
@@ -407,8 +437,9 @@
     public static String normalizeURI( String uri )
     {
         //
-        // If it's a relative URI, normalize it.  Note that we don't want to create a URI
-        // (very expensive) unless we think we'll need to.  "./" catches "../" and "./".
+        // If it's a relative URI, normalize it.  Note that we don't want
+        // to create a URI (very expensive) unless we think we'll need to.
+        // "./" catches "../" and "./".
         //
         if ( uri.indexOf( "./" ) != -1 )
         {

Added: beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedServletUtilsTest.java
URL: http://svn.apache.org/viewvc/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedServletUtilsTest.java?view=auto&rev=515658
==============================================================================
--- beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedServletUtilsTest.java (added)
+++ beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedServletUtilsTest.java Wed Mar  7 09:43:11 2007
@@ -0,0 +1,183 @@
+/*
+ * 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.test.pageflow.scoping;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.beehive.netui.test.servlet.ServletFactory;
+import org.apache.beehive.netui.pageflow.scoping.ScopedRequest;
+import org.apache.beehive.netui.pageflow.scoping.ScopedResponse;
+import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils;
+
+public class ScopedServletUtilsTest extends TestCase {
+
+    private HttpServletRequest _fauxRequest = null;
+    private HttpServletResponse _fauxResponse = null;
+    private ScopedRequest _fauxScopedRequest = null;
+    private ScopedResponse _fauxScopedResponse = null;
+    protected String _servletPath = "/somePageFlow/begin.do";
+    protected String _scopeId = "_scopeTest";
+    protected String _attrNameA = "scopedAttrA";
+    protected String _attrValueA = "scopedAttrValueA";
+    protected String _outerAttrName = "outerAttr";
+    protected String _outerAttrValue = "outerAttrValue";
+
+    public ScopedServletUtilsTest(String name) {
+        super(name);
+    }
+
+    public static Test suite() {
+        return new TestSuite(ScopedServletUtilsTest.class);
+    }
+
+    public static void main(String[] args) {
+        junit.textui.TestRunner.run(suite());
+    }
+
+    public HttpServletRequest getRequest() {
+        return _fauxRequest;
+    }
+
+    public HttpServletResponse getResponse() {
+        return _fauxResponse;
+    }
+
+    public ScopedRequest getScopedRequest() {
+        return _fauxScopedRequest;
+    }
+
+    public ScopedResponse getScopedResponse() {
+        return _fauxScopedResponse;
+    }
+
+    protected void setUp() {
+        _fauxRequest = ServletFactory.getServletRequest();
+        _fauxRequest.setAttribute(_outerAttrName, _outerAttrValue);
+        _fauxResponse = ServletFactory.getServletResponse();
+        String requestUri = _fauxRequest.getContextPath() + _servletPath;
+        _fauxScopedRequest = ScopedServletUtils.getScopedRequest(_fauxRequest, requestUri, null, _scopeId, false);
+        _fauxScopedRequest.setAttribute(_attrNameA, _attrValueA);
+        _fauxScopedResponse = ScopedServletUtils.getScopedResponse(_fauxResponse, _fauxScopedRequest);
+    }
+
+    protected void tearDown() {
+    }
+
+    public void testGetAndUpdateScopedResponse() {
+        ScopedResponse scopedResponse = getScopedResponse();
+        assertTrue(getResponse() == scopedResponse.getOuterResponse());
+
+        // test that we get the same cached scoped response
+        ScopedResponse cachedScopedResponse =
+                ScopedServletUtils.getScopedResponse(getResponse(), getScopedRequest());
+        assertEquals(scopedResponse, cachedScopedResponse);
+        cachedScopedResponse =
+                ScopedServletUtils.updateScopedResponse(getResponse(), getScopedRequest());
+        assertEquals(scopedResponse, cachedScopedResponse);
+
+        // test updating the cached scoped response
+        HttpServletResponse response = ServletFactory.getServletResponse();
+        ScopedResponse newScopedResponse =
+                ScopedServletUtils.updateScopedResponse(response, getScopedRequest());
+        assertNotNull("The updated scoped response was returned as null", newScopedResponse);
+
+        // new scoped response != old cached scoped response
+        assertTrue(!newScopedResponse.equals(cachedScopedResponse));
+        assertTrue(newScopedResponse.getOuterResponse() != cachedScopedResponse.getOuterResponse());
+
+        // cache has been updated so now a call to get the cached scoped response
+        // returns the new one, even if you pass in the old response.
+        cachedScopedResponse =
+                ScopedServletUtils.getScopedResponse(getResponse(), getScopedRequest());
+        assertEquals(newScopedResponse, cachedScopedResponse);
+    }
+
+    public void testRenameScope() {
+        ScopedRequest scopedRequest = getScopedRequest();
+        assertEquals(_scopeId, scopedRequest.getScopeKey());
+        String newScopeId = "newScopeId";
+        ScopedServletUtils.renameScope(_scopeId, newScopeId, getRequest());
+        assertEquals(newScopeId, scopedRequest.getScopeKey());
+        ScopedResponse cachedScopedResponse =
+                ScopedServletUtils.getScopedResponse(getResponse(), scopedRequest);
+        assertEquals(getScopedResponse(), cachedScopedResponse);
+    }
+
+    public void testOuterRequestsAndWrappers() {
+        ScopedRequest scopedRequest = getScopedRequest();
+        ScopedResponse scopedResponse = getScopedResponse();
+
+        TestRequestWrapper requestWrapper = new TestRequestWrapper(scopedRequest);
+        assertEquals(ScopedServletUtils.unwrapRequest(requestWrapper), scopedRequest);
+        assertEquals(ScopedServletUtils.getOuterRequest(requestWrapper), getRequest());
+
+        TestResponseWrapper responseWrapper = new TestResponseWrapper(scopedResponse);
+        assertEquals(ScopedServletUtils.unwrapResponse(responseWrapper), scopedResponse);
+    }
+
+    public void testGetScopedRequestAttribute() {
+        assertEquals(_outerAttrValue, ScopedServletUtils.getScopedRequestAttribute(_outerAttrName, getRequest()));
+        assertNull(ScopedServletUtils.getScopedRequestAttribute(_outerAttrName, getScopedRequest()));
+        assertNull(getScopedRequest().getAttribute(_outerAttrName));
+        assertNull(ScopedServletUtils.getScopedRequestAttribute(_attrNameA, getRequest()));
+        assertEquals(_attrValueA, ScopedServletUtils.getScopedRequestAttribute(_attrNameA, getScopedRequest()));
+
+        HttpServletRequest request = ServletFactory.getServletRequest();
+        request.setAttribute(_outerAttrName, _outerAttrValue);
+        String requestUri = request.getContextPath() + _servletPath;
+        ScopedRequest scopedRequest = ScopedServletUtils.getScopedRequest(request, requestUri, null, _scopeId, true);
+        scopedRequest.setAttribute(_attrNameA, _attrValueA);
+        assertEquals(_outerAttrValue, ScopedServletUtils.getScopedRequestAttribute(_outerAttrName, request));
+        assertNull(ScopedServletUtils.getScopedRequestAttribute(_outerAttrName, scopedRequest));
+        assertEquals(_outerAttrValue, scopedRequest.getAttribute(_outerAttrName));
+        assertNull(ScopedServletUtils.getScopedRequestAttribute(_attrNameA, request));
+        assertEquals(_attrValueA, ScopedServletUtils.getScopedRequestAttribute(_attrNameA, scopedRequest));
+    }
+
+    public void testRelativeURI() {
+        String contextPath = _fauxRequest.getContextPath();
+        String relativeURI = "/path/test/page.jsp";
+        String uri = "http://localhost:8080" + contextPath + relativeURI;
+        assertEquals(_servletPath, ScopedServletUtils.getRelativeURI(getScopedRequest()));
+        assertEquals(relativeURI, ScopedServletUtils.getRelativeURI(getRequest(), uri));
+        assertEquals(relativeURI, ScopedServletUtils.getRelativeURI(getScopedRequest(), uri));
+        assertEquals(relativeURI, ScopedServletUtils.getRelativeURI(contextPath, uri));
+    }
+
+    public final class TestRequestWrapper extends HttpServletRequestWrapper
+    {
+        public TestRequestWrapper(HttpServletRequest delegate) {
+            super(delegate);
+        }
+    }
+
+    public final class TestResponseWrapper extends HttpServletResponseWrapper
+    {
+        public TestResponseWrapper(HttpServletResponse delegate) {
+            super(delegate);
+        }
+    }
+}

Propchange: beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedServletUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native