1 19 20 package com.sslexplorer.core; 21 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpSession ; 27 28 import org.apache.struts.Globals; 29 import org.apache.struts.action.ActionForward; 30 import org.apache.struts.action.ActionMessages; 31 32 import com.sslexplorer.security.Constants; 33 34 47 public class RedirectWithMessages extends ActionForward { 48 49 53 public final static String MESSAGE_ID = "msgId"; 54 55 58 public final static String SESSION_MESSAGES = "sessionMessages"; 59 60 64 public RedirectWithMessages(ActionForward forward, HttpServletRequest request) { 65 this(forward.getPath(), request); 66 } 67 68 73 public RedirectWithMessages(String path, HttpServletRequest request) { 74 super(path, true); 75 76 if(path.startsWith(".")) { 78 throw new IllegalArgumentException ("You cannot use a tile with " + getClass().getName()); 79 } 80 81 int id = getNextId(request.getSession()); 82 Map <String , ActionMessages> messageMap = createMessageMap(id, request); 84 85 addMessageFromSession(Globals.MESSAGE_KEY, request, messageMap); 87 addMessageFromSession(Globals.ERROR_KEY, request, messageMap); 88 addMessageFromSession(Constants.REQ_ATTR_WARNINGS, request, messageMap); 89 addMessageFromSession(Constants.BUNDLE_ERRORS_KEY, request, messageMap); 90 addMessageFromSession(Constants.BUNDLE_MESSAGES_KEY, request, messageMap); 91 92 setPath(CoreUtil.addParameterToPath(CoreUtil.removeParameterFromPath(path, MESSAGE_ID), MESSAGE_ID, String.valueOf(id))); 94 } 95 96 static void addMessageFromSession(String key, HttpServletRequest request, Map <String , ActionMessages> messageMap) { 97 ActionMessages msgs = (ActionMessages) request.getAttribute(key); 98 if (msgs != null) { 99 messageMap.put(key, msgs); 100 } 101 } 102 103 static int getNextId(HttpSession session) { 104 synchronized (session) { 105 Integer i = (Integer ) session.getAttribute(MESSAGE_ID); 106 if (i == null) { 107 i = new Integer (0); 108 } else { 109 i = new Integer (i.intValue() + 1); 110 } 111 session.setAttribute(MESSAGE_ID, i); 112 return i.intValue(); 113 } 114 } 115 116 122 @SuppressWarnings ("unchecked") 123 public static void repopulate(HttpServletRequest request) { 124 synchronized (request.getSession()) { 125 Map <Integer , Map <String , ActionMessages>> map = (Map <Integer , Map <String , ActionMessages>>) request.getSession().getAttribute(SESSION_MESSAGES); 126 if (map != null) { 127 String msgId = request.getParameter(MESSAGE_ID); 128 if (msgId != null) { 129 Integer id = new Integer (msgId); 130 Map <String , ActionMessages> messageMap = map.get(id); 131 if (messageMap != null) { 132 for (String key : messageMap.keySet()) { 133 request.setAttribute(key, messageMap.get(key)); 134 } 135 map.remove(id); 136 } 137 } 138 if(map.size() == 0) { 139 request.getSession().removeAttribute(SESSION_MESSAGES); 140 } 141 } 142 } 143 } 144 145 152 public static String addMessages(HttpServletRequest request, String key, ActionMessages messages, String path) { 153 int id = getNextId(request.getSession()); 154 Map <String , ActionMessages> messageMap = createMessageMap(id, request); 156 messageMap.put(key, messages); 158 return CoreUtil.addParameterToPath(path, MESSAGE_ID, String.valueOf(id)); 159 } 160 161 166 @SuppressWarnings ("unchecked") 167 public static Map <String , ActionMessages> createMessageMap(int id, HttpServletRequest request) { 168 Map <Integer , Map <String , ActionMessages>> map = (Map <Integer , Map <String , ActionMessages>>) request.getSession().getAttribute(SESSION_MESSAGES); 169 170 if (map == null) { 172 map = new HashMap <Integer , Map <String , ActionMessages>>(); 173 request.getSession().setAttribute(SESSION_MESSAGES, map); 174 } 175 176 Map <String , ActionMessages> messageMap = new HashMap <String , ActionMessages>(); 178 map.put(new Integer (id), messageMap); 179 180 return messageMap; 181 } 182 } | Popular Tags |