1 5 package com.opensymphony.workflow.util; 6 7 import com.opensymphony.module.propertyset.PropertySet; 8 9 import com.opensymphony.workflow.*; 10 11 import webwork.action.Action; 12 import webwork.action.ActionContext; 13 14 import webwork.dispatcher.ActionResult; 15 import webwork.dispatcher.GenericDispatcher; 16 17 import java.lang.reflect.Method ; 18 19 import java.security.Principal ; 20 21 import java.util.*; 22 23 24 30 public class WebWorkValidator implements Validator { 31 33 public void validate(Map transientVars, Map args, PropertySet ps) throws WorkflowException { 34 final WorkflowContext wfContext = (WorkflowContext) transientVars.get("context"); 35 36 String actionName = (String ) args.get("action.name"); 37 GenericDispatcher gd = new GenericDispatcher(actionName); 38 gd.prepareContext(); 39 ActionContext.setPrincipal(new Principal () { 40 public String getName() { 41 return wfContext.getCaller(); 42 } 43 }); 44 ActionContext.setApplication(args); 45 ActionContext.setSession(ps.getProperties("")); 46 ActionContext.setLocale(Locale.getDefault()); 47 ActionContext.setParameters(Collections.unmodifiableMap(transientVars)); 48 49 boolean hasErrors = false; 50 InvalidInputException iie = new InvalidInputException(); 51 52 try { 53 gd.executeAction(); 54 55 ActionResult ar = gd.finish(); 56 gd.finalizeContext(); 57 58 List actions = ar.getActions(); 59 60 for (Iterator iterator = actions.iterator(); iterator.hasNext();) { 61 Action action = (Action) iterator.next(); 62 63 List errorMessages = getErrorMessages(action); 64 65 for (Iterator iterator2 = errorMessages.iterator(); 66 iterator2.hasNext();) { 67 String error = (String ) iterator2.next(); 68 iie.addError(error); 69 hasErrors = true; 70 } 71 72 Map errors = getErrors(action); 73 74 for (Iterator iterator2 = errors.entrySet().iterator(); 75 iterator2.hasNext();) { 76 Map.Entry entry = (Map.Entry) iterator2.next(); 77 String error = (String ) entry.getKey(); 78 String message = (String ) entry.getValue(); 79 iie.addError(error, message); 80 hasErrors = true; 81 } 82 } 83 } catch (Exception e) { 84 throw new WorkflowException("Could not execute action " + actionName, e); 85 } 86 87 if (hasErrors) { 88 throw iie; 89 } 90 } 91 92 private List getErrorMessages(Action action) { 93 try { 94 Method m = action.getClass().getMethod("getErrorMessages", new Class [] { 95 96 }); 97 98 return (List) m.invoke(action, new Object [] {}); 99 } catch (Throwable t) { 100 return Collections.EMPTY_LIST; 101 } 102 } 103 104 private Map getErrors(Action action) { 105 try { 106 Method m = action.getClass().getMethod("getErrors", new Class [] {}); 107 108 return (Map) m.invoke(action, new Object [] {}); 109 } catch (Throwable t) { 110 return Collections.EMPTY_MAP; 111 } 112 } 113 } 114 | Popular Tags |