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 import org.apache.struts.config.ForwardConfig; 29 30 /** 31 * Copies any <code>ActionMessages</code> from the request to the session if a 32 * redirecting forward is selected and the messages were not accessed. This 33 * allows messages to be retained across a redirect, and then later released by 34 * the <code>RemoveCachedMessages</code> command. The most common use case for 35 * this command is when the validator or exception handler places messages in 36 * the request. 37 * 38 * @version $Id$ 39 * @see RemoveCachedMessages 40 * @since Struts 1.4.0 41 */ 42 public class CacheMessages extends ActionCommandBase { 43 44 @Override 45 protected boolean execute_(ActionContext actionCtx) throws Exception { 46 ForwardConfig forwardConfig = actionCtx.getForwardConfig(); 47 if ((forwardConfig != null) && forwardConfig.getRedirect()) { 48 Map<String, Object> request = actionCtx.getRequestScope(); 49 Map<String, Object> session = actionCtx.getSessionScope(); 50 copyUnaccessedMessages(request, session, Globals.MESSAGE_KEY); 51 copyUnaccessedMessages(request, session, Globals.ERROR_KEY); 52 } 53 54 return CONTINUE_PROCESSING; 55 } 56 57 /** 58 * Adds any <code>ActionMessages</code> object to the destination scope, 59 * stored under the specified key, if the messages were not accessed through 60 * the source scope. 61 * 62 * @param fromScope The scope to check for unacessed messages. 63 * @param toScope The scope to copy messages into. 64 * @param key The key the messages are stored under. 65 */ 66 private void copyUnaccessedMessages(Map<String, Object> fromScope, Map<String, Object> toScope, String key) { 67 ActionMessages fromMessages = (ActionMessages) fromScope.get(key); 68 if ((fromMessages != null) && !fromMessages.isAccessed()) { 69 ActionMessages toMessages = (ActionMessages) toScope.get(key); 70 if (toMessages != null) { 71 toMessages.add(fromMessages); 72 } else { 73 toScope.put(key, fromMessages); 74 } 75 } 76 } 77 }