Author: pbenedict
Date: Sat Apr 14 15:05:31 2007
New Revision: 528882

URL: http://svn.apache.org/viewvc?view=rev&rev=528882
Log:
STR-3032: Cache unaccessed messages when redirecting

Added:
    
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/CacheMessages.java
   (with props)
Modified:
    
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/RemoveCachedMessages.java
    
struts/struts1/trunk/core/src/main/resources/org/apache/struts/chain/chain-config.xml

Added: 
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/CacheMessages.java
URL: 
http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/CacheMessages.java?view=auto&rev=528882
==============================================================================
--- 
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/CacheMessages.java
 (added)
+++ 
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/CacheMessages.java
 Sat Apr 14 15:05:31 2007
@@ -0,0 +1,76 @@
+/*
+ * $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.struts.chain.commands;
+
+import org.apache.struts.Globals;
+import org.apache.struts.action.ActionMessages;
+import org.apache.struts.chain.contexts.ActionContext;
+import org.apache.struts.config.ForwardConfig;
+
+import java.util.Map;
+
+/**
+ * Copies any <code>ActionMessages</code> from the request to the session if a
+ * redirecting forward is selected and the messages were not accessed. This
+ * allows messages to be retained across a redirect, and then later released by
+ * the <code>RemoveCachedMessages</code> command. The most common use case for
+ * this command is when the validator or exception handler places messages in
+ * the request.
+ * 
+ * @version $Id$
+ * @see RemoveCachedMessages
+ * @since Struts 1.4.0
+ */
+public class CacheMessages extends ActionCommandBase {
+
+    public boolean execute(ActionContext actionCtx) throws Exception {
+        ForwardConfig forwardConfig = actionCtx.getForwardConfig();
+        if ((forwardConfig != null) && forwardConfig.getRedirect()) {
+            Map request = actionCtx.getRequestScope();
+            Map session = actionCtx.getSessionScope();
+            copyUnaccessedMessages(request, session, Globals.MESSAGE_KEY);
+            copyUnaccessedMessages(request, session, Globals.ERROR_KEY);
+        }
+
+        return false;
+    }
+
+    /**
+     * Adds any <code>ActionMessages</code> object to the destination scope,
+     * stored under the specified key, if the messages were not accessed 
through
+     * the source scope.
+     * 
+     * @param fromScope The scope to check for unacessed messages.
+     * @param toScope The scope to copy messages into.
+     * @param key The key the messages are stored under.
+     */
+    private void copyUnaccessedMessages(Map fromScope, Map toScope, String 
key) {
+        ActionMessages fromMessages = (ActionMessages) fromScope.get(key);
+        if ((fromMessages != null) && !fromMessages.isAccessed()) {
+            ActionMessages toMessages = (ActionMessages) toScope.get(key);
+            if (toMessages != null) {
+                toMessages.add(fromMessages);
+            } else {
+                toScope.put(key, fromMessages);
+            }
+        }
+    }
+}

Propchange: 
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/CacheMessages.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/CacheMessages.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: 
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/RemoveCachedMessages.java
URL: 
http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/RemoveCachedMessages.java?view=diff&rev=528882&r1=528881&r2=528882
==============================================================================
--- 
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/RemoveCachedMessages.java
 (original)
+++ 
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/RemoveCachedMessages.java
 Sat Apr 14 15:05:31 2007
@@ -29,6 +29,7 @@
  * <p>Remove cached messages stored in the session.</p>
  *
  * @version $Id$
+ * @see CacheMessages
  * @since Struts 1.3.5
  */
 public class RemoveCachedMessages extends ActionCommandBase {

Modified: 
struts/struts1/trunk/core/src/main/resources/org/apache/struts/chain/chain-config.xml
URL: 
http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/resources/org/apache/struts/chain/chain-config.xml?view=diff&rev=528882&r1=528881&r2=528882
==============================================================================
--- 
struts/struts1/trunk/core/src/main/resources/org/apache/struts/chain/chain-config.xml
 (original)
+++ 
struts/struts1/trunk/core/src/main/resources/org/apache/struts/chain/chain-config.xml
 Sat Apr 14 15:05:31 2007
@@ -109,7 +109,7 @@
 
 
         <!-- Look up optional preprocess command -->
-        <lookup
+        <lookup 
                 catalogName="struts"
                 name="servlet-standard-preprocess"
                 optional="true"/>
@@ -181,7 +181,6 @@
         <command
                 
className="org.apache.struts.chain.commands.servlet.SelectForward"/>
 
-
         <!-- Select the include uri (if any) for the current action mapping -->
         <command
                 className="org.apache.struts.chain.commands.SelectInclude"/>
@@ -196,15 +195,17 @@
         <command
                 
className="org.apache.struts.chain.commands.servlet.CreateAction"/>
 
-
         <!-- Execute the Action for this request -->
         <command
                 
className="org.apache.struts.chain.commands.servlet.ExecuteAction"/>
 
-
         <!-- Perform action postprocess tasks -->
         <command
                 
className="org.apache.struts.chain.commands.servlet.ActionPostProcess"/>
+
+        <!-- Add unaccessed messages to the Session only when redirecting -->
+        <command
+                className="org.apache.struts.chain.commands.CacheMessages"/>
     </chain>
 
     <!-- ========== View Processing chain ======================== -->


Reply via email to