1 19 20 package com.sslexplorer.navigation.actions; 21 22 import java.io.PrintWriter ; 23 import java.io.StringWriter ; 24 import java.util.ArrayList ; 25 import java.util.Enumeration ; 26 import java.util.List ; 27 import java.util.Locale ; 28 import java.util.StringTokenizer ; 29 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 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 request, HttpServletResponse response) 53 throws Exception { 54 55 List options = new ArrayList (); 57 MessageResources resources = null; 58 59 String bundle = request.getParameter("bundle"); 60 String propertyPrefix = request.getParameter("propertyPrefix"); 61 String type = request.getParameter("type"); 62 if (type == null) { 63 type = "message"; 64 } 65 String arg0 = request.getParameter("arg0"); 66 boolean decorated = true; 67 String align = "center"; 68 69 Locale locale = (Locale )request.getSession().getAttribute(Globals.LOCALE_KEY); 70 71 if (mapping.getParameter() != null 74 && !mapping.getParameter().equals("")) { 75 StringTokenizer t = new StringTokenizer (mapping.getParameter(), ","); 76 if (t.hasMoreTokens()) { 77 type = t.nextToken(); 78 } 79 80 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 ("Could not find resource bundle " + bundle); 100 } 101 while (t.hasMoreTokens()) { 102 String 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 ("Could not find resource bundle " + bundle); 110 } 111 String [] 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 String title = resources.getMessage(locale, propertyPrefix + ".title"); 123 String subtitle = resources.getMessage(locale, propertyPrefix + ".subtitle"); 124 String message = resources 125 .getMessage(locale, propertyPrefix + ".message", arg0); 126 127 ConfirmForm confirmForm = (ConfirmForm) form; 129 confirmForm.initialize(type, title, subtitle, message, options, 130 decorated, align, arg0); 131 132 Throwable exception = type.equals(ConfirmForm.TYPE_EXCEPTION) ? (Throwable ) request 135 .getSession().getAttribute(Constants.EXCEPTION) 136 : null; 137 request.getSession().removeAttribute(Constants.EXCEPTION); 138 if (exception != null) { 139 StringBuffer mesgBuf = new StringBuffer (); 140 StringBuffer traceBuf = new StringBuffer (); 141 Throwable ex = exception; 142 while (ex != null) { 143 String 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 sw = new StringWriter (); 155 ex.printStackTrace(new PrintWriter (sw)); 156 if (traceBuf.length() == 0) { 157 traceBuf.append("\n"); 158 } 159 traceBuf.append(sw.toString()); 160 ex = ex.getCause(); 161 } 162 163 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 ) 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 locale, String optionText, String propertyPrefix, 189 HttpServletRequest request, MessageResources resources) { 190 191 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 name = optionText; 205 String 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 buf = new StringBuffer (forward); 222 if (includeParameters && !"".equals(forward)) { 223 for (Enumeration e = request.getParameterNames(); e 225 .hasMoreElements();) { 226 String paramName = (String ) e.nextElement(); 227 String [] 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 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 request, HttpServletResponse response) { 249 return SessionInfo.ALL_CONTEXTS; 250 } 251 252 } | Popular Tags |