1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.s1;
19
20 import java.util.Arrays;
21 import java.util.Iterator;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.apache.struts.Globals;
27 import org.apache.struts.action.Action;
28 import org.apache.struts.action.ActionForm;
29 import org.apache.struts.action.ActionForward;
30 import org.apache.struts.action.ActionMapping;
31 import org.apache.struts.action.ActionMessage;
32 import org.apache.struts.action.ActionMessages;
33 import org.apache.struts2.ServletActionContext;
34 import org.apache.struts2.StrutsException;
35 import org.apache.struts2.dispatcher.DefaultActionSupport;
36 import org.apache.struts2.dispatcher.Dispatcher;
37
38 import com.opensymphony.xwork2.ActionContext;
39 import com.opensymphony.xwork2.ObjectFactory;
40 import com.opensymphony.xwork2.config.entities.ActionConfig;
41 import com.opensymphony.xwork2.interceptor.ScopedModelDriven;
42
43 /***
44 * Wraps legacy Struts 1.3 Actions. Supports the following features:
45 * <ul>
46 * <li>ActionForms</li>
47 * <li>ActionForwards that have the same name as a result</li>
48 * <li>ActionMessages stored in the request, converted to Struts 2 messages</li>
49 * <li>Action-level validation flag</li>
50 * </ul>
51 * Still to do:
52 * <ul>
53 * <li>Custom ActionForward instances that don't have an associated result config</li>
54 * <li>setServlet() calls for the Action</li>
55 * <li>Most everything else...</li>
56 * </ul>
57 */
58 public class Struts1Action extends DefaultActionSupport implements ScopedModelDriven<ActionForm> {
59
60 private ActionForm actionForm;
61 private String className;
62 private boolean validate;
63 private String scopeKey;
64
65 public String execute() throws Exception {
66 ActionContext ctx = ActionContext.getContext();
67 ActionConfig actionConfig = ctx.getActionInvocation().getProxy().getConfig();
68 Action action = null;
69 try {
70 action = (Action) ObjectFactory.getObjectFactory().buildBean(className, null);
71 } catch (Exception e) {
72 throw new StrutsException("Unable to create the legacy Struts Action", e, actionConfig);
73 }
74
75
76
77 Struts1Factory strutsFactory = new Struts1Factory(Dispatcher.getInstance().getConfigurationManager().getConfiguration());
78 ActionMapping mapping = strutsFactory.createActionMapping(actionConfig);
79 HttpServletRequest request = ServletActionContext.getRequest();
80 HttpServletResponse response = ServletActionContext.getResponse();
81 ActionForward forward = action.execute(mapping, actionForm, request, response);
82
83 ActionMessages messages = (ActionMessages) request.getAttribute(Globals.MESSAGE_KEY);
84 if (messages != null) {
85 for (Iterator i = messages.get(); i.hasNext(); ) {
86 ActionMessage msg = (ActionMessage) i.next();
87 if (msg.getValues() != null && msg.getValues().length > 0) {
88 addActionMessage(getText(msg.getKey(), Arrays.asList(msg.getValues())));
89 } else {
90 addActionMessage(getText(msg.getKey()));
91 }
92 }
93 }
94
95 if (forward instanceof WrapperActionForward || actionConfig.getResults().containsKey(forward.getName())) {
96 return forward.getName();
97 } else {
98 throw new StrutsException("Unable to handle action forwards that don't have an associated result", actionConfig);
99 }
100 }
101
102 public void setModel(ActionForm model) {
103 actionForm = model;
104 }
105
106 public ActionForm getModel() {
107 return actionForm;
108 }
109
110 /***
111 * @return the validate
112 */
113 public boolean isValidate() {
114 return validate;
115 }
116
117 /***
118 * @param validate the validate to set
119 */
120 public void setValidate(boolean validate) {
121 this.validate = validate;
122 }
123
124 /***
125 * @param className the className to set
126 */
127 public void setClassName(String className) {
128 this.className = className;
129 }
130
131 public String getScopeKey() {
132 return scopeKey;
133 }
134
135 public void setScopeKey(String key) {
136 this.scopeKey = key;
137 }
138 }