1 19 20 21 package com.sslexplorer.core.actions; 22 23 import java.io.IOException ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 35 import org.apache.commons.beanutils.BeanUtils; 36 import org.apache.struts.action.Action; 37 import org.apache.struts.action.ActionForm; 38 import org.apache.struts.action.ActionForward; 39 import org.apache.struts.action.ActionMapping; 40 import org.apache.struts.config.ActionConfig; 41 import org.apache.struts.config.FormBeanConfig; 42 import org.apache.struts.util.RequestUtils; 43 44 import com.sslexplorer.boot.Util; 45 import com.sslexplorer.core.DefaultPanel; 46 import com.sslexplorer.core.Panel; 47 import com.sslexplorer.core.PanelManager; 48 import com.sslexplorer.core.forms.AbstractMultiFormDispatchForm; 49 import com.sslexplorer.policyframework.Permission; 50 import com.sslexplorer.policyframework.ResourceType; 51 import com.sslexplorer.security.SessionInfo; 52 53 55 public abstract class AbstractMultiFormDispatchAction extends AuthenticatedDispatchAction { 56 private final Map <String , AuthenticatedDispatchAction> actions = new HashMap <String , AuthenticatedDispatchAction>(); 57 private final int placement_; 58 59 66 public AbstractMultiFormDispatchAction(ResourceType resourceType, Permission permissions[], int placement) { 67 super(resourceType, permissions); 68 placement_ = placement; 69 } 70 71 @SuppressWarnings ("unchecked") 72 protected ActionForward dispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request, 73 HttpServletResponse response, String name) throws Exception { 74 Collection <Panel> panels = PanelManager.getInstance().getPanels(placement_, request, response, DefaultPanel.MAIN_LAYOUT); 75 Collection <SubActionWrapper> subActions = getSubActions(mapping, form, request, panels); 76 77 AbstractMultiFormDispatchForm dispatchForm = (AbstractMultiFormDispatchForm) form; 78 dispatchForm.init(subActions, mapping, request); 79 80 ActionForward fwd = provideActionTargets(subActions, dispatchForm, request, response, name); 81 if(fwd != null) { 82 return fwd; 83 } 84 85 if (null == name) 88 return unspecified(mapping, form, request, response); 89 90 return getActionForward(mapping, form, request, response, name); 91 } 92 93 private Collection <SubActionWrapper> getSubActions(ActionMapping mapping, ActionForm form, HttpServletRequest request, 94 Collection <Panel> panels) throws ClassNotFoundException , 95 IllegalAccessException , InstantiationException , InvocationTargetException { 96 Collection <SubActionWrapper> subActions = new ArrayList <SubActionWrapper>(); 97 int index = 0; 98 for (Panel panel : panels) { 99 104 ActionConfig config = mapping.getModuleConfig().findActionConfig("/" + panel.getId()); 105 if (null != config && config instanceof ActionMapping) { 106 ActionMapping subMapping = (ActionMapping) config; 107 SubActionWrapper subActionWrapper = getSubActionWrapper(mapping, form, subMapping, request, index); 108 if (null != subActionWrapper) 109 subActions.add(subActionWrapper); 110 } 111 index++; 112 } 113 return subActions; 114 } 115 116 private static SubActionWrapper getSubActionWrapper(ActionMapping mapping, ActionForm form, ActionMapping subMapping, 117 HttpServletRequest request, int index) throws ClassNotFoundException , 118 IllegalAccessException , InstantiationException , InvocationTargetException { 119 String formName = subMapping.getName(); 120 ActionForm subForm = getActionForm(subMapping, request); 121 122 FormBeanConfig formBean = mapping.getModuleConfig().findFormBeanConfig(formName); 123 String className = formBean == null ? null : formBean.getType(); 124 if (className == null) 125 return null; 126 127 if (subForm == null || !className.equals(subForm.getClass().getName())) 128 subForm = (ActionForm) Class.forName(className).newInstance(); 129 130 if ("request".equals(mapping.getScope())) 131 request.setAttribute(formName, subForm); 132 else 133 request.getSession().setAttribute(formName, subForm); 134 135 subForm.reset(mapping, request); 136 137 143 AbstractMultiFormDispatchForm dispatchForm = (AbstractMultiFormDispatchForm) form; 144 if (formName.equals(dispatchForm.getSubForm())) { 145 dispatchForm.setSelectedTab(dispatchForm.getTabName(index)); 146 BeanUtils.populate(subForm, request.getParameterMap()); 147 } 148 149 return new SubActionWrapper(subForm, subMapping); 150 } 151 152 private static ActionForm getActionForm(ActionMapping subMapping, HttpServletRequest request) { 153 String formName = subMapping.getName(); 154 if ("request".equals(subMapping.getScope())) 155 return (ActionForm) request.getAttribute(formName); 156 else 157 return (ActionForm) request.getSession().getAttribute(formName); 158 } 159 160 private ActionForward provideActionTargets(Collection <SubActionWrapper> subActions, AbstractMultiFormDispatchForm dispatchForm, 161 HttpServletRequest request, HttpServletResponse response, String name) 162 throws ClassNotFoundException , IOException , IllegalAccessException , InstantiationException { 163 if (null == name || name.equals("")) 164 name = "unspecified"; 165 boolean refreshAll = new Boolean (request.getParameter("refreshAll")); 167 168 String subForm = dispatchForm.getSubForm(); 169 ActionForm subform = (ActionForm) request.getSession().getAttribute(subForm); 170 ActionForward fwd = null; 171 172 for (SubActionWrapper wrapper : subActions) { 175 try { 176 Action action = processActionCreate(request, response, wrapper.getMapping()); 178 if (dispatchForm.isSubFormEmpty() || wrapper.getForm().equals(subform) || refreshAll) { 180 Method method = getMethod(action, name); 181 Object args[] = { wrapper.getMapping(), wrapper.getForm(), request, response }; 182 if(wrapper.getForm().equals(subform)) { 186 fwd = (ActionForward)method.invoke(action, args); 187 } 188 else { 189 method.invoke(action, args); 190 } 191 } 192 } catch (Exception e) { 193 log.error(e); 194 } 195 } 196 197 return fwd; 198 } 199 200 private Method getMethod(Action action, String name) throws NoSuchMethodException { 201 synchronized (methods) { 202 String methodName = action.getClass().getName() + "#" + name; 203 if (!methods.containsKey(methods)) { 204 Method method = action.getClass().getMethod(name, types); 205 methods.put(methodName, method); 206 } 207 return (Method ) methods.get(methodName); 208 } 209 } 210 211 private Action processActionCreate(HttpServletRequest request, HttpServletResponse response, ActionConfig mapping) 212 throws IOException , ClassNotFoundException , IllegalAccessException , InstantiationException { 213 synchronized (actions) { 214 String className = mapping.getType(); 215 if (!actions.containsKey(className)) { 216 if (log.isTraceEnabled()) 217 log.trace(" Creating new Action instance"); 218 AuthenticatedDispatchAction instance = (AuthenticatedDispatchAction) RequestUtils.applicationInstance(className); 219 instance.setServlet(servlet); 220 actions.put(className, instance); 221 } 222 return actions.get(className); 223 } 224 } 225 226 private ActionForward getActionForward(ActionMapping mapping, ActionForm form, HttpServletRequest request, 227 HttpServletResponse response, String name) throws Exception { 228 try { 229 Method method = getMethod(name); 230 Object args[] = { mapping, form, request, response }; 231 return (ActionForward) method.invoke(this, args); 232 } catch (ClassCastException e) { 233 log.error(getMessage(mapping, name, "dispatch.return"), e); 234 throw e; 235 } catch (IllegalAccessException e) { 236 log.error(getMessage(mapping, name, "dispatch.error"), e); 237 throw e; 238 } catch (NoSuchMethodException e) { 239 return unspecified(mapping, form, request, response); 240 } catch (InvocationTargetException e) { 241 Throwable t = e.getTargetException(); 242 if (t instanceof Exception ) { 243 throw (Exception ) t; 244 } else { 245 log.error(getMessage(mapping, name, "dispatch.error"), e); 246 throw new ServletException (t); 247 } 248 } 249 } 250 251 private static String getMessage(ActionMapping mapping, String name, String message) { 252 return messages.getMessage(message, mapping.getPath(), name); 253 } 254 255 263 public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, 264 HttpServletResponse response) { 265 return mapping.findForward("display"); 266 } 267 268 276 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 277 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT; 278 } 279 280 public static final class SubActionWrapper { 281 private final ActionForm form_; 282 private final ActionMapping mapping_; 283 284 SubActionWrapper(ActionForm form, ActionMapping mapping) { 285 form_ = form; 286 mapping_ = mapping; 287 } 288 289 public ActionForm getForm() { 290 return form_; 291 } 292 293 public ActionMapping getMapping() { 294 return mapping_; 295 } 296 } 297 } | Popular Tags |