KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > navigation > actions > ConfirmAction


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.navigation.actions;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.apache.struts.Globals;
34 import org.apache.struts.action.ActionForm;
35 import org.apache.struts.action.ActionForward;
36 import org.apache.struts.action.ActionMapping;
37 import org.apache.struts.util.MessageResources;
38
39 import com.sslexplorer.boot.Util;
40 import com.sslexplorer.core.CoreException;
41 import com.sslexplorer.core.CoreUtil;
42 import com.sslexplorer.core.actions.DefaultAction;
43 import com.sslexplorer.core.stringreplacement.VariableReplacement;
44 import com.sslexplorer.navigation.Option;
45 import com.sslexplorer.navigation.forms.ConfirmForm;
46 import com.sslexplorer.security.Constants;
47 import com.sslexplorer.security.SessionInfo;
48
49 public class ConfirmAction extends DefaultAction {
50
51     public ActionForward execute(ActionMapping mapping, ActionForm form,
52             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
53             throws Exception JavaDoc {
54
55         // Get all of the options from the resource bundle
56
List JavaDoc options = new ArrayList JavaDoc();
57         MessageResources resources = null;
58
59         String JavaDoc bundle = request.getParameter("bundle");
60         String JavaDoc propertyPrefix = request.getParameter("propertyPrefix");
61         String JavaDoc type = request.getParameter("type");
62         if (type == null) {
63             type = "message";
64         }
65         String JavaDoc arg0 = request.getParameter("arg0");
66         boolean decorated = true;
67         String JavaDoc align = "center";
68         
69         Locale JavaDoc locale = (Locale JavaDoc)request.getSession().getAttribute(Globals.LOCALE_KEY);
70
71         // parameter
72
// ="administration,deletePropertyProfile,yes=/updatePropertyProfile.do?action=delete,no="
73
if (mapping.getParameter() != null
74                 && !mapping.getParameter().equals("")) {
75             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(mapping.getParameter(), ",");
76             if (t.hasMoreTokens()) {
77                 type = t.nextToken();
78             }
79
80             // TODO this is a hack to get the extension store agreement aligning
81
// left, sort it out properly!
82
if (type.startsWith("align=")) {
83                 int idx = type.indexOf('=');
84                 align = type.substring(idx + 1);
85                 type = t.nextToken();
86             }
87
88             if (t.hasMoreTokens()) {
89                 decorated = t.nextToken().equalsIgnoreCase("true");
90             }
91             if (t.hasMoreTokens()) {
92                 bundle = t.nextToken();
93             }
94             if (t.hasMoreTokens()) {
95                 propertyPrefix = t.nextToken();
96             }
97             resources = getResources(request, bundle);
98             if (resources == null) {
99                 throw new Exception JavaDoc("Could not find resource bundle " + bundle);
100             }
101             while (t.hasMoreTokens()) {
102                 String JavaDoc option = t.nextToken();
103                 options.add(getOption(locale, option, propertyPrefix, request,
104                         resources));
105             }
106         } else {
107             resources = getResources(request, bundle);
108             if (resources == null) {
109                 throw new Exception JavaDoc("Could not find resource bundle " + bundle);
110             }
111             String JavaDoc[] optionValues = request.getParameterValues("option");
112             if (options != null) {
113                 for (int i = 0; i < optionValues.length; i++) {
114                     options.add(getOption(locale, optionValues[i], propertyPrefix,
115                             request, resources));
116                 }
117
118             }
119         }
120
121         // Get the title text and the description
122
String JavaDoc title = resources.getMessage(locale, propertyPrefix + ".title");
123         String JavaDoc subtitle = resources.getMessage(locale, propertyPrefix + ".subtitle");
124         String JavaDoc message = resources
125                 .getMessage(locale, propertyPrefix + ".message", arg0);
126
127         // Initialise the form
128
ConfirmForm confirmForm = (ConfirmForm) form;
129         confirmForm.initialize(type, title, subtitle, message, options,
130                 decorated, align, arg0);
131
132         // If this confirmation is the result on an exception then build up the
133
// exception text
134
Throwable JavaDoc exception = type.equals(ConfirmForm.TYPE_EXCEPTION) ? (Throwable JavaDoc) request
135                 .getSession().getAttribute(Constants.EXCEPTION)
136                 : null;
137         request.getSession().removeAttribute(Constants.EXCEPTION);
138         if (exception != null) {
139             StringBuffer JavaDoc mesgBuf = new StringBuffer JavaDoc();
140             StringBuffer JavaDoc traceBuf = new StringBuffer JavaDoc();
141             Throwable JavaDoc ex = exception;
142             while (ex != null) {
143                 String JavaDoc mesg = ex.getMessage();
144                 if (mesg != null) {
145                     mesg = mesg.trim();
146                     if (!mesg.endsWith(".")) {
147                         mesg += ".";
148                     }
149                     if (mesgBuf.length() == 0) {
150                         mesgBuf.append('\n');
151                     }
152                     mesgBuf.append(mesg);
153                 }
154                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
155                 ex.printStackTrace(new PrintWriter JavaDoc(sw));
156                 if (traceBuf.length() == 0) {
157                     traceBuf.append("\n");
158                 }
159                 traceBuf.append(sw.toString());
160                 ex = ex.getCause();
161             }
162
163             // If this is a code exception we can get the localised messages
164
if (exception instanceof CoreException) {
165                 CoreException ce = (CoreException) exception;
166                 MessageResources mr = CoreUtil.getMessageResources(request
167                         .getSession(), ce.getBundle());
168                 if (mr != null) {
169                     mesgBuf.append(" ");
170                     mesgBuf.append(mr.getMessage((Locale JavaDoc) request
171                             .getSession().getAttribute(Globals.LOCALE_KEY), ce
172                             .getBundleActionMessage().getKey(), ce
173                             .getBundleActionMessage().getArg0(), ce
174                             .getBundleActionMessage().getArg1(), ce
175                             .getBundleActionMessage().getArg2(), ce
176                             .getBundleActionMessage().getArg3()));
177
178                 }
179             }
180
181             confirmForm.setExceptionMessage(mesgBuf.toString());
182             confirmForm.setTraceMessage(traceBuf.toString());
183         }
184
185         return mapping.findForward("success");
186     }
187
188     public Option getOption(Locale JavaDoc locale, String JavaDoc optionText, String JavaDoc propertyPrefix,
189             HttpServletRequest JavaDoc request, MessageResources resources) {
190
191         /*
192          * If the path for the forward for this option ends with a !, then dont
193          * append parameters. This is used in places like the logon screen where
194          * you would not want parameters such as password, PIN etc to be passed
195          * back after an exception
196          */

197         boolean includeParameters = true;
198         if (optionText.endsWith("!")) {
199             optionText = optionText.substring(0, optionText.length() - 1);
200             includeParameters = false;
201         }
202
203         int idx = optionText.indexOf('=');
204         String JavaDoc name = optionText;
205         String JavaDoc forward = null;
206         if (idx != -1) {
207             forward = name.substring(idx + 1);
208             if (forward.equals("@")) {
209                 forward = CoreUtil.getReferer(request);
210                 if (forward == null) {
211                     forward = "/showHome.do";
212                 }
213             }
214             name = name.substring(0, idx);
215             
216             VariableReplacement r = new VariableReplacement();
217             r.setServletRequest(request);
218             forward = r.replace(forward);
219         }
220
221         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(forward);
222         if (includeParameters && !"".equals(forward)) {
223             // Build up the URL to forard to
224
for (Enumeration JavaDoc e = request.getParameterNames(); e
225                     .hasMoreElements();) {
226                 String JavaDoc paramName = (String JavaDoc) e.nextElement();
227                 String JavaDoc[] paramVals = request.getParameterValues(paramName);
228                 for (int i = 0; i < paramVals.length; i++) {
229                     if (buf.length() == forward.length()
230                             && forward.indexOf('?') == -1) {
231                         buf.append("?");
232                     } else {
233                         buf.append("&");
234                     }
235                     buf.append(Util.urlEncode(paramName));
236                     buf.append("=");
237                     buf.append(Util.urlEncode(paramVals[i]));
238                 }
239             }
240         }
241
242         String JavaDoc label = resources.getMessage(locale, propertyPrefix + ".option." + name);
243         return new Option(buf.toString(), label, name);
244
245     }
246
247     public int getNavigationContext(ActionMapping mapping, ActionForm form,
248             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
249         return SessionInfo.ALL_CONTEXTS;
250     }
251
252 }
Popular Tags