KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > RedirectWithMessages


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.core;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
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 /**
35  * A special {@link ActionForward} to get around the problem of losing messages
36  * and errors when using a redirecting action forward. Redirects are sometimes
37  * necessary so that <i>referer</i> can be used for page elements that may be
38  * used on any page (agent launching, chaning language or profile etc).
39  *
40  * <p>
41  * The errors are stored in the session under a unique key which is added to the
42  * request path. The request process then uses this unique key to retrieve the
43  * messages back from the sessio and place them in the
44  *
45  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
46  */

47 public class RedirectWithMessages extends ActionForward {
48
49     /**
50      * Request parameter name used to pass the <i>Messages Id</i>. This is also
51      * used to store the current message ID in the session
52      */

53     public final static String JavaDoc MESSAGE_ID = "msgId";
54
55     /**
56      * Session attribute name used to store the map of messages.
57      */

58     public final static String JavaDoc SESSION_MESSAGES = "sessionMessages";
59
60     /**
61      * @param forward
62      * @param request
63      */

64     public RedirectWithMessages(ActionForward forward, HttpServletRequest JavaDoc request) {
65         this(forward.getPath(), request);
66     }
67         
68     /**
69      * Constructor.
70      * @param path path
71      * @param request request
72      */

73     public RedirectWithMessages(String JavaDoc path, HttpServletRequest JavaDoc request) {
74         super(path, true);
75         
76         // Cannot use tiles with redirect with messages
77
if(path.startsWith(".")) {
78             throw new IllegalArgumentException JavaDoc("You cannot use a tile with " + getClass().getName());
79         }
80         
81         int id = getNextId(request.getSession());
82         // Create map of messages for the request
83
Map JavaDoc<String JavaDoc, ActionMessages> messageMap = createMessageMap(id, request);
84
85         // Add the messages
86
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         // Create a new path containing the ID
93
setPath(CoreUtil.addParameterToPath(CoreUtil.removeParameterFromPath(path, MESSAGE_ID), MESSAGE_ID, String.valueOf(id)));
94     }
95
96     static void addMessageFromSession(String JavaDoc key, HttpServletRequest JavaDoc request, Map JavaDoc<String JavaDoc, 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 JavaDoc session) {
104         synchronized (session) {
105             Integer JavaDoc i = (Integer JavaDoc) session.getAttribute(MESSAGE_ID);
106             if (i == null) {
107                 i = new Integer JavaDoc(0);
108             } else {
109                 i = new Integer JavaDoc(i.intValue() + 1);
110             }
111             session.setAttribute(MESSAGE_ID, i);
112             return i.intValue();
113         }
114     }
115
116     /**
117      * Look for the {@link #MESSAGE_ID} parameter and if found, move the
118      * messages from the session back into the request. This should be called by
119      * the request processor.
120      * @param request request
121      */

122     @SuppressWarnings JavaDoc("unchecked")
123     public static void repopulate(HttpServletRequest JavaDoc request) {
124         synchronized (request.getSession()) {
125             Map JavaDoc<Integer JavaDoc, Map JavaDoc<String JavaDoc, ActionMessages>> map = (Map JavaDoc<Integer JavaDoc, Map JavaDoc<String JavaDoc, ActionMessages>>) request.getSession().getAttribute(SESSION_MESSAGES);
126             if (map != null) {
127                 String JavaDoc msgId = request.getParameter(MESSAGE_ID);
128                 if (msgId != null) {
129                     Integer JavaDoc id = new Integer JavaDoc(msgId);
130                     Map JavaDoc<String JavaDoc, ActionMessages> messageMap = map.get(id);
131                     if (messageMap != null) {
132                         for (String JavaDoc 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     /**
146      * @param request
147      * @param key
148      * @param messages
149      * @param path
150      * @return String
151      */

152     public static String JavaDoc addMessages(HttpServletRequest JavaDoc request, String JavaDoc key, ActionMessages messages, String JavaDoc path) {
153         int id = getNextId(request.getSession());
154         // Create map of messages for the request
155
Map JavaDoc<String JavaDoc, ActionMessages> messageMap = createMessageMap(id, request);
156         // Add the messages
157
messageMap.put(key, messages);
158         return CoreUtil.addParameterToPath(path, MESSAGE_ID, String.valueOf(id));
159     }
160     
161     /**
162      * @param id
163      * @param request
164      * @return Map<String, ActionMessages>
165      */

166     @SuppressWarnings JavaDoc("unchecked")
167     public static Map JavaDoc<String JavaDoc, ActionMessages> createMessageMap(int id, HttpServletRequest JavaDoc request) {
168         Map JavaDoc<Integer JavaDoc, Map JavaDoc<String JavaDoc, ActionMessages>> map = (Map JavaDoc<Integer JavaDoc, Map JavaDoc<String JavaDoc, ActionMessages>>) request.getSession().getAttribute(SESSION_MESSAGES);
169
170         // Get all of the messages for this session
171
if (map == null) {
172             map = new HashMap JavaDoc<Integer JavaDoc, Map JavaDoc<String JavaDoc, ActionMessages>>();
173             request.getSession().setAttribute(SESSION_MESSAGES, map);
174         }
175
176         // Create map of messages for the request
177
Map JavaDoc<String JavaDoc, ActionMessages> messageMap = new HashMap JavaDoc<String JavaDoc, ActionMessages>();
178         map.put(new Integer JavaDoc(id), messageMap);
179         
180         return messageMap;
181     }
182 }
Popular Tags