View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts.chain.commands;
22  
23  import java.util.Map;
24  
25  import org.apache.struts.Globals;
26  import org.apache.struts.action.ActionMessages;
27  import org.apache.struts.chain.contexts.ActionContext;
28  
29  /**
30   * <p>Remove cached messages stored in the session.</p>
31   *
32   * @version $Id$
33   * @see CacheMessages
34   * @since Struts 1.3.5
35   */
36  public class RemoveCachedMessages extends ActionCommandBase {
37  
38      /**
39       * <p>Removes any <code>ActionMessages</code> object stored in the session
40       * under <code>Globals.MESSAGE_KEY</code> and <code>Globals.ERROR_KEY</code>
41       * if the messages' <code>isAccessed</code> method returns true.  This
42       * allows messages to be stored in the session, displayed one time, and be
43       * released here.</p>
44       *
45       * @param actionCtx The <code>Context</code> for the current request
46       * @return <code>false</code> so that processing continues
47       * @throws Exception on any error
48       */
49      @Override
50      protected boolean execute_(ActionContext actionCtx)
51          throws Exception {
52  
53          // Get session scope
54          Map<String, Object> session = actionCtx.getSessionScope();
55  
56          // Remove messages as needed
57          removeAccessedMessages(session, Globals.MESSAGE_KEY);
58  
59          // Remove error messages as needed
60          removeAccessedMessages(session, Globals.ERROR_KEY);
61  
62          return CONTINUE_PROCESSING;
63      }
64  
65      /**
66       * <p>Removes any <code>ActionMessages</code> object from the specified
67       * scope stored under the specified key if the messages'
68       * <code>isAccessed</code> method returns true.
69       *
70       * @param scope The scope to check for messages in.
71       * @param key The key the messages are stored under.
72       */
73      private void removeAccessedMessages(Map<String, Object> scope, String key) {
74          ActionMessages messages = (ActionMessages)scope.get(key);
75          if (messages != null && messages.isAccessed()) {
76              scope.remove(key);
77          }
78      }
79  }